Bonjour,
Il est possible d'utiliser le "reloadTab" pour faire des modifications dans l'onglet sans pour autant imposer le rechargement de la page en entier (et donc, sans perdre les données saisies dans la partie commune de l'item). Cela permet, en outre, de mettre à jours la base de donnée lors de l'appel de la méthode displayTabContentForItem. Ainsi, la mise à jours de la base de donnée est dans la même méthode que le "formulaire" qui vise à la modifier.

Par exemple, dans le patch ci-joint, cela permet d'ajouter ou de retirer un composant plus simplement (avec, au passage, un contrôle plus fin de la multiplicité des composants). Ainsi, la mise à jour de la base de donnée se passe en tête de la méthode showForComputer, alors que les "clics" de modification son 85 et 99 lignes plus bas).

C'est un mode de procéder que je n'ai pas rencontrer précédemment. Cela me semble facilité par le nouveau mode de fonctionnement des onglets (gestion des onglets par les classes elle-même et non par des appels AJAX).

* Doit-on éviter d'utiliser ce type de mécanisme ?
* Est-ce acceptable comme manière de procéder ?
* Est-ce une piste d'évolution souhaitable/souhaitée pour l'avenir ?

Damien

--
--------------------------------------------------------------------
Damien TOURAINE - Ingénieur de Recherche CNRS, LIMSI-CNRS
Groupe de RV&A "VENISE", (http://www.limsi.fr/venise/)
Bat. 508, Universite Paris-Sud 91403 Orsay cedex - +33 1 69 85 81 64
--------------------------------------------------------------------

Index: computer_device.class.php
===================================================================
--- computer_device.class.php   (revision 15701)
+++ computer_device.class.php   (working copy)
@@ -187,6 +187,23 @@
    static function showForComputer(Computer $computer, $withtemplate='') {
       global $DB, $LANG;
 
+      if (isset($_POST['deviceAction']) && isset($_POST['devicetype'])){
+        $compdev = new Computer_Device();
+        if (($_POST['deviceAction'] == 'add') &&
+            isset($_POST['computer']) &&
+            isset($_POST['devices_id'])) {
+           $input = array('computers_id' => $_POST['computer'],
+                          'itemtype' => $_POST['devicetype'],
+                          'items_id' => $_POST['devices_id']);
+           if ($compdev->can(-1, 'w', $input)) {
+              $compdev->add($input);
+           }
+        } else if (($_POST['deviceAction'] == 'remove') &&
+                   isset($_POST['computerdevice_id'])) {
+           $compdev->removeComputerDevice($_POST['devicetype'], 
$_POST['computerdevice_id']);
+        }
+      }
+
       $devtypes = self::getDeviceTypes();
 
       $ID = $computer->getField('id');
@@ -201,8 +218,9 @@
                 "' method='post'>";
          echo "<input type='hidden' name='computers_id' value='$ID'>";
       }
+      $global_colspan = 64;
       echo "<table class='tab_cadre_fixe' >";
-      echo "<tr><th 
colspan='63'>".Toolbox::ucfirst($LANG['log'][18])."</th></tr>";
+      echo "<tr><th 
colspan='$global_colspan'>".Toolbox::ucfirst($LANG['log'][18])."</th></tr>";
       $nb = 0;
 
       $specificity_units = array('DeviceProcessor'   => $LANG['setup'][35],
@@ -210,6 +228,7 @@
                                  'DeviceHardDrive'   => $LANG['common'][82],
                                  'DeviceGraphicCard' => $LANG['common'][82]);
 
+      $numberOfPreviousItem = 0;
       foreach ($devtypes as $itemtype) {
          Session::initNavigateListItems($itemtype,
                                         $computer->getTypeName()." = 
".$computer->getName());
@@ -226,74 +245,96 @@
          $fk        = 
getForeignKeyFieldForTable(getTableForItemType($itemtype));
 
          $query = "SELECT COUNT(*) AS NB,
-                          `id`,
                           `$fk`
-                          $specif_text
                    FROM `$linktable`
                    WHERE `computers_id` = '$ID'
-                   GROUP BY `$fk` $specif_text";
+                   GROUP BY `$fk`";
+        foreach ($DB->request($query) as $deviceFromSQL) {
+           if ($numberOfPreviousItem * $deviceFromSQL['NB'] > 1)
+              echo "<tr><td colspan='$global_colspan'><hr></td></tr>";
+           $numberOfPreviousItem = $deviceFromSQL['NB'];
+           $query = "SELECT `id`,
+                            `$fk`
+                            $specif_text
+                      FROM `$linktable`
+                      WHERE `$fk` = '".$deviceFromSQL[$fk]."'
+                      ORDER BY id";
+           $first = true;
+           foreach ($DB->request($query) as $data) {
+              Session::addToNavigateListItems($itemtype, $data[$fk]);
 
-         $prev = '';
-         foreach ($DB->request($query) as $data) {
-            Session::addToNavigateListItems($itemtype, $data[$fk]);
-
-            if ($device->getFromDB($data[$fk])) {
-               echo "<tr class='tab_bg_2'>";
-               echo "<td class='center'>";
-               Dropdown::showInteger("quantity_".$itemtype."_".$data['id'], 
$data['NB']);
-               echo "</td><td>";
-               if ($device->canCreate()) {
-                  echo "<a 
href='".$device->getSearchURL()."'>".$device->getTypeName()."</a>";
-               } else {
-                  echo $device->getTypeName();
-               }
-               echo "</td><td>".$device->getLink()."</td>";
-
-               $spec = $device->getFormData();
-               if (isset($spec['label']) && count($spec['label'])) {
-                  $colspan = (60/count($spec['label']));
-                  foreach ($spec['label'] as $i => $label) {
-
-                     if (isset($spec['value'][$i])) {
-                        echo "<td 
colspan='$colspan'>".$spec['label'][$i]."&nbsp;: ";
-                        echo $spec['value'][$i]."</td>";
-
-                     } else if ($canedit) {
-                        // Specificity
-                        echo "<td class='right' 
colspan='$colspan'>".$spec['label'][$i]."&nbsp;: ";
-                        echo "<input type='text' 
name='value_".$itemtype."_".$data['id']."' value='".
-                               $data['specificity']."' 
size='".$spec['size']."'>";
-                        if (isset($specificity_units[$device->getType()])) {
-                           echo 
'&nbsp;'.$specificity_units[$device->getType()];
-                        }
-                        echo "</td>";
-
-                     } else {
-                        echo "<td 
colspan='$colspan'>".$spec['label'][$i]."&nbsp;: ";
-                        echo $data['specificity'];
-                        if (isset($specificity_units[$device->getType()])) {
-                           echo 
'&nbsp;'.$specificity_units[$device->getType()];
-                        }
-                        echo "</td>";
-                     }
-                  }
-               } else {
-                  echo "<td colspan='60'>&nbsp;</td>";
-               }
-               echo "</tr>";
-               $nb++;
-            }
-         }
+              if ($device->getFromDB($data[$fk])) {
+                 echo "<tr class='tab_bg_2'>";
+                 if ($first) {
+                    if ($deviceFromSQL['NB'] > 1)
+                       $rowspan = "rowspan='".$deviceFromSQL['NB']."'";
+                    else
+                       $rowspan = "";
+                    echo "<td class='center' $rowspan>";
+                    echo "<input type='submit' class='submit' name='addDevice' 
value='".
+                       $LANG['buttons'][8]."' 
onclick='reloadTab(\"deviceAction=add&amp;devicetype=$itemtype&amp;computer=$ID&amp;devices_id=".$data[$fk]."\")'>";
+                    
//Dropdown::showInteger("quantity_".$itemtype."_".$data['id'], $data['NB']);
+                    echo "</td>";
+                    echo "<td $rowspan>";
+                    if ($device->canCreate()) {
+                       echo "<a 
href='".$device->getSearchURL()."'>".$device->getTypeName()."</a>";
+                    } else {
+                       echo $device->getTypeName();
+                    }
+                    echo "</td><td $rowspan>".$device->getLink()."</td>";
+                 }
+                 
+                 echo "<td class='center'>";
+                 echo "<input type='submit' class='submit' name='removeDevice' 
value='".
+                    $LANG['buttons'][6]."' 
onclick='reloadTab(\"deviceAction=remove&amp;devicetype=$itemtype&amp;computerdevice_id=".$data['id']."\")'>";
+                 echo "</td>";
+                 $spec = $device->getFormData();
+                 if (isset($spec['label']) && count($spec['label'])) {
+                    $colspan = (60/count($spec['label']));
+                    foreach ($spec['label'] as $i => $label) {
+                       
+                       if (isset($spec['value'][$i])) {
+                          echo "<td 
colspan='$colspan'>".$spec['label'][$i]."&nbsp;: ";
+                          echo $spec['value'][$i]."</td>";
+                          
+                       } else if ($canedit) {
+                          // Specificity
+                          echo "<td class='right' 
colspan='$colspan'>".$spec['label'][$i]."&nbsp;: ";
+                          echo "<input type='text' 
name='value_".$itemtype."_".$data['id']."' value='".
+                             $data['specificity']."' 
size='".$spec['size']."'>";
+                          if (isset($specificity_units[$device->getType()])) {
+                             echo 
'&nbsp;'.$specificity_units[$device->getType()];
+                          }
+                          echo "</td>";
+                          
+                       } else {
+                          echo "<td 
colspan='$colspan'>".$spec['label'][$i]."&nbsp;: ";
+                          echo $data['specificity'];
+                          if (isset($specificity_units[$device->getType()])) {
+                             echo 
'&nbsp;'.$specificity_units[$device->getType()];
+                          }
+                          echo "</td>";
+                       }
+                    }
+                 } else {
+                    echo "<td colspan='60'>&nbsp;</td>";
+                 }
+                 echo "</tr>";
+                 $nb++;
+              }
+              $first = false;
+           }
+        }
       }
 
       if ($canedit) {
          if ($nb > 0) {
-            echo "<tr><td colspan='63' class='tab_bg_1 center'>";
+            echo "<tr><td colspan='$global_colspan' class='tab_bg_1 center'>";
             echo "<input type='submit' class='submit' name='updateall' 
value='".
                    $LANG['buttons'][7]."'></td></tr>";
          }
 
-         echo "<tr><td colspan='63' class='tab_bg_1 center'>";
+         echo "<tr><td colspan='$global_colspan' class='tab_bg_1 center'>";
          echo $LANG['devices'][0]."&nbsp;: ";
          Dropdown::showAllItems('items_id', '', 0, -1, $devtypes);
          echo "<input type='submit' name='add' 
value=\"".$LANG['buttons'][8]."\" class='submit'>";
@@ -305,7 +346,23 @@
       echo "</div>";
    }
 
+   /**
+    * Remove an internal Computer Device Link
+    *
+    * @param $devicetype itemtype of device
+    * @param $compDevID computer device ID
+    **/
+   private function removeComputerDevice($deviceType,$compDevID) {
+      global $DB;
 
+      $linktable = getTableForItemType('Computer_'.$deviceType);
+      $this->forceTable($linktable);
+
+      if ($this->can($compDevID,'d'))
+        return $this->delete(array('id' => $compDevID, '_itemtype' => 
$deviceType));
+      return false;
+   }
+
    /**
     * Update an internal device quantity
     *
@@ -396,20 +453,10 @@
          return false;
       }
 
-      // Update specificity
-      $query = "SELECT `id`
-                FROM `$linktable`
-                WHERE `computers_id` = '".$this->fields["computers_id"]."'
-                      AND `$fk` = '".$this->fields[$fk]."'
-                      AND `specificity` = 
'".addslashes($this->fields["specificity"])."'";
-
-      $first = true;
-      foreach ($DB->request($query) as $data) {
-         $data['specificity'] = $newValue;
-         $data['_itemtype']   = $itemtype;
-         $this->update($data, $first);
-         $first = false;
-      }
+      $data = array('id'          => $compDevID,
+                   'specificity' => $newValue,
+                   '_itemtype'   => $itemtype);
+      $this->update($data, true);
    }
 
 
_______________________________________________
Glpi-dev mailing list
Glpi-dev@gna.org
https://mail.gna.org/listinfo/glpi-dev

Reply via email to