Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hierarchical relations across tags #2435

Merged
merged 12 commits into from
Sep 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions config/Migrations/20200820134346_CreateCategoriesTree.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
use Migrations\AbstractMigration;

class CreateCategoriesTree extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('categories_tree');
$table->addColumn('name', 'string', [
'default' => null,
'limit' => 255,
'null' => false,
]);
$table->addColumn('description', 'text', [
'default' => null,
'null' => true,
]);
$table->addColumn('parent_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
]);
$table->addColumn('lft', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
]);
$table->addColumn('rght', 'integer', [
'default' => null,
'limit' => 11,
'null' => true,
]);
$table->create();
}
}
23 changes: 23 additions & 0 deletions config/Migrations/20200820134407_AddCategoryIdToTags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
use Migrations\AbstractMigration;

class AddCategoryIdToTags extends AbstractMigration
{
/**
* Change Method.
*
* More information on this method is available here:
* http://docs.phinx.org/en/latest/migrations.html#the-change-method
* @return void
*/
public function change()
{
$table = $this->table('tags');
$table->addColumn('category_id', 'integer', [
'default' => null,
'limit' => 11,
'null' => false,
]);
$table->update();
}
}
1 change: 1 addition & 0 deletions config/auth_actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
],
'sentences_lists' => [ '*' => User::ROLE_CONTRIBUTOR_OR_HIGHER ],
'tags' => [ '*' => User::ROLE_ADV_CONTRIBUTOR_OR_HIGHER ],
'categories_tree' => [ '*' => User::ROLE_ADV_CONTRIBUTOR_OR_HIGHER ],
'transcriptions' => [ '*' => User::ROLE_CONTRIBUTOR_OR_HIGHER ],
'user' => [ '*' => User::ROLE_CONTRIBUTOR_OR_HIGHER ],
'users' => [ '*' => [ User::ROLE_ADMIN ] ],
Expand Down
120 changes: 120 additions & 0 deletions src/Controller/CategoriesTreeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* Tatoeba Project, free collaborative creation of multilingual corpuses project
* Copyright (C) 2020 Tatoeba Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Controller;

use App\Controller\AppController;


class CategoriesTreeController extends AppController
{
/**
* Controller name
*
* @var string
* @access public
*/
public $name = 'CategoriesTree';
public $components = ['CommonSentence', 'Flash'];
public $helpers = ['Pagination'];

public function manage(){
$this->loadModel('Tags');
$tags = [];
$tagList = $this->Tags->find('all')
->select(['id', 'name', 'nbrOfSentences', 'category_id'])
->order(['name']);
foreach ($tagList as $tag) {
$category = ($tag['category_id'] == null) ? -1 : $tag['category_id'];
if (!array_key_exists($category, $tags)) {
$tags[$category] = [];
}
array_push($tags[$category], [
'id' => $tag['id'],
'name' => $tag['name'],
'nbrOfSentences' => $tag['nbrOfSentences']
]);

}

$tree = $this->CategoriesTree->find('threaded', [
'order' => 'name'
]);

$this->set('tags', $tags);
$this->set('tree', $tree);
}

public function createorEditCategory() {
$name = $this->request->data('name');
$description = $this->request->data('description');
$parentName = $this->request->data('parentName');

$res = $this->CategoriesTree->createOrEdit($name, $description, $parentName);

return $this->redirect([
'controller' => 'categories_tree',
'action' => 'manage',
'?' => ['createCategory' => $res],
]);
}

public function removeCategory($categoryId) {
$res = $this->CategoriesTree->remove($categoryId);

return $this->redirect([
'controller' => 'categories_tree',
'action' => 'manage',
'?' => ['removeCategory' => $res],
]);
}

public function attachTagToCategory() {
$tagName = $this->request->data('tagName');
$categoryName = $this->request->data('categoryName');

$this->loadModel('Tags');
$res = $this->Tags->attachToCategory($tagName, $categoryName);

return $this->redirect([
'controller' => 'categories_tree',
'action' => 'manage',
'?' => ['attachTagToCategory' => $res],
]);
}

public function detachTagFromCategory($tagId) {
$this->loadModel('Tags');
$res = $this->Tags->detachFromCategory($tagId);

return $this->redirect([
'controller' => 'categories_tree',
'action' => 'manage',
'?' => ['detachTagFromCategory' => $res],
]);
}

public function autocomplete($search) {
$results = $this->CategoriesTree->Autocomplete($search);

$this->loadComponent('RequestHandler');
$this->set('results', $results);
$this->set('_serialize', ['results']);
$this->RequestHandler->renderAs($this, 'json');
}
}
20 changes: 3 additions & 17 deletions src/Controller/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,27 +242,13 @@ public function search()
]);
}

