Răsfoiți Sursa

Fix PHP error when updating profile/level, add flash msgs BT#13334

jmontoyaa 7 ani în urmă
părinte
comite
b872335f8e

+ 6 - 6
main/admin/skill.php

@@ -65,16 +65,16 @@ switch ($action) {
 
         if ($form->validate()) {
             $values = $form->exportValues();
-
             $profile = $em->getRepository('ChamiloSkillBundle:Profile')->find($values['profile_id']);
-            $item->setProfile($profile);
-
-            $em->persist($item);
-            $em->flush();
+            if ($profile) {
+                $item->setProfile($profile);
+                $em->persist($item);
+                $em->flush();
+                Display::addFlash(Display::return_message(get_lang('Updated')));
+            }
             header('Location: '.$listAction);
             exit;
         }
-
         break;
     case 'delete':
         $toolbarAction = Display::toolbarAction('toolbar', [Display::url(get_lang('List'), $listAction)]);

+ 35 - 23
main/admin/skill_level.php

@@ -1,6 +1,8 @@
 <?php
 /* For licensing terms, see /license.txt */
 
+use Chamilo\SkillBundle\Entity\Level;
+
 /**
  * Add a skill Level
  *
@@ -28,14 +30,14 @@ $id = isset($_GET['id']) ? $_GET['id'] : '';
 
 $item = null;
 if (!empty($id)) {
-    /** @var \Chamilo\SkillBundle\Entity\Level $item */
+    /** @var Level $item */
     $item = $em->getRepository('ChamiloSkillBundle:Level')->find($id);
     if (!$item) {
         api_not_allowed();
     }
 }
 
-$form = new FormValidator('Level', 'GET', api_get_self().'?action='.$action.'&id='.$id);
+$form = new FormValidator('level', 'GET', api_get_self().'?action='.$action.'&id='.$id);
 $form->addText('name', get_lang('Name'));
 $form->addText('short_name', get_lang('ShortName'));
 $form->addSelectFromCollection('profile_id', get_lang('Profile'), $profiles);
@@ -50,29 +52,35 @@ if (!empty($item)) {
         'profile_id' => $item->getProfile()->getId(),
     ]);
 }
-$formToDisplay = $form->returnForm();
+
+$formToDisplay = '';
 
 $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
-$interbreadcrumb[] = ['url' => api_get_self(), 'name' => get_lang('SkillProfile')];
+$interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin/skill.php', 'name' => get_lang('ManageSkillsLevels')];
+$interbreadcrumb[] = ['url' => api_get_self(), 'name' => get_lang('Level')];
 
