static function AddRelated($relation, &$objects = array()) { // set defaults switch ($relation) { case 'attached': case 'children': case 'parent': break; default: $relation = 'attached'; break; } $get_related = "get_{$relation}"; $find_related = "find_{$relation}"; // get related foreach ($objects as $key => $object) { foreach ($object->{$get_related} as $related_name => $related_params) { AppModel::RelationNameParams($related_name, $related_params); // d($find_related.': '.$related_name.' '.print_r($related_params, true)); $model = Globe::Init($related_name, 'model'); $strForeignKey = array_get($related_params, 'foreignKey', null); // TODO hook in 'where' from behavior: $strWhere = null; $array = $relation == 'parent' ? array($related_name, $strForeignKey) : array($related_name, $strWhere, $model->order_by, 10000, 0, $strForeignKey); $objects[$key]->{$related_name} = call_user_func_array(array($object, $find_related), $array); } } return $objects; }
function smarty_function_admin_relations($params = array(), &$smarty) { require_once $smarty->_get_plugin_filepath('function', 'admin_link'); $relations = array('parent' => 'edit ', 'children' => 'add/edit '); $object = null; $wrap = false; $links = array(); $output = ''; foreach ($params as $_key => $_value) { ${$_key} = $_value; } if (empty($object)) { $smarty->trigger_error("admin_relations: missing 'object' parameter", E_USER_NOTICE); return; } // cycle relations foreach ($relations as $relation_name => $relation_prefix) { // cycle object's 'get_' variables $i = 0; foreach ($object->{'get_' . $relation_name} as $model_name => $model_params) { AppModel::RelationNameParams($model_name, $model_params); // get controller $controller = Globe::Init($model_name, 'controller'); // TODO replace by ::singleton, find others // action & text switch ($relation_name) { case 'parent': $action = ($model = object_get($object, $model_name)) ? 'edit' . DS . $model->id : null; $text = ucwords(AppInflector::titleize($model_name)); $image = 'page_white_edit'; break; case 'children': $prefix = $i == 0 ? '' : AppInflector::tableize(get_class($object)) . '_id='; $action = '?filter=' . $prefix . $object->id; $text = ucwords(AppInflector::pluralize(AppInflector::titleize($model_name))); $image = 'magnifier'; break; default: $action = ''; $text = AppInflector::titleize($model_name); $image = 'magnifier'; break; } // build link $links[] = smarty_function_admin_link(array('controller' => AppInflector::fileize(get_class($controller), 'controller'), 'action' => $action, 'text' => "<span>{$relation_prefix}{$text}</span>" . ' <img src="/assets/images/admin/silk/' . $image . '.png" width="16" height="16">'), $smarty); $i++; } } foreach ($links as $link) { $output .= "<li>{$link}</li>\n"; } if ($wrap) { $output = '<ul class="relations">' . $output . '</ul>'; } return $output; }
private function _set_filter() { // filter? $value = Url::GetRequest('filter', null); // parents? $parents = $this->Model ? $this->Model->get_first_parent() : false; if (!empty($parents)) { $parent = $parents['key']; $parent_params = $parents['value']; AppModel::RelationNameParams($parent, $parent_params); $parents = add_all(AppModel::FindAllAssoc_options($parent)); } else { $parents = $parent = false; } // short filter? if ($short = is_numeric($value)) { if ($parent) { Globe::Load($parent, 'model'); $field = array_get($parent_params, 'foreignKey', AppInflector::tableize($parent) . '_id'); if ($value != false) { $where = "{$field}={$value}"; } else { $where = null; } } else { $field = $where = null; } } else { if (!empty($value)) { preg_match_all('/\\s*([^=]+)\\s*/', $value, $parts); // explode & trim! $field = array_shift($parts[0]); $value = implode('=', $parts[0]); $where = "{$field}='{$value}'"; } else { $field = $where = null; } } $this->assign('filter', $this->filter = (object) array('short' => $short, 'where' => $where, 'field' => $field, 'value' => $value, 'parent' => $parent, 'parents' => $parents)); }