CMS_TYPE Select?
Verfasst: Di 11. Dez 2012, 13:51
				
				Hallo, gibt es so was wie ein CMS_SELECT? Also ein das man beim Editieren eines Artikels statt ein Textfeld ein Dropdown hat?
			Das Diskussionsforum zum Open Source Content Management System
https://forum.contenido.org/
Code: Alles auswählen
?><?php
// Module input
$selectList = array(
    'a' => 'Apple',
    'b' => 'Banana',
    'c' => 'Cherry'
);
$cmsValueSelect = "CMS_VALUE[1]";
$cmsVarSelect = "CMS_VAR[1]";
?>
<select name="<?php echo $modContext->cmsVarSelect ?>">
<?php
foreach ($selectList as $k => $v) {
    $sel = ($cmsValueSelect == $k) ? ' selected="selected"' : '';
    echo '<option value="' . $k . '"' . $sel . '>' . $v . '</option>' . "\n";
}
?>
</select>
Code: Alles auswählen
<?php
// Module output
$selectList = array(
    'a' => 'Apple',
    'b' => 'Banana',
    'c' => 'Cherry'
);
$cmsValueSelect = "CMS_VALUE[1]";
if (isset($selectList[$cmsValueSelect])) {
    echo $selectList[$cmsValueSelect];
}
?>
Code: Alles auswählen
<select name="CMS_VAR[1]">
    <option value="1">Wert 1</option>
    <option value="2">Wert 2</option>
</select>
Code: Alles auswählen
<?php
// Module output
// Our select list
$selectList = array(
    ''  => '-- Select fruit --',
    'a' => 'Apple',
    'b' => 'Banana',
    'c' => 'Cherry'
);
// Are we in backend edit mode?
$isBackendEditMode = (!empty($contenido) && !empty($edit) && "true" == $edit);
$defaultCmsValueSelect = "a";
$cmsValueSelect = "";
$oPropertyColl = new PropertyCollection();
if ($isBackendEditMode) {
    // We are in backend edit mode, do form processing...
    if (!empty($_POST['frontend_cms_select_value'])) {
        // Form was send, save data
        $cmsValueSelect = $_POST['frontend_cms_select_value'];
        if (!isset($selectList[$cmsValueSelect])) {
            $cmsValueSelect = $defaultCmsValueSelect;
        }
        $oPropertyColl->setValue('article', (int) $idart, 'frontend_cms_select_name_type', 'frontend_cms_select_name', $cmsValueSelect);
    }
}
// Get selected value
$oPropertyColl->select("idclient = " . (int) $client . " AND itemtype = 'article' AND itemid = " . (int) $idart . " AND name='frontend_cms_select_name' AND type='frontend_cms_select_name_type'");
if ($oProperty = $oPropertyColl->next()) {
    $cmsValueSelect = $oProperty->get("value");
}
// Validate selected value
if (!isset($selectList[$cmsValueSelect])) {
    $cmsValueSelect = $defaultCmsValueSelect;
}
?>
<?php if ($isBackendEditMode) : ?>
    <?php /* Output the selectbox only in backend edit mode! */ ?>
    <select name="frontend_cms_select">
    <?php
    foreach ($selectList as $k => $v) {
        $sel = ($cmsValueSelect == $k) ? ' selected="selected"' : '';
        echo '<option value="' . $k . '"' . $sel . '>' . $v . '</option>' . "\n";
    }
    ?>
    </select>
    <a href="javascript:saveFrontendCmsSelect();"><img src="<?php echo $cfg['path']['contenido_fullhtml']?>images/but_ok.gif" border="0"></a>
    <script type="text/javascript">
    function saveFrontendCmsSelect() {
        var $form = $("form[name=editcontent]"),
            $select = $('select[name=frontend_cms_select]');
            $input = $form.find('input[name=frontend_cms_select_value]')[0];
        if (!$input) {
            $input = $('<input type="hidden" name="frontend_cms_select_value" value="" />');
            $form.append($input);
        }
        $input.val($select.val());
        $form.submit();
    }
    </script>
<?php endif; ?>
<!-- display the selected value -->
<pre>Selected value: <?php var_dump($cmsValueSelect); ?></pre>
Code: Alles auswählen
<?php
# Show a html select in edit mode, the selected value in frontend
$oArt = new Article($idart, $client, $lang, $idartlang);
$selected = $oArt->getContent('CMS_TEXT', 1001);
if ($edit) {
    if (isset($_POST['mySelectOption'])) {
        # Wert speichern
        conSaveContentEntry($idartlang, 'CMS_TEXT', 1001, $_POST['mySelectOption']);
        $selected = $_POST['mySelectOption'];
    }
    $options = explode(',', getEffectiveSetting('editor', 'options'));
?>
    <select id="mySelect" name="mySelect" onchange="saveSelect()">
        <option value="">- Bitte wählen -</option>
<?php
    for ($i = 0, $n = count($options); $i < $n; $i ++) {
        echo '
        <option value="' . trim($options[$i]) . '"' . ((trim($options[$i]) == $selected) ? ' selected="selected"' : '') . '>' . trim($options[$i]) . '</option>';
    }
?>
    </select>
    <script type="text/javascript">
    //<![CDATA[
        function saveSelect() {
            var f = document.forms['editcontent'];
            var s = document.getElementById('mySelect').value;
            var c = document.createElement('input');
            c.type = 'hidden';
            c.name = 'mySelectOption';
            c.value = s;
            f.appendChild(c);
            f.submit();
        }
    //]]>
    </script>
<?php
} else {
    echo $selected;
}
?>Code: Alles auswählen
Typ = editor
Name = options
Wert = Apfel,Banane,Limone,ZitroneCode: Alles auswählen
<?php
/**
 *
 * @package Classes
 * @subpackage Article Property Editor
 * @author marcus.gnass
 */
