Exemple #1
0
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;
        }
    }
}
 /**
  * Sets list of applied tags (from form)
  *
  * @param	mixed	$tags
  */
 public function set_tags($article_tags)
 {
     is_array($article_tags) or $article_tags = explode(',', $article_tags);
     $topics = array();
     $tags = array();
     class_exists('tag_service') or (require VIVVO_FS_INSTALL_ROOT . 'lib/vivvo/service/Tags.service.php');
     $tag_service = new tag_service();
     foreach ($article_tags as &$pair) {
         if (!preg_match('/^\\d+:\\d+$/', $pair)) {
             $name = trim(preg_replace('/(^\\[|\\]$)/', '', $pair));
             if (!$name) {
                 $pair = false;
                 continue;
             }
             $topic_id = 0;
             $tag_id = $tag_service->add_tag($name, make_sefriendly($name));
         } else {
             list($topic_id, $tag_id) = explode(':', $pair, 2);
         }
         if (!$tag_id) {
             $pair = false;
             continue;
         }
         $topics[$topic_id] = 1;
         $tags[$tag_id] = 1;
         $pair = array('tag_id' => $tag_id, 'topic_id' => $topic_id);
     }
     unset($pair);
     $topic_ids = secure_sql_in(array_keys($topics));
     $tag_ids = secure_sql_in(array_keys($tags));
     $db = vivvo_lite_site::get_instance()->get_db();
     $res = $db->query('SELECT id, name FROM ' . VIVVO_DB_PREFIX . "tags WHERE id IN ({$tag_ids})");
     if (PEAR::isError($res)) {
         return;
     }
     $tags = $res->fetchAll(MDB2_FETCHMODE_ASSOC, true);
     $res = $db->query('SELECT id, name FROM ' . VIVVO_DB_PREFIX . "tags_groups WHERE id IN ({$topic_ids})");
     if (PEAR::isError($res)) {
         return;
     }
     $topics = $res->fetchAll(MDB2_FETCHMODE_ASSOC, true);
     $this->applied_tags = array();
     foreach ($article_tags as $pair) {
         if ($pair and isset($tags[$pair['tag_id']]) and isset($topics[$pair['topic_id']])) {
             $this->applied_tags[] = array_merge($pair, array('tag_name' => $tags[$pair['tag_id']], 'topic_name' => $topics[$pair['topic_id']]));
         }
     }
 }
 /**
  * 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));
         }
     }
 }