/** * Edit Command for an entity. * * @param LibBaseTemplateObject $command The action object */ protected function _commandEdit($command) { $entity = $this->getController()->getItem(); $view = $this->getController()->getView()->getName(); $layout = pick($command->layout, 'edit'); $command->append(array('label' => JText::_('LIB-AN-ACTION-EDIT')))->href($entity->getURL() . '&layout=' . $layout)->setAttribute('data-action', 'edit'); }
function testArrayAccess() { $object = new \ArrayObject(); $object['test'] = 5; $this->assertSame(5, pick($object, 'test'), 'Key exists'); $this->assertSame(10, pick($object, 'dummy', 10), 'Key does not exists'); }
/** * Set the X_REQUEST header to request an output other than a full HTML page. */ public function route($path) { $this->args = explode('/', $path); $this->model = array_shift($this->args); $class = ucfirst($this->model) . "Commands"; $this->controller = new $class(); $this->command = pick(array_shift($this->args), 'index'); if ($_POST['returnFormat']) { $this->returnFormat = $_POST['returnFormat']; unset($_POST['returnFormat']); } $this->args[] = $_POST; if (!method_exists($this->controller, $this->command)) { throw new CommandException("Command {$command} not found."); } try { $this->result = call_user_func_array(array($this->controller, $this->command), $this->args); } catch (Exception $e) { $this->exception = $e; } $returnFormat = strtolower($_SERVER['HTTP_X_REQUEST']); $meth = "output_{$returnFormat}"; if (method_exists($this, $meth)) { return $this->{$meth}(); } return $this->output_full(); }
/** * Returns an array of federal taxes * * @return array */ public function getFederalTaxes() { $taxes = pick($this->meta->federal_taxes, array()); foreach ($taxes as &$tax) { $tax = new KConfig($tax); } return $taxes; }
/** * Render a custom element. * * @param string $name The name of the element * @param string $value The vlaue of the element * @param xmlElement $node The node xml element * @param string $control_name The control name * * @see JElement * * @return string */ public function fetchElement($name, $value, &$node, $control_name) { $data = pick(KConfig::unbox($this->_parent->template_data), array()); $data['name'] = $name; $data['value'] = $value; $data['node'] = $node; $data['control_name'] = $control_name; return (string) $this->_template->loadString($node->data(), $data); }
/** * Before _actionGet controller event * * @param KEvent $event Event object * * @return string */ public function onBeforeControllerGet(KEvent $event) { parent::onBeforeControllerGet($event); if ($this->getController()->isOwnable() && $this->getController()->actor) { $actor = pick($this->getController()->actor, get_viewer()); $this->setTitle(sprintf(JText::_('COM-STORIES-HEADER-STORIES'), $actor->name)); $this->setActor($actor); } }
/** * Before controller action. * * @param KEvent $event Event object * * @return string */ public function onBeforeControllerGet(KEvent $event) { parent::onBeforeControllerGet($event); $viewer = $this->getController()->viewer; $actor = pick($this->getController()->actor, $viewer); $layout = pick($this->getController()->layout, 'default'); $name = $this->getController()->getIdentifier()->name; $this->setTitle(JText::sprintf('COM-TOPICS-HEADER-TOPICS', $actor->name)); $this->addNavigation('topics', JText::_('COM-TOPICS-NAV-TOPICS'), array('option' => 'com_topics', 'view' => 'topics', 'oid' => $actor->id), $name == 'topic'); }
/** * Instantiate the set attribute with a comma seperate list of value * * @param string $data A comma seperated list of values * * @return AnDomainAttributeSet */ public function unserialize($data) { if ($data) { $data = explode(',', pick($data, '')); $data = array_combine($data, $data); } else { $data = array(); } $this->_data = $data; return $this; }
/** * {@inheritdoc} */ protected function _beforeRepositoryFetch(KCommandContext $context) { $query = $context->query; $repository = $query->getRepository(); $query->privacy = pick($query->privacy, new KConfig()); //weak link the stories with object nodes //and use the object.access instead of the story access if there are ny $query->link('object', array('type' => 'weak', 'bind_type' => false)); $query->privacy->append(array('use_access_column' => 'IF(@col(object.id) IS NULL,@col(story.access), @col(object.access))')); parent::_beforeRepositoryFetch($context); }
/** * Before controller action. * * @param KEvent $event Event object * * @return string */ public function onBeforeControllerGet(KEvent $event) { parent::onBeforeControllerGet($event); $viewer = get_viewer(); $actor = pick($this->getController()->actor, $viewer); $layout = pick($this->getController()->layout, 'default'); $name = $this->getController()->getIdentifier()->name; $this->setTitle(JText::sprintf('COM-PAGES-ACTOR-HEADER-' . strtoupper($name) . 'S', $actor->name)); //create navigations $this->addNavigation('pages', JText::_('COM-PAGES-LINK-PAGES'), array('option' => 'com_pages', 'view' => 'pages', 'oid' => $actor->id), $name == 'page'); }
protected function _beforeRepositoryFetch($context) { if (KService::has('com:people.viewer') && is_person(get_viewer()) && get_viewer()->admin()) { return; } $query = $context->query; $config = pick($query->privacy, new KConfig()); $config->append(array('viewer' => get_viewer())); $where = $this->buildCondition(get_viewer(), $config); $query->where($where); }
/** * Connects to the database or whines about how it couldn't. */ private function __construct() { $this->host = pick($host, DB_HOST, 'localhost'); $this->user = pick($user, DB_USER); $this->password = pick($password, DB_PASS); $this->database = pick($database, DB_DATA); $this->connection = mysql_connect($this->host, $this->user, $this->password); if (!mysql_select_db($this->database)) { throw new Exception("Could not connect to database."); } }
/** * (non-PHPdoc). * * @see ComBaseRouterAbstract::parse() */ public function parse(&$segments) { $path = implode('/', $segments); if (empty($segments)) { $segments[] = 'email'; } if (preg_match('#(connections|token)(/\\w+)?#', $path)) { return array('view' => array_shift($segments), 'service' => pick('facebook', array_pop($segments))); } return parent::parse($segments); }
/** * {@inheritdoc} */ protected function _beforeRepositoryFetch(KCommandContext $context) { if (KService::has('com:people.viewer') && is_person(get_viewer()) && get_viewer()->admin()) { return; } $query = $context->query; $repository = $query->getRepository(); $config = pick($query->privacy, new KConfig()); $config->append(array('visible_to_leaders' => true, 'viewer' => get_viewer(), 'graph_check' => true)); $where = $this->buildCondition('@col(id)', $config, '@col(access)'); $query->where($where); }
/** * Before controller action * * @param KEvent $event Event object * * @return string */ public function onBeforeControllerGet(KEvent $event) { parent::onBeforeControllerGet($event); $name = $this->getController()->getIdentifier()->name; if ($name != 'order') { return; } $viewer = $this->getController()->viewer; $actor = pick($this->getController()->actor, $viewer); $this->setActor($actor); $this->setTitle(JText::sprintf('COM-SUBSCRIPTIONS-ACTOR-HEADER-TITLE-ORDER', $actor->name)); }
/** * Authorize Read * * @return boolean */ public function canRead() { $actor = pick($this->actor, get_viewer()); //if repository is ownable then ask the actor if viewer can publish things if (in_array($this->getRequest()->get('layout'), array('add', 'edit', 'form', 'composer'))) { $result = $this->getItem() ? $this->canEdit() : $this->canAdd(); return $result; } if (!$this->getItem()) { return false; } //check if an entiy authorize access return $this->getItem()->authorize('access'); }
/** * converts an array to a delimited string * * @param array @array - the array to be flattened, used for recursion * @param string $delimiter - the delimiter to separate keys and values. Defaulted to "=" * @param string $separator - a string used to separate key value pairs. Defaulted to "," * @access public * @return System_Utility_Array */ public function flatten($array = false, $delimiter = '=', $separator = ',') { $array = pick($array, $this->_processed, $this->_value); $flattened = array(); foreach ($array as $key => $val) { if (is_array($val)) { $flattened[] = $this->flatten($val, $delimiter, $separator); } else { $flattened[] = $key . $delimiter . $val; } } $this->_processed = implode($separator, $flattened); return $this; }
/** * Before fetch. * * @param KCommandContext $context */ protected function _beforeRepositoryFetch(KCommandContext $context) { $query = $context->query; //$query->where('@col(comment.id) IS NULL'); $query->where('IF(@col(target.enabled) IS NULL, 1, @col(target.enabled) <> 0) AND IF(@col(subject.enabled) IS NULL, 1, @col(subject.enabled) <> 0)')->link('target', array('type' => 'weak', 'bind_type' => false)); //apply the privacy $privtable = $this->getBehavior('com://site/medium.domain.behavior.privatable'); $query->privacy = pick($query->privacy, new KConfig()); //weak link the stories with object nodes //and use the object.access instead of the story access if there are ny $query->link('object', array('type' => 'weak', 'bind_type' => false)); //we are using the object.access as the privacy reference $query->privacy->append(array('use_access_column' => '@col(object.access)')); $privtable->execute('before.fetch', $context); }
/** * defines the image and image type * TODO: add gd-created image support for the $file argument * * @param string $file -- the fullpath to the file to be loaded. Optional * @access protected * @return System_Utility_File_Image */ protected function define($file = false) { $file = pick($file, $this->_value->get('original')); $this->_image_info = getimagesize($file); $this->_image_type = $mime = $this->_image_info['mime']; if (preg_match('/jpg|jpeg/i', $mime)) { $this->_image = imagecreatefromjpeg($file); } elseif (preg_match('/gif/i', $mime)) { $this->_image = imagecreatefromgif($file); } elseif (preg_match('/png/i', $mime)) { $this->_image = imagecreatefrompng($file); } elseif (preg_match('/bmp/i', $mime)) { $this->_image = imagecreatefromwbmp($file); } return $this; }
/** * Command handler. * * @param string The command name * @param object The command context * * @return bool Can return both true or false. */ public function execute($name, KCommandContext $context) { $parts = explode('.', $name); if ($parts[0] == 'before') { $value = pick($this->{$this->getIdentifiableKey()}, $context->data->{$this->getIdentifiableKey()}); if ($value) { $parent = $this->getParentRepository()->fetch($value); $this->setParent($parent); //set the entity owner as the context actor of the controller if ($parent && $this->getRepository()->isOwnable() && $this->isOwnable()) { $this->setActor($parent->owner); } $context->data['parent'] = $parent; } } }
/** * Render script information. * * @param string The script information * @param bool True, if the script information is a URL. * @param array Associative array of attributes * * @return string */ protected function _renderScript($script, $link, $attribs = array()) { if ($link) { $script = pick($this->getService('com://site/base.template.asset')->getURL($script), $script); } //if ajax try to get the content of the file if (KRequest::type() == 'AJAX') { if ($link) { $file = $this->getService('com://site/base.template.asset')->getFilePath($script); if ($file) { $script = file_get_contents($file); $link = false; } } } return parent::_renderScript($script, $link, $attribs); }
/** * {@inheritdoc} */ protected function _beforeQuerySelect(KCommandContext $context) { if (KService::has('com:people.viewer') && is_person(get_viewer()) && get_viewer()->admin()) { return; } $query = $context->query; $repository = $query->getRepository(); $config = pick($query->privacy, new KConfig()); $config->append(array('visible_to_leaders' => true, 'viewer' => KService::get('com:people.viewer'), 'graph_check' => true)); $query->getRepository()->addBehavior('ownable'); //do a left join operation just in case an owner is missing $query->link('owner', array('type' => 'weak', 'bind_type' => false)); $config->append(array('use_access_column' => '@col(access)')); $c1 = $this->buildCondition('@col(owner.id)', $config, '@col(owner.access)'); $c2 = $this->buildCondition('@col(owner.id)', $config, $config->use_access_column); $where = "IF({$c1}, {$c2}, 0)"; $query->where($where); }
/** * Before _actionGet controller event. * * @param KEvent $event Event object * * @return string */ public function onBeforeControllerGet(KEvent $event) { $this->setActor(get_viewer()); parent::onBeforeControllerGet($event); $data = $event->data; $viewer = get_viewer(); $actor = $viewer; $layout = pick($this->getController()->layout, 'default'); $name = $this->getController()->getIdentifier()->name; if ($name == 'connection') { $name = $this->getController()->service; } $this->setTitle(JText::sprintf('COM-INVITES-ACTOR-HEADER-' . strtoupper($name) . 'S', $actor->name)); //create navigations $this->addNavigation('email', JText::_('COM-INVITES-LINK-EMAIL'), 'option=com_invites&view=email', $name == 'email'); if (ComConnectHelperApi::enabled('facebook')) { $this->addNavigation('facebook', JText::_('COM-INVITES-LINK-FACEBOOK'), 'option=com_invites&view=connections&service=facebook', $name == 'facebook'); } }
/** * Adds packages using the a set of composer files * * @param string $composers * * @return void */ public function addPackageFromComposerFiles($composers) { settype($composers, 'array'); foreach ($composers as $file) { $data = (array) json_decode(file_get_contents($file)); if (isset($data['type']) && $data['type'] == 'anahita-extension') { if (!isset($data['extension-source'])) { $data['extension-source'] = 'src'; } $data['extension-source'] = realpath(dirname($file) . '/src'); if (is_readable($data['extension-source'])) { $parts = array_filter(explode('/', @$data['name'])); $name = strtolower(pick(@$parts[1], basename(dirname($file)))); $vendor = strtolower(pick(@$parts[0], $name)); $this[] = new Package(array('composer_file' => $file, 'name' => $name, 'vendor' => $vendor, 'source' => $data['extension-source'])); } } } }
/** * Before controller action * * @param KEvent $event Event object * * @return string */ public function onBeforeControllerGet(KEvent $event) { parent::onBeforeControllerGet($event); $viewer = $this->getController()->viewer; $actor = pick($this->getController()->actor, $viewer); $layout = pick($this->getController()->getRequest()->layout, 'default'); $name = $this->getController()->getIdentifier()->name; //create title if ($layout == 'upload') { $this->setTitle(JText::sprintf('COM-PHOTOS-UPLOAD-PHOTOS', $actor->name)); } elseif ($name == 'set') { $this->setTitle(JText::sprintf('COM-PHOTOS-HEADER-ACTOR-SETS', $actor->name)); } else { $this->setTitle(JText::sprintf('COM-PHOTOS-HEADER-ACTOR-PHOTOS', $actor->name)); } //create navigations $this->addNavigation('photos', JText::_('COM-PHOTOS-LINKS-PHOTOS'), array('option' => 'com_photos', 'view' => 'photos', 'oid' => $actor->uniqueAlias), $name == 'photo' && in_array($layout, array('default', 'add', 'masonry'))); if ($actor->photos->getTotal() > 0) { $this->addNavigation('sets', JText::_('COM-PHOTOS-LINKS-SETS'), array('option' => 'com_photos', 'view' => 'sets', 'oid' => $actor->uniqueAlias), $name == 'set' && in_array($layout, array('default', 'add', 'edit'))); } }
/** * Token Read. * * @param KCommandContext $context */ protected function _actionRead(KCommandContext $context) { if ($this->token) { $token = $this->getRepository()->find(array('value' => $this->token)); $this->getToolbar('menubar')->setTitle(null); if (!$token || !isset($token->inviter)) { throw new LibBaseControllerExceptionNotFound('Token not found'); return; } if ($this->viewer->guest()) { KRequest::set('session.invite_token', $token->value); } $this->setItem($token); } else { $service = pick($this->service, 'facebook'); $token = $this->getRepository()->getEntity()->reset(); KRequest::set('session.invite_token', $token->value); $this->getView()->value($token->value); return $this->getView()->display(); } }
/** * Return the views output. * * @return string The output of the view */ public function display() { $name = $this->getName(); $data = array(); //if data is set the just json encode those if (count($this->_data)) { $this->output = $this->_data; } elseif (KInflector::isPlural($name)) { $this->output = $this->_getList(); } else { $this->output = $this->_getItem(); } //if null then return empty string $this->output = pick($this->output, ''); if (!is_string($this->output)) { $this->output = json_encode($this->_toArray(KConfig::unbox($this->output))); } //Handle JSONP if (!empty($this->_padding)) { $this->output = $this->_padding . '(' . $this->output . ');'; } return $this->output; }
/** * Context. * * @param KCommandContext $context */ protected function _actionBrowse(KCommandContext $context) { $serviceType = pick($this->service, 'facebook'); if (!ComConnectHelperApi::enabled($serviceType)) { throw new LibBaseControllerExceptionBadRequest('Service is not enabled'); return; } $this->getService('repos://site/connect.session'); $service = $this->viewer->sessions->{$serviceType}; if (!empty($service)) { try { $this->_state->setList($service->getFriends()); } catch (Exception $e) { $session = $this->viewer->sessions->find(array('api' => 'facebook')); if (isset($session)) { $session->delete()->save(); } $service = null; } } else { $service = null; } $this->service = $service; }
/** * Group a set of notifications by date. * * @param array $notifications * * @return array */ public function group($notifications) { $dates = array(); $actor = $this->getTemplate()->getView()->actor; $timezone = pick($actor->timezone, 0); foreach ($notifications as $notification) { $current = AnDomainAttributeDate::getInstance()->addHours($timezone); $diff = $current->compare($notification->creationTime->addHours($timezone)); if ($diff <= AnHelperDate::dayToSeconds('1')) { if ($current->day == $notification->creationTime->day) { $key = JText::_('LIB-AN-DATE-TODAY'); } else { $key = JText::_('LIB-AN-DATE-DAY'); } } else { $key = $this->getTemplate()->renderHelper('date.format', $notification->creationTime, array('format' => '%B %d')); } if (!isset($dates[$key])) { $dates[$key] = array(); } $dates[$key][] = $notification; } return $dates; }
function pick($n, $lat) { return is_zero(sub($n, 1)) ? car($lat) : pick(sub($n, 1), cdr($lat)); }