On 23/11/2012 10:00, David Durieux wrote:
On Fri, 23 Nov 2012 09:57:14 +0100, Damien Touraine wrote:
Bonjour,

On 23/11/2012 09:46, David Durieux wrote:
[...]
2°) modifier la classe NetworkPortEthernet pour autoriser tout type
de vitesse. Mais encore deux possibilités :
 a- on ne permet que la visualisation de cette valeur, sans pouvoir
créer un port avec une vitesse spécifique ;
 b- permettre de mettre n'importe quelle vitesse.


Au lieu d'avoir une dropdown, mettre une input text avec la valeur (voire même laisser la dropdown et mettre un bouton à coté pour pouvoir faire apparaitre la meme case en imput text, juste pour pouvoir mettre une valeur personalisée)
[...]

C'est ce que je pensais : dropdown + champ textuelle.

En fait, j'allais plus loin : choix "Autre ..."  dans la dropdown
avec apparition du champs de saisie textuel lorsqu'on le choisit.

Vi c'est ce à quoi je pensais

Peut-être puis-je ajouter cette option au Dropdown::showFromArray()
afin de le rendre disponible ailleurs, non ?

Pas sûr qu'on en ait bsoin ailleurs :p
Sais-ton jamais ...
Est-ce urgent ?

non ce n'est pas urgent
Dommage !

Je pense avoir une première solution opérationnelle dans le patch ci-joint.
Dites-moi ce que vous en penser avant que je ne le commit.

Damien
Index: inc/networkportethernet.class.php
===================================================================
--- inc/networkportethernet.class.php	(revision 19700)
+++ inc/networkportethernet.class.php	(working copy)
@@ -50,6 +50,31 @@
    }
 
 
+   function prepareInput($input) {
+
+      if ($input['speed'] == 'speed_other_value') {
+         $speed = self::transformPortSpeed($input['speed_other_value'], false);
+         if ($speed === false) {
+            unset($input['speed']);
+         } else {
+            $input['speed'] = $speed;
+         }
+      }
+
+      return $input;
+   }
+
+
+   function prepareInputForAdd($input) {
+      return parent::prepareInputForAdd($this->prepareInput($input));
+   }
+
+
+   function prepareInputForUpdate($input) {
+      return parent::prepareInputForUpdate($this->prepareInput($input));
+   }
+
+
    function showInstantiationForm(NetworkPort $netport, $options=array(), $recursiveItems) {
 
       if (!$options['several']) {
@@ -65,8 +90,17 @@
                               array('value' => $this->fields['type']));
       echo "</td>";
       echo "<td>" . __('Ethernet port speed') . "</td><td>\n";
-      Dropdown::showFromArray('speed', self::getPortSpeed(),
-                              array('value' => $this->fields['speed']));
+
+      $standard_speeds = self::getPortSpeed();
+      if (!isset($standard_speeds[$this->fields['speed']])) {
+         $speed = self::transformPortSpeed($this->fields['speed'], true);
+      } else {
+         $speed = true;
+      }
+
+      Dropdown::showFromArray('speed', $standard_speeds,
+                              array('value' => $this->fields['speed'],
+                                    'other' => $speed));
       echo "</td>";
       echo "</tr>\n";
 
@@ -200,6 +234,50 @@
     *
     * @return array or string
    **/
+   static function transformPortSpeed($val, $to_string) {
+
+      if ($to_string) {
+         if (($val % 1000) == 0) {
+            //TRANS: %d is the speed
+            return sprintf(__('%d Gbit/s'), $val / 1000);
+         }
+
+         if ((($val % 100) == 0) && ($val > 1000)) {
+            $val /= 100;
+            //TRANS: %f is the speed
+            return sprintf(__('%.1f Gbit/s'), $val / 10);
+         }
+
+         //TRANS: %d is the speed
+         return sprintf(__('%d Mbit/s'), $val);
+      } else {
+         $val = preg_replace( '/\s+/', '', strtolower($val));
+
+         $number = sscanf($val, "%f%s", $speed, $unit);
+         if ($number != 2) {
+            return false;
+         }
+
+         if ($unit == 'mbit/s') {
+            return (int)$speed;
+         }
+
+         if ($unit == 'gbit/s') {
+            return (int)($speed * 1000);
+         }
+
+         return false;
+      }
+   }
+
+
+   /**
+    * Get the possible value for Ethernet port speed
+    *
+    * @param $val if not set, ask for all values, else for 1 value (default NULL)
+    *
+    * @return array or string
+   **/
    static function getPortSpeed($val=NULL) {
 
       $tmp = array(0     => '',
Index: inc/dropdown.class.php
===================================================================
--- inc/dropdown.class.php	(revision 19700)
+++ inc/dropdown.class.php	(working copy)
@@ -1460,6 +1460,8 @@
     *    - size            : integer / number of rows for the select (default = 1)
     *    - mark_unmark_all : add buttons to select or deselect all options (only for multiple)
     *    - display         : boolean / display or return string
+    *    - other           : boolean or string if not false, then we can use an "other" value
+    *                        if it is a string, then the default value will be this string
     *    - rand            : specific rand if needed (default is generated one)
     *
     * Permit to use optgroup defining items in arrays
@@ -1479,6 +1481,7 @@
       $param['size']            = 1;
       $param['mark_unmark_all'] = false;
       $param['display']         = true;
+      $param['other']           = false;
       $param['rand']            = mt_rand();
 
       if (is_array($options) && count($options)) {
@@ -1491,6 +1494,20 @@
          }
       }
 
+      if ($param['other'] !== false) {
+         $other_select_option = $name . '_other_value';
+         $param['on_change'] .= "displayOtherSelectOptions(this, \"$other_select_option\");";
+
+         // If $param['other'] is a string, then we must highlight "other" option
+         if (is_string($param['other'])) {
+            if (!$param["multiple"]) {
+               $param['values'] = array($other_select_option);
+            } else {
+               $param['values'][] = $other_select_option;
+            }
+         }
+      }
+
       if ($param["multiple"]) {
          $field_name = $name."[]";
       } else {
@@ -1551,15 +1568,35 @@
             }
          }
 
+         if ($param['other'] !== false) {
+            $output .= "<option value='$other_select_option'";
+            if (is_string($param['other'])) {
+               $output .= " selected";
+            }
+            $output .= ">".__('Other ...')."</option>";
+         }
+
          $output .= "</select>";
+         if ($param['other'] !== false) {
+            $output .= "<input name='$other_select_option' id='$other_select_option' type='text'";
+            if (is_string($param['other'])) {
+               $output .= " value=\"" . $param['other'] . "\"";
+            } else {
+               $output .= " style=\"display: none\"";
+            }
+            $output .= ">";
+         }
+
          if ($param['mark_unmark_all'] && $param['multiple']) {
             $output .= "<br>\n";
-            $output .= "<input type='button' onclick=\"markSelect('$field_id')\" value='";
+            $output .= "<input type='button' onclick=\"selectAllOptions('$field_id')\" value='";
             $output .= __('Select all options')."'>";
-            $output .= "<input type='button' onclick=\"unMarkSelect('$field_id')\" value='";
+            $output .= "<input type='button' onclick=\"unselectAllOptions('$field_id')\" value='";
             $output .=  __('Deselect all options')."'>";
          }
+
       }
