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; }
static function tableize($class_name) { return strtolower(AppInflector::camelize(AppInflector::singularize($class_name))); }
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)); }
static function Parse($url) { // save 'original' Url::$data['_url'] = $url; // send through routes Url::$data['url'] = Url::DoRoutes($url); // get extension preg_match('/(.*)\\.(\\w{1,4})$/Ui', Url::$data['url'], $matches); if (!empty($matches)) { Url::$data['url'] = $matches[1]; Url::$data['type'] = $matches[2]; } // get parts $parts = explode(DS, Url::$data['url']); // is subdomain? // if (count($host_parts=explode('.', $_SERVER['HTTP_HOST'])) > 2 && $host_parts[0]!='www') { if (($subdomain = Url::GetSubdomain()) != null) { Url::$data['is_subdomain'] = true; Url::$data['subdomain'] = $subdomain; } Url::$data['host'] = 'http' . (array_get($_SERVER, 'HTTPS') ? 's' : '') . '://' . wrap($subdomain, '', '.') . DOMAIN; // d($url);d_arr($parts);d_arr(Url::$data);die; // nothing left? if (array_empty($parts)) { if (Url::GetData('is_admin')) { // is admin $parts = array(ADMIN_DEFAULT_CONTROLLER, ADMIN_DEFAULT_ACTION); } else { // is default $parts = explode(DS, Url::GetData('is_subdomain') ? SUBDOMAIN_DEFAULT_URL : DEFAULT_URL); } } // is execute? if (Url::$data['type'] == 'php') { Url::$data['model'] = 'app'; Url::$data['modelName'] = 'AppModel'; Url::$data['controller'] = EXECUTE_CONTROLLER; Url::$data['controllerName'] = ucfirst(EXECUTE_CONTROLLER) . 'Controller'; Url::$data['action'] = 'include_to_buffer'; // set filename Url::$data['params'] = array('filename' => implode(DS, $parts) . '.php'); } else { // set controller Url::$data['controller'] = array_shift($parts); Url::$data['controllerName'] = AppInflector::classify(Url::$data['controller'], 'controller'); // set model Url::$data['model'] = AppInflector::singularize(Url::$data['controller']); Url::$data['modelName'] = AppInflector::classify(Url::$data['model'], 'model'); // set action Url::$data['action'] = ($action = array_shift($parts)) != null ? $action : 'index'; // add prefix to admin actions if (Url::GetData('is_admin')) { Url::$data['action'] = ADMIN_PREFIX . Url::$data['action']; } // set params while (($value = array_shift($parts)) != null) { Url::$data['params'][] = $value; } } // set request Url::$data['request'] = $_REQUEST; // set domain Url::$data['domain'] = DOMAIN; // d_arr(Url::$data);die; }
<?php test('Story', 'story', AppInflector::classify('story', 'model')); test('Story', 'stories', AppInflector::classify('stories', 'model')); test('ManualChaptersController', 'manual_chapter', AppInflector::classify('manual_chapter', 'controller')); test('ManualChaptersController', 'Manual_Chapters', AppInflector::classify('Manual_Chapters', 'controller')); test('ManualChaptersController', 'ManualChapter', AppInflector::classify('ManualChapter', 'controller')); test('SalesPeopleController', 'sales_person', AppInflector::classify('sales_person', 'controller')); test('SalesPeopleController', 'sales_people', AppInflector::classify('sales_people', 'controller')); test('SalesPerson', 'sales_person', AppInflector::classify('sales_person', 'model')); test('SalesPerson', 'SalesPeople', AppInflector::classify('SalesPeople', 'model')); test('SalesPerson', 'SalesPeople', Inflector::classify('SalesPeople')); test('SalesPerson', 'Sales People', Inflector::classify('Sales People')); test('Sales People', 'Sales Person', Inflector::pluralize('Sales Person')); test('SalesPerson', 'Sales People', Inflector::classify('Sales People')); function test($intended, $input, $result) { echo "<pre class=\"test " . ($result == $intended ? 'success' : 'failure') . "\">{$input} -> {$result} ({$intended})</pre>"; } ?> <style type="text/css" media="screen"> pre.test { margin: 10px 0; padding: 10px; } pre.success { background-color: #080; color: #8f8; } pre.failure { background-color: #800;
static function Init($name, $type = 'class', $show_errors = true) { if (self::Load($name, $type, $show_errors)) { $class_name = AppInflector::classify($name, $type); return $type == 'model' ? AppModel::Create($class_name) : new $class_name(); } else { return new stdClass(); } }
function smarty_function_link($params = array(), &$smarty) { require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars'); $output = ''; $text = ''; $controller = ''; $action = ''; $set_gets = false; $extra = ''; $prefix = DS; foreach ($params as $_key => $_value) { switch ($_key) { case 'text': case 'controller': case 'action': case 'parameters': case 'href': case 'set_gets': case 'prefix': ${$_key} = $_value; break; case 'confirm': $extra .= ' onclick="return confirm(\'' . str_replace_js($_value) . '\');"'; break; default: if (!is_array($_value)) { $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_value) . '"'; } else { $smarty->trigger_error("link: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE); } break; } } if (empty($text)) { $smarty->trigger_error("link: missing 'text' parameter", E_USER_NOTICE); return; } if (empty($href)) { if (empty($controller)) { $smarty->trigger_error("link: missing 'controller' parameter", E_USER_NOTICE); return; } else { if (!is_string($controller)) { if (is_subclass_of($controller, 'AppModel') || is_subclass_of($controller, 'AppController')) { $controller = AppInflector::fileize(get_class($controller), 'controller'); } else { // no link possible... return $text; } } } $output .= '<a href="' . $prefix . $controller; if (!empty($action)) { $output .= DS . $action; } if (!empty($parameters)) { $output .= DS . $parameters; } } else { $output .= '<a href="'; if (preg_match('/.*@.*\\..{2,5}/', $href)) { $output .= 'mailto:'; } $output .= $href; } if ($set_gets != false) { require_once $smarty->_get_plugin_filepath('function', 'set_gets'); $output .= smarty_function_set_gets(is_bool($set_gets) ? array() : array('vars' => $set_gets), $smarty); } $output .= '"' . $extra . '>' . $text . '</a>'; return $output; }
private function _handle_file_upload($field = null, $params = array()) { require_once ENDO_PACKAGES_ROOT . 'VerotUpload' . DS . 'class.upload.php'; // TODO add 'delete-checkbox' functionality if (!empty($_FILES)) { // defaults $path = array_get($params, 'path', 'assets'); $allowed = array_get($params, 'allowed', 'image'); $options = array_get($params, 'options', array()); $file = new Upload($_FILES[$field]); if ($file->uploaded) { // path $path = 'uploads' . DS . AppInflector::pluralize(get_class($this)) . DS . $path . DS . $this->id . DS; $folder = WEB_ROOT . $path; // settings if ($allowed == 'image') { $file->allowed = array('image/gif', 'image/jpg', 'image/jpeg', 'image/png', 'image/bmp'); // defaults if (!array_get($options, 'width') && !array_get($options, 'height')) { $options = array_merge(array('width' => 100), $options); } // resize $file->image_max_pixels = 1000000; $file->image_resize = true; $file->image_convert = 'jpg'; // determine method if (array_get($options, 'width') && !array_get($options, 'height')) { // width only $file->image_x = $options['width']; $file->image_ratio_y = true; } elseif (!array_get($options, 'width') && array_get($options, 'height')) { // height only $file->image_y = $options['height']; $file->image_ratio_x = true; } else { // width and height $file->image_x = $options['width']; $file->image_y = $options['height']; // crop? if (array_get($options, 'crop')) { $file->image_ratio_crop = true; } else { $file->image_ratio = true; } } } elseif (is_array($allowed)) { $file->allowed = $allowed; } // process! $file->Process($folder); // success? if ($file->processed) { // remove old file? @unlink($folder . str_replace(DS . $path, '', $this->{$field . '_old'})); $file->Clean(); $this->{$field} = DS . $path . $file->file_dst_name; return parent::save(); } else { // TODO add endo form error message echo 'ERROR: ' . $file->error; return false; } } } return true; }