Tag / taxonomy enabling made easy, fixes #914

- Enable tags for a taxonomy in one go
- Have an indicator of how many tags there are in the taxonomy and how many are enabled
pull/919/head
Iglocska 2016-02-04 18:21:02 +01:00
parent cf62635655
commit 9ad6f93e7d
3 changed files with 54 additions and 22 deletions

View File

@ -11,6 +11,12 @@ class TaxonomiesController extends AppController {
public $paginate = array(
'limit' => 60,
'maxLimit' => 9999, // LATER we will bump here on a problem once we have more than 9999 events <- no we won't, this is the max a user van view/page.
'contain' => array(
'TaxonomyPredicate' => array(
'fields' => array('TaxonomyPredicate.id'),
'TaxonomyEntry' => array('fields' => array('TaxonomyEntry.id'))
)
),
'order' => array(
'Taxonomy.id' => 'DESC'
),
@ -18,7 +24,18 @@ class TaxonomiesController extends AppController {
public function index() {
$this->paginate['recursive'] = -1;
$this->set('taxonomies', $this->paginate());
$taxonomies = $this->paginate();
$this->loadModel('Tag');
foreach ($taxonomies as &$taxonomy) {
$total = 0;
foreach ($taxonomy['TaxonomyPredicate'] as &$predicate) {
$total += empty($predicate['TaxonomyEntry']) ? 1 : count($predicate['TaxonomyEntry']);
}
$taxonomy['total_count'] = $total;
$taxonomy['current_count'] = $this->Tag->find('count', array('conditions' => array('lower(Tag.name) LIKE ' => strtolower($taxonomy['Taxonomy']['namespace']) . ':%')));
unset($taxonomy['TaxonomyPredicate']);
}
$this->set('taxonomies', $taxonomies);
}
public function view($id) {
@ -160,19 +177,24 @@ class TaxonomiesController extends AppController {
$this->redirect(array('controller' => 'taxonomies', 'action' => 'index'));
}
public function addTag() {
public function addTag($taxonomy_id = false) {
if ((!$this->_isSiteAdmin() && !$this->userRole['perm_tagger']) || !$this->request->is('post')) throw new NotFoundException('You don\'t have permission to do that.');
if (isset($this->request->data['Taxonomy'])) {
$this->request->data['Tag'] = $this->request->data['Taxonomy'];
unset($this->request->data['Taxonomy']);
}
if (isset($this->request->data['Tag']['request'])) $this->request->data['Tag'] = $this->request->data['Tag']['request'];
if (!isset($this->request->data['Tag']['nameList'])) $this->request->data['Tag']['nameList'] = array($this->request->data['Tag']['name']);
else $this->request->data['Tag']['nameList'] = json_decode($this->request->data['Tag']['nameList'], true);
if ($this->Taxonomy->addTags($this->request->data['Tag']['taxonomy_id'], $this->request->data['Tag']['nameList'])) {
$this->Session->setFlash('The tag has been saved.');
if ($taxonomy_id) {
$result = $this->Taxonomy->addTags($taxonomy_id);
} else {
$this->Session->setFlash('The tag could not be saved. Please, try again.');
if (isset($this->request->data['Taxonomy'])) {
$this->request->data['Tag'] = $this->request->data['Taxonomy'];
unset($this->request->data['Taxonomy']);
}
if (isset($this->request->data['Tag']['request'])) $this->request->data['Tag'] = $this->request->data['Tag']['request'];
if (!isset($this->request->data['Tag']['nameList'])) $this->request->data['Tag']['nameList'] = array($this->request->data['Tag']['name']);
else $this->request->data['Tag']['nameList'] = json_decode($this->request->data['Tag']['nameList'], true);
$result = $this->Taxonomy->addTags($this->request->data['Tag']['taxonomy_id'], $this->request->data['Tag']['nameList']);
}
if ($result) {
$this->Session->setFlash('The tag(s) has been saved.');
} else {
$this->Session->setFlash('The tag(s) could not be saved. Please, try again.');
}
$this->redirect($this->referer());
}

View File

@ -199,8 +199,8 @@ class Taxonomy extends AppModel{
}
}
public function addTags($id, $tagList) {
if (!is_array($tagList)) $tagList = array($tagList);
public function addTags($id, $tagList = false) {
if ($tagList && !is_array($tagList)) $tagList = array($tagList);
$this->Tag = ClassRegistry::init('Tag');
App::uses('ColourPaletteTool', 'Tools');
$paletteTool = new ColourPaletteTool();
@ -209,14 +209,22 @@ class Taxonomy extends AppModel{
$tags = $this->Tag->getTagsForNamespace($taxonomy['Taxonomy']['namespace']);
$colours = $paletteTool->generatePaletteFromString($taxonomy['Taxonomy']['namespace'], count($taxonomy['entries']));
foreach ($taxonomy['entries'] as $k => &$entry) {
foreach ($tagList as $tagName) {
if ($tagName === $entry['tag']) {
if (isset($tags[strtoupper($entry['tag'])])) {
$this->Tag->quickEdit($tags[strtoupper($entry['tag'])], $tagName, $colours[$k]);
} else {
$this->Tag->quickAdd($tagName, $colours[$k]);
if ($tagList) {
foreach ($tagList as $tagName) {
if ($tagName === $entry['tag']) {
if (isset($tags[strtoupper($entry['tag'])])) {
$this->Tag->quickEdit($tags[strtoupper($entry['tag'])], $tagName, $colours[$k]);
} else {
$this->Tag->quickAdd($tagName, $colours[$k]);
}
}
}
} else {
if (isset($tags[strtoupper($entry['tag'])])) {
$this->Tag->quickEdit($tags[strtoupper($entry['tag'])], $entry['tag'], $colours[$k]);
} else {
$this->Tag->quickAdd($entry['tag'], $colours[$k]);
}
}
}
return true;

View File

@ -23,6 +23,7 @@
<th><?php echo $this->Paginator->sort('description');?></th>
<th><?php echo $this->Paginator->sort('version');?></th>
<th><?php echo $this->Paginator->sort('enabled');?></th>
<th>Active Tags</th>
<th class="actions"><?php echo __('Actions');?></th>
</tr><?php
foreach ($taxonomies as $item): ?>
@ -31,11 +32,12 @@ foreach ($taxonomies as $item): ?>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/taxonomies/view/".h($item['Taxonomy']['id']);?>'"><?php echo h($item['Taxonomy']['namespace']); ?>&nbsp;</td>
<td ondblclick="document.location.href ='<?php echo $baseurl."/taxonomies/view/".h($item['Taxonomy']['id']);?>'"><?php echo h($item['Taxonomy']['description']); ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/taxonomies/view/".h($item['Taxonomy']['id']);?>'"><?php echo h($item['Taxonomy']['version']); ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/taxonomies/view/".h($item['Taxonomy']['id']);?>'"><?php echo $item['Taxonomy']['enabled'] ? '<span class="green">Yes</span>' : '<span class="red">No</span>'; ?>&nbsp;</td>
<td class="short" ondblclick="document.location.href ='<?php echo $baseurl."/taxonomies/view/".h($item['Taxonomy']['id']);?>'"><?php echo $item['Taxonomy']['enabled'] ? '<span class="green">Yes</span>' : '<span class="red">No</span>'; ?>&nbsp;</td>
<td class="shortish"><span><span class="bold"><?php echo h($item['current_count']);?></span> / <?php echo h($item['total_count']);?> <?php if ($item['current_count'] != $item['total_count'] && $isSiteAdmin && $item['Taxonomy']['enabled']) echo '(' . $this->Form->postLink('enable all', array('action' => 'addTag', h($item['Taxonomy']['id'])), array('title' => 'Enable all tags'), ('Are you sure you want to enable every tag associated to this taxonomy?')) . ')'; ?></span></td>
<td class="short action-links">
<?php
if ($isSiteAdmin) {
if ($item['Taxonomy']['enabled']) {
if ($item['Taxonomy']['enabled']) {
echo $this->Form->postLink('', array('action' => 'disable', h($item['Taxonomy']['id'])), array('class' => 'icon-minus', 'title' => 'Disable'), ('Are you sure you want to disable this taxonomy library?'));
} else {
echo $this->Form->postLink('', array('action' => 'enable', h($item['Taxonomy']['id'])), array('class' => 'icon-plus', 'title' => 'Enable'), ('Are you sure you want to enable this taxonomy library?'));