public function getCommands() { $name = $this->getController()->getIdentifier()->name; $menu = array('Articles'); foreach ($menu as $menu) { $this->addCommand($menu, array('href' => 'view=' . KStringInflector::pluralize(strtolower($menu)), 'active' => $name == KStringInflector::singularize(strtolower($menu)))); } return parent::getCommands(); }
protected function _initialize(KObjectConfig $config) { $config->append(array('behaviors' => array('purgeable'))); if ($this->getIdentifier()->getPackage() != 'activities') { $aliases = array('com:activities.model.activities' => array('path' => array('model'), 'name' => KStringInflector::pluralize($this->getIdentifier()->getName())), 'com:activities.controller.behavior.purgeable' => array('path' => array('controller', 'behavior'), 'name' => 'purgeable'), 'com:activities.controller.permission.activity' => array('path' => array('controller', 'permission')), 'com:activities.controller.toolbar.activity' => array('path' => array('controller', 'toolbar'))); foreach ($aliases as $identifier => $alias) { $alias = array_merge($this->getIdentifier()->toArray(), $alias); $manager = $this->getObject('manager'); // Register the alias if a class for it cannot be found. if (!$manager->getClass($alias, false)) { $manager->registerAlias($identifier, $alias); } } } parent::_initialize($config); }
/** * Get a fully qualified path based on a class name * * @param string $class The class name * @param string $basepath The base path * @return string|boolean Returns the path on success FALSE on failure */ public function locate($class, $basepath = null) { if (substr($class, 0, 3) === 'Mod') { /* * Exception rule for Exception classes * * Transform class to lower case to always load the exception class from the /exception/ folder. */ if ($pos = strpos($class, 'Exception')) { $filename = substr($class, $pos + strlen('Exception')); $class = str_replace($filename, ucfirst(strtolower($filename)), $class); } $word = strtolower(preg_replace('/(?<=\\w)([A-Z])/', ' \\1', $class)); $parts = explode(' ', $word); array_shift($parts); $package = array_shift($parts); $namespace = ucfirst($package); $module = 'mod_' . $package; $file = array_pop($parts); if (count($parts)) { if ($parts[0] === 'view') { $parts[0] = KStringInflector::pluralize($parts[0]); } $path = implode('/', $parts); $path = $path . '/' . $file; } else { $path = $file; } //Switch basepath if (!$this->getNamespace($namespace)) { $basepath = $this->getNamespace('\\'); } else { $basepath = $this->getNamespace($namespace); } return $basepath . '/' . $module . '/' . $path . '.php'; } return false; }
/** * Get the children entity * * @return object */ public function getChildren() { if(!($this->_children instanceof KModelEntityInterface)) { $identifier = $this->getIdentifier()->toArray(); $identifier['path'] = array('model', 'entity'); $identifier['name'] = KStringInflector::pluralize($this->getIdentifier()->name); //The row default options $options = array( 'identity_key' => $this->getIdentityKey() ); $this->_children = $this->getObject($identifier, $options); } return $this->_children; }
/** * Method to set a model object attached to the controller * * @param mixed $model An object that implements KObjectInterface, KObjectIdentifier object * or valid identifier string * @return KControllerView */ public function setModel($model) { if (!$model instanceof KModelInterface) { if (is_string($model) && strpos($model, '.') === false) { // Model names are always plural if (KStringInflector::isSingular($model)) { $model = KStringInflector::pluralize($model); } $identifier = $this->getIdentifier()->toArray(); $identifier['path'] = array('model'); $identifier['name'] = $model; $identifier = $this->getIdentifier($identifier); } else { $identifier = $this->getIdentifier($model); } $model = $identifier; } $this->_model = $model; return $this->_model; }
/** * Renders a listbox with autocomplete behavior * * @see ComKoowaTemplateHelperBehavior::_listbox * * @param array|KObjectConfig $config * @return string The html output */ protected function _autocomplete($config = array()) { $config = new KObjectConfigJson($config); $config->append(array('name' => '', 'attribs' => array(), 'model' => KStringInflector::pluralize($this->getIdentifier()->package), 'validate' => true, 'filter' => array()))->append(array('element' => $config->attribs->id ? '#' . $config->attribs->id : 'input[name=' . $config->name . ']', 'options' => array('multiple' => (bool) $config->attribs->multiple), 'deselect' => true, 'prompt' => '- ' . $this->getObject('translator')->translate('Select') . ' -', 'unique' => true, 'select2' => false, 'value' => $config->name, 'selected' => $config->{$config->name}, 'identifier' => 'com://' . $this->getIdentifier()->domain . '/' . $this->getIdentifier()->package . '.model.' . $config->model))->append(array('label' => $config->value))->append(array('text' => $config->label, 'filter' => array('sort' => $config->label))); if (!$config->url) { $identifier = $this->getIdentifier($config->identifier); $parts = array('component' => $identifier->package, 'view' => $identifier->name, 'format' => 'json'); if ($config->filter) { $parts = array_merge($parts, KObjectConfig::unbox($config->filter)); } $config->url = $this->getTemplate()->route($parts, false, false); } $html = ''; // TODO: Remove when select2 properly support AJAX multiple listboxes by sending choices // as an array (presumably for v4). if ($config->attribs->multiple) { $html .= '<script> kQuery(function($) { var el = $("' . $config->element . '"); var form = el.closest("form"); var explode = function(e) { if (el.val()) { if (el.attr("name").substr(-2) !== "[]") { // Make the input an array. el.attr("name", el.attr("name") + "[]"); } var values = el.val().split(","); $.each(values, function(idx, value) { form.append(el.clone().val(value)); }); el.remove(); } else { // If there is no value, then we remove the element anyways to mimic a real select box. el.remove(); } }; if (form.hasClass("-koowa-form") || form.hasClass("-koowa-grid")) { form.submit(explode); } else { // See: https://github.com/joomla/joomla-cms/pull/5914 for why we use onsubmit var element = form.get(0), previous = element.onsubmit; element.onsubmit = function() { if (typeof previous === "function") { previous(); } explode(); // Avoid explode to be executed more than once. element.onsubmit = previous; }; } });</script>'; } $html .= $this->getTemplate()->createHelper('behavior')->autocomplete($config); $config->attribs->name = $config->name; if ($config->selected) { //Compose the selected array if ($config->selected instanceof KModelEntityInterface) { $selected = array(); foreach ($config->selected as $entity) { $selected[] = $entity->{$config->value}; } $config->selected = $selected; } $config->attribs->value = json_encode(KObjectConfig::unbox($config->selected)); } $attribs = $this->buildAttributes($config->attribs); $html .= "<input type=\"hidden\" {$attribs} />"; return $html; }
/** * Method to set a table object attached to the model * * @param mixed $table An object that implements ObjectInterface, ObjectIdentifier object * or valid identifier string * @throws UnexpectedValueException If the identifier is not a table identifier * @return KModelDatabase */ public function setTable($table) { if (!$table instanceof KDatabaseTableInterface) { if (is_string($table) && strpos($table, '.') === false) { $identifier = $this->getIdentifier()->toArray(); $identifier['path'] = array('database', 'table'); $identifier['name'] = KStringInflector::pluralize(KStringInflector::underscore($table)); $identifier = $this->getIdentifier($identifier); } else { $identifier = $this->getIdentifier($table); } if ($identifier->path[1] != 'table') { throw new UnexpectedValueException('Identifier: ' . $identifier . ' is not a table identifier'); } $table = $identifier; } $this->_table = $table; return $this; }
/** * Initializes the options for the object. * * Called from {@link __construct()} as a first step of object instantiation. * * @param KObjectConfig $config Configuration options. */ protected function _initialize(KObjectConfig $config) { $data = $config->data; $config->append(array('format' => '{actor} {action} {object.type} title {object}', 'object_table' => $data->package . '_' . KStringInflector::pluralize($data->name), 'object_column' => $data->package . '_' . $data->name . '_id', 'translator' => 'com:activities.activity.translator', 'objects' => array('actor', 'action', 'object', 'target', 'generator', 'provider'))); parent::_initialize($config); }
/** * Initializes the options for the object * * Called from {@link __construct()} as a first step of object instantiation. * * @param KObjectConfig $config Configuration options * @return void */ protected function _initialize(KObjectConfig $config) { $config->append(array('priority' => self::PRIORITY_LOW, 'package' => null, 'entity' => null, 'category_entity' => 'category'))->append(array('model' => KStringInflector::pluralize($config->entity))); parent::_initialize($config); }
/** * Fetch a new entity from the data source * * @param KModelContext $context A model context object * @return KModelEntityInterface The entity */ protected function _actionFetch(KModelContext $context) { $identifier = $this->getIdentifier()->toArray(); $identifier['path'] = array('model', 'entity'); $identifier['name'] = KStringInflector::pluralize($identifier['name']); $options = array('identity_key' => $context->getIdentityKey()); return $this->getObject($identifier, $options); }
protected function _initialize(KObjectConfig $config) { $data = $config->data; $config->append(array('format' => '{actor} {action} {object.subtype} {object.type} {target.type} {target}', 'object_table' => $data->package . '_' . KStringInflector::pluralize($data->name))); parent::_initialize($config); }
protected function _treelistbox($config = array()) { $config = new KObjectConfigJson($config); $config->append(array('name' => '', 'attribs' => array(), 'model' => KStringInflector::pluralize($this->getIdentifier()->package), 'deselect' => true, 'prompt' => '- Select -', 'unique' => false, 'check_access' => false))->append(array('select2' => false, 'value' => $config->name, 'selected' => $config->{$config->name}, 'identifier' => 'com://' . $this->getIdentifier()->domain . '/' . $this->getIdentifier()->package . '.model.' . KStringInflector::pluralize($config->model)))->append(array('label' => $config->value))->append(array('filter' => array('sort' => $config->label)))->append(array('indent' => '- ', 'ignore' => array())); $ignore = KObjectConfig::unbox($config->ignore); $model = $this->getObject($config->identifier); $state = KObjectConfig::unbox($config->filter); $count = $model->setState($state)->count(); $offset = 0; $options = array(); while ($offset < $count) { $categories = $model->setState($state)->limit(100)->offset($offset)->fetch(); foreach ($categories as $key => $item) { $value = $item->getProperty($config->value); if ($config->check_access && $item->isPermissible() && $value != $config->selected && !$item->canPerform('add')) { continue; } if (in_array($item->id, $ignore)) { continue; } $options[] = $this->option(array('label' => str_repeat($config->indent, $item->level - 1) . $item->{$config->label}, 'value' => $item->{$config->value})); } $offset += 100; } //Add the options to the config object $config->options = $options; $html = ''; if ($config->autocomplete) { $html .= $this->_autocomplete($config); } else { $html .= $this->optionlist($config); } return $html; }
/** * Find the referrer based on the context * * Method is being called when no referrer can be found in the request or when request url and referrer are * identical. Function should return a url that is different from the request url to avoid redirect loops. * * @param KControllerContextInterface $context * @return KHttpUrl A HttpUrl object */ public function findReferrer(KControllerContextInterface $context) { $controller = $this->getMixer(); $identifier = $controller->getIdentifier(); $component = $identifier->package; $view = KStringInflector::pluralize($identifier->name); $referrer = $controller->getView()->getRoute('component=' . $component . '&view=' . $view, true, false); return $this->getObject('lib:http.url', array('url' => $referrer)); }
protected function _treelistbox($config = array()) { $config = new KObjectConfigJson($config); $config->append(array( 'name' => '', 'attribs' => array(), 'model' => KStringInflector::pluralize($this->getIdentifier()->package), 'deselect' => true, 'prompt' => '- Select -', 'unique' => false, // Overridden since there can be categories in different levels with the same name 'check_access' => false ))->append(array( 'select2' => false, 'value' => $config->name, 'selected' => $config->{$config->name}, 'identifier' => 'com://'.$this->getIdentifier()->domain.'/'.$this->getIdentifier()->package.'.model.'.KStringInflector::pluralize($config->model) ))->append(array( 'label' => $config->value, ))->append(array( 'filter' => array('sort' => $config->label), ))->append(array( 'indent' => '- ', 'ignore' => array(), )); $list = $this->getObject($config->identifier)->setState(KObjectConfig::unbox($config->filter))->fetch(); //Get the list of items $items = array(); foreach($list as $key => $item) { $items[$key] = $item->getProperty($config->value); } if ($config->unique) { $items = array_unique($items); } //Compose the options array $options = array(); $ignore = KObjectConfig::unbox($config->ignore); foreach ($items as $key => $value) { $item = $list->find($key); if ($config->check_access && $item->isPermissible() && ($value != $config->selected) && !$item->canPerform('add')) { continue; } if (in_array($item->id, $ignore)) { continue; } $options[] = $this->option(array('label' => str_repeat($config->indent, $item->level-1) . $item->{$config->label}, 'value' => $item->{$config->value})); } //Add the options to the config object $config->options = $options; $html = ''; if($config->autocomplete) { $html .= $this->_autocomplete($config); } else { $html .= $this->optionlist($config); } return $html; }
/** * Initializes the default configuration for the object * * Called from {@link __construct()} as a first step of object instantiation. * * @param KObjectConfig $config Configuration options * @return void */ protected function _initialize(KObjectConfig $config) { $config->append(array('package' => strtolower(substr(get_class($this), 9))))->append(array('entity' => $config->package))->append(array('layout' => $config->entity, 'model' => KStringInflector::pluralize($config->entity), 'context' => $config->package, 'extension' => 'com_' . $config->package, 'type_title' => ucfirst($config->entity), 'instructions' => array(FinderIndexer::TEXT_CONTEXT => array('description'), FinderIndexer::META_CONTEXT => array('created_by_name'), FinderIndexer::PATH_CONTEXT => array('slug')))); }
/** * @dataProvider providePlurals */ public function testPluralize($singular, $plural) { $this->assertEquals(KStringInflector::pluralize($singular), $plural); }