/**
 * Erstellt den nötigen HTML Code um ein Formular zu erweitern
 *
 * @param $sqlFields rex_sql-objekt, dass die zu verarbeitenden Felder enthält
 * @param $activeItem objekt, dass mit getValue() die Werte des akuellen Eintrags zurückgibt
 * @param $formatCallback callback, dem die infos als Array übergeben werden und den formatierten HTML Text zurückgibt
 */
function rex_a62_metaFields($sqlFields, $activeItem, $formatCallback, $epParams)
{
    global $I18N;
    $s = '';
    // Startwert für MEDIABUTTON, MEDIALIST, LINKLIST zähler
    $media_id = 1;
    $mlist_id = 1;
    $link_id = 1;
    $llist_id = 1;
    $sqlFields->reset();
    for ($i = 0; $i < $sqlFields->getRows(); $i++) {
        // Umschliessendes Tag von Label und Formularelement
        $tag = 'p';
        $tag_attr = '';
        $name = $sqlFields->getValue('name');
        $title = $sqlFields->getValue('title');
        $params = $sqlFields->getValue('params');
        $typeLabel = $sqlFields->getValue('label');
        $attr = $sqlFields->getValue('attributes');
        $dblength = $sqlFields->getValue('dblength');
        $dbvalues = array(htmlspecialchars($sqlFields->getValue('default')));
        $dbvalues_esc = $dbvalues;
        if ($activeItem) {
            $itemValue = $activeItem->getValue($name);
            if (strpos($itemValue, '|+|') !== false) {
                // Alte notation mit |+| als Trenner
                $dbvalues = explode('|+|', $activeItem->getValue($name));
            } else {
                // Neue Notation mit | als Trenner
                $dbvalues = explode('|', $activeItem->getValue($name));
            }
            $dbvalues_esc = array_map('htmlspecialchars', $dbvalues);
        }
        if ($title != '') {
            $label = htmlspecialchars(rex_translate($title));
        } else {
            $label = htmlspecialchars($name);
        }
        $id = preg_replace('/[^a-zA-Z\\-0-9_]/', '_', $label);
        $attr .= rex_tabindex();
        $labelIt = true;
        $field = '';
        switch ($typeLabel) {
            case 'text':
                $field = '<input type="' . $typeLabel . '" name="' . $name . '" value="' . $dbvalues_esc[0] . '" id="' . $id . ' "maxlength="' . $dblength . '" ' . $attr . ' />';
                break;
            case 'checkbox':
                $name .= '[]';
            case 'radio':
                $values = array();
                if (rex_sql::getQueryType($params) == 'SELECT') {
                    $sql = new rex_sql();
                    $value_groups = $sql->getDBArray($params, MYSQL_NUM);
                    foreach ($value_groups as $value_group) {
                        if (isset($value_group[1])) {
                            $values[$value_group[1]] = $value_group[0];
                        } else {
                            $values[$value_group[0]] = $value_group[0];
                        }
                    }
                } else {
                    $value_groups = explode('|', $params);
                    foreach ($value_groups as $value_group) {
                        if (strpos($value_group, ':') !== false) {
                            $temp = explode(':', $value_group);
                            $values[$temp[0]] = $temp[1];
                        } else {
                            $values[$value_group] = $value_group;
                        }
                    }
                }
                $class = $typeLabel == 'radio' ? 'rex-rdo' : 'rex-chckbx';
                $oneValue = count($values) == 1;
                if (!$oneValue) {
                    $tag = '';
                    $labelIt = false;
                    $tag = 'div';
                    $tag_attr = ' class="rex-chckbxs rex-ptag"';
                    $field .= '<p>' . $label . '</p>';
                }
                foreach ($values as $key => $value) {
                    $id = preg_replace('/[^a-zA-Z\\-0-9_]/', '_', $id . $key);
                    $key = htmlspecialchars($key);
                    // wenn man keine Werte angibt (Boolean Chkbox/Radio)
                    // Dummy Wert annehmen, damit an/aus unterscheidung funktioniert
                    if ($oneValue && $key == '') {
                        $key = 'true';
                    }
                    $selected = '';
                    if (in_array($key, $dbvalues_esc)) {
                        $selected = ' checked="checked"';
                    }
                    if ($oneValue) {
                        $tag_attr = ' class="' . $class . '"';
                        $field .= '<input type="' . $typeLabel . '" name="' . $name . '" value="' . $key . '" id="' . $id . '" ' . $attr . $selected . ' />' . "\n";
                    } else {
                        $field .= '<p class="' . $class . '">' . "\n";
                        $field .= '<label for="' . $id . '"><span>' . htmlspecialchars($value) . '</span></label>';
                        $field .= '<input type="' . $typeLabel . '" name="' . $name . '" value="' . $key . '" id="' . $id . '" ' . $attr . $selected . ' />' . "\n";
                        $field .= '</p>' . "\n";
                    }
                }
                break;
            case 'select':
                $select = new rex_select();
                $select->setName($name);
                $select->setId($id);
                // hier mit den "raw"-values arbeiten, da die rex_select klasse selbst escaped
                $select->setSelected($dbvalues);
                foreach (rex_split_string($attr) as $attr_name => $attr_value) {
                    if (empty($attr_name)) {
                        continue;
                    }
                    $select->setAttribute($attr_name, $attr_value);
                    if ($attr_name == 'multiple') {
                        $select->setName($name . '[]');
                    }
                }
                if (rex_sql::getQueryType($params) == 'SELECT') {
                    // Werte via SQL Laden
                    $select->addDBSqlOptions($params);
                } else {
                    // Optionen mit | separiert
                    // eine einzelne Option kann mit key:value separiert werden
                    $values = array();
                    $value_groups = explode('|', $params);
                    foreach ($value_groups as $value_group) {
                        if (strpos($value_group, ':') !== false) {
                            $temp = explode(':', $value_group);
                            $values[$temp[0]] = $temp[1];
                        } else {
                            $values[$value_group] = $value_group;
                        }
                    }
                    $select->addOptions($values);
                }
                $field .= $select->get();
                break;
            case 'datetime':
            case 'date':
                if ($dbvalues_esc[0] == '') {
                    $dbvalues_esc[0] = time();
                }
                $style = 'class="rex-fdate"';
                $yearStyle = 'class="rex-fdatey"';
                $yearSelect = new rex_select();
                $yearSelect->addOptions(range(2005, date('Y') + 10), true);
                $yearSelect->setName($name . '[year]');
                $yearSelect->setSize(1);
                $yearSelect->setId($id);
                $yearSelect->setStyle($yearStyle);
                $yearSelect->setSelected(date('Y', $dbvalues_esc[0]));
                $monthSelect = new rex_select();
                $monthSelect->addOptions(range(1, 12), true);
                $monthSelect->setName($name . '[month]');
                $monthSelect->setSize(1);
                $monthSelect->setStyle($style);
                $monthSelect->setSelected(date('m', $dbvalues_esc[0]));
                $daySelect = new rex_select();
                $daySelect->addOptions(range(1, 31), true);
                $daySelect->setName($name . '[day]');
                $daySelect->setSize(1);
                $daySelect->setStyle($style);
                $daySelect->setSelected(date('j', $dbvalues_esc[0]));
                if ($typeLabel == 'datetime') {
                    $hourSelect = new rex_select();
                    $hourSelect->addOptions(range(1, 23), true);
                    $hourSelect->setName($name . '[hour]');
                    $hourSelect->setSize(1);
                    $hourSelect->setStyle($style);
                    $hourSelect->setSelected(date('G', $dbvalues_esc[0]));
                    $minuteSelect = new rex_select();
                    $minuteSelect->addOptions(range(0, 59), true);
                    $minuteSelect->setName($name . '[minute]');
                    $minuteSelect->setSize(1);
                    $minuteSelect->setStyle($style);
                    $minuteSelect->setSelected(date('i', $dbvalues_esc[0]));
                    $field = $daySelect->get() . $monthSelect->get() . $yearSelect->get() . '-' . $hourSelect->get() . $minuteSelect->get();
                } else {
                    $field = $daySelect->get() . $monthSelect->get() . $yearSelect->get();
                }
                break;
            case 'textarea':
                $field = '<textarea name="' . $name . '" id="' . $id . '" cols="50" rows="6" ' . $attr . '>' . $dbvalues_esc[0] . '</textarea>';
                break;
            case 'REX_MEDIA_BUTTON':
                $tag = 'div';
                $tag_attr = ' class="rex-ptag"';
                $field = rex_var_media::getMediaButton($media_id);
                $field = str_replace('REX_MEDIA[' . $media_id . ']', $dbvalues_esc[0], $field);
                $field = str_replace('MEDIA[' . $media_id . ']', $name, $field);
                $id = 'REX_MEDIA_' . $media_id;
                $media_id++;
                break;
            case 'REX_MEDIALIST_BUTTON':
                $tag = 'div';
                $tag_attr = ' class="rex-ptag"';
                $name .= '[]';
                $field = rex_var_media::getMediaListButton($mlist_id, implode(',', $dbvalues_esc));
                $field = str_replace('MEDIALIST[' . $mlist_id . ']', $name, $field);
                $id = 'REX_MEDIALIST_' . $mlist_id;
                $mlist_id++;
                break;
            case 'REX_LINK_BUTTON':
                $tag = 'div';
                $tag_attr = ' class="rex-ptag"';
                $category = '';
                if ($activeItem) {
                    $category = $activeItem->getValue('category_id');
                }
                $field = rex_var_link::getLinkButton($link_id, $dbvalues_esc[0], $category);
                $field = str_replace('LINK[' . $link_id . ']', $name, $field);
                $id = 'LINK_' . $link_id;
                $link_id++;
                break;
            case 'REX_LINKLIST_BUTTON':
                $tag = 'div';
                $tag_attr = ' class="rex-ptag"';
                $category = '';
                if ($activeItem) {
                    $category = $activeItem->getValue('category_id');
                }
                $name .= '[]';
                $field = rex_var_link::getLinklistButton($llist_id, implode(',', $dbvalues), $category);
                $field = str_replace('LINKLIST[' . $llist_id . ']', $name, $field);
                $id = 'REX_LINKLIST_' . $llist_id;
                $llist_id++;
                break;
            default:
                // ----- EXTENSION POINT
                list($field, $tag, $tag_attr, $id, $label, $labelIt) = rex_register_extension_point('A62_CUSTOM_FIELD', array($field, $tag, $tag_attr, $id, $label, $labelIt, 'type' => $typeLabel, 'sql' => $sqlFields));
        }
        $s .= rex_call_func($formatCallback, array($field, $tag, $tag_attr, $id, $label, $labelIt), false);
        $sqlFields->next();
    }
    return $s;
}
/**
 * Übernimmt die gePOSTeten werte in ein rex_sql-Objekt
 *
 * @param $sqlSave rex_sql-objekt, in das die aktuellen Werte gespeichert werden sollen
 * @param $sqlFields rex_sql-objekt, dass die zu verarbeitenden Felder enthält
 */
