public final function __construct($name, $foreign_table, $foreign_key, $key, moojon_base_column $column) { $this->name = $name; $this->foreign_class = moojon_inflect::singularize($foreign_table); $this->foreign_table = moojon_inflect::pluralize($foreign_table); $this->foreign_key = $foreign_key; $this->key = $key; $this->column = $column; }
function get_primary_key_id_property(moojon_base_model $model) { $resource = moojon_inflect::pluralize(get_class($model)); if (moojon_routes::has_rest_route($resource)) { $rest_route = moojon_routes::get_rest_route($resource); return $rest_route->get_id_property(); } else { return moojon_primary_key::NAME; } }
protected function init() { $this->actions = array_key_exists('actions', $this->params) ? $this->params['actions'] : array('index', '_new', 'show', 'edit', 'delete', 'update', 'create', 'destroy'); $this->resource = moojon_inflect::pluralize($this->pattern); $this->params['resource'] = $this->resource; $this->app = array_key_exists('app', $this->params) ? $this->params['app'] : moojon_config::get_or_null('default_app'); $this->params['app'] = $this->app; $this->params['controller'] = array_key_exists('controller', $this->params) ? $this->params['controller'] : $this->resource; $this->id_property = array_key_exists('id_property', $this->params) ? $this->params['id_property'] : moojon_primary_key::NAME; $this->method = strtolower(moojon_server::method()); $this->custom_collection_routes = array_key_exists('custom_collection_routes', $this->params) ? $this->params['custom_collection_routes'] : array(); $this->custom_member_routes = array_key_exists('custom_member_routes', $this->params) ? $this->params['custom_member_routes'] : array(); $model_class = moojon_inflect::singularize($this->pattern); $model = new $model_class(); $this->relationship_routes = $model->get_relationship_names(); }
public static function scaffold($app, $model, $controller) { self::try_define('APP', $app); self::try_define('CONTROLLER', $controller); if (moojon_routes::has_rest_route($model)) { $route = moojon_routes::get_rest_route($model); if (APP != $route->get_app()) { throw new moojon_exception("Scaffold app & route app must be the same (" . APP . ' != ' . $route->get_app() . ")"); } $id_property = $route->get_id_property(); } else { $id_property = moojon_primary_key::NAME; self::add_route("new moojon_rest_route('{$model}', array('app' => '" . APP . "')),"); } $swaps = array(); $swaps['plural'] = moojon_inflect::pluralize($model); $swaps['singular'] = moojon_inflect::singularize($model); $swaps['Human'] = str_replace('_', ' ', ucfirst(moojon_inflect::singularize($model))); $swaps['human'] = str_replace('_', ' ', moojon_inflect::singularize($model)); $swaps['Humans'] = str_replace('_', ' ', ucfirst(moojon_inflect::pluralize($model))); $swaps['humans'] = str_replace('_', ' ', moojon_inflect::pluralize($model)); $swaps['id_property'] = $id_property; $views_path = moojon_paths::get_project_views_app_controller_directory(APP, CONTROLLER); moojon_files::attempt_mkdir($views_path); self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'controller.template', moojon_paths::get_project_controllers_app_directory(APP, CONTROLLER) . "{$controller}.controller.class.php", $swaps, false, true); self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . '_form.template', $views_path . '_form.php', $swaps, false, true); self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'new.view.template', $views_path . 'new.view.php', $swaps, false, true); self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'delete.view.template', $views_path . 'delete.view.php', $swaps, false, true); self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'index.view.template', $views_path . 'index.view.php', $swaps, false, true); self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'show.view.template', $views_path . 'show.view.php', $swaps, false, true); self::run(moojon_paths::get_moojon_templates_scaffolds_directory() . 'edit.view.template', $views_path . 'edit.view.php', $swaps, false, true); }
public static function get_relationship_object_where(moojon_base_relationship $relationship, moojon_base_model $accessor) { $key = $relationship->get_key(); switch (get_class($relationship)) { case 'moojon_has_one_relationship': $foreign_table = $relationship->get_foreign_table(); $foreign_key = $relationship->get_foreign_key(); $return = "`{$foreign_table}`.`{$key}` = :{$foreign_key}"; break; case 'moojon_has_many_relationship': $foreign_table = $relationship->get_foreign_table(); $foreign_key = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$foreign_table}`.`{$foreign_key}` = :{$key}"; break; case 'moojon_has_many_to_many_relationship': $foreign_table = moojon_inflect::pluralize($relationship->get_class($accessor)); $foreign_key1 = moojon_primary_key::get_foreign_key($relationship->get_foreign_table()); $foreign_key2 = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$key}` IN (SELECT `{$foreign_key1}` FROM `{$foreign_table}` WHERE `{$foreign_key2}` = :{$key})"; break; case 'moojon_belongs_to_relationship': $foreign_key = moojon_primary_key::get_foreign_key(get_class($accessor)); $return = "`{$key}` = :{$foreign_key}"; break; } return $return; }
protected final function add_relationship($relationship_type, $name, $foreign_table, $foreign_key, $key) { if ($this->has_property($name)) { throw new moojon_exception("Duplicate property when adding relationship ({$name})"); } $key = $key ? $key : moojon_primary_key::NAME; if (!$this->has_column($key)) { throw new moojon_exception("no such column to use as key for relationship ({$key})"); } $foreign_table = $foreign_table ? $foreign_table : moojon_inflect::pluralize($name); $foreign_table = self::strip_base($foreign_table); $foreign_key = $foreign_key ? $foreign_key : moojon_primary_key::get_foreign_key($foreign_table); $this->relationships[$name] = new $relationship_type($name, $foreign_table, $foreign_key, $key, $this->get_column($key)); }
function get_collection_rest_route($model) { $resource = is_subclass_of($model, 'moojon_base_model') ? moojon_inflect::pluralize(get_class($model)) : $model; return moojon_routes::get_rest_route($resource); }
public static function get_model_upload_directory(moojon_base_model $model, $public = false) { $class = moojon_inflect::pluralize(get_class($model)); $id_column = moojon_primary_key::NAME; $root = !$public ? self::get_uploads_directory() : self::get_public_upload_path(); return "{$root}{$class}/" . $model->{$id_column} . '/'; }
public static function get_table($foreign_key) { return moojon_inflect::pluralize(self::get_class($foreign_key)); }