Inheritance: implements Serializabl\Serializable, implements ArrayAcces\ArrayAccess, implements IteratorAggregat\IteratorAggregate, implements Countabl\Countable
 /**
  * Build reference info from parent record.
  *
  * @param BaseModel $parentRecord
  * @param string $relationId
  * @return array
  */
 protected function buildRecordReferences(BaseModel $parentRecord, $relationId)
 {
     $parentSchema = $parentRecord->getSchema();
     $relationship = $parentSchema->getRelation($relationId);
     $refs = [];
     $refs[$relationship['foreign_column']] = ['record' => $parentRecord->toArray(), 'key' => $relationship['self_column'], 'referedRelationship' => $relationship->accessor];
     return $refs;
 }
Beispiel #2
0
 /**
  * array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\" ]]]] ).
  */
 public function importResource($fd, $unsetPrimaryKey = false)
 {
     $schema = $this->model->getSchema();
     if ($this->columnMap) {
         $columnNames = $this->columnMap;
     } else {
         $columnNames = fgetcsv($fd);
     }
     while (($fields = fgetcsv($fd)) !== false) {
         $args = array();
         foreach ($columnNames as $idx => $columnName) {
             $columnValue = $fields[$idx];
             $args[$columnName] = $columnValue;
         }
         if ($unsetPrimaryKey) {
             if ($pk = $schema->primaryKey) {
                 unset($args[$pk]);
             }
         }
         $ret = $this->model->create($args);
     }
 }
Beispiel #3
0
 /**
  * Create record action object from record
  *
  * @return ActionKit\RecordAction\BaseRecordAction
  */
 public function getRecordAction(BaseModel $record)
 {
     $action = $record->id ? $record->asUpdateAction() : $record->asCreateAction();
     return $action;
 }
Beispiel #4
0
 protected function appendRecordInplace(DOMDocument $dom, DOMElement $root, BaseModel $record, SchemaInterface $schema = NULL, $recursive = true)
 {
     if (!$schema) {
         $schema = $record->getSchema();
     }
     $columns = $schema->getColumns();
     foreach ($columns as $column) {
         $columnElement = $dom->createElement($column->name);
         $columnElement->setAttribute('isa', $column->isa);
         if ($column->type) {
             $columnElement->setAttribute('type', $column->type);
         }
         if ($column->contentType) {
             $columnElement->setAttribute('content-type', $column->contentType);
         }
         $value = $record->getValue($column->name);
         $columnElement->appendChild(new DOMText($value));
         $root->appendChild($columnElement);
     }
     if (!$recursive) {
         return;
     }
     foreach ($schema->getRelations() as $rId => $r) {
         if ($r['type'] === Relationship::HAS_MANY) {
             $foreignRecords = $record->get($rId);
             if (!$foreignRecords || $foreignRecords->size() === 0) {
                 continue;
             }
             $relationElement = $dom->createElement($rId);
             $root->appendChild($relationElement);
             $relationElement->setAttribute('type', 'has-many');
             $collectionElement = $dom->createElement('collection');
             $relationElement->appendChild($collectionElement);
             foreach ($foreignRecords as $foreignRecord) {
                 $this->appendRecord($dom, $collectionElement, $foreignRecord, NULL, false);
             }
         } elseif ($r['type'] === Relationship::HAS_ONE) {
             $foreignRecord = $record->get($rId);
             if (!$foreignRecord) {
                 continue;
             }
             $relationElement = $dom->createElement($rId);
             $root->appendChild($relationElement);
             $relationElement->setAttribute('type', 'has-one');
             $this->appendRecord($dom, $relationElement, $foreignRecord, NULL, false);
         } elseif ($r['type'] === Relationship::MANY_TO_MANY) {
             $foreignRecords = $record->get($rId);
             if (!$foreignRecords || $foreignRecords->size() === 0) {
                 continue;
             }
             // $relationElement = $dom->createElement($rId);
             // $relationElement->setAttribute('type', 'many-to-many');
             // $root->ownerDocument->firstChild->appendChild($relationElement);
             // $relationElement->appendChild($collectionElement);
             $collectionElement = $dom->createElement('collection');
             $refNode = $root->ownerDocument->firstChild->insertBefore($collectionElement, $root->ownerDocument->firstChild->firstChild);
             foreach ($foreignRecords as $foreignRecord) {
                 $this->appendRecord($dom, $collectionElement, $foreignRecord, NULL, false);
             }
         }
     }
 }
Beispiel #5
0
 /**
  * Update session data from record
  *
  * @param mixed User record object
  */
 protected function updateSessionFromRecord(BaseModel $record)
 {
     // get column maes to register
     foreach ($record->getColumnNames() as $name) {
         $val = $record->{$name};
         $this->session->set($name, is_object($val) ? $val->__toString() : $val);
     }
     if ($record instanceof CurrentUserRole) {
         $this->session->set('roles', $record->getRoles());
     } else {
         if (method_exists($record, 'getRoles')) {
             $this->session->set('roles', $record->getRoles());
         } else {
             if (isset($record->role)) {
                 if ($record->role instanceof BaseModel) {
                     $this->session->set('roles', array($record->role->identity));
                 } else {
                     $this->session->set('roles', array($record->role));
                 }
             } else {
                 $this->session->set('roles', array());
             }
         }
     }
 }
