示例#1
0
 /**
  * Return the HTML selector code for the given category hierarchy.
  *
  * @param array        $cats             The category hierarchy to generate a HTML selector for.
  * @param string       $field            The field value to return (optional) (default='id').
  * @param string|array $selectedValue    The selected category (optional) (default=0).
  * @param string       $name             The name of the selector field to generate (optional) (default='category[parent_id]').
  * @param intiger      $defaultValue     The default value to present to the user (optional) (default=0).
  * @param string       $defaultText      The default text to present to the user (optional) (default='').
  * @param intiger      $allValue         The value to assign to the "all" option (optional) (default=0).
  * @param string       $allText          The text to assign to the "all" option (optional) (default='').
  * @param boolean      $submit           Whether or not to submit the form upon change (optional) (default=false).
  * @param boolean      $displayPath      If false, the path is simulated, if true, the full path is shown (optional) (default=false).
  * @param boolean      $doReplaceRootCat Whether or not to replace the root category with a localized string (optional) (default=true).
  * @param intiger      $multipleSize     If > 1, a multiple selector box is built, otherwise a normal/single selector box is build (optional) (default=1).
  * @param boolean      $fieldIsAttribute True if the field is attribute (optional) (default=false).
  *
  * @return The HTML selector code for the given category hierarchy
  */
 public static function getSelector_Categories($cats, $field = 'id', $selectedValue = '0', $name = 'category[parent_id]', $defaultValue = 0, $defaultText = '', $allValue = 0, $allText = '', $submit = false, $displayPath = false, $doReplaceRootCat = true, $multipleSize = 1, $fieldIsAttribute = false)
 {
     $line = '---------------------------------------------------------------------';
     if ($multipleSize > 1 && strpos($name, '[]') === false) {
         $name .= '[]';
     }
     if (!is_array($selectedValue)) {
         $selectedValue = array((string) $selectedValue);
     }
     $id = strtr($name, '[]', '__');
     $multiple = $multipleSize > 1 ? ' multiple="multiple"' : '';
     $multipleSize = $multipleSize > 1 ? " size=\"{$multipleSize}\"" : '';
     $submit = $submit ? ' onchange="this.form.submit();"' : '';
     $lang = ZLanguage::getLanguageCode();
     $html = "<select name=\"{$name}\" id=\"{$id}\"{$multipleSize}{$multiple}{$submit}>";
     if (!empty($defaultText)) {
         $sel = in_array((string) $defaultValue, $selectedValue) ? ' selected="selected"' : '';
         $html .= "<option value=\"{$defaultValue}\"{$sel}>{$defaultText}</option>";
     }
     if ($allText) {
         $sel = in_array((string) $allValue, $selectedValue) ? ' selected="selected"' : '';
         $html .= "<option value=\"{$allValue}\"{$sel}>{$allText}</option>";
     }
     $count = 0;
     if (!isset($cats) || empty($cats)) {
         $cats = array();
     }
     foreach ($cats as $cat) {
         if ($fieldIsAttribute) {
             $sel = in_array((string) $cat['__ATTRIBUTES__'][$field], $selectedValue) ? ' selected="selected"' : '';
         } else {
             $sel = in_array((string) $cat[$field], $selectedValue) ? ' selected="selected"' : '';
         }
         if ($displayPath) {
             if ($fieldIsAttribute) {
                 $v = $cat['__ATTRIBUTES__'][$field];
                 $html .= "<option value=\"{$v}\"{$sel}>{$cat['path']}</option>";
             } else {
                 $html .= "<option value=\"{$cat[$field]}\"{$sel}>{$cat['path']}</option>";
             }
         } else {
             $cslash = StringUtil::countInstances(isset($cat['ipath_relative']) ? $cat['ipath_relative'] : $cat['ipath'], '/');
             $indent = '';
             if ($cslash > 0) {
                 $indent = substr($line, 0, $cslash * 2);
             }
             $indent = '|' . $indent;
             //if ($count) {
             //    $indent = '|' . $indent;
             //} else {
             //    $indent = '&nbsp;' . $indent;
             //}
             if (isset($cat['display_name'][$lang]) && !empty($cat['display_name'][$lang])) {
                 $catName = $cat['display_name'][$lang];
             } else {
                 $catName = $cat['name'];
             }
             if ($fieldIsAttribute) {
                 $v = $cat['__ATTRIBUTES__'][$field];
                 $html .= "<option value=\"{$v}\"{$sel}>{$indent} " . DataUtil::formatForDisplayHtml($catName) . "</option>";
             } else {
                 $html .= "<option value=\"{$cat[$field]}\"{$sel}>{$indent} " . DataUtil::formatForDisplayHtml($catName) . "</option>";
             }
         }
         $count++;
     }
     $html .= '</select>';
     if ($doReplaceRootCat) {
         $html = str_replace('__SYSTEM__', __('Root category'), $html);
     }
     return $html;
 }