function _rex_a62_metainfo_handleSave(&$params, &$sqlSave, $sqlFields)
{
    global $REX;
    if ($_SERVER['REQUEST_METHOD'] != 'POST') {
        return;
    }
    for ($i = 0; $i < $sqlFields->getRows(); $i++, $sqlFields->next()) {
        $fieldName = $sqlFields->getValue('name');
        $fieldType = $sqlFields->getValue('type');
        $fieldAttributes = $sqlFields->getValue('attributes');
        $postValue = rex_post($fieldName, 'array');
        // dont save restricted fields
        $attrArray = rex_split_string($fieldAttributes);
        if (isset($attrArray['perm'])) {
            if (!$REX['USER']->hasPerm($attrArray['perm'])) {
                continue;
            }
            unset($attrArray['perm']);
        }
        // handle date types with timestamps
        if (isset($postValue['year']) && isset($postValue['month']) && isset($postValue['day']) && isset($postValue['hour']) && isset($postValue['minute'])) {
            if (isset($postValue['active'])) {
                $saveValue = mktime((int) $postValue['hour'], (int) $postValue['minute'], 0, (int) $postValue['month'], (int) $postValue['day'], (int) $postValue['year']);
            } else {
                $saveValue = 0;
            }
        } elseif (isset($postValue['year']) && isset($postValue['month']) && isset($postValue['day'])) {
            if (isset($postValue['active'])) {
                $saveValue = mktime(0, 0, 0, (int) $postValue['month'], (int) $postValue['day'], (int) $postValue['year']);
            } else {
                $saveValue = 0;
            }
        } elseif (isset($postValue['hour']) && isset($postValue['minute'])) {
            if (isset($postValue['active'])) {
                $saveValue = mktime((int) $postValue['hour'], (int) $postValue['minute'], 0, 0, 0, 0);
            } else {
                $saveValue = 0;
            }
        } else {
            if (count($postValue) > 1) {
                // Mehrwertige Felder
                $saveValue = '|' . implode('|', $postValue) . '|';
            } else {
                $postValue = isset($postValue[0]) ? $postValue[0] : '';
                if ($fieldType == REX_A62_FIELD_SELECT && strpos($fieldAttributes, 'multiple') !== false || $fieldType == REX_A62_FIELD_CHECKBOX) {
                    // Mehrwertiges Feld, aber nur ein Wert ausgewählt
                    $saveValue = '|' . $postValue . '|';
                } else {
                    // Einwertige Felder
                    $saveValue = $postValue;
                }
            }
        }
        // Wert in SQL zum speichern
        $sqlSave->setValue($fieldName, $saveValue);
        // Werte im aktuellen Objekt speichern, dass zur Anzeige verwendet wird
        if (isset($params['activeItem'])) {
            $params['activeItem']->setValue($fieldName, stripslashes($saveValue));
        }
    }
}
 /**
  * Trennt einen String an Leerzeichen auf.
  * Abschnitte die in "" oder '' stehen, werden als ganzes behandelt und
  * darin befindliche Leerzeichen nicht getrennt.
  * @access protected
  */
 function splitString($string)
 {
     return rex_split_string($string);
 }
 /**
  * return array with the meta name & values
  * it test if the form is posted or not and add the necessary values
  * @return array
  */
 public function getMetaValues()
 {
     global $REX;
     $returnArray = array();
     $this->sqlFields->reset();
     for ($i = 0; $i < $this->sqlFields->getRows(); $i++, $this->sqlFields->next()) {
         $fieldName = $this->sqlFields->getValue('name');
         $fieldType = $this->sqlFields->getValue('type');
         $fieldAttributes = $this->sqlFields->getValue('attributes');
         $postValue = rex_post($fieldName, 'array', null);
         // Wert aus der DB nehmen, falls keiner extern und keiner im POST angegeben
         if ($postValue === null && $this->sql->getRows() == 1 && $this->sql->hasValue($fieldName)) {
             $postValue = $this->sql->getValue($fieldName);
         }
         // dont save restricted fields
         $attrArray = rex_split_string($fieldAttributes);
         if (isset($attrArray['perm'])) {
             if (!$REX['USER']->hasPerm($attrArray['perm'])) {
                 continue;
             }
             unset($attrArray['perm']);
         }
         // handle date types with timestamps
         if (is_array($postValue) && isset($postValue['year']) && isset($postValue['month']) && isset($postValue['day']) && isset($postValue['hour']) && isset($postValue['minute'])) {
             if (isset($postValue['active'])) {
                 $saveValue = mktime((int) $postValue['hour'], (int) $postValue['minute'], 0, (int) $postValue['month'], (int) $postValue['day'], (int) $postValue['year']);
             } else {
                 $saveValue = 0;
             }
         } elseif (is_array($postValue) && isset($postValue['year']) && isset($postValue['month']) && isset($postValue['day'])) {
             if (isset($postValue['active'])) {
                 $saveValue = mktime(0, 0, 0, (int) $postValue['month'], (int) $postValue['day'], (int) $postValue['year']);
             } else {
                 $saveValue = 0;
             }
         } elseif (is_array($postValue) && isset($postValue['hour']) && isset($postValue['minute'])) {
             if (isset($postValue['active'])) {
                 $saveValue = mktime((int) $postValue['hour'], (int) $postValue['minute'], 0, 0, 0, 0);
             } else {
                 $saveValue = 0;
             }
         } else {
             if (count($postValue) > 1) {
                 // Mehrwertige Felder
                 $saveValue = '|' . implode('|', $postValue) . '|';
             } else {
                 $postValue = is_array($postValue) && isset($postValue[0]) ? $postValue[0] : $postValue;
                 if ($fieldType == REX_A62_FIELD_SELECT && strpos($fieldAttributes, 'multiple') !== false || $fieldType == REX_A62_FIELD_CHECKBOX) {
                     // Mehrwertiges Feld, aber nur ein Wert ausgewählt
                     $saveValue = '|' . $postValue[0] . '|';
                 } else {
                     // Einwertige Felder
                     $saveValue = $postValue;
                 }
             }
         }
         // Wert in SQL zum speichern
         $returnArray[$fieldName] = $saveValue;
     }
     return $returnArray;
 }