class ArticlePropertyEditor {
    /**
     * Type to be used for property table.
     *
     * @var string
     */
    protected $_type = '';
    /**
     * Name to be used for property table.
     * This name is also used for the editors hidden input field.
     *
     * @var string
     */
    protected $_name = '';
    /**
     * Default value to be assumed when no valid value is given.
     *
     * @var string
     */
    protected $_default = '';
    /**
     * If this member is set to an array a selectbox is displayed in backend
     * edit mode allowing the selection of one of its values.
     * The array has
     * then to be associative with the option values as keys and human
     * readable
     * labels as values.
     *
     * @var array || NULL
     */
    protected $_options = NULL;
    /**
     * Random index between 1 and 1,000,000
     *
     * @var int
     */
    protected $_index = 0;
    /**
     * Collection object to retrieve & store property values.
     *
     * @var PropertyCollection
     */
    protected $_collection = NULL;
    /**
     * Stores given params as members & creates a property collection.
     *
     * @param string $type
     * @param string $name
     * @param string $default
     * @param array $options
     */
    public function __construct($type, $name, $default = '', array $options = NULL) {
        // store params as members
        $this->_type = $type;
        $this->_name = $name;
        $this->_default = $default;
        $this->_options = $options;
        // get random index
        $this->_index = rand(1, 1000000);
        // create property collection
        $this->_collection = new PropertyCollection();
    }
    /**
     * Sets the default value to use when no valid value is given.
     */
    public function setDefault($default) {
        $this->_default = $default;
    }
    /**
     * Sets the options to display for selecction of a value.
     */
    public function setOptions(array $options) {
        $this->_options = $options;
    }
    /**
     * Saves the posted value in backend edit mode and returns the posted
     * value.
     * When not called in backend edit mode or no value is posted the value
     * is read from database and then returned.
     */
    public function storeValue() {
        if (false === cRegistry::isBackendEditMode()) {
            return $this->readValue();
        }
        $fieldName = $this->_getFieldName();
        if (empty($_POST[$fieldName])) {
            return $this->readValue();
        }
        $value = $_POST[$fieldName];
        $value = $this->_validate($value);
        $itemid = cRegistry::getArticleId();
        $this->_collection->setValue('article', $itemid, $this->_type, $this->_name, $value);
        return $value;
    }
    /**
     * Reads and returns the value from database.
     * If no value was stored or is
     * an invalid value, the default value will be returned.
     */
    public function readValue() {
        $property = $this->_getProperty();
        $value = $this->_default;
        if (false !== $property) {
            $value = $property->get('value');
            $value = $this->_validate($value);
        }
        return $value;
    }
    /**
     * get article property
     */
    protected function _getProperty() {
        $idclient = cRegistry::getClientId();
        $itemid = cRegistry::getArticleId();
        // TODO validation to prevent SQLI
        $this->_collection->select(implode(' AND ', array(
            "idclient = $idclient",
            "itemtype = 'article'",
            "itemid = $itemid",
            "type = '$this->_type'",
            "name = '$this->_name'"
        )));
        $item = $this->_collection->next();
        return $item;
    }
    /**
     * validate value
     */
    protected function _validate($value) {
        if (false === is_array($this->_options)) {
            return $value;
        }
        if (true === isset($this->_options[$value])) {
            return $value;
        }
        return $this->_default;
    }
    /**
     * Returns the editor to edit the property value.
     */
    public function getEditor($selectedValue) {
        $cfg = cRegistry::getConfig();
        $contenido_fullhtml = $cfg['path']['contenido_fullhtml'];
        $fieldName = $this->_getFieldName();
        $function = "saveArticleProperty$this->_index";
        $editor = '<label class="con-label-edit">' . i18n('property') . ': ' . $this->_type . '/' . $this->_name . '</label>' . "\n";
        if (true === is_array($this->_options)) {
            // select element
            $editor .= "<select name=\"$fieldName\">\n";
            foreach ($this->_options as $value => $option) {
                $attrib = ($selectedValue == $value)? ' selected="selected"' : '';
                $editor .= "<option value=\"$value\"$attrib>$option</option>\n";
            }
            $editor .= "</select>\n";
        } else {
            $editor .= "<input type=\"text\" name=\"$fieldName\" value=\"$selectedValue\">";
        }
        // save icon
        $editor .= "<a href=\"javascript:$function()\">";
        $editor .= "<img src=\"${contenido_fullhtml}images/but_ok.gif\" border=\"0\">";
        $editor .= "</a>";
        // script
        // TODO does not yet work with input
        $editor .= "
<script type=\"text/javascript\">
function $function() {
    var form = $('form[name=editcontent]'),
        select = $('select[name=$fieldName]');
        input = form.find('input[name=$fieldName]');
    if (0 === input.length) {
        input = $('<input type=\"hidden\" name=\"$fieldName\" value=\"\" />');
        form.append(input);
    }
    input.val(select.val());
    form.submit();
}
</script>\n";
        return $editor;
    }
    /**
     *
     * @return string
     */
    protected function _getFieldName() {
        return 'cms_property_' . $this->_type . '_' . $this->_name;
    }
}
?>Code: Alles auswählen
<?php
// create instance of ArticlePropertyEditor
$propertyEditor = new ArticlePropertyEditor('map', 'display');
$propertyEditor->setDefault('true');
$propertyEditor->setOptions(array(
    '' => '-- whatever --',
    'true' => 'true',
    'false' => 'false'
));
// get value & editor
if (cRegistry::isBackendEditMode()) {
    $value = $propertyEditor->storeValue();
    echo $propertyEditor->getEditor($value);
} else {
    $value = $propertyEditor->readValue();
}
?>