Example #1
0
 /**
  * This will generate a multi-select (by default) of all numbers in a pool
  * or list of pools and select those assigned to a classes foreign id.
  * The values of the select option will be the number id.
  *
  * This dropdown can also be configured to be a select a single number example:
  *     echo numbering::dropdown(array(
  *          'classType' => 'AutoAttendantNumber',
  *          'multiple' => FALSE,
  *          'listAssigned' => FALSE,
  *          'optGroups' => FALSE
  *     ));
  * and if you would like to have a 'null select' option add to the array:
  *    nullOption => 'Select'
  *
  *
  * Additional Data Options:
  *  classType   = a single pool type to list or an array of types, if empty
  *                    all are used.
  *  optGroups   = Render the select options in optgroups
  *                    by assigned/unassigned
  *  nullOption  = If this is a string then it is used as the '0' option,
  *                    or if false then no such option will exist
  * listAssigned = Include the assigned number in the list
  *
  * NOTE: you can not change the name of this dropdown
  *
  * @param   string|array  input name or an array of HTML attributes
  * @param   string        A foreign id of the class type
  * @param   string        a string to be attached to the end of the attributes
  * @return  string
  */
 public static function dropdown($data, $selected = NULL, $extra = '')
 {
     // standardize the $data as an array, strings default to the class_type
     if (!is_array($data)) {
         $data = array('classType' => $data);
     }
     // if we dont have a class type then we fail
     if (empty($data['classType'])) {
         kohana::log('error', 'You can not render the numbering::assigndropdown without a class type!');
         return FALSE;
     }
     // add in all the defaults if they are not provided
     $data += array('multiple' => 'multiple', 'size' => 6, 'id' => 'assign' . $data['classType'], 'optGroups' => TRUE, 'nullOption' => FALSE, 'listAssigned' => TRUE);
     $data['name'] = '_numbers[assign][]';
     // if multiple is empty then unset it
     if (empty($data['multiple'])) {
         unset($data['multiple']);
     }
     // append or insert the class
     $data = arr::update($data, 'class', ' numbers_dropdown');
     // get any numbers already assigned to the is dropdown
     $options['Assigned'] = self::getAssignedNumbers($data['classType'], $selected);
     // get all the unassigned number of the class types in unassignedTypes
     $options['Unassigned'] = self::getUnassignedNumbersByPool($data['classType']);
     unset($data['classType']);
     // render a null option if its been set in data
     if (!empty($data['nullOption'])) {
         array_unshift($options['Unassigned'], __($data['nullOption']));
     }
     unset($data['nullOption']);
     // the dropdown expects the assigned array in reverse order
     $selected = array_flip($options['Assigned']);
     // see if there are any posts for this dropdown
     if ($post = arr::parse_str($data['name'], $_POST)) {
         $selected += $post;
     }
     // remove any attributes the dropdown helper will not understand
     $localAttr = array('additionalTypes', 'class_type');
     $data = array_diff_key($data, array_flip($localAttr));
     // if we are not listing the assigned numbers in this select
     if (!$data['listAssigned']) {
         $options['Assigned'] = array();
     }
     // if we are rendering this as optGroups ensure the markup will be valid
     // otherwise merge the array
     if ($data['optGroups']) {
         // remove the empty optgroups (illegal markup to have a empty group)
         if (empty($options['Assigned'])) {
             unset($options['Assigned']);
         }
         if (empty($options['Unassigned'])) {
             unset($options['Unassigned']);
         }
     } else {
         // Array merge re-indexs the keys so manually merge
         $assigned = $options['Assigned'];
         $options = $options['Unassigned'];
         foreach ($assigned as $key => $number) {
             $options[$key] = $number;
         }
     }
     unset($data['optGroups']);
     // use kohana helper to generate the markup
     $layout = form::dropdown($data, $options, $selected, $extra);
     $layout .= form::hidden('containsAssigned[]', $data['name']);
     return $layout;
 }