Exemplo n.º 1
0
 /**
  * Provide a row that can be linked to all the fields from the supplied
  * table.  If the field has a value, we'll look up the row using the table's
  * find() method.  Otherwise, we'll create a new row.  Note that an exception
  * is throw if you attempt to use this linker with a field that doesn't
  * itself have a row associated with it already.  Often you'll link to the
  * first row using a \Dewdrop\Fields\RowEditor\Link\QueryString rule and
  * then string Field linker on after that.
  *
  * @throws \Dewdrop\Fields\Exception
  * @param Table $table
  * @return \Dewdrop\Db\Row
  */
 public function link(Table $table)
 {
     if (!$this->field->hasRow()) {
         $this->field->setRow($this->rowEditor->getRow($this->field->getGroupName()));
     }
     $value = $this->field->getValue();
     if ($value) {
         $row = $table->find($value);
     } else {
         $row = $table->createRow();
     }
     return $row;
 }
Exemplo n.º 2
0
 protected function logActivity(RowEditor $rowEditor)
 {
     $model = $this->component->getPrimaryModel();
     $rows = $rowEditor->getRows();
     $id = null;
     /* @var $row \Dewdrop\Db\Row */
     foreach ($rows as $row) {
         if ($row->getTable() === $model) {
             $id = $row->get(current($model->getPrimaryKey()));
         }
     }
     if ($id) {
         /* @var $handler \Dewdrop\ActivityLog\Handler\CrudHandlerAbstract */
         $handler = $this->component->getActivityLogHandler();
         $handler->restore($id);
     }
 }
Exemplo n.º 3
0
 /**
  * Get the \Dewdrop\Fields\RowEditor object that will assist with the
  * editing of items in this component.
  *
  * @return RowEditor
  */
 public function getRowEditor()
 {
     if (!$this->rowEditor) {
         $fields = $this->getFields();
         $this->rowEditor = new RowEditor($fields, $this->getRequest());
         $this->rowEditor->linkByQueryString('users', 'user_id')->link();
         if ($this->rowEditor->isNew()) {
             $this->addPasswordFields($fields);
         }
     }
     return $this->rowEditor;
 }
Exemplo n.º 4
0
 public function testSavingWillTraverseLinkedFieldsToHookRowsTogether()
 {
     $db = Pimple::getResource('db');
     $db->query('DELETE FROM dewdrop_test_fruits');
     $animalModel = new RowEditorAnimalModel();
     $this->fields->add($animalModel->field('name'));
     $this->fields->add($animalModel->field('is_fierce'));
     $this->fields->add($animalModel->field('is_cute'));
     $this->rowEditor->linkByQueryString('dewdrop_test_animals', 'dewdrop_test_animal_id');
     $this->rowEditor->linkByField('dewdrop_test_fruits', $animalModel->field('favorite_fruit_id'));
     $this->rowEditor->link();
     $this->assertTrue($this->rowEditor->isValid(array('dewdrop_test_fruits:name' => 'Banana', 'dewdrop_test_fruits:level_of_deliciousness' => 8, 'dewdrop_test_animals:name' => 'Gorilla', 'dewdrop_test_animals:is_fierce' => 1, 'dewdrop_test_animals:is_cute' => 1)));
     $this->rowEditor->save();
     $this->assertEquals($db->fetchOne('SELECT MAX(dewdrop_test_fruit_id) FROM dewdrop_test_fruits'), $db->fetchOne('SELECT favorite_fruit_id FROM dewdrop_test_animals ORDER BY dewdrop_test_animal_id DESC LIMIT 1'));
 }
Exemplo n.º 5
0
 public function renderAjaxResponse()
 {
     if (!$this->request->isPost() && !$this->request->isGet()) {
         return ['result' => 'error', 'message' => 'AJAX edit requests must be POST or GET'];
     } elseif ($this->request->isPost() && !$this->invalidSubmission) {
         return ['result' => 'success', 'id' => $this->component->getListing()->getPrimaryKey()->getValue(), 'data' => $this->getData()];
     } elseif ($this->request->isGet()) {
         return $this->renderAjaxForm();
     } else {
         $messages = [];
         foreach ($this->fields->getEditableFields() as $field) {
             $messages[$field->getHtmlId()] = $this->rowEditor->getMessages($field);
         }
         return ['result' => 'invalid', 'messages' => $messages];
     }
 }
Exemplo n.º 6
0
 /**
  * Create a Field object for handling deletion of items.
  *
  * @param RowEditor $editor
  * @return Field
  */
 private function createDeleteField(RowEditor $editor)
 {
     $field = new Field();
     $field->setId('delete')->setEditable(true)->assignHelperCallback('InputFilter', function () {
         $input = new \Zend\InputFilter\Input('delete');
         $input->setAllowEmpty(true);
         return $input;
     })->assignHelperCallback('EditControl.Label', function () {
         return '<span class="glyphicon glyphicon-trash"></span>';
     })->assignHelperCallback('EditControl.Control', function () use($editor) {
         if ($editor->isNew()) {
             $out = '<button data-is-new="1" class="btn btn-danger btn-delete">';
         } elseif ($editor->hasDeleteField()) {
             $out = '<button data-is-new="0" class="btn btn-danger btn-delete">';
         } else {
             $out = '<button data-is-new="0" class="btn btn-danger btn-delete disabled">';
         }
         $out .= '<span class="glyphicon glyphicon-trash"></span>';
         $out .= '</button>';
         return $out;
     });
     return $field;
 }
Exemplo n.º 7
0
 /**
  * Pass dependencies into the View.
  */
 public function render()
 {
     $this->view->assign(['component' => $this->component, 'componentModel' => $this->component->getPrimaryModel(), 'fields' => $this->fields, 'rowEditor' => $this->rowEditor, 'breadcrumbTitle' => $this->rowEditor->isNew() ? 'Add' : 'Edit']);
     return $this->renderView();
 }
Exemplo n.º 8
0
 /**
  * Not 100% certain that resetting and re-linking will be sufficient in all
  * cases for the row editor initialization so providing this little hook to
  * allow custom initialization logic by a sub-class.
  *
  * @param RowEditor $rowEditor
  * @return $this
  */
 protected function initializeRowEditor(RowEditor $rowEditor)
 {
     $rowEditor->reset()->link();
     return $this;
 }