Beispiel #6
0
 public function assertRecordLoaded(BaseModel $record, $message = NULL)
 {
     $data = $record->getStashedData();
     $this->assertNotEmpty($data, $message ?: 'Record loaded');
 }
Beispiel #7
0
 /**
  * Translate LazyRecord RuntimeColumn to ActionKit param object.
  *
  * @param RuntimeColumn $column
  * @param BaseModel $record
  * @return Param;
  */
 public static function toParam(RuntimeColumn $column, BaseModel $record = null, Action $action = null)
 {
     $name = $column->name;
     $param = new Param($name, $action);
     if ($column->isa) {
         $param->isa($column->isa);
     }
     // Convert notNull to required
     // required() is basically the same as notNull but provides extra
     // software validation.
     // When creating records, the primary key with auto-increment support is not required.
     // However when updating records, the primary key is required for updating the existing record..
     if ($column->notNull) {
         if ($action && $column->primary) {
             if ($action instanceof CreateRecordAction) {
                 // autoIncrement is not defined, then it requires the input value.
                 if ($column->autoIncrement) {
                     $param->required = false;
                 } else {
                     $param->required = true;
                 }
             } else {
                 if ($action instanceof UpdateRecordAction || $action instanceof DeleteRecordAction) {
                     // primary key column is required to update/delete records.
                     $param->required = true;
                 }
             }
         } else {
             $param->required = true;
         }
     }
     foreach ($column->attributes as $k => $v) {
         // if the model column validator is not compatible with action validator
         if ($k === 'validator' || $v instanceof Raw) {
             continue;
         }
         $param->{$k} = $v;
     }
     // if we got record, load the value from it.
     if ($record) {
         // $val = $record->{$name};
         // $val = $val instanceof BaseModel ? $val->dataKeyValue() : $val;
         $val = $record->getValue($name);
         $param->value = $val;
         // XXX: should get default value (from column definition)
         //      default value is only used in create action.
     } else {
         $default = $column->getDefaultValue();
         if (!$default instanceof Raw) {
             $param->value = $default;
         }
     }
     // convert related collection model to validValues
     if ($param->refer && !$param->validValues) {
         if (class_exists($param->refer, true)) {
             $referClass = $param->refer;
             // it's a `has many`-like relationship
             if (is_subclass_of($referClass, 'LazyRecord\\BaseCollection', true)) {
                 $collection = new $referClass();
                 $options = array();
                 foreach ($collection as $item) {
                     $label = method_exists($item, 'dataLabel') ? $item->dataLabel() : $item->id;
                     $options[$label] = $item->dataKeyValue();
                 }
                 $param->validValues = $options;
             } elseif (is_subclass_of($referClass, 'LazyRecord\\BaseModel', true)) {
                 // it's a `belongs-to`-like relationship
                 $class = $referClass . 'Collection';
                 $collection = new $class();
                 $options = array();
                 foreach ($collection as $item) {
                     $label = method_exists($item, 'dataLabel') ? $item->dataLabel() : $item->id;
                     $options[$label] = $item->dataKeyValue();
                 }
                 $param->validValues = $options;
             } elseif (is_subclass_of($referClass, 'LazyRecord\\Schema\\DeclareSchema', true)) {
                 $schema = new $referClass();
                 $collection = $schema->newCollection();
                 $options = array();
                 foreach ($collection as $item) {
                     $label = method_exists($item, 'dataLabel') ? $item->dataLabel() : $item->id;
                     $options[$label] = $item->dataKeyValue();
                 }
                 $param->validValues = $options;
             } else {
                 throw new Exception('Unsupported refer type');
             }
         } elseif ($relation = $record->getSchema()->getRelation($param->refer)) {
             // so it's a relationship reference
             // TODO: implement this
             throw new Exception('Unsupported refer type');
         }
     }
     //  Convert column type to param type.
     // copy widget attributes
     if ($column->widgetClass) {
         $param->widgetClass = $column->widgetClass;
     }
     if ($column->widgetAttributes) {
         $param->widgetAttributes = $column->widgetAttributes;
     }
     if ($column->immutable) {
         $param->widgetAttributes['readonly'] = 'readonly';
     }
     if ($column->renderAs) {
         $param->renderAs($column->renderAs);
     } elseif ($param->validValues || $param->validPairs || $param->optionValues) {
         $param->renderAs('SelectInput');
     } elseif ($param->name === 'id') {
         $param->renderAs('HiddenInput');
     } else {
         // guess input widget from data type
         $typeMapping = array('date' => 'DateInput', 'datetime' => 'DateTimeInput', 'text' => 'TextareaInput');
         if (isset($typeMapping[$param->type])) {
             $param->renderAs($typeMapping[$param->type]);
         } else {
             $param->renderAs('TextInput');
         }
     }
     return $param;
 }