/**
  * @param $is_ajax whether the edit is called by ajax or not
  * @param string $u_action phpbb acp-u_action
  */
 private function handle_edit($u_action, $is_ajax = true)
 {
     if (!$is_ajax) {
         $new_tag_name = $this->request->variable('new_tag_name', '');
         if (empty($new_tag_name)) {
             $tag_id = $this->request->variable('tag_id', 0);
             $old_tag_name = $this->tags_manager->get_tag_by_id($tag_id);
             $this->template->assign_vars(array('U_ACTION' => $this->get_tag_link($u_action, $tag_id) . '&action=edit_non_ajax', 'S_EDIT_TAG_NON_AJAX' => true, 'OLD_TAG_NAME' => $old_tag_name));
             return;
         }
         // now new_tag_name and old_tag_name are set and the normal procedure can take place.
     }
     $old_tag_name = $this->request->variable('old_tag_name', '');
     $new_tag_name = $this->request->variable('new_tag_name', '');
     if (empty($old_tag_name) || empty($new_tag_name)) {
         $error_msg = $this->user->lang('TOPICTAGS_MISSING_TAG_NAMES');
         $this->simple_response($u_action, $error_msg, false);
     } else {
         if ($is_ajax) {
             $old_tag_name = rawurldecode(base64_decode($old_tag_name));
             $new_tag_name = rawurldecode(base64_decode($new_tag_name));
         }
         if ($old_tag_name == $new_tag_name) {
             $error_msg = $this->user->lang('TOPICTAGS_NO_MODIFICATION', $old_tag_name);
             $this->simple_response($u_action, $error_msg, false);
         }
         $old_ids = $this->tags_manager->get_existing_tags(array($old_tag_name), true);
         if (empty($old_ids)) {
             $error_msg = $this->user->lang('TOPICTAGS_TAG_DOES_NOT_EXIST', $old_tag_name);
             $this->simple_response($u_action, $error_msg, false);
         }
         // if we reach here, we know that we got a single valid old tag
         $old_id = $old_ids[0];
         $new_tag_name_clean = $this->tags_manager->clean_tag($new_tag_name);
         $is_valid = $this->tags_manager->is_valid_tag($new_tag_name_clean, true);
         if (!$is_valid) {
             $error_msg = $this->user->lang('TOPICTAGS_TAG_INVALID', $new_tag_name);
             $this->simple_response($u_action, $error_msg, false);
         }
         // old tag exist and new tag is valid
         $new_ids = $this->tags_manager->get_existing_tags(array($new_tag_name), true);
         if (!empty($new_ids)) {
             // new tag exist -> merge
             $new_id = $new_ids[0];
             $new_tag_count = $this->tags_manager->merge($old_id, $new_tag_name, $new_id);
             $msg = $this->user->lang('TOPICTAGS_TAG_MERGED', $new_tag_name_clean);
             $this->simple_response($u_action, $msg, true, array('success' => true, 'merged' => true, 'new_tag_count' => $new_tag_count, 'msg' => base64_encode(rawurlencode($msg))));
         }
         // old tag exist and new tag is valid and does not exist -> rename it
         $this->tags_manager->rename($old_id, $new_tag_name_clean);
         $msg = $this->user->lang('TOPICTAGS_TAG_CHANGED');
         $this->simple_response($u_action, $msg);
     }
 }
Ejemplo n.º 2
0
    public function test_merge()
    {
        global $table_prefix;
        // uses auth, so we set up the mock/stub
        // to allow reading first forum
        $this->auth->expects($this->exactly(2))->method('acl_getf')->with($this->equalTo('f_read'))->willReturn(array(1 => array('f_read' => true)));
        $tag_to_delete = "tag1";
        $tag_to_delete_id = 1;
        $tag_to_keep = "tag2";
        $tag_to_keep_id = 2;
        $result = $this->db->sql_query('SELECT count
			FROM ' . $table_prefix . tables::TAGS . '
			WHERE id=' . $tag_to_keep_id);
        $count = $this->db->sql_fetchfield('count');
        $this->assertEquals(0, $count);
        // 2 assignments but only 1 is readable
        $count_of_assignments = $this->tags_manager->merge($tag_to_delete_id, $tag_to_keep, $tag_to_keep_id);
        $this->assertEquals(1, $count_of_assignments);
        $result = $this->db->sql_query('SELECT COUNT(*) as count
			FROM ' . $table_prefix . tables::TOPICTAGS . '
			WHERE tag_id=' . $tag_to_keep_id);
        $count = $this->db->sql_fetchfield('count');
        $this->assertEquals(2, $count);
        // tag1 must be deleted
        $result = $this->db->sql_query('SELECT COUNT(*) as count
			FROM ' . $table_prefix . tables::TAGS . '
			WHERE id=' . $tag_to_delete_id);
        $count = $this->db->sql_fetchfield('count');
        $this->assertEquals(0, $count);
        // tag2 must be assignet to both topics
        // but only counted once because of tagging disabled forum
        $result = $this->db->sql_query('SELECT count
			FROM ' . $table_prefix . tables::TAGS . '
			WHERE id=' . $tag_to_keep_id);
        $count = $this->db->sql_fetchfield('count');
        $this->assertEquals(1, $count);
        $result = $this->db->sql_query('SELECT topic_id
			FROM ' . $table_prefix . tables::TOPICTAGS . '
			WHERE tag_id=' . $tag_to_keep_id);
        $topics = array();
        while ($row = $this->db->sql_fetchrow($result)) {
            $topics[] = $row['topic_id'];
        }
        $this->assertEquals(array(1, 2), $topics);
        // test if keep-tag is already assigned to topics that the deleted one is assigned to
        $tag_to_delete = "tag2";
        $tag_to_delete_id = 2;
        $tag_to_keep = "anothertag3";
        $tag_to_keep_id = 3;
        // ensure test setup (tag2 must be assigned because of the merge before)
        $tags = $this->tags_manager->get_assigned_tags(2);
        sort($tags);
        $this->assertEquals(array('anothertag3', 'tag2'), $tags);
        // 3 assignments, but only 2 are valid
        $count_of_assignments = $this->tags_manager->merge($tag_to_delete_id, $tag_to_keep, $tag_to_keep_id);
        $this->assertEquals(2, $count_of_assignments);
        $result = $this->db->sql_query('SELECT COUNT(*) as count
			FROM ' . $table_prefix . tables::TOPICTAGS . '
			WHERE tag_id=' . $tag_to_keep_id);
        $count = $this->db->sql_fetchfield('count');
        $this->assertEquals(3, $count);
        $result = $this->db->sql_query('SELECT COUNT(*) as count
			FROM ' . $table_prefix . tables::TOPICTAGS . '
			WHERE tag_id=' . $tag_to_keep_id . ' AND topic_id=2');
        $count = $this->db->sql_fetchfield('count');
        $this->assertEquals(1, $count, 'the topic must not be assigned twice');
        $tags = $this->tags_manager->get_assigned_tags(2);
        $this->assertEquals(array('anothertag3'), $tags);
    }