Exemplo n.º 1
0
 public function performIndex()
 {
     $this->prepareControl();
     if ($this->isListing) {
         /*
          * Provide default action on set of selected model's items.
          */
         $this->browser->setPagerVolatility("none");
         if (is_callable($this->browserCustomizer)) {
             call_user_func($this->browserCustomizer, $this, $this->browser);
         } else {
             // try generate some commonly useful databrowser
             $controller = $this;
             $this->browser->addColumn('label', \de\toxa\txf\_L('Label'), false, function ($value, $propName, $record, $id) use($controller) {
                 return $controller->getModel()->getMethod('select')->invoke(null, $controller->source, $id)->describe();
             })->setRowCommander(function ($id, $data) use($controller) {
                 $items = array('view' => markup::link(sprintf($controller->getUrls()->view, $id), \de\toxa\txf\_L('view item'), 'controller-list-action-view'), 'edit' => markup::link(sprintf($controller->getUrls()->edit, $id), \de\toxa\txf\_L('edit item'), 'controller-list-action-edit'), 'delete' => markup::link(sprintf($controller->getUrls()->delete, $id), \de\toxa\txf\_L('delete item'), 'controller-list-action-delete'));
                 if (!user::current()->isAuthenticated()) {
                     unset($items['edit'], $items['delete']);
                 }
                 return implode(' ', array_filter($items));
             });
             if (user::current()->isAuthenticated()) {
                 $controller->setPanelControl('add', markup::link($controller->getUrls()->add, \de\toxa\txf\_L('add item'), 'controller-list-action-add'));
             }
         }
         if (user::current()->isAuthenticated()) {
             if (!$this->hasPanelControl('add')) {
                 $this->setPanelControl('add', markup::link(context::selfURL(false, array_merge(array_pad(array(), $this->modelClass->getMethod('idSize')->invoke(null), 0), array('edit'))), \de\toxa\txf\_L('Add')));
             }
         }
         $this->browser->processInput();
         return $this->browser->getCode();
     }
     /**
      * Provide default action on single instance of selected model.
      */
     $this->editor->mayEdit(false)->mayDelete(false);
     if (is_callable($this->viewCustomizer)) {
         call_user_func($this->viewCustomizer, $this, false);
     } else {
         $data = $this->editor->item()->published();
         // reduce relations
         foreach ($data as $key => $value) {
             if (is_array($value)) {
                 $data[$key] = implode(', ', $value);
             }
         }
         return html::arrayToCard($data);
     }
     if (user::current()->isAuthenticated()) {
         if (!$this->hasPanelControl('edit')) {
             $this->setPanelControl('edit', markup::link(context::selfURL(false, array_merge($this->item->id(), array('edit'))), \de\toxa\txf\_L('Edit')));
         }
     }
     if ($this->editor->processInput($this->editorValidator)) {
         txf::redirectTo($this->getUrls()->list);
     }
     return $this->editor->render();
 }
Exemplo n.º 2
0
 /**
  * Renders editor with fields limited to displaying values instead of
  * providing controls for editing them.
  *
  * @return string
  * @throws http_exception on trying to render without selecting item first
  */
 public function renderReadonly()
 {
     if (!$this->item) {
         throw new http_exception(400, \de\toxa\txf\_L('Your request is not including selection of item to be displayed.'));
     }
     $fields = $this->fields;
     $editor = $this;
     $modelLabelFormatter = array($this->class->getName(), 'formatHeader');
     $modelCellFormatter = array($this->class->getName(), 'formatCell');
     $labelFormatter = function ($name) use($modelLabelFormatter, $fields, $editor) {
         if (array_key_exists($name, $fields)) {
             $label = $fields[$name]->label();
             if ($label) {
                 return sprintf('%s:', $label);
             } else {
                 if ($label === false) {
                     return '';
                 }
             }
         }
         return call_user_func($modelLabelFormatter, $name);
     };
     $cellFormatter = function ($value, $name, $record, $id) use($modelCellFormatter, $fields, $editor) {
         $field = @$fields[$name];
         /** @var model_editor_field $field */
         return $field ? $field->type()->formatValue($name, $value, $editor, $field) : null;
     };
     $record = $this->item->published();
     data::rearrangeArray($record, $this->sortingOrder ? $this->sortingOrder : array_keys($fields));
     return html::arrayToCard($record, strtolower(basename(strtr($this->class->getName(), '\\', '/'))) . 'Details', $cellFormatter, $labelFormatter, \de\toxa\txf\_L('-'));
 }