public function index() { $dotfile = "digraph G {\n"; foreach ($this->_get_models() as $model) { $dotfile .= $model->object_name . ' [shape="Mrecord", label=< <FONT POINT-SIZE="18.0">' . $model->object_name . '</FONT><BR ALIGN="CENTER"/>'; foreach ($model->table_columns as $column => $meta) { if (substr($column, -3) == '_id' or $column == 'id') { continue; } $dotfile .= '<FONT COLOR="darkgreen">' . $column . '</FONT> <FONT COLOR="grey">(' . $meta['type'] . ')' . '</FONT><BR ALIGN="LEFT"/>'; } $dotfile .= '>];' . "\n"; foreach ($model->has_one as $related) { $dotfile .= $model->object_name . ' -> ' . $related . ";\n"; //" [arrowhead=\"tee\"];\n"; } foreach ($model->has_many as $related) { $dotfile .= $model->object_name . ' -> ' . inflector::singular($related) . ";\n"; //" [arrowhead=\"crow\"];\n"; } } $dotfile .= '}'; return $this->_render($dotfile); foreach ($this->_get_models() as $model) { echo $model->object_name . '<br />'; foreach ($model->table_columns as $column => $meta) { echo ' : ' . $column . '<br />'; } echo '<br />'; } }
/** * Sets up foreign and through properly. * * @param string $model * @param string $column * @return void */ public function initialize($model, $column) { // Default to the name of the column if (empty($this->foreign)) { $foreign_model = inflector::singular($column); $this->foreign = $foreign_model . '.' . $foreign_model . ':primary_key'; } elseif (is_string($this->foreign) and FALSE === strpos($this->foreign, '.')) { $this->foreign = $this->foreign . '.' . $this->foreign . ':primary_key'; } // Create an array from them for easier access if (!is_array($this->foreign)) { $this->foreign = array_combine(array('model', 'field'), explode('.', $this->foreign)); } // Create the default through connection if (empty($this->through) or is_string($this->through)) { if (empty($this->through)) { // Find the join table based on the two model names pluralized, // sorted alphabetically and with an underscore separating them $through = array(inflector::plural($this->foreign['model']), inflector::plural($model)); sort($through); $this->through = implode('_', $through); } $this->through = array('model' => $this->through, 'fields' => array(inflector::singular($model) . ':foreign_key', inflector::singular($this->foreign['model']) . ':foreign_key')); } parent::initialize($model, $column); }
/** * Overload ORM::__get to support "parent" and "children" properties. * * @param string column name * @return mixed */ public function __get($column) { if ($column === 'parent') { if (empty($this->related[$column])) { // Load child model $model = ORM::factory(inflector::singular($this->children)); if (array_key_exists($this->parent_key, $this->object)) { // Find children of this parent $model->where($model->primary_key, $this->object[$this->parent_key])->find(); } $this->related[$column] = $model; } return $this->related[$column]; } elseif ($column === 'children') { if (empty($this->related[$column])) { $model = ORM::factory(inflector::singular($this->children)); if ($this->children === $this->table_name) { // Load children within this table $this->related[$column] = $model->where($this->parent_key, $this->object[$this->primary_key])->find_all(); } else { // Find first selection of children $this->related[$column] = $model->where($this->foreign_key(), $this->object[$this->primary_key])->where($this->parent_key, NULL)->find_all(); } } return $this->related[$column]; } return parent::__get($column); }
/** * Overrides the initialize to automatically provide the column name * * @param string $model * @param string $column * @return void */ public function initialize($model, $column) { // Default to the name of the column if (empty($this->foreign)) { $foreign_model = inflector::singular($column); $this->foreign = $foreign_model . '.' . $foreign_model . ':primary_key'; } elseif (FALSE === strpos($this->foreign, '.')) { $this->foreign = $this->foreign . '.' . $this->foreign . ':primary_key'; } // Split them apart $foreign = explode('.', $this->foreign); // Create an array from them $this->foreign = array('model' => $foreign[0], 'column' => $foreign[1]); // We can work with nothing passed or just a model if (empty($this->through) or is_string($this->through)) { if (empty($this->through)) { // Find the join table based on the two model names pluralized, // sorted alphabetically and with an underscore separating them $through = array(inflector::plural($this->foreign['model']), inflector::plural($model)); // Sort sort($through); // Bring them back together $this->through = implode('_', $through); } $this->through = array('model' => $this->through, 'columns' => array(inflector::singular($model) . ':foreign_key', inflector::singular($this->foreign['model']) . ':foreign_key')); } parent::initialize($model, $column); }
protected function _tables() { // Prepare an array to hold tables $tables = array(); // Create a new database table with name and database $table = new Database_Table($this->_model->table(), $this->_db); // Get the model's primary keys as an array $model_pks = is_array($this->_model->pk()) ? $this->_model->pk() : array($this->_model->pk()); // Loop through each field within the model foreach ($this->_model->fields() as $field) { // Check if the field implaments the migratable field interface if ($field instanceof Sprig_Field_Migratable) { // Loop through each column in the field foreach ($field->columns() as $column) { // Add the column to the table $table->add_column($column); } } elseif ($field->in_db) { // If the field is unique if ($field->unique) { // Add a unique constraint to the table $table->add_constraint(new Database_Constraint_Unique($field->column)); } // Loop through every column in the model foreach ($this->_columns($field, $table) as $column) { // Add the column to the table $table->add_column($column); } } elseif ($field instanceof Sprig_Field_ManyToMany) { // ManyToMany fields also contain a pivot table $pivot = new Database_Table($field->through, $this->_db); // Get the columns associated with the first half $columns = $this->_columns(new Sprig_Field_BelongsTo(array('model' => $field->model)), $pivot); // Foreach column in the first half foreach ($columns as $column) { // Add it to the pivot table $pivot->add_column($column); } // Get the columns associated with the second half $columns = $this->_columns(new Sprig_Field_BelongsTo(array('model' => inflector::singular($this->_model->table()))), $pivot); // Foreach column in the second half foreach ($columns as $column) { // Add it to the pivot table $pivot->add_column($column); } // Add a primary key constraint on all fields within the pivot table $pivot->add_constraint(new Database_Constraint_Primary(array_keys($pivot->columns()), $pivot->name)); // Add the pivot table to the list of tables $tables[] = $pivot; } } // Add the primary key constraints to the table $table->add_constraint(new Database_Constraint_Primary($model_pks, $table->name)); // Add the table to the list $tables[] = $table; // And return all tables. return $tables; }
/** * Retrieve the list of synonyms using a meaning id. * @param string $meaning_field Name of the meaning field, either taxon_meaning_id or meaning_id. * @param int $meaning_id Id value of the meaning to search for * @param boolean $within_list Search within the current list only (true=default) or * across all lists (false). * @return ORM_Iterator List of synonyms */ public function getSynonomy($meaning_field, $meaning_id, $within_list = true) { $filters = array('preferred' => 'f', 'deleted' => 'f', $meaning_field => $meaning_id); if ($within_list) { $list_id_field = $this->list_id_field; $filters[$list_id_field] = $this->{$list_id_field}; } return ORM::factory(inflector::singular($this->table_name))->where($filters)->find_all(); }
public function tool() { if (empty($_GET['tool_id'])) { die('invalid tool_id'); } $tool_id = valid::id_key($_GET['tool_id']); $tool = ORM::factory('tool', $tool_id); if (!$tool->loaded) { die('invalid tool'); } $toolname = strtolower($tool->system_tool->name); # load the tool parent $parent = ORM::factory($toolname, $tool->parent_id); if (!$parent->loaded) { die('invalid parent table'); } # build the object. $export = new stdClass(); $export->name = $toolname; # export the parent table. $parent_table = new stdClass(); foreach ($parent->table_columns as $key => $value) { $parent_table->{$key} = $parent->{$key}; } $export->parent_table = $parent_table; # export any child tables. $child_tables = new stdClass(); # loop through data from available child tables. foreach ($parent->has_many as $table_name) { $table_name = inflector::singular($table_name); $child_tables->{$table_name} = array(); # get the child table model so we can iterate through the fields. $table = ORM::factory($table_name); # get any rows beloning to the parent. $rows = ORM::factory($table_name)->where(array('fk_site' => $this->site_id, "{$toolname}_id" => $parent->id))->find_all(); foreach ($rows as $row) { $object = new stdClass(); foreach ($table->table_columns as $key => $value) { $object->{$key} = $row->{$key}; } array_push($child_tables->{$table_name}, $object); } } $export->child_tables = $child_tables; # get the css file. $export->css = file_get_contents($this->assets->themes_dir("{$this->theme}/tools/{$toolname}/_created/{$parent->id}/{$parent->type}_{$parent->view}.css")); $json = json_encode($export); echo '<h2>Copy this exactly and place into the importer=)</h2>'; echo "<textarea style='width:99%;height:400px;'>{$json}</textarea>"; die; echo kohana::debug($export); die; # just testing ... echo self::import($json); die; }
/** * Convert an ordered array to XML * Wraps each element in the singular form of the root element * * @param array ordered array * @param string root element, plural * @return string XML String */ public static function ordered2xml($ordered, $root_element = "elements") { if (!is_array($ordered) || count($ordered) == 0) { return "<" . $root_element . " />\n"; } foreach ($ordered as $element) { $xml .= xml::assoc2xml($element, inflector::singular($root_element)); } return "<" . $root_element . ">\n" . xml::pad($xml) . "</" . $root_element . ">\n"; }
/** * Determines the actual foreign model and field that the * relationship is tied to. * * @param string $model * @param string $column * @return void */ public function initialize($model, $column) { parent::initialize($model, $column); // Empty? The model defaults to the the singularized name // of this field, and the field defaults to this field's model's foreign key if (empty($this->foreign)) { $this->foreign = inflector::singular($this->name) . '.' . $model . ':foreign_key'; } elseif (FALSE === strpos($this->foreign, '.')) { $this->foreign = $this->foreign . '.' . $model . ':foreign_key'; } // Create an array fo easier access to the separate parts $this->foreign = array_combine(array('model', 'field'), explode('.', $this->foreign)); }
/** * Overrides the initialize to automatically provide the column name * * @param string $model * @param string $column * @return void */ public function initialize($model, $column) { // Default to the name of the column if (empty($this->foreign)) { $this->foreign = inflector::singular($column) . '.' . $model . ':foreign_key'; } elseif (FALSE === strpos($this->foreign, '.')) { $this->foreign = $this->foreign . '.' . $model . ':foreign_key'; } // Split them apart $foreign = explode('.', $this->foreign); // Create an array from them $this->foreign = array('model' => $foreign[0], 'column' => $foreign[1]); parent::initialize($model, $column); }
public function __construct() { parent::__construct(); if (in_array(Router::$method, $this->excluded_actions)) { Event::run('system.404'); } if (!$this->model_name) { $this->model_name = inflector::singular(Router::$controller); } $this->plural = inflector::plural($this->model_name); $this->directory = $this->base_route . $this->plural; // Do not render, by default, if the request is an ajax request if (request::is_ajax()) { $this->auto_render = false; } }
function __construct($config = array()) { parent::__construct(); $name = Uri::instance()->segment(1, Kohana::config('controls.default_controller')); $setup = Kohana::config('controls.' . $name); if ($setup != false) { $this->setup = $setup; } else { $this->setup = array(); $this->setup['name'] = $name; $this->setup['model'] = inflector::singular($name); $this->setup['table'] = $name; $this->setup['view'] = 'standard'; } $this->name = $this->setup['name']; $this->set_listeners(); }
/** * return list * * @param object $lc * @return array * @author Andy Bennett */ public function get_list() { $data = array('action' => 'list', 'name' => $this->name, 'role' => User::instance()->get_role()); Event::run('steamcore.aclcheck', $data); $tdata = array(); $limit = 10; $page_num = $this->input->get('page', 1); $offset = ($page_num - 1) * $limit; $model = ORM::factory($this->setup['model'], null); $role = inflector::singular(URI::instance()->segment(1)); $data['query'] = $model->where('role', $role)->find_all($limit, $offset); $data['controller'] = Kohana::instance()->uri->segment(1); $data['pagination'] = Pagination::factory(array('style' => 'digg', 'items_per_page' => $limit, 'query_string' => 'page', 'total_items' => $model->count_last_query())); // merge any passed data and the data returned from the model $tdata = array_merge($tdata, $data); // return the result return $tdata; }
public function find_related($key) { $model = ORM::factory(inflector::singular($key)); // Load the "end" model if (in_array($key, $this->has_one)) { // one<>many relationship return $model->where($model->primary_key, $this->object[$this->foreign_key($model->object_name)]); } else { // many<>many relationship $through = ORM::factory(inflector::singular($this->has_many[$key])); // Load the "middle" model // Join ON target model's primary key set to 'through' model's foreign key // User-defined foreign keys must be defined in the 'through' model $join_table = $through->table_name; $join_col1 = $through->foreign_key($model->object_name, $join_table); $join_col2 = $model->table_name . '.' . $model->primary_key; return $model->join($join_table, $join_col1, $join_col2)->where($through->foreign_key($this->object_name, $join_table), $this->object[$this->primary_key]); } }
/** * Creates checkboxes list * * @param string $name input name * @param array $data array of checkboxes * @param array $values checked values * @param string $label * @param string|array $error * @param string|array $tip * @return string */ public static function checkboxes_wrap($name, $data = array(), $values = array(), $label = '', $error = '', $class = '', $tip = '') { // Get checkboxes $checkboxes = isset($data[$name]) ? $data[$name] : $data; if (!empty($checkboxes)) { // Create internal id $singular = inflector::singular($name) . '_'; // Get values $values = array_key_exists($name, $values) ? $values[$name] : $values; $input = empty($class) ? "<ul>\n" : '<ul class="' . $class . "\">\n"; foreach ($checkboxes as $checkbox_id => $checkbox_name) { $internal_id = $singular . $checkbox_id; $input .= '<li>'; $input .= form::checkbox(array('id' => $internal_id, 'name' => $name . '[' . $checkbox_id . ']'), 1, isset($values[$checkbox_id])); $input .= form::label($internal_id, $checkbox_name); $input .= "</li>\n"; } $input .= "</ul>\n"; return form::wrap($input, $name, $label, $error, $tip); } }
public function select_list($key, $display, $order_by = 'id', $where = array()) { $rows = array(); $query = empty($where) ? $this->fetch_all($order_by) : $this->db->where($where)->orderby($order_by)->get($this->table_name)->result(TRUE, inflector::singular(ucwords($this->table_name)) . '_Model'); $array_display = is_array($display); foreach ($query as $row) { if ($array_display) { $display_str = array(); foreach ($display as $text) { $display_str[] = $row->{$text}; } $rows[$row->{$key}] = implode(' - ', $display_str); } else { $rows[$row->{$key}] = $row->{$display}; } } return $rows; }
/** * Determines the name of a foreign key for a specific table. * * @param string related table name * @param string prefix table name (used for JOINs) * @return string */ public function foreign_key($table = NULL, $prefix_table = NULL) { if ($table === TRUE) { // Return the name of this tables PK return $this->table_name . '.' . $this->primary_key; } if (is_string($prefix_table)) { // Add a period for prefix_table.column support $prefix_table .= '.'; } if (!is_string($table) or !isset($this->object[$table . '_' . $this->primary_key])) { // Use this table $table = $this->table_name; if ($this->table_names_plural === TRUE) { // Make the key name singular $table = inflector::singular($table); } } return $prefix_table . $table . '_' . $this->primary_key; }
function __construct($id = null) { parent::__construct($id); $this->model_name = inflector::singular($this->table_name); }
?> " width="100" alt="<?php echo $products->meta_title; ?> "/> <div class="product-details"> <h3 class="curly"><?php echo $products->name; ?> </h3> <p><?php echo $products->short_description; ?> </p> <span class="price">Starting at <?php echo money_format('%.2n', ORM::factory('product', $products->id)->getPriceStartingAt()) . ' per ' . inflector::singular($products->unit); ?> </span> </div><!-- product-deatils --> <a href="/products/show/<?php echo $products->title_url; ?> " class="link-btn"><img src="/env/images/<?php echo My_Template_Controller::getViewPrefix(); ?> /get_started_button.png" alt="Get Started" /></a> </div><!-- box --> <?php } ?> <div class="box grey-border left grey-corner product-box-standard">
/** * Edit a $this->model_name * @Developer brandon * @Date Apr 21, 2010 */ public function edit($id = NULL) { $item = ORM::factory($this->model_name, (string) $id); $form = Formo::factory()->plugin('orm')->plugin('habtm')->orm($this->model_name, $id)->set('action', $item->update_path()); // Add the related objects if ($this->show_habtm) { foreach ($item->has_and_belongs_to_many as $relationship) { $form->habtm($this->model_name, inflector::singular($relationship)); } } // Add the submit button $form->add('submit'); if (!$item->loaded) { flash::set_error('The ' . format::friendly_model_name($this->model_name) . ' you were looking for was not found'); url::redirect($this->directory . '/index'); } $this->template->set('content', View::factory($this->directory . '/edit')->set('form', $form)->set($this->model_name, $item)); }
/** * Determines the name of a foreign key for a specific table. * * @param string related table name * @param string prefix table name (used for JOINs) * @return string */ public function foreign_key($table = NULL, $prefix_table = NULL) { if ($table === TRUE) { if (is_string($prefix_table)) { // Use prefix table name and this table's PK return $prefix_table . '.' . $this->primary_key; } else { // Return the name of this table's PK return $this->table_name . '.' . $this->primary_key; } } if (is_string($prefix_table)) { // Add a period for prefix_table.column support $prefix_table .= '.'; } if (isset($this->foreign_key[$table])) { // Use the defined foreign key name, no magic here! $foreign_key = $this->foreign_key[$table]; } else { if (!is_string($table) or !array_key_exists($table . '_' . $this->primary_key, $this->object)) { // Use this table $table = $this->table_name; if (strpos($table, '.') !== FALSE) { // Hack around support for PostgreSQL schemas list($schema, $table) = explode('.', $table, 2); } if ($this->table_names_plural === TRUE) { // Make the key name singular $table = inflector::singular($table); } } $foreign_key = $table . '_' . $this->primary_key; } return $prefix_table . $foreign_key; }
/** * Returns time difference between two timestamps, in the format: * N year, N months, N weeks, N days, N hours, N minutes, and N seconds ago * * @param integer timestamp * @param integer timestamp, defaults to the current time * @param string formatting string * @return string */ public static function timespan_string($time1, $time2 = NULL, $output = 'years,months,weeks,days,hours,minutes,seconds') { if ($difference = date::timespan($time1, $time2, $output) and is_array($difference)) { // Determine the key of the last item in the array $last = end($difference); $last = key($difference); $span = array(); foreach ($difference as $name => $amount) { if ($name !== $last and $amount === 0) { // Skip empty amounts continue; } // Add the amount to the span $span[] = ($name === $last ? ' and ' : ', ') . $amount . ' ' . ($amount === 1 ? inflector::singular($name) : $name); } // Replace difference by making the span into a string $difference = trim(implode('', $span), ','); } elseif (is_int($difference)) { // Single-value return $difference = $difference . ' ' . ($difference === 1 ? inflector::singular($output) : $output); } return $difference; }
/** * Determines the name of a foreign key for a specific table. * * @param string related table name * @param string prefix table name (used for JOINs) * @return string */ public function foreign_key($table = nil, $prefix_table = nil) { if ($table === YES) { // Return the name of this tables PK return $this->table_name . '.' . $this->primary_key; } if (is_string($prefix_table)) { // Add a period for prefix_table.column support $prefix_table .= '.'; } if (isset($this->foreign_key[$table])) { // Use the defined foreign key name, no magic here! $foreign_key = $this->foreign_key[$table]; } else { if (!is_string($table) or !isset($this->object[$table . '_' . $this->primary_key])) { // Use this table $table = $this->table_name; if ($this->table_names_plural === YES) { // Make the key name singular $table = inflector::singular($table); } } $foreign_key = $table . '_' . $this->primary_key; } return $prefix_table . $foreign_key; }
public function __call($method, $args) { $this->auto_render = FALSE; if (array_key_exists(0, $args)) { $action = $args[0]; } else { $action = 'view'; } if (array_key_exists(1, $args)) { $id = $args[1]; } else { $id = 0; } $model_name = inflector::singular($method); if ($this->input->post('action')) { $action = $this->input->post('action'); } if ($this->input->post('perform')) { $perform = $this->input->post('perform'); } if ($action == 'multiple') { $this->save_multiple_from_files($model_name); try { $list_view = new View('admin/' . $method); } catch (Kohana_Exception $e) { $list_view = new View('admin/content'); } $view = $list_view; $item = ORM::factory($model_name); $items = $item->find_all(); $view->set('item', $item); $view->set('items', $items); $this->template->content = $view->render(TRUE); return; } if ($action == 'single') { $this->save_file_only($model_name); try { $list_view = new View('admin/' . $method); } catch (Kohana_Exception $e) { $list_view = new View('admin/content'); } $view = $list_view; $item = ORM::factory($model_name); $items = $item->find_all(); $view->set('item', $item); $view->set('items', $items); $this->template->content = $view->render(TRUE); return; } if ($action == 'action' or !empty($perform)) { $method_to_perform = $id; if ($perform) { $method_to_perform = $perform; } if (empty($id) and array_key_exists(2, $args)) { $id = $args[2]; } if ($method_to_perform and $id) { $item = ORM::factory($model_name, $id); if (method_exists($item, $method_to_perform)) { $post_data = $this->input->post(); $item->{$method_to_perform}($post_data); } } return; } if ($id) { $item = ORM::factory($model_name, $id); try { $list_view = new View('admin/' . $method); } catch (Kohana_Exception $e) { $list_view = new View('admin/content'); } switch ($action) { case 'edit': try { $view = new View('admin/form/' . $method); } catch (Kohana_Exception $e) { $view = new View('admin/form'); } $view->set('item', $item); break; case 'save': $this->save($model_name, $id); $items = ORM::factory($model_name)->find_all(); $list_view->set('item', $item); $list_view->set('items', $items); $view = $list_view; break; case 'delete': $item = ORM::factory($model_name, $id); $item->delete(); $item = ORM::factory($model_name); $items = $item->find_all(); $list_view->set('item', $item); $list_view->set('items', $items); $view = $list_view; break; } } else { try { $view = new View('admin/' . $method); } catch (Kohana_Exception $e) { $view = new View('admin/content'); } switch ($action) { case 'save': $this->create($model_name); $item = ORM::factory($model_name); $items = $item->find_all(); $view->set('item', $item); $view->set('items', $items); break; case 'add': try { $view = new View('admin/form/' . $method); } catch (Kohana_Exception $e) { $view = new View('admin/form'); } $item = ORM::factory($model_name); $view->set('item', $item); break; default: $item = ORM::factory($model_name); $items = $item->find_all(); $view->set('item', $item); $view->set('items', $items); } } $this->template->content = $view->render(TRUE); }
/** * Returns an array of all values from this model and its super models ready to be * loaded into a form. */ protected function getModelValues() { $struct = $this->model->get_submission_structure(); // Get this model's values. If the structure needs a specified field prefix then use it, otherwise it will default to the model name. $r = $this->model->getPrefixedValuesArray(array_key_exists('fieldPrefix', $struct) ? $struct['fieldPrefix'] : null); if (array_key_exists('superModels', $struct)) { foreach ($struct['superModels'] as $super => $content) { // Merge the supermodel's values into the main array. Use a specified fieldPrefix if there is one. $r = array_merge($r, $this->model->{$super}->getPrefixedValuesArray(array_key_exists('fieldPrefix', $content) ? $content['fieldPrefix'] : null)); } } // Output a list of values for each joined record in the joinsTo links. if (array_key_exists('joinsTo', $struct)) { foreach ($struct['joinsTo'] as $joinsTo) { $ids = array(); foreach ($this->model->{$joinsTo} as $joinedModel) { $r['joinsTo:' . inflector::singular($joinsTo) . ':' . $joinedModel->id] = 'on'; } } } return $r; }
?> " width="100" alt="<?php echo $product->products_description->image_alt; ?> " /> <div class="product-details"> <h3 class="curly"><?php echo $product->name; ?> </h3> <p><?php echo $product->products_description->short_description; ?> </p> <span class="price">Starting at <?php echo money_format('%.2n', $product->getPriceStartingAt()) . ' per ' . inflector::singular($product->unit); ?> </span> <a href="/products/show/<?php echo $product->products_description->title_url; ?> "><img src="/env/images/<?php echo My_Template_Controller::getViewPrefix(); ?> /get_started_button.png" /></a> </div><!-- product-deatils --> </div><!-- box --> <?php } else { ?> <div class="box grey-border left grey-corner">
/** * Converts any fk_* fields in a save array into the fkFields structure ready to be looked up. * [occ|smp|loc|srv|psn]Attr:fk_* are looked up in createAttributeRecord() * * @param $submission array Submission containing the foreign key field definitions to convert * @param $saveArray array Original form data being wrapped, which can contain filters to operate against the lookup table * of the form fkFilter:table:field=value. * @return array The submission structure containing the fkFields element. */ private function getFkFields($submission, $saveArray) { foreach ($submission['fields'] as $field => $value) { if (substr($field, 0, 3) == 'fk_') { // This field is a fk_* field which contains the text caption of a record which we need to lookup. // First work out the model to lookup against. The format is fk_{fieldname}(:{search field override})? $fieldTokens = explode(':', substr($field, 3)); $fieldName = $fieldTokens[0]; if (array_key_exists($fieldName, $this->belongs_to)) { $fkTable = $this->belongs_to[$fieldName]; } elseif ($this instanceof ORM_Tree && $fieldName == 'parent') { $fkTable = inflector::singular($this->getChildren()); } else { $fkTable = $fieldName; } // Create model without initialising, so we can just check the lookup variables kohana::log('debug', $fkTable); $fkModel = ORM::Factory($fkTable, -1); // allow the linked lookup field to override the default model search field if (count($fieldTokens) > 1) { $fkModel->search_field = $fieldTokens[1]; } // let the model map the lookup against a view if necessary $lookupAgainst = isset($fkModel->lookup_against) ? $fkModel->lookup_against : $fkTable; // Generate a foreign key instance $submission['fkFields'][$field] = array('fkIdField' => "{$fieldName}" . "_id", 'fkTable' => $lookupAgainst, 'fkSearchField' => $fkModel->search_field, 'fkSearchValue' => trim($value['value']), 'readableTableName' => ucfirst(preg_replace('/[\\s_]+/', ' ', $fkTable))); // if the save array defines a filter against the lookup table then also store that. E.g. // a search in the taxa_taxon_list table may want to filter by the taxon list. This is done // by adding a value such as fkFilter:taxa_taxon_list:taxon_list_id=2. // Search through the save array for a filter value foreach ($saveArray as $filterfield => $filtervalue) { if (substr($filterfield, 0, strlen("fkFilter:{$fkTable}:")) == "fkFilter:{$fkTable}:") { // found a filter for this fkTable. So extract the field name as the 3rd part $arr = explode(':', $filterfield); $submission['fkFields'][$field]['fkSearchFilterField'] = $arr[2]; // and remember the value $submission['fkFields'][$field]['fkSearchFilterValue'] = $filtervalue; } } } } return $submission; }
private function setMergeTabColumn($name, $tablename, $separator, $where = '', $display = '') { // in this case the data for the column in merged into one, if there are more than one records // To do this we highjack the attribute handling functionality. $tableKey = inflector::singular($this->tables[$this->tableIndex]['tablename']) . '_id'; $thisDefn = new stdClass(); $thisDefn->caption = 'caption'; $thisDefn->main_id = $tableKey; // main_id is the name of the column in the subquery holding the PK value of the parent table. $thisDefn->parentKey = "lt" . $this->tableIndex . "_id"; // parentKey holds the column in the main query to compare the main_id against. $thisDefn->id = 'id'; // id is the name of the column in the subquery holding the attribute id. $thisDefn->separator = $separator; $thisDefn->hideVagueDateFields = 'false'; $thisDefn->columnPrefix = 'merge_' . count($this->attributes); if ($display == '') { $display = $tablename . ' ' . $name; } $thisDefn->query = "SELECT " . $tableKey . ", '" . $display . "' as caption, '' as id, 'T' as data_type, " . $name . " as text_value, 't' as multi_value FROM " . $tablename . ($where == '' ? '' : " WHERE " . $where); $this->attributes[] = $thisDefn; // Make sure id column of parent table is in list of columns returned from query. $this->mergeTabColumn('id', '', '', '', '', 'false', true); }
/** * Find a resource file in a given directory. Files will be located according * to the order of the include paths. Configuration and i18n files will be * returned in reverse order. * * @throws Kohana_Exception if file is required and not found * @param string directory to search in * @param string filename to look for (including extension only if 4th parameter is TRUE) * @param boolean file required * @param string file extension * @return array if the type is config, i18n or l10n * @return string if the file is found * @return FALSE if the file is not found */ public static function find_file($directory, $filename, $required = FALSE, $ext = FALSE) { // NOTE: This test MUST be not be a strict comparison (===), or empty // extensions will be allowed! if ($ext == '') { // Use the default extension $ext = EXT; } else { // Add a period before the extension $ext = '.' . $ext; } // Search path $search = $directory . '/' . $filename . $ext; if (isset(self::$internal_cache['find_file_paths'][$search])) { return self::$internal_cache['find_file_paths'][$search]; } // Load include paths $paths = self::$include_paths; // Nothing found, yet $found = NULL; if ($directory === 'config' or $directory === 'i18n') { // Search in reverse, for merging $paths = array_reverse($paths); foreach ($paths as $path) { if (is_file($path . $search)) { // A matching file has been found $found[] = $path . $search; } } } else { foreach ($paths as $path) { if (is_file($path . $search)) { // A matching file has been found $found = $path . $search; // Stop searching break; } } } if ($found === NULL) { if ($required === TRUE) { // Directory i18n key $directory = 'core.' . inflector::singular($directory); // If the file is required, throw an exception throw new Kohana_Exception('core.resource_not_found', self::lang($directory), $filename); } else { // Nothing was found, return FALSE $found = FALSE; } } if (!isset(self::$write_cache['find_file_paths'])) { // Write cache at shutdown self::$write_cache['find_file_paths'] = TRUE; } return self::$internal_cache['find_file_paths'][$search] = $found; }
if (is_file(APPPATH . 'scaffold/' . $model_name . '/_edit' . EXT)) { include APPPATH . 'scaffold/' . $model_name . '/_edit' . EXT; } echo form::submit('', 'ok'); echo form::close(); ?> </fieldset> <br /> <fieldset> <legend>Relationships</legend> <?php foreach ($model->has_many as $c) { ?> <a href="javascript:void(relationship('<?php echo inflector::singular($c); ?> ', <?php echo $model->id; ?> , '<?php echo $model->object_name; ?> '))"><?php echo $c . ' (' . count($model->{$c}) . ')'; ?> </a> <?php } ?> </fieldset>