/** * Create bootstrap tabs. * * @param array $data * @param string $id * @return string * @SuppressWarnings(PHPMD.ShortVariable) */ public function tabs($id, array $data = []) { $i = 0; $output = []; foreach ($data as $key => $options) { $i++; $title = !is_array($options) ? $options : $key; $alias = Text::slug((string) Inflector::underscore($key), '-'); if (is_string($options)) { $options = []; } $_options = ['linkOptions' => ['data-toggle' => 'tab']]; if (isset($options['title'])) { $title = $options['title']; unset($options['title']); } $_options['linkOptions']['title'] = $title; if ($i == 1) { $_options = $this->addClass($_options, 'active'); } $options = Hash::merge($_options, $options); $linkOptions = $options['linkOptions']; unset($options['linkOptions']); $link = $this->Html->link($title, '#' . $alias, $linkOptions); $output[] = $this->Html->tag('li', $link, $options); } return $this->Html->tag('ul', implode('', $output), ['class' => 'nav nav-tabs', 'id' => $id]); }
/** * Slug a field passed in the default config with its replacement. * @param $value The string that needs to be processed * @param $config The config array is passed here * @return string */ private function slug($value = null, $config = []) { // generate slug $slug = strtolower(Text::slug($value, $config['replacement'])); // unique slug? if ($config['unique']) { // does the slug already exist in db? $field = $this->_table->alias() . '.' . $config['slug']; $conditions = [$field => $slug]; $suffix = ''; $i = 0; // loop till unique slug is found while ($this->_table->exists($conditions)) { $i++; $suffix = $config['replacement'] . $i; $conditions[$field] = $slug . $suffix; } // got suffix? append it if ($suffix) { $slug .= $suffix; } } // return slug return $slug; }
/** * Gets the tag slug (virtual field) * @return string|null */ protected function _getSlug() { if (empty($this->_properties['tag'])) { return null; } return Text::slug($this->_properties['tag']); }
/** * Generate slug. * * @param string $string Input string. * @param string $replacement Replacement string. * @return string Sluggified string. */ public function slug($string, $replacement = '-') { $config = $this->config; $config['replacement'] = $replacement; $string = Text::slug($string, $config); if ($config['lowercase']) { return mb_strtolower($string); } return $string; }
public function slug(Entity $entity) { $config = $this->config(); $value = $entity->get($config['field']); $data['plugin'] = $config['plugin']; $data['controller'] = $config['controller']; $data['action'] = $config['action']; $data['pass'] = $entity->get($config['pass']); $redirect = $this->Slugs->find()->where($data)->first(); $data['created'] = date('Y-m-d H:i:s'); $data['slug'] = strtolower(Text::slug($value, $config['replacement'])); // debug($redirect); if (!is_object($redirect)) { $redirect = $this->Slugs->newEntity(); } $redirect = $this->Slugs->patchEntity($redirect, $data); $this->Slugs->save($redirect); }
public function edit($id = NULL, $slug = NULL) { $this->loadModel('Post_Type'); $post_type = $this->Post_Type->get($id); $this->set('title_for_layout', 'Post Type : ' . $post_type->title); if (empty($post_type)) { throw new NotFoundException('Could not find that post type.'); } else { $this->set(compact('post_type')); } if ($this->request->is(['post', 'put'])) { //Validation $validator = new Validator(); $validator->requirePresence('title')->notEmpty('title', 'A title is required.'); $errors_array = $validator->errors($this->request->data()); $error_msg = []; foreach ($errors_array as $errors) { if (is_array($errors)) { foreach ($errors as $error) { $error_msg[] = $error; } } else { $error_msg[] = $errors; } } if (!empty($error_msg)) { $this->Flash->set('Please fix the following error(s): ' . implode('\\n \\r', $error_msg), ['element' => 'alert-box', 'params' => ['class' => 'danger']]); return $this->redirect(['action' => 'edit', $id]); } //Slug $post_type->slug = Text::slug(strtolower($this->request->data('title'))); //Save $this->Post_Type->patchEntity($post_type, $this->request->data); if ($this->Post_Type->save($post_type)) { $this->Flash->set('The post type has been updated.', ['element' => 'alert-box', 'params' => ['class' => 'success']]); return $this->redirect(['action' => 'edit', $id]); } $this->Flash->set('Unable to update post type.', ['element' => 'alert-box', 'params' => ['class' => 'danger']]); } }
/** * Lists posts for a tag * @param string $tag Tag name * @return \Cake\Network\Response|null|void * @throws RecordNotFoundException */ public function view($tag = null) { //Data can be passed as query string, from a widget if ($this->request->query('q')) { return $this->redirect([$this->request->query('q')]); } $page = $this->request->query('page') ? $this->request->query('page') : 1; //Sets the cache name $cache = sprintf('tag_%s_limit_%s_page_%s', md5($tag), $this->paginate['limit'], $page); //Tries to get data from the cache list($posts, $paging) = array_values(Cache::readMany([$cache, sprintf('%s_paging', $cache)], $this->PostsTags->cache)); //If the data are not available from the cache if (empty($posts) || empty($paging)) { $query = $this->PostsTags->Posts->find('active')->contain(['Categories' => function ($q) { return $q->select(['title', 'slug']); }, 'Tags' => function ($q) { return $q->order(['tag' => 'ASC']); }, 'Users' => function ($q) { return $q->select(['first_name', 'last_name']); }])->matching('Tags', function ($q) use($tag) { return $q->where(['Tags.tag' => Text::slug($tag, ['replacement' => ' '])]); })->select(['id', 'title', 'subtitle', 'slug', 'text', 'created'])->order([sprintf('%s.created', $this->PostsTags->Posts->alias()) => 'DESC']); if ($query->isEmpty()) { throw new RecordNotFoundException(__d('me_cms', 'Record not found')); } $posts = $this->paginate($query)->toArray(); //Writes on cache Cache::writeMany([$cache => $posts, sprintf('%s_paging', $cache) => $this->request->param('paging')], $this->PostsTags->cache); //Else, sets the paging parameter } else { $this->request->params['paging'] = $paging; } $this->set(am(['tag' => $posts[0]->tags[array_search($tag, array_map(function ($tag) { return $tag->tag; }, $posts[0]->tags))]], compact('posts'))); }
public function edit($id = NULL, $slug = NULL) { //Post Types $this->loadModel('Post_Type'); $post_type_ids = $this->Post_Type->find('list'); $this->set(compact('post_type_ids')); //Categories $this->loadModel('Categories'); $category_ids = $this->Categories->find('list'); $this->set(compact('category_ids')); $this->loadModel('Articles'); $article = $this->Articles->get($id); $this->set('title_for_layout', 'Article : ' . $article->title); if (empty($article)) { throw new NotFoundException('Could not find that article.'); } else { $selected_categories = explode(',', $article->category_id); $this->set(compact('article')); $this->set(compact('selected_categories')); } if ($this->request->is(['post', 'put'])) { //Validation $validator = new Validator(); $validator->requirePresence('post_type_id')->notEmpty('post_type_id', 'A post type is required.')->requirePresence('title')->notEmpty('title', 'A title is required.'); $errors_array = $validator->errors($this->request->data, false); $error_msg = []; foreach ($errors_array as $errors) { if (is_array($errors)) { foreach ($errors as $error) { $error_msg[] = $error; } } else { $error_msg[] = $errors; } } if (!empty($error_msg)) { $this->Flash->set('Please fix the following error(s): ' . implode('\\n \\r', $error_msg), ['element' => 'alert-box', 'params' => ['class' => 'danger']]); return $this->redirect(['action' => 'edit', $id]); } //Slug $article->slug = Text::slug(strtolower($this->request->data('title'))); //User ID $current_user = $this->Auth->user(); $article->user_id = $current_user['id']; //Date $article->updated_at = date('Y-m-d H:i:s'); //Category array $cat_array = $this->request->data('category_id'); $cat_final = implode(',', $cat_array); $this->request->data['category_id'] = $cat_final; //Save $this->Articles->patchEntity($article, $this->request->data); if ($this->Articles->save($article)) { $this->Flash->set('The article has been updated.', ['element' => 'alert-box', 'params' => ['class' => 'success']]); return $this->redirect(['action' => 'edit', $id]); } $this->Flash->set('Unable to update article.', ['element' => 'alert-box', 'params' => ['class' => 'danger']]); } }
/** * testSlug method * * @param string $string String * @param array $options Options * @param String $expected Exepected string * @return void * @dataProvider slugInputProvider */ public function testSlug($string, $options, $expected) { $result = Text::slug($string, $options); $this->assertEquals($expected, $result); }
/** * Set the section css class that is applied to the html body of the action. * Format is "prefix--controller-action" where prefix is either "app" or the name of the plugin. * * @return void */ protected function _setSectionCssClass() { $plugin = $this->request->params['plugin']; $prefix = $plugin !== null ? $plugin : 'app'; $this->set('sectionCssClass', strtolower(Text::slug($prefix) . '--' . preg_replace('/\\//', '--', $this->request->params['controller']) . '-' . $this->request->params['action'])); }