Exemple #1
0
 /**
  * @param $element
  *
  * @return mixed
  */
 public function decorateValue($element)
 {
     $name = $element->getAttribute('name');
     if (in_array($element->getTag(), ['input', 'select', 'textarea']) && $this->record && $this->record->__isset($name) && is_object($element) && $element->getAttribute('type') != 'password') {
         $value = $this->record->get($name);
         $element->setValue($value);
     }
     return $element;
 }
Exemple #2
0
 /**
  *
  */
 public function execute()
 {
     $data = $this->entity->tabelizeRecord($this->record);
     /**
      * We need to get entity table extensions.
      * Lets hardcode them for now.
      */
     $extensions = ['i18n', 'p17n', ''];
     $table = $this->entity->getTable();
     $primaryKeys = $this->entity->getRepository()->getCache()->getTablePrimaryKeys($table);
     if (!$primaryKeys) {
         throw new Exception('Will NOT delete from table without primary keys ...');
     }
     foreach ($extensions as $ext) {
         if ($ext) {
             $ext = '_' . $ext;
         }
         if ($this->entity->getRepository()->getCache()->hasTable($table . $ext)) {
             /**
              * We will delete record from $table ...
              */
             $query = (new Delete())->setTable($table . $ext);
             /**
              * ... add primary key condition ...
              */
             foreach ($primaryKeys as $key) {
                 $query->where($key, $data[$table][$key]);
             }
             /**
              * ... prepare query ...
              */
             $prepare = $this->repository->prepareQuery($query);
             /**
              *  ... and execute it.
              */
             $this->repository->executePrepared($prepare);
         }
     }
     $this->record->setSaved(false);
     $this->record->setDeleted(true);
     return true;
 }
Exemple #3
0
 /**
  * @return null
  * @throws Exception
  */
 public function execute()
 {
     $data = $this->entity->tabelizeRecord($this->record);
     foreach ($data as $table => $insert) {
         if ($this->tables && !in_array($table, $this->tables)) {
             continue;
         }
         if ($this->record->{$this->entity->getPrimaryKey()}) {
             /**
              * Primary key is already set, we need to update it.
              */
             $insert[$this->entity->getPrimaryKey()] = $this->record->{$this->entity->getPrimaryKey()};
             $this->insert($table, $insert);
         } else {
             /**
              * Primary key is not set yet, we need to set it now.
              */
             $this->record->{$this->entity->getPrimaryKey()} = $this->insert($table, $insert);
             $this->record->setSaved();
         }
     }
     return $this->record->{$this->entity->getPrimaryKey()};
 }
Exemple #4
0
 /**
  * Transforms Record to two-dimensional array of tables and fields.
  *
  * @param Record $record
  *
  * @return array
  */
 public function tabelizeRecord(Record $record)
 {
     $dataArray = $record->__toArray(null, 1, false);
     $extensionArray = [];
     /**
      * Holds all available fields in database table cache.
      */
     $keys = [$this->table => $this->repository->getCache()->getTableFields($this->table)];
     foreach (get_class_methods($this) as $method) {
         /**
          * Get extension's fields.
          */
         if ($method != 'getFields' && substr($method, 0, 3) == 'get' && substr($method, -6) == 'Fields') {
             $suffix = $this->{'get' . substr($method, 3, -6) . 'TableSuffix'}();
             if (substr($this->table, strlen($this->table) - strlen($suffix)) != $suffix && $this->repository->getCache()->hasTable($this->table . $suffix)) {
                 $keys[$this->table . $suffix] = $this->{$method}();
             }
         }
         /**
          * Get extension's foreign key values.
          */
         if ($method != 'getForeignKeys' && substr($method, 0, 3) == 'get' && substr($method, -11) == 'ForeignKeys') {
             $suffix = $this->{'get' . substr($method, 3, -11) . 'TableSuffix'}();
             if (substr($this->table, strlen($this->table) - strlen($suffix)) != $suffix && $this->repository->getCache()->hasTable($this->table . $suffix)) {
                 // base table
                 $extensionArray[$this->table . $suffix] = $this->{$method}($record);
             } elseif (strrpos($this->table, $suffix) == strlen($this->table) - strlen($suffix) && $this->repository->getCache()->hasTable($this->table)) {
                 // extendee table
                 $extensionArray[$this->table] = $this->{$method}($record);
             }
         }
     }
     // fill array with tables and fields
     $values = [];
     foreach ($keys as $table => $fields) {
         foreach ($fields as $field) {
             /**
              * Add value if field exists in data array and repository has that field.
              */
             if ($this->repository->getCache()->tableHasField($table, $field)) {
                 if (isset($extensionArray[$table]) && array_key_exists($field, $extensionArray[$table])) {
                     $values[$table][$field] = $extensionArray[$table][$field];
                 } elseif (array_key_exists($field, $dataArray)) {
                     $values[$table][$field] = $dataArray[$field];
                 }
             }
         }
     }
     return $values;
 }
Exemple #5
0
 public function __getTranslatableExtension(Record $record, $key)
 {
     if ($record->relationExists('_translations')) {
         foreach ($record->getRelation('_translations') as $translation) {
             if ($translation->keyExists($key)) {
                 return $translation->{$key};
             }
         }
     }
 }