public function onAfterConstruct(DataModel &$model) { // This only applies to the front-end if (!$model->getContainer()->platform->isFrontend()) { return; } // Get the page parameters /** @var \JRegistry $params */ $params = \JFactory::getApplication()->getPageParameters(); // Extract the page parameter keys $asArray = $params->toArray(); if (empty($asArray)) { // There are no keys; no point in going on. return; } $keys = array_keys($asArray); unset($asArray); // Loop all page parameter keys foreach ($keys as $key) { // This is the current model state $currentState = $model->getState($key); // This is the explicitly requested state in the input $explicitInput = $model->input->get($key, null, 'raw'); // If the current state is empty and there's no explicit input we'll use the page parameters instead if (is_null($currentState) && is_null($explicitInput)) { $model->setState($key, $params->get($key)); } } }
/** * @param DataModel $model * @param \stdClass $dataObject */ public function onBeforeUpdate(&$model, &$dataObject) { // Make sure we're not modifying a locked record $userId = $model->getContainer()->platform->getUser()->id; $isLocked = $model->isLocked($userId); if ($isLocked) { return; } // Handle the modified_on field if ($model->hasField('modified_on')) { $model->setFieldValue('modified_on', $model->getContainer()->platform->getDate()->toSql(false, $model->getDbo())); $modifiedOnField = $model->getFieldAlias('modified_on'); $dataObject->{$modifiedOnField} = $model->getFieldValue('modified_on'); } // Handle the modified_by field if ($model->hasField('modified_by')) { $model->setFieldValue('modified_by', $userId); $modifiedByField = $model->getFieldAlias('modified_by'); $dataObject->{$modifiedByField} = $model->getFieldValue('modified_by'); } }
/** * @param DataModel $model * @param \stdClass $dataObject */ public function onBeforeCreate(&$model, &$dataObject) { // Handle the created_on field if ($model->hasField('created_on')) { $nullDate = $model->getDbo()->getNullDate(); $created_on = $model->getFieldValue('created_on'); if (empty($created_on) || $created_on == $nullDate) { $model->setFieldValue('created_on', $model->getContainer()->platform->getDate()->toSql(false, $model->getDbo())); $createdOnField = $model->getFieldAlias('created_on'); $dataObject->{$createdOnField} = $model->getFieldValue('created_on'); } } // Handle the created_by field if ($model->hasField('created_by')) { $created_by = $model->getFieldValue('created_by'); if (empty($created_by)) { $model->setFieldValue('created_by', $model->getContainer()->platform->getUser()->id); $createdByField = $model->getFieldAlias('created_by'); $dataObject->{$createdByField} = $model->getFieldValue('created_by'); } } }
/** * Public constructor. Initialises the relation. * * @param DataModel $parentModel The data model we are attached to * @param string $foreignModelName The name of the foreign key's model in the format "modelName@com_something" * @param string $localKey The local table key for this relation * @param string $foreignKey The foreign key for this relation * @param string $pivotTable For many-to-many relations, the pivot (glue) table * @param string $pivotLocalKey For many-to-many relations, the pivot table's column storing the local key * @param string $pivotForeignKey For many-to-many relations, the pivot table's column storing the foreign key */ public function __construct(DataModel $parentModel, $foreignModelName, $localKey = null, $foreignKey = null, $pivotTable = null, $pivotLocalKey = null, $pivotForeignKey = null) { $this->parentModel = $parentModel; $this->foreignModelClass = $foreignModelName; $this->localKey = $localKey; $this->foreignKey = $foreignKey; $this->pivotTable = $pivotTable; $this->pivotLocalKey = $pivotLocalKey; $this->pivotForeignKey = $pivotForeignKey; $this->container = $parentModel->getContainer(); $class = $foreignModelName; if (strpos($class, '@') === false) { $this->foreignModelComponent = null; $this->foreignModelName = $class; } else { $foreignParts = explode('@', $class, 2); $this->foreignModelComponent = $foreignParts[1]; $this->foreignModelName = $foreignParts[0]; } }
private function applyModelField(DataModel $model, \SimpleXMLElement &$headerSet, \SimpleXMLElement &$fieldSet, $fieldName, $modelName) { // This will fail if the model is invalid, e.g. we have example_foobar_id but no #__example_foobars table. The // error will balloon up the stack and the field will be rendered as simple numeric field instead of a Model // field. /** @var DataModel $foreignModel */ $foreignModel = $model->getContainer()->factory->model($modelName); $value_field = $foreignModel->getKeyName(); if ($foreignModel->hasField('title')) { $value_field = $foreignModel->getFieldAlias('title'); } $langDefs = $this->getFieldLabel($fieldName); $this->addString($langDefs['label']['key'], $langDefs['label']['value']); $this->addString($langDefs['desc']['key'], $langDefs['desc']['value']); $header = $headerSet->addChild('header'); $header->addAttribute('name', $fieldName); $header->addAttribute('type', 'Model'); $header->addAttribute('model', $modelName); $header->addAttribute('key_field', $foreignModel->getKeyName()); $header->addAttribute('value_field', $value_field); $header->addAttribute('label', $langDefs['label']['key']); $header->addAttribute('sortable', 'true'); $field = $fieldSet->addChild('field'); $field->addAttribute('name', $fieldName); $field->addAttribute('type', 'Model'); $field->addAttribute('model', $modelName); $field->addAttribute('key_field', $foreignModel->getKeyName()); $field->addAttribute('value_field', $value_field); $field->addAttribute('label', $langDefs['label']['key']); }