function tag_service(&$sm, $action, $command) { require_once dirname(__FILE__) . '/Tags.service.php'; $tag_service = new tag_service($sm); $dm = $sm->get_dump_manager(); $um = $sm->get_url_manager(); $lang = $sm->get_lang(); if ($command == 'add') { $name = $um->get_param('TAG_name'); $sefriendly = $um->get_param('TAG_sefriendly'); if ($tag_service->add_tag($name, $sefriendly)) { $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_ADD_SUCCESS')); vivvo_hooks_manager::call('tag_add_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'addGroup') { $name = $um->get_param('TAG_GROUP_name'); $url = $um->get_param('TAG_GROUP_url'); $template = $um->get_param('TAG_GROUP_template'); $tag_template = $um->get_param('TAG_GROUP_tag_template'); $metadata = $um->get_param_regexp('/^TAG_GROUP_META_/'); $via_ajax = $um->isset_param('json_output'); $new_tags = $um->get_param('TAG_GROUP_new_tags'); if ($group_id = $tag_service->add_tag_group($name, $url, $template, $tag_template, $metadata, $via_ajax, $new_tags)) { $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_GROUP_ADD_SUCCESS')); vivvo_hooks_manager::call('tag_addGroup_postaction'); header('X-Vivvo-Action-Status: 1'); if ($via_ajax) { exit('{"new_tags_group_id":"' . $group_id . '"}'); } return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'edit') { $id = $um->get_param('TAG_id'); $name = $um->get_param('TAG_name'); $sefriendly = $um->get_param('TAG_sefriendly'); if ($tag_service->edit_tag($id, $name, $sefriendly)) { $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_EDIT_SUCCESS')); vivvo_hooks_manager::call('tag_edit_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'editGroup') { $id = $um->get_param('TAG_GROUP_id'); $name = $um->get_param('TAG_GROUP_name'); $url = $um->get_param('TAG_GROUP_url'); $template = $um->get_param('TAG_GROUP_template'); $tag_template = $um->get_param('TAG_GROUP_tag_template'); $metadata = $um->get_param_regexp('/^TAG_GROUP_META_/'); $new_tags = $um->get_param('TAG_GROUP_new_tags'); if ($tag_service->edit_tag_group($id, $name, $url, $template, $tag_template, $metadata, $new_tags)) { $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_GROUP_EDIT_SUCCESS')); vivvo_hooks_manager::call('tag_editGroup_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'delete') { if ($tag_service->delete_tag($um->get_param('TAG_id'), $um->get_param('all_matching'))) { $dm->_dump_pool = array(); $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_DELETE_SUCCESS')); vivvo_hooks_manager::call('tag_delete_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'deleteGroup') { if ($tag_service->delete_tag_group($um->get_param('TAG_GROUP_id'))) { $dm->_dump_pool = array(); $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_GROUP_DELETE_SUCCESS')); vivvo_hooks_manager::call('tag_deleteGroup_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'addTagToGroup') { $tag_id = $um->get_param('TAG_id'); $group_id = $um->get_param('TAG_GROUP_id'); if ($tag_service->add_tag_to_group($tag_id, $group_id)) { $dm->_dump_pool = array(); $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_GROUP_LINK_SUCCESS')); vivvo_hooks_manager::call('tag_addTagToGroup_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } else { if ($command == 'addTagsToTopic') { $tag_names = $um->get_param('TAG_names'); $topic_id = $um->get_param('TAG_topic_id'); if ($tag_service->add_tag_names_to_topic($tag_names, $topic_id)) { $dm->_dump_pool = array(); $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_GROUP_LINK_SUCCESS')); vivvo_hooks_manager::call('tag_addTagsToTopic_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'removeTagFromGroup') { $tag_id = $um->get_param('TAG_id'); $group_id = $um->get_param('TAG_GROUP_id'); if ($tag_service->remove_tag_from_group($tag_id, $group_id)) { $dm->_dump_pool = array(); $dm->add_dump('info', 0, $lang->get_value('LNG_INFO_TAG_GROUP_UNLINK_SUCCESS')); vivvo_hooks_manager::call('tag_removeTagFromGroup_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'deleteLink') { $tag_id = $um->get_param('TAG_link_id'); if ($tag_service->delete_tag_link($tag_id)) { vivvo_hooks_manager::call('tag_deleteLink_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } elseif ($command == 'addLink') { $article_id = $um->get_param('TAG_article_id'); $name = $um->get_param('TAG_name'); if ($tag_service->add_tag_link($article_id, $name)) { vivvo_hooks_manager::call('tag_addLink_postaction'); header('X-Vivvo-Action-Status: 1'); return true; } header('X-Vivvo-Action-Status: 0'); header('X-Vivvo-Action-Error: ' . json_encode($tag_service->get_error_info())); return false; } } }
/** * Manages (add/remove) article tags * * @param array $tags */ public function manageTags($tags, $article_id) { $sm = vivvo_lite_site::get_instance(); $db = $sm->get_db(); $res = $db->query('SELECT CONCAT(tags_group_id, ":", tag_id) FROM ' . VIVVO_DB_PREFIX . "articles_tags WHERE article_id = {$article_id}"); if (!PEAR::isError($res)) { $old = array_map(create_function('$x', 'return $x[0];'), $res->fetchAll()); $new = array(); class_exists('tag_service') or (require VIVVO_FS_INSTALL_ROOT . 'lib/vivvo/service/Tags.service.php'); $tag_service = new tag_service(); foreach ($tags as $pair) { if (!preg_match('/^\\d+:\\d+$/', $pair)) { $name = trim(preg_replace('/(^\\[|\\]$)/', '', $pair)); if (!strlen($name) or ($tag_id = $tag_service->add_tag($name, make_sefriendly($name))) === false) { continue; } $pair = '0:' . $tag_id; $tag_service->add_tag_to_group($tag_id, 0); } $new[] = $pair; } $new = array_unique($new); $del = empty($new) ? $old : array_diff($old, $new); $add = empty($old) ? $new : array_diff($new, $old); foreach ($del as $item) { list($topic_id, $tag_id) = explode(':', $item); $db->query('DELETE FROM ' . VIVVO_DB_PREFIX . "articles_tags WHERE tag_id = {$tag_id} AND tags_group_id = {$topic_id} AND article_id = {$article_id}"); } if (!empty($add)) { $user_id = $sm->user->get_id(); $values = array(); foreach ($add as $item) { list($topic_id, $tag_id) = explode(':', $item); $values[] = "({$tag_id}, {$topic_id}, {$article_id}, {$user_id})"; } $db->query('INSERT INTO ' . VIVVO_DB_PREFIX . 'articles_tags (tag_id, tags_group_id, article_id, user_id) VALUES ' . implode(', ', $values)); } } }