/** * Sets the path and returns the object * * @param ModelObject $obj The plugin * * @return ModelObject */ public function postCacheTranslateObject(ModelObject $obj) { if (strpos($obj->Path, $this->rootPath) === FALSE) { $obj->setPath($this->rootPath . DIRECTORY_SEPARATOR . $obj->Path); } return $obj; }
/** * Performs an UPDATE for the given ModelObject through our DAO. * ModifiedDate is updated, and the validator is run. * * Validation Errors will throw a ValidationException * * @param ModelObject $obj The ModelObject to edit * * @return mixed See the edit() method of our DAO */ public function edit(ModelObject $obj) { if (!$obj->hasModifiedDate()) { $obj->ModifiedDate = $this->DateFactory->newStorageDate(); } $this->validator->validateFor(__FUNCTION__, $obj)->throwOnError(); return $this->dao->edit($obj); }
/** * Ensures we have a valid model object, and that it's slug is not in conflict, * then validates the object * * @param ModelObject $obj The object to validate * * @return void */ protected function edit(ModelObject $obj) { if ($obj->{$obj->getPrimaryKey()} == null) { $this->errors->reject("{$obj->getPrimaryKey()} is required to edit.")->throwOnError(); } if ($this->dao->getByID($obj->{$obj->getPrimaryKey()}) == false) { $this->getErrors()->reject("Record not found for ID: " . $obj->{$obj->getPrimaryKey()})->throwOnError(); } if ($this->dao->slugExists($obj->Slug, $obj->{$obj->getPrimaryKey()})) { $this->errors->reject("The slug for this " . get_class($obj) . " record is already in use.")->throwOnError(); } $this->errors->validateModelObject($obj); }
/** * doIncrement method will allow user to increment * a given field by calling this function from its model. * * @param ModelObject $model * @param integer $id - Record Id for which the $field is to be incremented * @param integer (optional) $incrementValue, default is 1 * @param string $field (optional) - If not supplied then field name which was provided * during initialization is used, otherwise * it is overwritten with the supplied argument. * @return boolean */ function doIncrement(&$model, $id, $incrementValue = 1, $field = null) { $answer = false; if (empty($field)) { $field = $this->__settings[$model->alias]['incrementFieldName']; } // Save the internal variables for the model $recursiveLevel = $model->recursive; $data = $model->data; $model->recursive = -1; $model->data = $model->findById((int) $id, array('id', $field)); if (!empty($model->data)) { $counter = (int) $model->data[$model->alias][$field] + (int) $incrementValue; $conditions = array($model->alias . '.id' => $id); $fields = array($field => $counter); // Issue updateAll as it won't call any other methods like beforeSave and such in the Model or the // Behavior methods. Just a step for saving callbacks which are not required. $answer = $model->updateAll($fields, $conditions); } // restore the variables back to original $model->data = $data; $model->recursive = $recursiveLevel; return $answer; }
/** * Return name * * @return string */ public function getName() { $name = $this->get("name"); if ($name) { return $name; } $first = $this->get("firstName"); $middle = $this->get("middleName"); $last = $this->get("lastName"); $name .= $first; if ($middle) { $name .= " {$middle}"; } if ($last) { $name .= " {$last}"; } parent::set("name", $name); //no re-setting sub-names return $name; }
/** * Sets the schema for the element * * @param Element $obj The element to analyze * * @return Element the translated element */ public function preCacheTranslateObject(ModelObject $obj) { // merge aspects //$db = $this->getConnection(); // $q = new Query(); // $q->select('aspectid'); // $q->from('elementaspectrel'); // $q->where("elementid = {$db->quote($obj->{$obj->getPrimaryKey()})}"); // $aspectIDs = $db->readCol($q); // $this->populateRels(); // $aspectSlugs = array_key_exists($obj->Slug, $this->aspectrel)?$this->aspectrel[$obj->Slug]['Aspects']:array(); $aspectSlugs = $obj->getAspectSlugs(); $schema = new NodeSchema(); if (!empty($aspectSlugs)) { $aspects = $this->AspectDAO->multiGetBySlug($aspectSlugs); $newAspects = array(); foreach ($aspects as $aspect) { // $plugin = $this->PluginService->getByID($aspect['PluginID']); // if(empty($plugin) || !$plugin->isInstalled() || !$plugin->isEnabled()) // continue; $aspectSchema = $aspect->getSchema(); foreach ($aspectSchema->getTagDefs() as $tagDef) { $schema->addTagDef($tagDef); } foreach ($aspectSchema->getMetaDefs() as $metaDef) { $schema->addMetaDef($metaDef); } $newAspects[] = $aspect; } $obj->setAspects($newAspects); } $obj->setSchema($schema); $obj->setAnchoredSite($this->SiteService->getAnchoredSite()); $obj->setAnchoredSiteSlug($obj->getAnchoredSite()->getSlug()); return $obj; }
/** * Returns the {@link $obj} with the parsed NodeSchema attached * * @param ModelObject $obj The object to translate * * @return ModelObject {@link $obj} with the 'Schema' field set. */ public function preCacheTranslateObject(ModelObject $obj) { $plugin = $this->PluginService->getByID($obj->PluginID); if (empty($plugin) || !$plugin->isInstalled() || !$plugin->isEnabled()) { return null; } if ($obj->getXMLSchema() != '') { // resolve schema $schemaXML = "<?xml version='1.0'?><schema>"; $schemaXML .= preg_replace('/\\<\\?xml([^\\>\\/]*)\\>/', '', $obj->getXMLSchema()); $schemaXML .= "</schema>"; try { $schema = $this->NodeSchemaParser->parse($schemaXML); } catch (Exception $e) { throw new SchemaException("Unable to parse schema for aspect [{$obj->Slug}]:\n " . $e->getMessage()); } $obj->setSchema($schema); } else { $obj->setSchema(new NodeSchema()); } return $obj; }
public function cmsNavItemToXML(ModelObject $CMSNavItem) { $xml = new SimpleXMLExtended('<item/>'); $xml->addAttribute('id', $CMSNavItem->CMSNavItemID); $xml->addAttribute('pluginid', $CMSNavItem->PluginID); $xml->addAttribute('uri', $CMSNavItem->URI); foreach (array('slug', 'label', 'sort_order', 'permissions') as $key) { $camel = StringUtils::camelize($key); if (!empty($CMSNavItem->{$camel})) { $xml->addAttribute($key, $CMSNavItem->{$camel}); } } $xml->addAttribute('enabled', $CMSNavItem->isEnabled() ? 'true' : 'false'); if (!empty($CMSNavItem->DoAddLinksFor)) { $xml->addAttribute('create_add_menu', $CMSNavItem->DoAddLinksFor); } // if($CMSNavItem->hasModifiedDate()) // $xml->addChild('modified_date', $this->DateFactory->toStorageDate($CMSNavItem->ModifiedDate)->toMySQLDate()); // if($CMSNavItem->hasCreationDate()) // $xml->addChild('creation_date', $this->DateFactory->toStorageDate($CMSNavItem->CreationDate)->toMySQLDate()); $sort_array = array(); $children = $CMSNavItem->getChildren(); if (!empty($children)) { foreach ($children as $child) { $sort_array[] = $child->SortOrder; $sort_array2[] = $child->Slug; } array_multisort($sort_array, SORT_ASC, $sort_array2, SORT_ASC, $children); foreach ($children as $child) { $xml->addXMLElement($this->cmsNavItemToXML($child)); } } return $xml; }
/** * Validates the model object, rejecting the fields that fail validation * * @param ModelObject $model The object to validate * * @return void */ public function validateModelObject(ModelObject $model) { $fields = $model->getPersistentFields(); foreach ($fields as $fieldName) { $value = $model->{$fieldName}; $fieldResolved = get_class($model) . '.' . $fieldName; $this->rejectIfInvalid($fieldResolved, 'field', $model->getFieldTitle($fieldName), $value, new ValidationExpression($model->getValidation($fieldName))); } }
/** * Constructor. * * Provide a Query object to run this query and return found objects. * Provide an integer to look for this ID (or $column). * Provide an array to create an new object filled with these values. * * @param mixed $mixed What to get? * @param mixed $column Column to search in. * * @return void */ public function __construct($mixed = null, $column = 'id') { //set modelname $this->modelName = get_called_class(); //set table name if (empty($this->tableName)) { $this->tableName = Config::getSetting('db_table_prefix') . strtolower($this->modelName); } //cache fields parent::__construct(); //determine action if ($mixed !== null) { if (is_object($mixed) && get_class($mixed) == 'Query' && Clockwork::isModuleLoaded('Data/Query')) { if ($column == 1) { $this->values = $mixed->from(substr($this->tableName, strlen(Config::getSetting('db_table_prefix'))), null, true, true)->limit(1)->run(1); if (empty($this->values)) { $this->setError(404, 'Object not found'); } } else { $this->objects = $this->createObjects($mixed->from(substr($this->tableName, strlen(Config::getSetting('db_table_prefix'))), null, true, true)->run()); } } else { if (is_array($mixed)) { foreach ($mixed as $key => $value) { $this->set($key, $value); } } else { if (Clockwork::isModuleLoaded('Data/Query')) { $query = new Query(); $this->values = $query->from(strtolower($this->modelName))->where($column . " = '" . $mixed . "'")->run(1); if (empty($this->values)) { $this->setError(404, 'Object not found'); } } } } } }
/** * Returns an array of only the persistent fields of {@link $model} * * @param ModelObject $model The modelObject to analyze * * @return array An array with persistent field names as keys with their values */ public function modelToPersistentArray(ModelObject $model) { $persistent = array_intersect_key($model->toArray(), array_flip($model->getPersistentFields())); return $persistent; }