/**
  * This function returns the HtmlElements to display for 1:n relations
  *
  * @todo this only works with single valued identifiers
  * @param array $assocMapping Mapping information for this relation
  * @param string $entityClass FQCN of the foreign entity
  * @param int $entityId ID of the local entity
  * @return array Set of \Cx\Core\Html\Model\Entity\HtmlElement instances
  */
 protected function getIdentifyingDisplayValue($assocMapping, $entityClass, $entityId)
 {
     global $_CORELANG;
     $localEntityMetadata = \Env::get('em')->getClassMetadata($this->entityClass);
     $localEntityIdentifierField = $localEntityMetadata->getSingleIdentifierFieldName();
     $localEntityRepo = \Env::get('em')->getRepository($this->entityClass);
     $localEntity = $localEntityRepo->find($entityId);
     if (!$localEntity) {
         throw new \Exception('Entity not found');
     }
     $foreignEntityGetter = 'get' . preg_replace('/_([a-z])/', '\\1', ucfirst($assocMapping["fieldName"]));
     $foreignEntities = $localEntity->{$foreignEntityGetter}();
     $htmlElements = array();
     foreach ($foreignEntities as $index => $foreignEntity) {
         // entity base implements __toString()
         $displayValue = (string) $foreignEntity;
         $foreignEntityMetadata = \Env::get('em')->getClassMetadata(get_class($foreignEntity));
         $foreignEntityIdentifierField = $foreignEntityMetadata->getSingleIdentifierFieldName();
         $entityValueSerialized = 'vg_increment_number=' . $this->formId;
         $fieldsToParse = $foreignEntityMetadata->fieldNames;
         foreach ($fieldsToParse as $dbColName => $fieldName) {
             $entityValueSerialized .= '&' . $fieldName . '=' . $this->getDataElementValueAsString($foreignEntityMetadata->getFieldValue($foreignEntity, $fieldName));
         }
         // add relations
         foreach ($foreignEntityMetadata->associationMappings as $foreignAssocMapping) {
             if (!$foreignAssocMapping['isOwningSide']) {
                 continue;
             }
             $joinColumns = reset($foreignAssocMapping['joinColumns']);
             // if the association is a backreference to our main entity we skip it
             if ($foreignAssocMapping['targetEntity'] == $this->entityClass && $joinColumns['referencedColumnName'] == $localEntityIdentifierField) {
                 continue;
             }
             $foreignForeignEntity = $foreignEntityMetadata->getFieldValue($foreignEntity, $foreignAssocMapping['fieldName']);
             if (!$foreignForeignEntity) {
                 continue;
             }
             $foreignEntityIdentifierGetter = 'get' . preg_replace('/_([a-z])/', '\\1', ucfirst($foreignEntityIdentifierField));
             $entityValueSerialized .= '&' . $foreignAssocMapping['fieldName'] . '=' . $foreignForeignEntity->{$foreignEntityIdentifierGetter}();
         }
         $sorroundingDiv = new \Cx\Core\Html\Model\Entity\HtmlElement('div');
         $sorroundingDiv->setAttribute('class', 'oneToManyEntryRow');
         $displaySpan = new \Cx\Core\Html\Model\Entity\HtmlElement('span');
         $displaySpan->addChild(new \Cx\Core\Html\Model\Entity\TextElement($displayValue));
         $displaySpan->allowDirectClose(false);
         $hiddenInput = new \Cx\Core\Html\Model\Entity\DataElement('input');
         $hiddenInput->setAttributes(array('type' => 'hidden', 'name' => $this->createCssClassNameFromEntity($entityClass) . '[]', 'value' => $entityValueSerialized));
         $editLink = new \Cx\Core\Html\Model\Entity\HtmlElement('a');
         $editLink->setAttributes(array('class' => 'edit', 'title' => $_CORELANG['TXT_EDIT']));
         $editLink->allowDirectClose(false);
         $deleteLink = new \Cx\Core\Html\Model\Entity\HtmlElement('a');
         $deleteLink->setAttributes(array('onclick' => 'deleteAssociationMappingEntry(this)', 'class' => 'remove existing', 'title' => $_CORELANG['TXT_DELETE']));
         $deleteLink->allowDirectClose(false);
         $sorroundingDiv->addChild($displaySpan);
         $sorroundingDiv->addChild($hiddenInput);
         $sorroundingDiv->addChild($editLink);
         $sorroundingDiv->addChild($deleteLink);
         $htmlElements[] = $sorroundingDiv;
     }
     return $htmlElements;
 }
    public function getDataElement($name, $type, $length, $value, $options)
    {
        global $_ARRAYLANG;
        if (isset($options['formfield']) && is_callable($options['formfield'])) {
            $formFieldGenerator = $options['formfield'];
            $formField = $formFieldGenerator($name, $type, $length, $value, $options);
            if (is_a($formField, 'Cx\\Core\\Html\\Model\\Entity\\HtmlElement')) {
                return $formField;
            } else {
                $value = $formField;
            }
        }
        if (isset($options['showDetail']) && $options['showDetail'] === false) {
            return '';
        }
        switch ($type) {
            case 'bool':
            case 'boolean':
                // yes/no checkboxes
                $fieldset = new \Cx\Core\Html\Model\Entity\HtmlElement('div');
                $inputYes = new \Cx\Core\Html\Model\Entity\DataElement($name, 'yes');
                $inputYes->setAttribute('type', 'radio');
                $inputYes->setAttribute('value', '1');
                $inputYes->setAttribute('id', 'form-' . $this->formId . '-' . $name . '_yes');
                if (isset($options['attributes'])) {
                    $inputYes->setAttributes($options['attributes']);
                }
                $fieldset->addChild($inputYes);
                $labelYes = new \Cx\Core\Html\Model\Entity\HtmlElement('label');
                $labelYes->setAttribute('for', 'form-' . $this->formId . '-' . $name . '_yes');
                $labelYes->addChild(new \Cx\Core\Html\Model\Entity\TextElement($_ARRAYLANG['TXT_YES']));
                $fieldset->addChild($labelYes);
                $inputNo = new \Cx\Core\Html\Model\Entity\DataElement($name, 'no');
                $inputNo->setAttribute('id', 'form-' . $this->formId . '-' . $name . '_no');
                $inputNo->setAttribute('type', 'radio');
                $inputNo->setAttribute('value', '0');
                if (isset($options['attributes'])) {
                    $inputNo->setAttributes($options['attributes']);
                }
                $fieldset->addChild($inputNo);
                $labelNo = new \Cx\Core\Html\Model\Entity\HtmlElement('label');
                $labelNo->setAttribute('for', 'form-' . $this->formId . '-' . $name . '_no');
                $labelNo->addChild(new \Cx\Core\Html\Model\Entity\TextElement($_ARRAYLANG['TXT_NO']));
                $fieldset->addChild($labelNo);
                if ($value) {
                    $inputYes->setAttribute('checked');
                } else {
                    $inputNo->setAttribute('checked');
                }
                return $fieldset;
                break;
            case 'int':
            case 'integer':
                // input field with type number
                $inputNumber = new \Cx\Core\Html\Model\Entity\DataElement($name, $value, \Cx\Core\Html\Model\Entity\DataElement::TYPE_INPUT, new \Cx\Core\Validate\Model\Entity\RegexValidator('/-?[0-9]*/'));
                if (isset($options['attributes'])) {
                    $inputNumber->setAttributes($options['attributes']);
                }
                $inputNumber->setAttribute('type', 'number');
                return $inputNumber;
                break;
            case 'Cx\\Model\\Base\\EntityBase':
                $entityClass = get_class($value);
                $entities = \Env::get('em')->getRepository($entityClass)->findAll();
                $foreignMetaData = \Env::get('em')->getClassMetadata($entityClass);
                $primaryKeyName = $foreignMetaData->getSingleIdentifierFieldName();
                $selected = $foreignMetaData->getFieldValue($value, $primaryKeyName);
                $arrEntities = array();
                $closeMetaData = \Env::get('em')->getClassMetadata($this->entityClass);
                $assocMapping = $closeMetaData->getAssociationMapping($name);
                if (!isset($assocMapping['joinColumns'][0]['nullable']) || $assocMapping['joinColumns'][0]['nullable']) {
                    $arrEntities['NULL'] = $_ARRAYLANG['TXT_CORE_NONE'];
                }
                foreach ($entities as $entity) {
                    $arrEntities[\Env::get('em')->getClassMetadata($entityClass)->getFieldValue($entity, $primaryKeyName)] = $entity;
                }
                $select = new \Cx\Core\Html\Model\Entity\DataElement($name, \Html::getOptions($arrEntities, $selected), \Cx\Core\Html\Model\Entity\DataElement::TYPE_SELECT);
                if (isset($options['attributes'])) {
                    $select->setAttributes($options['attributes']);
                }
                return $select;
                break;
            case 'Country':
                // this is for customizing only:
                $data = \Cx\Core\Country\Controller\Country::getNameById($value);
                if (empty($data)) {
                    $value = 204;
                }
                $options = \Cx\Core\Country\Controller\Country::getMenuoptions($value);
                $select = new \Cx\Core\Html\Model\Entity\DataElement($name, $options, \Cx\Core\Html\Model\Entity\DataElement::TYPE_SELECT);
                if (isset($options['attributes'])) {
                    $select->setAttributes($options['attributes']);
                }
                return $select;
                break;
            case 'DateTime':
            case 'datetime':
            case 'date':
                // input field with type text and class datepicker
                if ($value instanceof \DateTime) {
                    $value = $value->format(ASCMS_DATE_FORMAT);
                }
                if (is_null($value)) {
                    $value = '';
                }
                $input = new \Cx\Core\Html\Model\Entity\DataElement($name, $value);
                $input->setAttribute('type', 'text');
                $input->setAttribute('class', 'datepicker');
                if (isset($options['readonly']) && $options['readonly']) {
                    $input->setAttribute('disabled');
                }
                if (isset($options['attributes'])) {
                    $input->setAttributes($options['attributes']);
                }
                \DateTimeTools::addDatepickerJs();
                \JS::registerCode('
                        cx.jQuery(function() {
                          cx.jQuery(".datepicker").datetimepicker();
                        });
                        ');
                return $input;
                break;
            case 'multiselect':
            case 'select':
                $values = array();
                if (isset($options['validValues'])) {
                    if (is_array($options['validValues'])) {
                        $values = $options['validValues'];
                    } else {
                        $values = explode(',', $options['validValues']);
                        $values = array_combine($values, $values);
                    }
                }
                if ($type == 'multiselect') {
                    $value = explode(',', $value);
                    $value = array_combine($value, $value);
                }
                $selectOptions = \Html::getOptions($values, $value);
                $select = new \Cx\Core\Html\Model\Entity\DataElement($name, $selectOptions, \Cx\Core\Html\Model\Entity\DataElement::TYPE_SELECT);
                if ($type == 'multiselect') {
                    $select->setAttribute('multiple');
                }
                if (isset($options['attributes'])) {
                    $select->setAttributes($options['attributes']);
                }
                return $select;
                break;
            case 'slider':
                // this code should not be here
                // create sorrounding div
                $element = new \Cx\Core\Html\Model\Entity\HtmlElement('div');
                // create div for slider
                $slider = new \Cx\Core\Html\Model\Entity\HtmlElement('div');
                $slider->setAttribute('class', 'slider');
                $element->addChild($slider);
                // create hidden input for slider value
                $input = new \Cx\Core\Html\Model\Entity\DataElement($name, $value + 0, \Cx\Core\Html\Model\Entity\DataElement::TYPE_INPUT);
                $input->setAttribute('type', 'hidden');
                if (isset($options['attributes'])) {
                    $input->setAttributes($options['attributes']);
                }
                $element->addChild($input);
                // add javascript to update input value
                $min = 0;
                $max = 10;
                if (isset($options['validValues'])) {
                    $values = explode(',', $options['validValues']);
                    $min = $values[0];
                    if (isset($values[1])) {
                        $max = $values[1];
                    }
                }
                if (!isset($value)) {
                    $value = 0;
                }
                $script = new \Cx\Core\Html\Model\Entity\HtmlElement('script');
                $script->addChild(new \Cx\Core\Html\Model\Entity\TextElement('
                    cx.jQuery("#form-' . $this->formId . '-' . $name . ' .slider").slider({
                        value: ' . ($value + 0) . ',
                        min: ' . ($min + 0) . ',
                        max: ' . ($max + 0) . ',
                        slide: function( event, ui ) {
                            cx.jQuery("input[name=' . $name . ']").val(ui.value);
                            cx.jQuery("input[name=' . $name . ']").change();
                        }
                    });
                '));
                $element->addChild($script);
                return $element;
                break;
            case 'checkboxes':
                $dataElementGroupType = \Cx\Core\Html\Model\Entity\DataElementGroup::TYPE_CHECKBOX;
            case 'radio':
                $values = array();
                if (isset($options['validValues'])) {
                    $values = explode(',', $options['validValues']);
                    $values = array_combine($values, $values);
                }
                if (!isset($dataElementGroupType)) {
                    $dataElementGroupType = \Cx\Core\Html\Model\Entity\DataElementGroup::TYPE_RADIO;
                }
                $radio = new \Cx\Core\Html\Model\Entity\DataElementGroup($name, $values, $value, $dataElementGroupType);
                if (isset($options['attributes'])) {
                    $radio->setAttributes($options['attributes']);
                }
                return $radio;
                break;
            case 'text':
                // textarea
                $textarea = new \Cx\Core\Html\Model\Entity\HtmlElement('textarea');
                $textarea->setAttribute('name', $name);
                if (isset($options['readonly']) && $options['readonly']) {
                    $textarea->setAttribute('disabled');
                }
                $textarea->addChild(new \Cx\Core\Html\Model\Entity\TextElement($value));
                if (isset($options['attributes'])) {
                    $textarea->setAttributes($options['attributes']);
                }
                return $textarea;
                break;
            case 'phone':
                // input field with type phone
                $input = new \Cx\Core\Html\Model\Entity\DataElement($name, $value);
                $input->setAttribute('type', 'phone');
                if (isset($options['readonly']) && $options['readonly']) {
                    $input->setAttribute('disabled');
                }
                if (isset($options['attributes'])) {
                    $input->setAttributes($options['attributes']);
                }
                return $input;
                break;
            case 'mail':
                // input field with type mail
                $emailValidator = new \Cx\Core\Validate\Model\Entity\EmailValidator();
                $input = new \Cx\Core\Html\Model\Entity\DataElement($name, $value, 'input', $emailValidator);
                $input->setAttribute('onkeyup', $emailValidator->getJavaScriptCode());
                $input->setAttribute('type', 'mail');
                if (isset($options['attributes'])) {
                    $input->setAttributes($options['attributes']);
                }
                if (isset($options['readonly']) && $options['readonly']) {
                    $input->setAttribute('disabled');
                }
                return $input;
                break;
            case 'uploader':
                \JS::registerCode('
                    function javascript_callback_function(data) {
                        if(data.type=="file") {
                                cx.jQuery("#' . $name . '").val(data.data[0].datainfo.filepath);
                        }
                    }

                ');
                $mediaBrowser = new \Cx\Core_Modules\MediaBrowser\Model\Entity\MediaBrowser();
                $mediaBrowser->setOptions(array('type' => 'button'));
                $mediaBrowser->setCallback('javascript_callback_function');
                $mediaBrowser->setOptions(array('data-cx-mb-views' => 'filebrowser,uploader', 'id' => 'page_target_browse', 'cxMbStartview' => 'MediaBrowserList'));
                $input = new \Cx\Core\Html\Model\Entity\DataElement($name, $value);
                $input->setAttribute('type', 'text');
                $input->setAttribute('id', $name);
                $div = new \Cx\Core\Html\Model\Entity\HtmlElement('div');
                $div->addChild($input);
                $div->addChild(new \Cx\Core\Html\Model\Entity\TextElement($mb = $mediaBrowser->getXHtml($_ARRAYLANG['TXT_CORE_CM_BROWSE'])));
                return $div;
                break;
            case 'image':
                \JS::registerCode('
                    function javascript_callback_function(data) {
                        if ( data.data[0].datainfo.extension=="Jpg"
                            || data.data[0].datainfo.extension=="Gif"
                            || data.data[0].datainfo.extension=="Png"
                        ) {
                            cx.jQuery("#' . $name . '").attr(\'value\', data.data[0].datainfo.filepath);
                            cx.jQuery("#' . $name . '").prevAll(\'.deletePreviewImage\').first().css(\'display\', \'inline-block\');
                            cx.jQuery("#' . $name . '").prevAll(\'.previewImage\').first().attr(\'src\', data.data[0].datainfo.filepath);
                        }
                    }

                    jQuery(document).ready(function(){
                        jQuery(\'.deletePreviewImage\').click(function(){
                            cx.jQuery("#' . $name . '").attr(\'value\', \'\');
                            cx.jQuery(this).prev(\'img\').attr(\'src\', \'/images/Downloads/no_picture.gif\');
                            cx.jQuery(this).css(\'display\', \'none\');
                            cx.jQuery(this).nextAll(\'input\').first().attr(\'value\', \'\');
                        });
                    });

                ');
                $mediaBrowser = new \Cx\Core_Modules\MediaBrowser\Model\Entity\MediaBrowser();
                $mediaBrowser->setOptions(array('type' => 'button'));
                $mediaBrowser->setCallback('javascript_callback_function');
                $defaultOptions = array('data-cx-mb-views' => 'filebrowser,uploader', 'id' => 'page_target_browse', 'cxMbStartview' => 'MediaBrowserList');
                $mediaBrowser->setOptions(is_array($options['options']) ? array_merge($defaultOptions, $options['options']) : $defaultOptions);
                // create hidden input to save image
                $input = new \Cx\Core\Html\Model\Entity\DataElement($name, $value);
                $input->setAttribute('type', 'hidden');
                $input->setAttribute('id', $name);
                $div = new \Cx\Core\Html\Model\Entity\HtmlElement('div');
                if (isset($value) && in_array(pathinfo($value, PATHINFO_EXTENSION), array('gif', 'jpg', 'png')) || $name == 'imagePath') {
                    // this image is meant to be a preview of the selected image
                    $previewImage = new \Cx\Core\Html\Model\Entity\HtmlElement('img');
                    $previewImage->setAttribute('class', 'previewImage');
                    $previewImage->setAttribute('src', $value != '' ? $value : '/images/Downloads/no_picture.gif');
                    // this image is uesd as delete function for the selected image over javascript
                    $deleteImage = new \Cx\Core\Html\Model\Entity\HtmlElement('img');
                    $deleteImage->setAttribute('class', 'deletePreviewImage');
                    $deleteImage->setAttribute('src', '/core/Core/View/Media/icons/delete.gif');
                    $div->addChild($previewImage);
                    $div->addChild($deleteImage);
                    $div->addChild(new \Cx\Core\Html\Model\Entity\HtmlElement('br'));
                }
                $div->addChild($input);
                $div->addChild(new \Cx\Core\Html\Model\Entity\TextElement($mediaBrowser->getXHtml($_ARRAYLANG['TXT_CORE_CM_BROWSE'])));
                return $div;
                break;
            case 'sourcecode':
                //set mode
                $mode = 'html';
                if (isset($options['options']['mode'])) {
                    switch ($options['options']['mode']) {
                        case 'js':
                            $mode = 'javascript';
                            break;
                        case 'yml':
                        case 'yaml':
                            $mode = 'yaml';
                            break;
                    }
                }
                //define textarea
                $textarea = new \Cx\Core\Html\Model\Entity\HtmlElement('textarea');
                $textarea->setAttribute('name', $name);
                $textarea->setAttribute('id', $name);
                $textarea->setAttribute('style', 'display:none;');
                $textarea->addChild(new \Cx\Core\Html\Model\Entity\TextElement($value));
                //define pre
                $pre = new \Cx\Core\Html\Model\Entity\HtmlElement('pre');
                $pre->setAttribute('id', 'editor-' . $name);
                $pre->addChild(new \Cx\Core\Html\Model\Entity\TextElement(contrexx_raw2xhtml($value)));
                //set readonly if necessary
                $readonly = '';
                if (isset($options['readonly']) && $options['readonly']) {
                    $readonly = 'editor.setReadOnly(true);';
                    $textarea->setAttribute('disabled');
                }
                //create div and add all stuff
                $div = new \Cx\Core\Html\Model\Entity\HtmlElement('div');
                $div->addChild($textarea);
                $div->addChild($pre);
                //register js
                $jsCode = <<<CODE
var editor;                        
\$J(function(){
if (\$J("#editor-{$name}").length) {
    editor = ace.edit("editor-{$name}");
    editor.getSession().setMode("ace/mode/{$mode}");
    editor.setShowPrintMargin(false);
    editor.focus();
    editor.gotoLine(1);
    {$readonly}
}

\$J('form').submit(function(){
    \$J('#{$name}').val(editor.getSession().getValue());
});

});
CODE;
                \JS::activate('ace');
                \JS::registerCode($jsCode);
                return $div;
                break;
            case 'string':
            case 'hidden':
            default:
                // convert NULL to empty string
                if (is_null($value)) {
                    $value = '';
                }
                // input field with type text
                $input = new \Cx\Core\Html\Model\Entity\DataElement($name, $value);
                if (isset($options['validValues'])) {
                    $input->setValidator(new \Cx\Core\Validate\Model\Entity\RegexValidator('/^' . $options['validValues'] . '$/'));
                }
                if ($type == 'hidden') {
                    $input->setAttribute('type', 'hidden');
                } else {
                    $input->setAttribute('type', 'text');
                    $input->setClass('form-control');
                }
                if (isset($options['readonly']) && $options['readonly']) {
                    $input->setAttribute('disabled');
                }
                if (isset($options['attributes'])) {
                    $input->setAttributes($options['attributes']);
                }
                return $input;
                break;
        }
    }
 public function __construct($attrs = array(), $options = array())
 {
     global $_ARRAYLANG;
     if ($attrs instanceof \Cx\Core_Modules\Listing\Model\Entity\DataSet) {
         $hasMasterTableHeader = !empty($options['header']);
         // add master table-header-row
         if ($hasMasterTableHeader) {
             $this->addRow(array(0 => $options['header']), null, 'th');
         }
         $first = true;
         $row = 1 + $hasMasterTableHeader;
         foreach ($attrs as $rowname => $rows) {
             $col = 0;
             $virtual = $rows['virtual'];
             unset($rows['virtual']);
             if (isset($options['multiActions'])) {
                 $this->setCellContents($row, $col, '<input name="select-' . $rowname . '" value="' . $rowname . '" type="checkbox" />', 'TD', '0', false);
                 $col++;
             }
             foreach ($rows as $header => $data) {
                 $encode = true;
                 if (isset($options['fields']) && isset($options['fields'][$header]) && isset($options['fields'][$header]['showOverview']) && !$options['fields'][$header]['showOverview']) {
                     continue;
                 }
                 $origHeader = $header;
                 if (isset($options['fields'][$header]['sorting'])) {
                     $sorting = $options['fields'][$header]['sorting'];
                 } else {
                     if (isset($options['functions']['sorting'])) {
                         $sorting = $options['functions']['sorting'];
                     }
                 }
                 if ($first) {
                     if (isset($options['fields'][$header]['header'])) {
                         $header = $options['fields'][$header]['header'];
                     }
                     if (isset($_ARRAYLANG[$header])) {
                         $header = $_ARRAYLANG[$header];
                     }
                     if (is_array($options['functions']) && isset($options['functions']['sorting']) && $options['functions']['sorting'] && $sorting !== false) {
                         $order = '';
                         $img = '&uarr;&darr;';
                         if (isset($_GET['order'])) {
                             $supOrder = explode('/', $_GET['order']);
                             if (current($supOrder) == $origHeader) {
                                 $order = '/DESC';
                                 $img = '&darr;';
                                 if (count($supOrder) > 1 && $supOrder[1] == 'DESC') {
                                     $order = '';
                                     $img = '&uarr;';
                                 }
                             }
                         }
                         $header = '<a href="' . \Env::get('cx')->getRequest()->getUrl() . '&order=' . $origHeader . $order . '" style="white-space: nowrap;">' . $header . ' ' . $img . '</a>';
                     }
                     if ($hasMasterTableHeader) {
                         $this->setCellContents(1, $col, $header, 'td', 0);
                     } else {
                         $this->setCellContents(0, $col, $header, 'th', 0);
                     }
                 }
                 if (isset($options['fields']) && isset($options['fields'][$origHeader]) && isset($options['fields'][$origHeader]['table']) && isset($options['fields'][$origHeader]['table']['parse']) && is_callable($options['fields'][$origHeader]['table']['parse'])) {
                     $callback = $options['fields'][$origHeader]['table']['parse'];
                     $data = $callback($data, $rows);
                     $encode = false;
                     // todo: this should be set by callback
                 } else {
                     if (is_object($data) && get_class($data) == 'DateTime') {
                         $data = $data->format(ASCMS_DATE_FORMAT);
                     } else {
                         if (isset($options['fields'][$origHeader]) && isset($options['fields'][$origHeader]['type']) && $options['fields'][$origHeader]['type'] == '\\Country') {
                             $data = \Cx\Core\Country\Controller\Country::getNameById($data);
                             if (empty($data)) {
                                 $data = \Cx\Core\Country\Controller\Country::getNameById(204);
                             }
                         } else {
                             if (gettype($data) == 'boolean') {
                                 $data = '<i>' . ($data ? $_ARRAYLANG['TXT_YES'] : $_ARRAYLANG['TXT_NO']) . '</i>';
                                 $encode = false;
                             } else {
                                 if ($data === null) {
                                     $data = '<i>' . $_ARRAYLANG['TXT_CORE_NONE'] . '</i>';
                                     $encode = false;
                                 } else {
                                     if (empty($data)) {
                                         $data = '<i>(empty)</i>';
                                         $encode = false;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $this->setCellContents($row, $col, $data, 'TD', 0, $encode);
                 $col++;
             }
             if ($this->hasRowFunctions($options['functions'], $virtual)) {
                 if ($first) {
                     $header = 'Functions';
                     if (isset($_ARRAYLANG['TXT_FUNCTIONS'])) {
                         $header = $_ARRAYLANG['TXT_FUNCTIONS'];
                     }
                     if ($hasMasterTableHeader) {
                         $this->setCellContents(1, $col, $header, 'td', 0, true);
                     } else {
                         $this->setCellContents(0, $col, $header, 'th', 0, true);
                     }
                 }
                 $this->updateColAttributes($col, array('style' => 'text-align:right;'));
                 if (empty($options['functions']['baseUrl'])) {
                     $options['functions']['baseUrl'] = clone \Env::get('cx')->getRequest()->getUrl();
                 }
                 $this->setCellContents($row, $col, $this->getFunctionsCode($rowname, $rows, $options['functions'], $virtual), 'TD', 0);
             }
             $first = false;
             $row++;
         }
         // adjust colspan of master-table-header-row
         if ($hasMasterTableHeader) {
             $this->setCellAttributes(0, 0, array('colspan' => $col + is_array($options['functions'])));
             $this->updateRowAttributes(1, array('class' => 'row3'), true);
         }
         // add multi-actions
         if (isset($options['multiActions'])) {
             $multiActionsCode = '
                 <img src="images/icons/arrow.gif" width="38" height="22" alt="^" title="^">
                 <a href="#" onclick="jQuery(\'input[type=checkbox]\').prop(\'checked\', true);return false;">' . $_ARRAYLANG['TXT_SELECT_ALL'] . '</a> /
                 <a href="#" onclick="jQuery(\'input[type=checkbox]\').prop(\'checked\', false);return false;">' . $_ARRAYLANG['TXT_DESELECT_ALL'] . '</a>
                 <img alt="-" title="-" src="images/icons/strike.gif">
             ';
             $multiActions = array('' => $_ARRAYLANG['TXT_SUBMIT_SELECT']);
             foreach ($options['multiActions'] as $actionName => $actionProperties) {
                 $actionTitle = $actionName;
                 if (isset($actionProperties['title'])) {
                     $actionTitle = $actionProperties['title'];
                 } else {
                     if (isset($_ARRAYLANG[$actionName])) {
                         $actionTitle = $_ARRAYLANG[$actionName];
                     }
                 }
                 if (isset($actionProperties['jsEvent'])) {
                     $actionName = $actionProperties['jsEvent'];
                 }
                 $multiActions[$actionName] = $actionTitle;
             }
             $select = new \Cx\Core\Html\Model\Entity\DataElement('cxMultiAction', \Html::getOptions($multiActions), \Cx\Core\Html\Model\Entity\DataElement::TYPE_SELECT);
             // this is not a nice place for this code
             // but we should cleanup this complete class and make
             // it base on templates
             $select->setAttribute('onchange', '
                     var regex = /([a-zA-Z\\/]+):([a-zA-Z\\/]+)/;
                     var matches = jQuery(this).val().match(regex);
                     if (!matches) {
                         return false;
                     }
                     var checkboxes = jQuery(this).closest("table").find("input[type=checkbox]");
                     var activeRows = [];
                     checkboxes.filter(":checked").each(function(el) {
                         activeRows.push(jQuery(this).val());
                     });
                     cx.trigger(matches[1], matches[2], activeRows);
                     checkboxes.prop("checked", false);
                     jQuery(this).val("");
                 ');
             $this->setCellContents($row, 0, $multiActionsCode . $select, 'TD', 0);
             $this->setCellAttributes($row, 0, array('colspan' => $col + is_array($options['functions'])));
         }
         $attrs = array();
     }
     parent::__construct(array_merge($attrs, array('class' => 'adminlist', 'width' => '100%')));
 }
Example #4
0
 public function __construct($attrs = array(), $options = array())
 {
     global $_ARRAYLANG;
     if ($attrs instanceof \Cx\Core_Modules\Listing\Model\Entity\DataSet) {
         $hasMasterTableHeader = !empty($options['header']);
         // add master table-header-row
         if ($hasMasterTableHeader) {
             $this->addRow(array(0 => $options['header']), null, 'th');
         }
         $first = true;
         $row = 1 + $hasMasterTableHeader;
         $sortBy = isset($options['functions']['sortBy']) && is_array($options['functions']['sortBy']) ? $options['functions']['sortBy'] : array();
         $sortingKey = !empty($sortBy) && isset($sortBy['sortingKey']) ? $sortBy['sortingKey'] : '';
         $sortField = !empty($sortingKey) && isset($sortBy['field']) ? key($sortBy['field']) : '';
         $component = !empty($sortBy) && isset($sortBy['component']) ? $sortBy['component'] : '';
         $entity = !empty($sortBy) && isset($sortBy['entity']) ? $sortBy['entity'] : '';
         $sortOrder = !empty($sortBy) && isset($sortBy['sortOrder']) ? $sortBy['sortOrder'] : '';
         $pagingPos = !empty($sortBy) && isset($sortBy['pagingPosition']) ? $sortBy['pagingPosition'] : '';
         foreach ($attrs as $rowname => $rows) {
             $col = 0;
             $virtual = $rows['virtual'];
             unset($rows['virtual']);
             if (isset($options['multiActions'])) {
                 $this->setCellContents($row, $col, '<input name="select-' . $rowname . '" value="' . $rowname . '" type="checkbox" />', 'TD', '0', false);
                 $col++;
             }
             foreach ($rows as $header => $data) {
                 if (!empty($sortingKey) && $header === $sortingKey) {
                     //Add the additional attribute id, for getting the updated sort order after the row sorting
                     $this->updateRowAttributes($row, array('id' => 'sorting' . $entity . '_' . $data), true);
                 }
                 $encode = true;
                 if (isset($options['fields']) && isset($options['fields'][$header]) && isset($options['fields'][$header]['showOverview']) && !$options['fields'][$header]['showOverview']) {
                     continue;
                 }
                 if (!empty($sortField) && $header === $sortField) {
                     //Add the additional attribute class, to display the updated sort order after the row sorting
                     $this->updateColAttributes($col, array('class' => 'sortBy' . $sortField));
                 }
                 $origHeader = $header;
                 if (isset($options['fields'][$header]['sorting'])) {
                     $sorting = $options['fields'][$header]['sorting'];
                 } else {
                     if (isset($options['functions']['sorting'])) {
                         $sorting = $options['functions']['sorting'];
                     }
                 }
                 if ($first) {
                     if (isset($options['fields'][$header]['header'])) {
                         $header = $options['fields'][$header]['header'];
                     }
                     if (isset($_ARRAYLANG[$header])) {
                         $header = $_ARRAYLANG[$header];
                     }
                     if (is_array($options['functions']) && isset($options['functions']['sorting']) && $options['functions']['sorting'] && $sorting !== false) {
                         $order = '';
                         $img = '&uarr;&darr;';
                         $sortParamName = !empty($sortBy) ? $entity . 'Order' : 'order';
                         if (isset($_GET[$sortParamName])) {
                             $supOrder = explode('/', $_GET[$sortParamName]);
                             if (current($supOrder) == $origHeader) {
                                 $order = '/DESC';
                                 $img = '&darr;';
                                 if (count($supOrder) > 1 && $supOrder[1] == 'DESC') {
                                     $order = '';
                                     $img = '&uarr;';
                                 }
                             }
                         }
                         $header = '<a href="' . \Env::get('cx')->getRequest()->getUrl() . '&' . $sortParamName . '=' . $origHeader . $order . '" style="white-space: nowrap;">' . $header . ' ' . $img . '</a>';
                     }
                     if ($hasMasterTableHeader) {
                         $this->setCellContents(1, $col, $header, 'td', 0);
                     } else {
                         $this->setCellContents(0, $col, $header, 'th', 0);
                     }
                 }
                 /* We use json to do parse the field function. The 'else if' is for backwards compatibility so you can declare
                  * the function directly without using json. This is not recommended and not working over session */
                 if (isset($options['fields']) && isset($options['fields'][$origHeader]) && isset($options['fields'][$origHeader]['table']) && isset($options['fields'][$origHeader]['table']['parse'])) {
                     $callback = $options['fields'][$origHeader]['table']['parse'];
                     if (is_array($callback) && isset($callback['adapter']) && isset($callback['method'])) {
                         $json = new \Cx\Core\Json\JsonData();
                         $jsonResult = $json->data($callback['adapter'], $callback['method'], array('data' => $data, 'rows' => $rows));
                         if ($jsonResult['status'] == 'success') {
                             $data = $jsonResult["data"];
                         }
                     } else {
                         if (is_callable($callback)) {
                             $data = $callback($data, $rows);
                         }
                     }
                     $encode = false;
                     // todo: this should be set by callback
                 } else {
                     if (is_object($data) && get_class($data) == 'DateTime') {
                         $data = $data->format(ASCMS_DATE_FORMAT);
                     } else {
                         if (isset($options['fields'][$origHeader]) && isset($options['fields'][$origHeader]['type']) && $options['fields'][$origHeader]['type'] == '\\Country') {
                             $data = \Cx\Core\Country\Controller\Country::getNameById($data);
                             if (empty($data)) {
                                 $data = \Cx\Core\Country\Controller\Country::getNameById(204);
                             }
                         } else {
                             if (gettype($data) == 'boolean') {
                                 $data = '<i>' . ($data ? $_ARRAYLANG['TXT_YES'] : $_ARRAYLANG['TXT_NO']) . '</i>';
                                 $encode = false;
                             } else {
                                 if ($data === null) {
                                     $data = '<i>' . $_ARRAYLANG['TXT_CORE_NONE'] . '</i>';
                                     $encode = false;
                                 } else {
                                     if (empty($data)) {
                                         $data = '<i>(empty)</i>';
                                         $encode = false;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $this->setCellContents($row, $col, $data, 'TD', 0, $encode);
                 $col++;
             }
             if ($this->hasRowFunctions($options['functions'], $virtual)) {
                 if ($first) {
                     $header = 'Functions';
                     if (isset($_ARRAYLANG['TXT_FUNCTIONS'])) {
                         $header = $_ARRAYLANG['TXT_FUNCTIONS'];
                     }
                     if ($hasMasterTableHeader) {
                         $this->setCellContents(1, $col, $header, 'td', 0, true);
                     } else {
                         $this->setCellContents(0, $col, $header, 'th', 0, true);
                     }
                 }
                 $this->updateColAttributes($col, array('style' => 'text-align:right;'));
                 if (empty($options['functions']['baseUrl'])) {
                     $options['functions']['baseUrl'] = clone \Env::get('cx')->getRequest()->getUrl();
                 }
                 $this->setCellContents($row, $col, $this->getFunctionsCode($rowname, $rows, $options['functions'], $virtual), 'TD', 0);
             }
             $first = false;
             $row++;
         }
         // adjust colspan of master-table-header-row
         if ($hasMasterTableHeader) {
             $this->setCellAttributes(0, 0, array('colspan' => $col + is_array($options['functions'])));
             $this->updateRowAttributes(1, array('class' => 'row3'), true);
         }
         // add multi-actions
         if (isset($options['multiActions'])) {
             $multiActionsCode = '
                 <img src="images/icons/arrow.gif" width="38" height="22" alt="^" title="^">
                 <a href="#" onclick="jQuery(\'input[type=checkbox]\').prop(\'checked\', true);return false;">' . $_ARRAYLANG['TXT_SELECT_ALL'] . '</a> /
                 <a href="#" onclick="jQuery(\'input[type=checkbox]\').prop(\'checked\', false);return false;">' . $_ARRAYLANG['TXT_DESELECT_ALL'] . '</a>
                 <img alt="-" title="-" src="images/icons/strike.gif">
             ';
             $multiActions = array('' => $_ARRAYLANG['TXT_SUBMIT_SELECT']);
             foreach ($options['multiActions'] as $actionName => $actionProperties) {
                 $actionTitle = $actionName;
                 if (isset($actionProperties['title'])) {
                     $actionTitle = $actionProperties['title'];
                 } else {
                     if (isset($_ARRAYLANG[$actionName])) {
                         $actionTitle = $_ARRAYLANG[$actionName];
                     }
                 }
                 if (isset($actionProperties['jsEvent'])) {
                     $actionName = $actionProperties['jsEvent'];
                 }
                 $multiActions[$actionName] = $actionTitle;
             }
             $select = new \Cx\Core\Html\Model\Entity\DataElement('cxMultiAction', \Html::getOptions($multiActions), \Cx\Core\Html\Model\Entity\DataElement::TYPE_SELECT);
             // this is not a nice place for this code
             // but we should cleanup this complete class and make
             // it base on templates
             $select->setAttribute('onchange', '
                     var regex = /([a-zA-Z\\/]+):([a-zA-Z\\/]+)/;
                     var matches = jQuery(this).val().match(regex);
                     if (!matches) {
                         return false;
                     }
                     var checkboxes = jQuery(this).closest("table").find("input[type=checkbox]");
                     var activeRows = [];
                     checkboxes.filter(":checked").each(function(el) {
                         activeRows.push(jQuery(this).val());
                     });
                     cx.trigger(matches[1], matches[2], activeRows);
                     checkboxes.prop("checked", false);
                     jQuery(this).val("");
                 ');
             $this->setCellContents($row, 0, $multiActionsCode . $select, 'TD', 0);
             $this->setCellAttributes($row, 0, array('colspan' => $col + is_array($options['functions'])));
         }
         $attrs = array();
     }
     //add the sorting parameters as table attribute
     //if the row sorting functionality is enabled
     $className = 'adminlist';
     if (!empty($sortField)) {
         $className = '\'adminlist sortable\'';
         if (!empty($component)) {
             $attrs['data-component'] = $component;
         }
         if (!empty($entity)) {
             $attrs['data-entity'] = $entity;
         }
         if (!empty($sortOrder)) {
             $attrs['data-order'] = $sortOrder;
         }
         if (!empty($sortField)) {
             $attrs['data-field'] = $sortField;
         }
         if (isset($pagingPos)) {
             $attrs['data-pos'] = $pagingPos;
         }
         $attrs['data-object'] = 'Html';
         $attrs['data-act'] = 'updateOrder';
         if (isset($sortBy['jsonadapter']) && !empty($sortBy['jsonadapter']['object']) && !empty($sortBy['jsonadapter']['act'])) {
             $attrs['data-object'] = $sortBy['jsonadapter']['object'];
             $attrs['data-act'] = $sortBy['jsonadapter']['act'];
         }
     }
     parent::__construct(array_merge($attrs, array('class' => $className, 'width' => '100%')));
 }