/** * Get the rendering of this field type for a repeatable (grid) display, * e.g. in a view listing many item (typically a "browse" task) * * @since 2.0 * * @return string The field HTML * * @throws DataModelRequired */ public function getRepeatable() { if (!$this->item instanceof DataModel) { throw new DataModelRequired(__CLASS__); } $config = $this->getConfig(); $html = '<div class="btn-group">'; // Render a published field if ($this->item->hasField('enabled')) { $publishedFieldName = $this->item->getFieldAlias('enabled'); if ($config['published'] || $config['unpublished']) { // Generate a FieldInterfacePublished field $publishedField = $this->getPublishedField($publishedFieldName); // Render the publish button $html .= $publishedField->getRepeatable(); } if ($config['archived']) { $archived = $this->item->getFieldValue($publishedFieldName) == 2 ? true : false; // Create dropdown items $action = $archived ? 'unarchive' : 'archive'; JHtml::_('actionsdropdown.' . $action, 'cb' . $this->rowid); } if ($config['trash']) { $trashed = $this->item->getFieldValue($publishedFieldName) == -2 ? true : false; $action = $trashed ? 'untrash' : 'trash'; JHtml::_('actionsdropdown.' . $action, 'cb' . $this->rowid); } // Render dropdown list if ($config['archived'] || $config['trash']) { $html .= JHtml::_('actionsdropdown.render', $this->item->title); } } $html .= '</div>'; return $html; }
public function onAfterSave(DataModel &$model) { if (!$model->hasField('asset_id') || !$model->isAssetsTracked()) { return true; } $assetFieldAlias = $model->getFieldAlias('asset_id'); $currentAssetId = $model->getFieldValue('asset_id'); unset($model->{$assetFieldAlias}); // Create the object used for inserting/udpating data to the database $fields = $model->getTableFields(); // Let's remove the asset_id field, since we unset the property above and we would get a PHP notice if (isset($fields[$assetFieldAlias])) { unset($fields[$assetFieldAlias]); } // Asset Tracking $parentId = $model->getAssetParentId(); $name = $model->getAssetName(); $title = $model->getAssetTitle(); $asset = \JTable::getInstance('Asset'); $asset->loadByName($name); // Re-inject the asset id. $this->{$assetFieldAlias} = $asset->id; // Check for an error. $error = $asset->getError(); // Since we are using JTable, there is no way to mock it and test for failures :( // @codeCoverageIgnoreStart if ($error) { throw new \Exception($error); } // @codeCoverageIgnoreEnd // Specify how a new or moved node asset is inserted into the tree. // Since we're unsetting the table field before, this statement is always true... if (empty($model->{$assetFieldAlias}) || $asset->parent_id != $parentId) { $asset->setLocation($parentId, 'last-child'); } // Prepare the asset to be stored. $asset->parent_id = $parentId; $asset->name = $name; $asset->title = $title; if ($model->getRules() instanceof \JAccessRules) { $asset->rules = (string) $model->getRules(); } // Since we are using JTable, there is no way to mock it and test for failures :( // @codeCoverageIgnoreStart if (!$asset->check() || !$asset->store()) { throw new \Exception($asset->getError()); } // @codeCoverageIgnoreEnd // Create an asset_id or heal one that is corrupted. if (empty($model->{$assetFieldAlias}) || $currentAssetId != $model->{$assetFieldAlias} && !empty($model->{$assetFieldAlias})) { // Update the asset_id field in this table. $model->{$assetFieldAlias} = (int) $asset->id; $k = $model->getKeyName(); $db = $model->getDbo(); $query = $db->getQuery(true)->update($db->qn($model->getTableName()))->set($db->qn($assetFieldAlias) . ' = ' . (int) $model->{$assetFieldAlias})->where($db->qn($k) . ' = ' . (int) $model->{$k}); $db->setQuery($query)->execute(); } return true; }
/** * @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'); } } }
/** * Get the rendering of this field type for a repeatable (grid) display, * e.g. in a view listing many item (typically a "browse" task) * * @since 2.0 * * @return string The field HTML */ public function getRepeatable() { if (is_array($this->value)) { $this->value = print_r($this->value, true); } if (isset($this->element['legacy'])) { return $this->getInput(); } // Should I support checked-out elements? $checkoutSupport = false; if (isset($this->element['checkout'])) { $checkoutSupportValue = (string) $this->element['checkout']; $checkoutSupport = in_array(strtolower($checkoutSupportValue), array('yes', 'true', 'on', 1)); } // Initialise $class = $this->class ? $this->class : $this->id; $format_string = $this->element['format'] ? (string) $this->element['format'] : ''; $format_if_not_empty = in_array((string) $this->element['format_if_not_empty'], array('true', '1', 'on', 'yes')); $parse_value = in_array((string) $this->element['parse_value'], array('true', '1', 'on', 'yes')); $link_url = $this->element['url'] ? (string) $this->element['url'] : ''; $empty_replacement = $this->element['empty_replacement'] ? (string) $this->element['empty_replacement'] : ''; $format_source_file = empty($this->element['format_source_file']) ? '' : (string) $this->element['format_source_file']; $format_source_class = empty($this->element['format_source_class']) ? '' : (string) $this->element['format_source_class']; $format_source_method = empty($this->element['format_source_method']) ? '' : (string) $this->element['format_source_method']; if ($link_url && $this->item instanceof DataModel) { $link_url = $this->parseFieldTags($link_url); } else { $link_url = false; } // Get the (optionally formatted) value $value = $this->value; if (!empty($empty_replacement) && empty($this->value)) { $value = JText::_($empty_replacement); } if ($parse_value) { $value = $this->parseFieldTags($value); } if (!empty($format_string) && (!$format_if_not_empty || $format_if_not_empty && !empty($this->value))) { $format_string = $this->parseFieldTags($format_string); $value = sprintf($format_string, $value); } elseif ($format_source_class && $format_source_method) { // Maybe we have to load a file? if (!empty($format_source_file)) { $format_source_file = $this->form->getContainer()->template->parsePath($format_source_file, true); if ($this->form->getContainer()->filesystem->fileExists($format_source_file)) { include_once $format_source_file; } } // Make sure the class and method exist if (class_exists($format_source_class, true) && in_array($format_source_method, get_class_methods($format_source_class))) { $value = $format_source_class::$format_source_method($value); $value = $this->parseFieldTags($value); } else { $value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8'); } } else { $value = htmlspecialchars($value, ENT_COMPAT, 'UTF-8'); } // Create the HTML $html = '<span class="' . $class . '">'; $userId = $this->form->getContainer()->platform->getUser()->id; if ($checkoutSupport && $this->item->isLocked($userId)) { $key_field = $this->item->getKeyName(); $key_id = $this->item->{$key_field}; $lockedBy = ''; $lockedOn = ''; if ($this->item->hasField('locked_by')) { $lockedUser = $this->form->getContainer()->platform->getUser($this->item->getFieldValue('locked_by')); $lockedBy = $lockedUser->name . ' (' . $lockedUser->username . ')'; } if ($this->item->hasField('locked_on')) { $lockedOn = $this->item->getFieldValue('locked_on'); } $html .= \JHtml::_('jgrid.checkedout', $key_id, $lockedBy, $lockedOn, '', true); } if ($link_url) { $html .= '<a href="' . $link_url . '">'; } $html .= $value; if ($link_url) { $html .= '</a>'; } $html .= '</span>'; return $html; }