-$tpl = new Template($action);
 switch ($action) {
     case 'add':
-        $tpl->assign('form', $formToDisplay);
+        $formToDisplay = $form->returnForm();
         if ($form->validate()) {
             $values = $form->exportValues();
-            $item = new \Chamilo\SkillBundle\Entity\Level();
-            $item->setName($values['name']);
-            $item->setShortName($values['short_name']);
-            $profile = $em->getRepository('ChamiloSkillBundle:Profile')->find($values['profile_id']);
-            $item->setProfile($profile);
-
-            $em->persist($item);
-            $em->flush();
+            if (isset($values['profile_id']) && !empty($values['profile_id'])) {
+                $profile = $em->getRepository('ChamiloSkillBundle:Profile')->find($values['profile_id']);
+                if ($profile) {
+                    $item = new Level();
+                    $item->setName($values['name']);
+                    $item->setShortName($values['short_name']);
+                    $item->setProfile($profile);
+                    $em->persist($item);
+                    $em->flush();
+                    Display::addFlash(Display::return_message(get_lang('Added')));
+                } else {
+                    Display::addFlash(Display::return_message(get_lang('YouNeedToCreateASkillProfile')));
+                }
+            }
             header('Location: '.$listAction);
             exit;
         }
-
         $toolbarAction = Display::url(
             Display::return_icon(
                 'list_badges.png',
@@ -85,7 +93,7 @@ switch ($action) {
         );
         break;
     case 'edit':
-        $tpl->assign('form', $formToDisplay);
+        $formToDisplay = $form->returnForm();
         $toolbarAction = Display::url(
             Display::return_icon(
                 'list_badges.png',
@@ -103,7 +111,9 @@ switch ($action) {
             $item->setName($values['name']);
             $item->setShortName($values['short_name']);
             $profile = $em->getRepository('ChamiloSkillBundle:Profile')->find($values['profile_id']);
-            $item->setProfile($profile);
+            if ($profile) {
+                $item->setProfile($profile);
+            }
 
             $em->persist($item);
             $em->flush();
@@ -122,8 +132,11 @@ switch ($action) {
             $listAction,
             ['title' => get_lang('List')]
         );
-        $em->remove($item);
-        $em->flush();
+        if ($item) {
+            $em->remove($item);
+            $em->flush();
+            Display::addFlash(Display::return_message(get_lang('Deleted')));
+        }
         header('Location: '.$listAction);
         exit;
 
@@ -141,12 +154,11 @@ switch ($action) {
         );
 }
 
+$tpl = new Template($action);
+$tpl->assign('form', $formToDisplay);
 $tpl->assign('list', $list);
 $templateName = $tpl->get_template('admin/skill_level.tpl');
 $contentTemplate = $tpl->fetch($templateName);
-$tpl->assign(
-    'actions',
-    Display::toolbarAction('toolbar', [$toolbarAction])
-);
+$tpl->assign('actions', Display::toolbarAction('toolbar', [$toolbarAction]));
 $tpl->assign('content', $contentTemplate);
 $tpl->display_one_col_template();

+ 34 - 19
main/admin/skill_profile.php

@@ -1,6 +1,9 @@
 <?php
 /* For licensing terms, see /license.txt */
 
+use Chamilo\SkillBundle\Entity\Profile;
+use Chamilo\SkillBundle\Entity\Level;
+
 /**
  * Add a skill Profile
  *
@@ -43,7 +46,7 @@ if (!empty($item)) {
 $formToDisplay = $form->returnForm();
 
 $interbreadcrumb[] = ['url' => 'index.php', 'name' => get_lang('PlatformAdmin')];
-$interbreadcrumb[] = ['url' => 'skill.php', 'name' => get_lang('ManageSkillsLevels')];
+$interbreadcrumb[] = ['url' => api_get_path(WEB_CODE_PATH).'admin/skill.php', 'name' => get_lang('ManageSkillsLevels')];
 $interbreadcrumb[] = ['url' =>  api_get_self(), 'name' => get_lang('SkillProfile')];
 
 $toolbar = null;
@@ -51,29 +54,33 @@ $toolbar = null;
 $tpl = new Template($action);
 switch ($action) {
     case 'move_up':
-        /** @var \Chamilo\SkillBundle\Entity\Level $item */
+        /** @var Level $item */
         $item = $em->getRepository('ChamiloSkillBundle:Level')->find($_GET['level_id']);
+        if ($item) {
+            $position = $item->getPosition();
+            if (!empty($position)) {
+                $item->setPosition($position - 1);
+            }
+            $em->persist($item);
+            $em->flush();
+            Display::addFlash(Display::return_message(get_lang('Updated')));
+        }
 
-        $position = $item->getPosition();
 
-        if (!empty($position)) {
-            $item->setPosition($position - 1);
-        }
-        $em->persist($item);
-        $em->flush();
         header('Location: '.$listAction);
         exit;
         break;
     case 'move_down':
-        /** @var \Chamilo\SkillBundle\Entity\Level $item */
+        /** @var Level $item */
         $item = $em->getRepository('ChamiloSkillBundle:Level')->find($_GET['level_id']);
+        if ($item) {
+            $position = $item->getPosition();
+            $item->setPosition($position + 1);
+            $em->persist($item);
+            $em->flush();
+            Display::addFlash(Display::return_message(get_lang('Updated')));
+        }
 
-        $position = $item->getPosition();
-
-        $item->setPosition($position + 1);
-
-        $em->persist($item);
-        $em->flush();
         header('Location: '.$listAction);
         exit;
         break;
@@ -81,10 +88,12 @@ switch ($action) {
         $tpl->assign('form', $formToDisplay);
         if ($form->validate()) {
             $values = $form->exportValues();
-            $item = new \Chamilo\SkillBundle\Entity\Profile();
+            $item = new Profile();
             $item->setName($values['name']);
             $em->persist($item);
             $em->flush();
+
+            Display::addFlash(Display::return_message(get_lang('Added')));
             header('Location: '.$listAction);
             exit;
         }
@@ -117,6 +126,7 @@ switch ($action) {
             $item->setName($values['name']);
             $em->persist($item);
             $em->flush();
+            Display::addFlash(Display::return_message(get_lang('Updated')));
             header('Location: '.$listAction);
             exit;
         }
@@ -133,11 +143,16 @@ switch ($action) {
             $listAction,
             ['title' => get_lang('List')]
         );
-        $em->remove($item);
-        $em->flush();
+
+        try {
+            $em->remove($item);
+            $em->flush();
+            Display::addFlash(Display::return_message(get_lang('Deleted')));
+        } catch (Exception $e) {
+            Display::addFlash(Display::return_message(get_lang('DeleteError'), 'error'));
+        }
         header('Location: '.$listAction);
         exit;
-
         break;
     default:
         $toolbar = Display::url(

+ 23 - 5
main/template/default/admin/skill.tpl

@@ -1,4 +1,3 @@
-
 <h3>{{ 'Skills' | get_lang }}</h3>
 <ul>
     <li>
@@ -14,16 +13,35 @@
 </ul>
 
 {{ form }}
-
+<table class="data_table">
+    <tr>
+        <th>
+            {{ 'Name' | get_lang }}
+        </th>
+        <th>
+            {{ 'Profile' | get_lang }}
+        </th>
+        <th>
+            {{ 'Actions' | get_lang }}
+        </th>
+    </tr>
 {% for item in list %}
-    <h4>
+    <tr>
+        <td>
         {{ item.name }}
         {% if item.shortCode %}
             ({{ item.shortCode }})
         {% endif %}
+        </td>
+        <td>
+        {{ item.profile }}
+        </td>
+        <td>
         <a href="{{ _p.web_main }}admin/skill.php?action=edit&id={{ item.id }}">
             <img src="{{ 'edit.png'|icon(22) }}">
         </a>
-    </h4>
-    {{ item.profile }}
+        </td>
+    </tr>
+
 {% endfor %}
+</table>

+ 0 - 2
main/template/default/admin/skill_level.tpl

@@ -4,7 +4,6 @@
     <tr>
         <th>{{ 'Name' | get_lang }}</th>
         <th>{{ 'ShortName' | get_lang }}</th>
-
         <th>{{ 'Profile' | get_lang }}</th>
         <th>{{ 'Actions' | get_lang }}</th>
     </tr>
@@ -17,7 +16,6 @@
                 <a href="{{ _p.web_main }}admin/skill_level.php?action=edit&id={{ item.id }}">
                     <img src="{{ 'edit.png'|icon(22) }}">
                 </a>
-
                 <a href="{{ _p.web_main }}admin/skill_level.php?action=delete&id={{ item.id }}">
                     <img src="{{ 'delete.png'|icon(22) }}">
                 </a>

+ 50 - 38
main/template/default/admin/skill_profile.tpl

@@ -1,42 +1,54 @@
 {{ form }}
 
+<table class="data_table">
+    <tr>
+        <th>{{ 'Name' | get_lang }}</th>
+        <th>{{ 'Level' | get_lang }}</th>
+        <th>{{ 'Actions' | get_lang }}</th>
+    </tr>
 {% for item in list %}
-
-    <h4>{{ item.name }}
-
-        {#<a href="{{ _p.web_main }}admin/skill_level.php?action=add_level&id={{ item.id }}">#}
-        {#<img src="{{ 'add.png'|icon(22) }}">#}
-        {#</a>#}
-
-        <a href="{{ _p.web_main }}admin/skill_profile.php?action=edit&id={{ item.id }}">
-            <img src="{{ 'edit.png'|icon(22) }}"> </a>
-
-        <a href="{{ _p.web_main }}admin/skill_profile.php?action=delete&id={{ item.id }}">
-            <img src="{{ 'delete.png'|icon(22) }}">
-        </a>
-    </h4>
-
-    <ul>
-        {% for level in item.levels %}
-            <li>
-                {{ level }}
-
-                {% if loop.first %}
-                    <img src="{{ 'up_na.png'|icon(22) }}">
-                {% else %}
-                    <a href="{{ _p.web_main }}admin/skill_profile.php?action=move_up&level_id={{ level.id }}&id={{ item.id }}">
-                        <img src="{{ 'up.png'|icon(22) }}">
-                    </a>
-                {% endif %}
-
-                {% if loop.last %}
-                    <img src="{{ 'down_na.png'|icon(22) }}">
-                {% else %}
-                    <a href="{{ _p.web_main }}admin/skill_profile.php?action=move_down&level_id={{ level.id }}&id={{ item.id }}">
-                        <img src="{{ 'down.png'|icon(22) }}">
-                    </a>
-                {% endif %}
-            </li>
-        {% endfor %}
-    </ul>
+    <tr>
+        <td>
+            {{ item.name }}
+            {#<a href="{{ _p.web_main }}admin/skill_level.php?action=add_level&id={{ item.id }}">#}
+            {#<img src="{{ 'add.png'|icon(22) }}">#}
+            {#</a>#}
+        </td>
+
+        <td>
+        <ul>
+            {% for level in item.levels %}
+                <li>
+                    {{ level }}
+
+                    {% if loop.first %}
+                        <img src="{{ 'up_na.png'|icon(22) }}">
+                    {% else %}
+                        <a href="{{ _p.web_main }}admin/skill_profile.php?action=move_up&level_id={{ level.id }}&id={{ item.id }}">
+                            <img src="{{ 'up.png'|icon(22) }}">
+                        </a>
+                    {% endif %}
+
+                    {% if loop.last %}
+                        <img src="{{ 'down_na.png'|icon(22) }}">
+                    {% else %}
+                        <a href="{{ _p.web_main }}admin/skill_profile.php?action=move_down&level_id={{ level.id }}&id={{ item.id }}">
+                            <img src="{{ 'down.png'|icon(22) }}">
+                        </a>
+                    {% endif %}
+                </li>
+            {% endfor %}
+        </ul>
+        </td>
+        <td>
+            <a href="{{ _p.web_main }}admin/skill_profile.php?action=edit&id={{ item.id }}">
+                <img src="{{ 'edit.png'|icon(22) }}"> </a>
+
+            <a href="{{ _p.web_main }}admin/skill_profile.php?action=delete&id={{ item.id }}">
+                <img src="{{ 'delete.png'|icon(22) }}">
+            </a>
+        </td>
+    </tr>
 {% endfor %}
+
+</table>