/**
* Display list of tags for autocompletion.
*
* @param String $filter Filters the tags list with only those that contain the
* search string.
*/
public function autocomplete($search)
{
$this->helpers[] = 'Tags';

$query = $this->Tags->find();
$query->select(['name', 'id', 'nbrOfSentences']);
if (!empty($search)) {
$pattern = str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $search).'%';
$query->where(['name LIKE :search'])->bind(':search', $pattern, 'string');
}
$allTags = $query->order(['nbrOfSentences' => 'DESC'])->limit(10)->all();
$allTags = $this->Tags->Autocomplete($search);

$this->loadComponent('RequestHandler');
$this->set('allTags', $allTags);
$this->set('_serialize', ['allTags']);
$this->set('results', $allTags);
$this->set('_serialize', ['results']);
$this->RequestHandler->renderAs($this, 'json');
}
}
73 changes: 73 additions & 0 deletions src/Model/Behavior/AutocompletableBehavior.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php
/**
* Tatoeba Project, free collaborative creation of languages corpuses project
* Copyright (C) 2020 Tatoeba Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Model\Behavior;

use Cake\ORM\Behavior;
use Cake\ORM\Query;

/**
* Behavior for handling autocompletion
*
* This behavior adds the method 'Autocomplete' to a table which returns
* the top n results that contain a given query
*/
class AutocompletableBehavior extends Behavior
{
/**
* Default config
*
* index column to search from
* fields columns to return
*
* @var array
*/
protected $_defaultConfig = [
'implementedMethods' =>
['Autocomplete' => 'Autocomplete'],
'index' => 'name',
'fields' => ['id', 'name'],
'order' => [],
'limit' => 10
];

public function initialize(array $config)
{
foreach (['index', 'fields', 'order', 'limit'] as $conf) {
if (isset($config[$conf])) {
$this->_config[$conf] = $config[$conf];
}
}
}

public function Autocomplete($search)
{
$query = $this->getTable()->find();
$query->select($this->getConfig('fields'));

if (!empty($search)) {
$pattern = str_replace(['\\', '%', '_'], ['\\\\', '\%', '\_'], $search).'%';
$query->where(["{$this->getConfig('index')} LIKE" => $pattern]);
}

$query->order($this->getConfig('order'));
$query->limit($this->getConfig('limit'));

return $query->all();
}
}
92 changes: 92 additions & 0 deletions src/Model/Table/CategoriesTreeTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Tatoeba Project, free collaborative creation of multilingual corpuses project
* Copyright (C) 2020 Tatoeba Project
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace App\Model\Table;

use Cake\ORM\Table;

class CategoriesTreeTable extends Table
{
public function initialize(array $config)
{
$this->hasMany('Tags')->setForeignKey('category_id');

$this->addBehavior('Tree');
$this->addBehavior('Autocompletable', [
'order' => ['name']
]);
}

/**
* Add a tag category
*
* @param String $name
* @param String $description
* @param Integer $parentId
*
* @return bool
*/
public function createOrEdit($name, $description, $parentName)
{
if (empty($name))
return false;
else {
$data = $this->newEntity([
'name' => $name,
'description' => $description,
'parent_id' => $this->getIdFromName($parentName)
]);

$query = $this->find('all')->select(['id'])->where(['name' => $name]);
if (!$query->isEmpty()) {
$id = $query->first()['id'];
$data->set('id', $id);
}
return $this->save($data);
}

}

/**
* Remove a tag category
*
* @param Integer $categoryId
*/
public function remove($categoryId)
{
// check this category contains no tag
if ($this->Tags->exists(['category_id' => $categoryId]))
return false;

// check this category contains no other category
if ($this->exists(['parent_id' => $categoryId]))
return false;

return $this->deleteAll(['id' => $categoryId]);
}

public function getIdFromName($name) {
$result = $this->find('all')
->where(['name' => $name])
->select(['id'])
->first();

return $result ? $result->id : null;
}

}
Loading