public static function slimSetup(\Slim\Slim &$slim, One_Scheme $scheme) { //TODO: read specs from behaviour options or from a file $opt = $scheme->get('behaviorOptions.restable'); $route = $opt['route']; // retrieve $slim->get("/{$route}", function () use($scheme) { One_Controller_Rest::restGetAll($scheme); }); // retrieve one $slim->get("/{$route}/:idOrAlias", function ($idOrAlias) use($scheme) { One_Controller_Rest::restGet($scheme, $idOrAlias); }); // create new $slim->post("/{$route}", function () use($scheme) { One_Controller_Rest::restPost($scheme); }); // update existing $slim->put("/{$route}/:idOrAlias", function ($idOrAlias) use($scheme) { One_Controller_Rest::restPut($scheme, $idOrAlias); }); // delete existing $slim->delete("/{$route}/:idOrAlias", function ($idOrAlias) use($scheme) { One_Controller_Rest::restDelete($scheme, $idOrAlias); }); }
/** * Adds the fields that are searchable to the scheme on loading of the scheme * * @param One_Scheme $scheme */ public function onLoadScheme($scheme) { $options = $scheme->get('behaviorOptions.searchable'); if (is_null($options['search']) || trim($options['search']) == '') { throw new One_Exception('When defining a searchable behavior, you must define an attribute.'); } $scheme->oneSearchableFields = explode(':', $options['search']); }
/** * Returns the class that overrides the default One_Model * * @param One_Scheme $scheme * @return One_Model */ public function onCreateModel(One_Scheme $scheme) { $options = $scheme->get('behaviorOptions.class'); $className = $options['className']; if (!$className) { $className = 'One_Model_' . ucFirst($scheme->getName()); } return new $className($scheme); }
/** * Convert all the excessive data into a json string * * @param One_Scheme $scheme * @param One_Model $model */ private function flexToJson(One_Scheme $scheme, One_Model $model) { $ignoreAttributes = array_merge(self::$_ignoreAttributes, $scheme->getForeignKeys()); // ignore all local foreign key attributes as well $bOptions = $scheme->get('behaviorOptions.' . strtolower($this->getName())); $flexfield = $bOptions['flexfield']; unset($model->{$flexfield}); // Flexfields should not be set manually // // only auto-set the flexfield, if it's not in the modified fields // // if it is set in the modified fields, then the flex field was intentionally set manually // if(!array_key_exists($flexfield, $model->getModified())) // { $data = $model->toArray(); $attributes = $scheme->get('attributes'); foreach ($attributes as $attr) { unset($data[$attr->getName()]); } foreach ($ignoreAttributes as $ignore) { unset($data[$ignore]); } $model->{$flexfield} = json_encode($data); // } }
/** * Deletes children of a certain model on deletion of the parent model * * @param One_Scheme $scheme * @param One_Model $model */ public function beforeDeleteModel(One_Scheme $scheme, One_Model $model) { $options = $scheme->get('behaviorOptions.deletechildren'); $dependent = explode(';', $options['dependent']); $cascade = explode(';', $options['cascade']); $dependentLeft = array(); if (count($dependent) > 0) { foreach ($dependent as $depends) { if (trim($depends) != '') { $related = $model->getRelated(trim($depends)); if (count($related) > 0) { $dependentLeft[] = trim($depends); break; } } } } if (count($dependentLeft) > 0) { throw new One_Exception('You can not delete this item untill all items of "' . implode('", "', $dependentLeft) . '" have been deleted.'); return false; } else { $tbds = array_merge($dependent, $cascade); if (count($tbds) > 0) { foreach ($tbds as $tbd) { if (trim($tbd) != '') { $related = $model->getRelated($tbd); if (count($related) > 0) { foreach ($related as $relation) { $relation->delete(); } } } } } } return true; }
/** * When the model is loaded, add a slug-field to the model that is composed of the specified fields * * @param One_Scheme $scheme * @param One_Model $model */ public function afterLoadModel(One_Scheme $scheme, One_Model $model) { if (null !== $scheme->getAttribute('slug')) { // don't create the slug if the attribute "slug" actually exists return; } $options = $scheme->get('behaviorOptions.slug'); $createFrom = $options['createFrom']; $parts = preg_split('/\\+/', $createFrom); $mangled = array(); foreach ($parts as $part) { if (preg_match('/^([a-z0-9\\_\\-]+):([a-z0-9\\_\\-]+)$/', $part, $matches) > 0) { $scheme = $model->getScheme(); $link = $scheme->getLink($matches[1]); if (!is_null($link)) { if ($link->getAdapterType() == 'manytoone') { $related = $model->getRelated($matches[1]); if (!is_null($related)) { $targetPart = $matches[2]; $mangle = !is_null($related->{$targetPart}) ? trim($this->mangle($related->{$targetPart})) : NULL; if (!is_null($mangle)) { $mangled[] = $mangle; } } } } } else { $mangle = !is_null($model->{$part}) ? trim($this->mangle($model->{$part})) : NULL; if (!is_null($mangle)) { $mangled[] = $mangle; } } } if (count($mangled) > 0) { $model->slug = implode('_', $mangled); } }
/** * Unset the attribute so that it is never inserted or updated * @param One_Scheme $scheme * @param One_Model $model */ protected function unsetAttribute(One_Scheme $scheme, One_Model $model) { $bOptions = $scheme->get('behaviorOptions.' . strtolower($this->getName())); $forAttribute = $bOptions['attribute']; unset($model->{$forAttribute}); }