/** * Method that adds footer element to the Layout. * * @param Cake\Event\Event $event Event object * @return void */ public function getFooter(Event $event) { if (!$event->subject()->elementExists(static::ELEMENT_FOOTER)) { return; } $event->result = $event->subject()->element(static::ELEMENT_FOOTER); }
/** * Method that prepares entity to run through pretiffy logic. * * @param \Cake\ORM\Entity $entity Entity * @param \Cake\Event\Event $event Event instance * @return void */ protected function _prettifyEntity(Entity $entity, Event $event) { if (!in_array($event->subject()->request->query('format'), [static::FORMAT_PRETTY])) { return; } $this->_prettify($entity, $event->subject()->{$event->subject()->name}, []); }
public function construct(Event $event) { // @codingStandardsIgnoreEnd // @codingStandardsIgnoreStart // CakePHP specific tags $event->subject()->getTwig()->addTokenParser(new TokenParser\Cell()); $event->subject()->getTwig()->addTokenParser(new TokenParser\Element()); // @codingStandardsIgnoreEnd }
/** * getMyHead method * * In case we're operating with dynamic CSV tables, * we want to overwrite the page title to be used as moduleAlias(). * * @param Cake\Event\Event $event used for getting reports * @return void */ public function getMyHead(Event $event) { $table = TableRegistry::get($event->subject()->request['controller']); if ($table) { if (method_exists($table, 'moduleAlias') && is_callable([$table, 'moduleAlias'])) { $event->subject()->assign('title', $table->moduleAlias()); } } }
/** * Automatically parse and contain related table classes * * @param \Cake\Event\Event $event Before paginate event * @return void */ public function beforePaginate(Event $event) { $contained = $event->subject()->query->contain(); if (!empty($contained)) { return; } $models = $this->models(); if (empty($models)) { return; } $event->subject()->query->contain(array_keys($models)); }
/** * Data collection callback. * * @param \Cake\Event\Event $event The shutdown event. * @return void */ public function shutdown(Event $event) { $controller = $event->subject(); /* @var \Cake\Network\Request $request */ $request = $controller ? $controller->request : null; $this->_data = ['matchedRoute' => $request ? $request->param('_matchedRoute') : null]; }
/** * Set To Html * * @param Cake\Event\Event $event Event * @return mixed void or null if no uri founded */ public function seoToHtml(Event $event) { if (array_key_exists('prefix', $this->_controller->request->params) && in_array($this->_controller->request->params['prefix'], $this->config('excludePrefix'))) { return; } $uri = $this->getUriDatas(); if (!$uri) { return null; } $event->subject()->assign('title', $this->getTitle($uri)); if ($uri->seo_meta_tags) { $metas = $this->getMetaTags($uri->seo_meta_tags); $event->subject()->append('meta', implode('', $metas)); } $event->subject()->prepend('meta', $this->getCanonicalTag($uri)); }
/** * Shutdown event * * @param \Cake\Event\Event $event The event * @return void */ public function shutdown(Event $event) { $controller = $event->subject(); $errors = []; $walker = function (&$item) use(&$walker) { if ($item instanceof Query || $item instanceof ResultSet) { $item = $item->toArray(); } elseif ($item instanceof Closure || $item instanceof PDO || $item instanceof SimpleXmlElement) { $item = 'Unserializable object - ' . get_class($item); } elseif ($item instanceof Exception) { $item = sprintf('Unserializable object - %s. Error: %s in %s, line %s', get_class($item), $item->getMessage(), $item->getFile(), $item->getLine()); } elseif (is_object($item) && method_exists($item, '__debugInfo')) { // Convert objects into using __debugInfo. $item = array_map($walker, $item->__debugInfo()); } return $item; }; // Copy so viewVars is not mutated. $vars = $controller->viewVars; array_walk_recursive($vars, $walker); foreach ($vars as $k => $v) { // Get the validation errors for Entity if ($v instanceof EntityInterface) { $errors[$k] = $this->_getErrors($v); } elseif ($v instanceof Form) { $formError = $v->errors(); if (!empty($formError)) { $errors[$k] = $formError; } } } $this->_data = ['content' => $vars, 'errors' => $errors]; }
public function injectEditor(Event $event, $layoutFile) { $_view = $event->subject(); $content = $_view->fetch('content'); if (Configure::read('Editorial.autoload')) { $searchClass = Configure::read('Editorial.autoload'); if (empty($searchClass)) { $searchClass = 'editor'; } $plugin = Configure::read('Editorial.editor'); list($vendor, $class) = $this->vendorSplit($plugin); $searchRegex = '/(<textarea.*class\\=\\".*' . Configure::read('Editorial.class') . '.*\\"[^>]*>.*<\\/textarea>)/isU'; //preg_match_all($searchRegex, $content, $matches); //debug($matches); if (Plugin::loaded($plugin) !== false && preg_match_all($searchRegex, $content, $matches)) { if (!$_view->helpers()->has('Editor')) { $options['className'] = $class . '.' . $class; if ($vendor) { $options['className'] = $vendor . '/' . $options['className']; } $options['options'] = $plugin . '.defaults'; if ($editorDefaults = Configure::read('Editorial.' . $class . '.defaults')) { $options['options'] = $editorDefaults; } $_view->loadHelper('Editor', $options); $_view->Editor->initialize(); } $_view->Editor->connect($content); } } }
/** * Callback * * @param \Cake\Event\Event $event * @return \Cake\Network\Response|array|null */ public function beforeFilter(Event $event) { $this->Controller = $event->subject(); if (!$this->config('enabled')) { return null; } if ($actions = $this->config('actions')) { $action = !empty($this->Controller->request->params['action']) ? $this->Controller->request->params['action'] : ''; if (!in_array($action, $actions)) { return null; } } $this->Controller->request->params['isJson'] = isset($this->Controller->request->params['url']['_ext']) && $this->Controller->request->params['url']['_ext'] === 'json'; $modelName = $this->config('modelName'); if (empty($modelName)) { $modelName = $this->Controller->modelClass; } list(, $modelName) = pluginSplit($modelName); $this->config('modelName', $modelName); if (!$this->Controller->{$modelName}->behaviors()->has('Ratable')) { $this->Controller->{$modelName}->behaviors()->load('Ratings.Ratable', $this->_config); } $this->Controller->helpers[] = 'Ratings.Rating'; $params = $this->request->data + $this->request->query + $this->_config['params']; if (!method_exists($this->Controller, 'rate')) { if (isset($params['rate']) && isset($params['rating'])) { $userId = $this->config('userId') ?: $this->Controller->Auth->user($this->config('userIdField')); return $this->rate($params['rate'], $params['rating'], $userId, $params['redirect']); } } }
/** * Check * * @param \Cake\Event\Event $event The beforeFind event that was fired. * @param \Cake\ORM\Query $query Query * @param \ArrayObject $options The options for the query * @return void */ public function checkRecordAccess(Event $event, Query $query, ArrayObject $options) { $table = TableRegistry::get('RolesCapabilities.Capabilities'); // current request parameters $request = $table->getCurrentRequest(); // skip if current model does not match request's model if (array_diff(pluginSplit($event->subject()->registryAlias()), [$request['plugin'], $request['controller']])) { return; } // get capability owner type identifier $type = $table->getTypeOwner(); // get user's action capabilities $userActionCapabilities = $table->getUserActionCapabilities(); // skip if no user's action capabilities found or no user's action // owner specific capabilities found for current request's action if (empty($userActionCapabilities)) { return; } if (!isset($userActionCapabilities[$request['plugin']][$request['controller']][$request['action']][$type])) { return; } // set query where clause based on user's owner capabilities assignment fields foreach ($userActionCapabilities[$request['plugin']][$request['controller']][$request['action']][$type] as $userActionCapability) { $query->where([$userActionCapability->getField() => $table->getCurrentUser('id')]); } }
/** * beforeFilter initTabsItems * * @param Cake/Event/Event $event Event * @return void */ public function beforeFiler(Event $event) { $this->setController($event->subject()); if (method_exists($this->Controller, 'initTabsItems')) { $this->Controller->initTabsItems($event); } }
/** * Send an activation link on success registered user * @param Event $event */ public function onSuccessRegister(Event $event) { $controller = $event->subject(); $user = $event->data['user']; $controller->_sendActivationEmail($user)->template('Bazibartar.register')->subject('فعال سازی حساب کاربری')->send(); $controller->Flash->success(__d('users', 'you are success register, and an activation email has been send, check your email')); }
/** * Startup callback. * * @param Event $event */ public function startup(Event $event) { if (!$event->subject()->isAdmin()) { $this->__setForLayout(); $this->__createModules(); } }
/** * Data collection callback. * * @param \Cake\Event\Event $event The shutdown event. * @return void */ public function shutdown(Event $event) { /* @var Controller $controller */ $controller = $event->subject(); $request = $controller->request; $this->_data = ['params' => $request->params, 'query' => $request->query, 'data' => $request->data, 'cookie' => $request->cookies, 'get' => $_GET, 'matchedRoute' => $request->param('_matchedRoute'), 'headers' => ['response' => headers_sent($file, $line), 'file' => $file, 'line' => $line]]; }
/** * Tests the subject() method * * @return void * @triggers fake.event $this * @triggers fake.event */ public function testSubject() { $event = new Event('fake.event', $this); $this->assertSame($this, $event->subject()); $event = new Event('fake.event'); $this->assertNull($event->subject()); }
/** * Shutdown event * * @param \Cake\Event\Event $event The event * @return void */ public function shutdown(Event $event) { $controller = $event->subject(); $errors = []; array_walk_recursive($controller->viewVars, function (&$item) { // Execute queries so we can show the results in the toolbar. if ($item instanceof Query) { $item = $item->all(); } if ($item instanceof Closure || $item instanceof PDO || $item instanceof SimpleXmlElement) { $item = 'Unserializable object - ' . get_class($item); } if ($item instanceof Exception) { $item = sprintf('Unserializable object - %s. Error: %s in %s, line %s', get_class($item), $item->getMessage(), $item->getFile(), $item->getLine()); } return $item; }); foreach ($controller->viewVars as $k => $v) { // Get the validation errors for Entity if ($v instanceof EntityInterface) { $errors[$k] = $this->_getErrors($v); } elseif ($v instanceof Form) { $formError = $v->errors(); if (!empty($formError)) { $errors[$k] = $formError; } } } $this->_data = ['content' => $controller->viewVars, 'errors' => $errors]; }
/** * shutdown callback * * @param \Cake\Event\Event $event The event * @return array */ public function shutdown(Event $event) { $request = $event->subject()->request; if ($request) { $this->_data = ['content' => $request->session()->read()]; } }
/** * Hook admin actions * @param Event $event */ public function onBeforeAdminTemplateStructure(Event $event) { $this->_View = $view = $event->subject(); $this->__hookAdminActions(); $this->__hookAdminBoxes(); $this->__hookAdminForms(); }
/** * Callback fired before the output is sent to the browser and launches the * event on websockets if need be. * * @param \Cake\Controller\ComponentRegistry $collection A ComponentCollection * this component can use to lazy load its components. */ public function shutdown(Event $event) { $action = $event->subject()->request->action; if (!isset($event->subject()->viewVars['_ws'])) { return; } $_ws = $event->subject()->viewVars['_ws']; $data = isset($_ws) && isset($_ws['data']) ? $_ws['data'] : null; $users = isset($_ws) && isset($_ws['users']) ? $_ws['users'] : null; if (get_class($users) === 'Cake\\ORM\\ResultSet') { $users = $users->extract('id')->toArray(); } else { // not implemented yet } $trigger = new Trigger($this->_controller); $trigger->publish($data, $users); }
/** * On plugin config action. * * @param Event $event * @return void */ public function onConfig(Event $event) { /** @var AppController $controller */ $controller = $event->subject(); /** @var \Union\Community\Model\Table\RolesTable $roleTable */ $roleTable = TableRegistry::get('Union/Community.Roles'); $controller->set('roles', $roleTable->getTreeList()); }
/** * On setup application. * * @param CakeEvent $event * @return void */ public function onSetup(CakeEvent $event) { /** @var \Union\Core\Controller\AppController $controller */ $controller = $event->subject(); if ($controller->App->isAdmin()) { $this->_onSetupAdmin(); } }
/** * Before render callback. * * @param Event $event * @param $viewFile */ public function beforeRender(Event $event, $viewFile) { $view = $event->subject(); if (isset($view->viewVars['module_for_layout'])) { $this->_positions = $view->viewVars['module_for_layout']; foreach ($this->_positions as $position => $entity) { $this->_View->assign($position, $this->_create($position)); } } }
/** * Shutdown callback * * @param \Cake\Event\Event $event The event. * * @return void */ public function shutdown(Event $event) { /** * @var $controller Controller; */ $controller = $event->subject(); if ($controller instanceof RequestsController) { $this->_injectScriptsAndStyles($controller->response); } }
/** * beforeRender will look for any flash message and add it to the response * * @param Event $event * @return void */ public function beforeRender(Event $event) { $controller = $event->subject(); if (empty($this->messages) || !$this->isApiRequest($controller)) { return; } $flash = [$this->config('key') => $this->messages]; $controller->set($this->config('key'), $flash); $controller->viewVars['_serialize'][] = $this->config('key'); }
public function onAfterSpiderControllerConstruct(Event $event) { $controller = $event->subject(); if ($controller->request->is('ajax')) { $controller->viewBuilder()->autoLayout(false); } if ($this->request->query('debug')) { Configure::write('debug', true); } }
/** * Callback for Controller.startup event. * * @param \Cake\Event\Event $event Event instance. * @return void|\Cake\Network\Response */ public function startup(Event $event) { $controller = $event->subject(); $method = $controller->request->params['action']; $reflectionMethod = new \ReflectionMethod(get_class($controller), $method); $values = $this->getAnnotationValues($reflectionMethod); if (!empty($values)) { $this->anyPermitted($values['permissions'], $values['logical']); } }
/** * Try and attach footprint listener to models. * * It also passing the user record to footprint listener after user is * identified by AuthComponent. * * @param \Cake\Event\Event $event Event. * @return void */ public function footprint(Event $event) { if (!$this->_listener) { $this->_listener = new FootprintListener($this->_getCurrentUser()); } if ($event->name() === 'Auth.afterIdentify') { $this->_listener->setUser($this->_getCurrentUser($event->data)); return; } $event->subject()->eventManager()->attach($this->_listener); }
/** * Checks if the event is triggered for this listener. * * @param \Cake\Event\Event $event Event object. * @return bool */ public function canTrigger(Event $event) { $class = get_class($event->subject()); if (!isset($this->_options['allowedSubject'])) { throw new RuntimeException(self::class . ' Missing subject filter options!'); } if (is_string($this->_options['allowedSubject'])) { $this->_options['allowedSubject'] = [$this->_options['allowedSubject']]; } return in_array($class, $this->_options['allowedSubject']); }
public function change(Event $event, ProfferPath $path) { // Detect and select the right file extension $type = $event->subject()[$path->getField()]['type']; switch ($type) { default: case "image/jpeg": $ext = '.jpg'; break; case "image/png": $ext = '.png'; break; case "image/gif": $ext = '.gif'; break; } $newFilename = md5(uniqid(true)) . $ext; $path->setFilename($newFilename); $event->subject()[$path->getField()]['name'] = $newFilename; return $path; }