/** * Returns a fully qualified class name for a given identifier. * * @param KObjectIdentifier $identifier An identifier object * @param bool $fallback Use the fallbacks to locate the identifier * @return string|false Return the class name on success, returns FALSE on failure */ public function locate(KObjectIdentifier $identifier, $fallback = true) { $class = KStringInflector::camelize(implode('_', $identifier->path)) . ucfirst($identifier->name); if (empty($identifier->domain)) { $domain = ucfirst($this->getObject('object.bootstrapper')->getComponentDomain($identifier->package)); } else { $domain = ucfirst($identifier->domain); } $package = ucfirst($identifier->package); $file = ucfirst($identifier->name); $path = $identifier->path; //Make an exception for 'view' and 'module' types $type = !empty($path) ? array_shift($path) : ''; if (!in_array($type, array('view', 'module'))) { $path = ucfirst($type) . KStringInflector::camelize(implode('_', $path)); } else { $path = ucfirst($type); } //Allow locating default classes if $path is empty. if (empty($path)) { $path = $file; $file = ''; } $info = array('identifier' => $identifier, 'class' => $class, 'package' => $package, 'domain' => $domain, 'path' => $path, 'file' => $file); return $this->find($info, $fallback); }
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(); }
/** * Returns a fully qualified class name for a given identifier. * * @param KObjectIdentifier $identifier An identifier object * @param bool $fallback Use the fallback sequence to locate the identifier * @return string|false Return the class name on success, returns FALSE on failure */ public function locate(KObjectIdentifier $identifier, $fallback = true) { $domain = empty($identifier->domain) ? 'koowa' : ucfirst($identifier->domain); $package = ucfirst($identifier->package); $path = KStringInflector::camelize(implode('_', $identifier->path)); $file = ucfirst($identifier->name); $class = $path . $file; $info = array('identifier' => $identifier, 'class' => $class, 'package' => $package, 'domain' => $domain, 'path' => $path, 'file' => $file); return $this->find($info, $fallback); }
protected function _buildQueryJoins(KDatabaseQueryInterface $query) { $state = $this->getState(); parent::_buildQueryJoins($query); if ($state->table) { $identity_column = KStringInflector::singularize($state->table) . '_id'; $table = KStringInflector::singularize($state->table) . '_translations'; $query->columns('IFNULL(' . $identity_column . ', 0) AS translated')->columns('IFNULL(r.original, 0) AS original')->join(array('r' => $table), 'r.locale = tbl.lang_code AND r.' . $identity_column . ' = :id', 'left')->bind(array('id' => $state->row)); } }
/** * Generic read action, fetches an item * * @param KControllerContextInterface $context A command context object * @throws KControllerExceptionResourceNotFound * @return KModelEntityInterface */ protected function _actionRead(KControllerContextInterface $context) { //Request if ($this->getIdentifier()->domain === 'admin') { if ($this->isEditable() && KStringInflector::isSingular($this->getView()->getName())) { //Use JInput as we do not pass the request query back into the Joomla context JFactory::getApplication()->input->set('hidemainmenu', 1); } } return parent::_actionRead($context); }
/** * Create an entity or a collection instance * * @param KObjectConfigInterface $config A KObjectConfig object with configuration options * @param KObjectManagerInterface $manager A ObjectInterface object * @return KEventPublisher */ public static function getInstance(KObjectConfigInterface $config, KObjectManagerInterface $manager) { $name = $config->object_identifier->name; if (KStringInflector::isSingular($name)) { $class = 'KModelEntityRow'; } else { $class = 'KModelEntityRowset'; } $instance = new $class($config); return $instance; }
/** * Instantiate the object * * If the behavior is auto mixed also lazy mix it into related row objects. * * @param KObjectConfigInterface $config A KObjectConfig object with configuration options * @param KObjectManagerInterface $manager A KObjectInterface object * @return KDatabaseBehaviorAbstract */ public static function getInstance(KObjectConfigInterface $config, KObjectManagerInterface $manager) { $class = $manager->getClass($config->object_identifier); $instance = new $class($config); //Lazy mix behavior into related row objects. A supported behavior always has one is[Behaviorable] method. if ($instance->isSupported() && $instance->getMixer() && count($instance->getMixableMethods()) > 1) { $identifier = $instance->getMixer()->getIdentifier()->toArray(); $identifier['path'] = array('database', 'row'); $identifier['name'] = KStringInflector::singularize($identifier['name']); $manager->registerMixin($identifier, $instance); } return $instance; }
/** * Redirect to the form with the last category preselected if it exists in the URL * * @param KControllerContextInterface $context * @return KModelEntityInterface */ protected function _setRedirect(KControllerContextInterface $context) { $referrer = $this->getReferrer($context); if ($referrer) { $query = $referrer->getQuery(true); if (!empty($query['category'])) { $identifier = $this->getIdentifier(); $view = KStringInflector::singularize($identifier->name); $url = sprintf('index.php?option=com_%s&view=%s&category=%d', $identifier->package, $view, $query['category']); $context->response->setRedirect($this->getObject('lib:http.url', array('url' => $url))); } } }
/** * Saves the current row and redirects to a new edit form * * @param KControllerContextInterface $context * @return KModelEntityInterface */ protected function _actionSave2new(KControllerContextInterface $context) { // Cache and lock the referrer since _ActionSave would unset it $referrer = $this->getReferrer($context); $this->_lockReferrer($context); $entity = $this->save($context); // Re-set the referrer $cookie = $this->getObject('lib:http.cookie', array('name' => $this->_cookie_name, 'value' => (string) $referrer, 'path' => $this->_cookie_path)); $context->response->headers->addCookie($cookie); $identifier = $this->getMixer()->getIdentifier(); $view = KStringInflector::singularize($identifier->name); $url = sprintf('index.php?option=com_%s&view=%s', $identifier->package, $view); $context->response->setRedirect($this->getObject('lib:http.url', array('url' => $url))); return $entity; }
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 the list of commands * * Will attempt to use information from the xml manifest if possible * * @return array */ public function getCommands() { $name = $this->getController()->getIdentifier()->name; $package = $this->getIdentifier()->package; $manifest = JPATH_ADMINISTRATOR . '/components/com_' . $package . '/' . $package . '.xml'; if (file_exists($manifest)) { $xml = simplexml_load_file($manifest); if (isset($xml->administration->submenu)) { foreach ($xml->administration->submenu->children() as $menu) { $view = (string) $menu['view']; $this->addCommand((string) $menu, array('href' => 'option=com_' . $package . '&view=' . $view, 'active' => $name == KStringInflector::singularize($view))); } } } return parent::getCommands(); }
/** * 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; }
/** * Method to set a controller object attached to the dispatcher * * @param mixed $controller An object that implements KControllerInterface, KObjectIdentifier object * or valid identifier string * @param array $config An optional associative array of configuration options * @return KDispatcherAbstract */ public function setController($controller, $config = array()) { if (!$controller instanceof KControllerInterface) { if (is_string($controller) && strpos($controller, '.') === false) { // Controller names are always singular if (KStringInflector::isPlural($controller)) { $controller = KStringInflector::singularize($controller); } $identifier = $this->getIdentifier()->toArray(); $identifier['path'] = array('controller'); $identifier['name'] = $controller; $identifier = $this->getIdentifier($identifier); } else { $identifier = $this->getIdentifier($controller); } //Set the configuration $identifier->getConfig()->append($config); $controller = $identifier; } $this->_controller = $controller; 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); }
/** * Forward the call to the current row * * Search the mixin method map and call the method or forward the call to each row * * This function implements a just in time mixin strategy. Available table behaviors are only mixed when needed. * Lazy mixing is triggered by calling KDatabaseRowTable::is[Behaviorable](); * * @param string $method The function name * @param array $arguments The function arguments * @throws \BadMethodCallException If method could not be found * @return mixed The result of the function */ public function __call($method, $arguments) { $result = null; if ($this->isConnected()) { $parts = KStringInflector::explode($method); //Check if a behavior is mixed if ($parts[0] == 'is' && isset($parts[1])) { $row = $this->getIterator()->current(); if ($row && !in_array($method, $row->getMethods())) { //Lazy mix behaviors $behavior = strtolower($parts[1]); if ($row->getTable()->hasBehavior($behavior)) { $row->mixin($row->getTable()->getBehavior($behavior)); } else { return false; } } } } if ($row = $this->getIterator()->current()) { // Call_user_func_array is ~3 times slower than direct method calls. switch (count($arguments)) { case 0: $result = $row->{$method}(); break; case 1: $result = $row->{$method}($arguments[0]); break; case 2: $result = $row->{$method}($arguments[0], $arguments[1]); break; case 3: $result = $row->{$method}($arguments[0], $arguments[1], $arguments[2]); break; default: // Resort to using call_user_func_array for many segments $result = call_user_func_array(array($row, $method), $arguments); } } return $result; }
public function getTranslation() { if (!isset($this->__object_translation)) { $identifier = array(); if ($this->getMixer() instanceof KDatabaseTableInterface) { $identifier = $this->getMixer()->getIdentifier()->toArray(); } if ($this->getMixer() instanceof KModelEntityInterface) { $identifier = $this->getMixer()->getTable()->getIdentifier()->toArray(); } $identifier['path'] = array('model'); $identifier['name'] = KStringInflector::singularize($identifier['name']) . '_translations'; $identifier = new KObjectIdentifier($identifier); $this->__object_translation = $this->getObject($identifier); } return $this->__object_translation; }
/** * Command handler * * @param KCommandInterface $command The command * @param KCommandChainInterface $chain The chain executing the command * @return mixed|null If a handler breaks, returns the break condition. NULL otherwise. */ public function execute(KCommandInterface $command, KCommandChainInterface $chain) { $type = ''; $package = ''; $subject = ''; if ($command->getSubject()) { $identifier = $command->getSubject()->getIdentifier()->toArray(); $package = $identifier['package']; if ($identifier['path']) { $type = array_shift($identifier['path']); $subject = $identifier['name']; } else { $type = $identifier['name']; } } $parts = explode('.', $command->getName()); $when = array_shift($parts); // Before or After $name = KStringInflector::implode($parts); // Read Dispatch Select etc. // Create Specific and Generic event names $event_specific = 'on' . ucfirst($when) . ucfirst($package) . ucfirst($subject) . ucfirst($type) . $name; $event_generic = 'on' . ucfirst($when) . ucfirst($type) . $name; // Clone the context if ($this->_immutable) { $event = clone $command; } else { $event = $command; } // Create event object to check for propagation $event = $this->getEventPublisher()->publishEvent($event_specific, $event->getAttributes(), $event->getSubject()); // Ensure event can be propagated and event name is different if ($event->canPropagate() && $event_specific != $event_generic) { $event->setName($event_generic); $this->getEventPublisher()->publishEvent($event); } }
/** * 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; }
/** * Supports a simple form of Fluent Interfaces. Allows you to assign variables to the view by using the variable * name as the method name. If the method name is a setter method the setter will be called instead. * * For example : $view->data(array('foo' => 'bar'))->title('name')->render() * * @param string $method Method name * @param array $args Array containing all the arguments for the original call * @return KViewAbstract * * @see http://martinfowler.com/bliki/FluentInterface.html */ public function __call($method, $args) { if (!isset($this->_mixed_methods[$method])) { //If one argument is passed we assume a setter method is being called if (count($args) == 1) { if (!method_exists($this, 'set' . ucfirst($method))) { $this->{$method} = $args[0]; return $this; } else { return $this->{'set' . ucfirst($method)}($args[0]); } } //Check if a behavior is mixed $parts = KStringInflector::explode($method); if ($parts[0] == 'is' && isset($parts[1])) { return false; } } return parent::__call($method, $args); }
/** * Create a new entity and insert it * * This function will either clone the entity object, or create a new instance of the entity object for each entity * being inserted. By default the entity will be cloned. * * @param array $properties The entity properties * @param string $status The entity status * @return KModelEntityComposite */ public function create(array $properties = array(), $status = null) { if ($this->_prototypable) { if (!$this->_prototype instanceof KModelEntityInterface) { $identifier = $this->getIdentifier()->toArray(); $identifier['path'] = array('model', 'entity'); $identifier['name'] = KStringInflector::singularize($this->getIdentifier()->name); //The entity default options $options = array('identity_key' => $this->getIdentityKey()); $this->_prototype = $this->getObject($identifier, $options); } $entity = clone $this->_prototype; $entity->setStatus($status); $entity->setProperties($properties, $entity->isNew()); } else { $identifier = $this->getIdentifier()->toArray(); $identifier['path'] = array('model', 'entity'); $identifier['name'] = KStringInflector::singularize($this->getIdentifier()->name); //The entity default options $options = array('data' => $properties, 'status' => $status, 'identity_key' => $this->getIdentityKey()); $entity = $this->getObject($identifier, $options); } //Insert the entity into the collection $this->insert($entity); return $entity; }
/** * 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); }
/** * 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; }
/** * Get a list of the computed properties * * @return array An array */ public function getComputedProperties() { if (!$this->__computed_properties) { $properties = array(); foreach ($this->getMethods() as $method) { if (substr($method, 0, 11) == 'getProperty' && $method !== 'getProperty') { $property = KStringInflector::underscore(substr($method, 11)); $properties[$property] = $property; } } $this->__computed_properties = $properties; } return $this->__computed_properties; }
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); }
/** * Search the mixin method map and call the method or trigger an error * * This function check to see if the method exists in the mixing map if not it will call the 'listbox' function. * The method name will become the 'name' in the config array. * * This can be used to auto-magically create select filters based on the function name. * * @param string $method The function name * @param array $arguments The function arguments * @throws BadMethodCallException If method could not be found * @return mixed The result of the function */ public function __call($method, $arguments) { if (!in_array($method, $this->getMethods())) { $config = $arguments[0]; if (!isset($config['name'])) { $config['name'] = KStringInflector::singularize(strtolower($method)); } return $this->_render($config); } return parent::__call($method, $arguments); }
/** * Search the behaviors to see if this table behaves as. * * Function is also capable of checking is a behavior has been mixed successfully using is[Behavior] function. * If the behavior exists the function will return TRUE, otherwise FALSE. * * @param string $method The function name * @param array $arguments The function arguments * @throws \BadMethodCallException If method could not be found * @return mixed The result of the function */ public function __call($method, $arguments) { if (!isset($this->_mixed_methods[$method])) { // If the method is of the form is[Bahavior] handle it. $parts = KStringInflector::explode($method); if ($parts[0] == 'is' && isset($parts[1])) { return false; } } return parent::__call($method, $arguments); }
/** * 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; }
/** * Add a command by it's name * * @param string $method Method name * @param array $args Array containing all the arguments for the original call * @return mixed * @see addCommand() */ public function __call($method, $args) { $parts = KStringInflector::explode($method); if ($parts[0] == 'add' && isset($parts[1])) { $config = isset($args[0]) ? $args[0] : array(); $command = $this->addCommand(strtolower($parts[1]), $config); return $command; } return parent::__call($method, $args); }
/** * Execute a controller action by it's name. * * Function is also capable of checking is a behavior has been mixed successfully using is[Behavior] function. If * the behavior exists the function will return TRUE, otherwise FALSE. * * @param string $method Method name * @param array $args Array containing all the arguments for the original call * @return mixed * @see execute() */ public function __call($method, $args) { //Handle action alias method if (in_array($method, $this->getActions())) { //Get the data $data = !empty($args) ? $args[0] : array(); //Create a context object if (!$data instanceof KCommandInterface) { $context = $this->getContext(); //Store the parameters in the context $context->param = $data; //Force the result to false before executing $context->result = false; } else { $context = $data; } //Execute the action return $this->execute($method, $context); } if (!isset($this->_mixed_methods[$method])) { //Check if a behavior is mixed $parts = KStringInflector::explode($method); if ($parts[0] == 'is' && isset($parts[1])) { return false; } } return parent::__call($method, $args); }
/** * 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); }