+
       if ($param['display']) {
          echo $output;
          return $param['rand'];
Index: inc/networkport.class.php
===================================================================
--- inc/networkport.class.php	(revision 19700)
+++ inc/networkport.class.php	(working copy)
@@ -92,20 +92,20 @@
       $forbidden[] = 'update';
       return $forbidden;
    }
-   
+
    /**
     * @since version 0.84
     *
     * @see CommonDBTM::getPreAdditionalInfosForName
    **/
    function getPreAdditionalInfosForName() {
-   
+
       if ($item = $this->getItem()) {
          return $item->getName() . ' <';
       }
       return '';
    }
-   
+
    /**
     * \brief get the list of available network port type.
     *
Index: script.js
===================================================================
--- script.js	(revision 19700)
+++ script.js	(working copy)
@@ -440,11 +440,11 @@
 
 
 /**
- * marks all option inside the given select
+ * select all option inside the given select
  *
  * @param    select_id    DOM select id
 **/
-function markSelect(select_id) {
+function selectAllOptions(select_id) {
 
    var options = document.getElementById(select_id).getElementsByTagName('option');
    for (var j=0 ; j<options.length ; j++ ) {
@@ -455,11 +455,29 @@
 
 
 /**
- * marks all option inside the given select
+ * display "other" text input field in case of selecting "other" option
  *
+ * @param    select_object     DOM select object
+ * @param    other_option_name the name of both the option and the text input field
+**/
+function displayOtherSelectOptions(select_object, other_option_name) {
+
+   var SelIndex = select_object.selectedIndex;
+   if (select_object.options[select_object.selectedIndex].value == other_option_name) {
+      document.getElementById(other_option_name).style.display = "inline";
+   } else {
+      document.getElementById(other_option_name).style.display = "none";
+   }
+   return true;
+}
+
+
+/**
+ * unselect all option inside the given select
+ *
  * @param    select_id    DOM select id
 **/
-function unMarkSelect(select_id) {
+function unselectAllOptions(select_id) {
 
    var options = document.getElementById(select_id).getElementsByTagName('option');
    for (var j=0 ; j<options.length ; j++ ) {
_______________________________________________
Glpi-dev mailing list
Glpi-dev@gna.org
https://mail.gna.org/listinfo/glpi-dev

Reply via email to