<?php
 require_once(DIR . '/includes/functions.php'); class vB_DataManager_Tag extends vB_DataManager { public $validfields = [ 'tagid' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_INCR, vB_DataManager_Constants::VF_METHOD, 'verify_nonzero'], 'tagtext' => [vB_Cleaner::TYPE_NOHTML, vB_DataManager_Constants::REQ_YES, vB_DataManager_Constants::VF_METHOD, 'verify_nonempty'], 'canonicaltagid' => [vB_Cleaner::TYPE_UINT, vB_DataManager_Constants::REQ_NO], 'dateline' => [vB_Cleaner::TYPE_UNIXTIME, vB_DataManager_Constants::REQ_YES, vB_DataManager_Constants::VF_METHOD, 'verify_nonzero'] ]; public $table = 'vBForum:tag'; public $keyField = 'tagid'; protected $tag; public function fetch_by_id($id) { $this->set_condition(['tagid' => intval($id)]); return $this->load_existing(); } public function fetch_by_tagtext($label) { $this->set_condition(['tagtext' => $label]); return $this->load_existing(); } public function fetch_fields() { $fields = $this->existing; $table = $this->fetchTableBase($this->table); if (isset($this->{$table})) { foreach ($this->{$table} as $name => $value) { $field[$name] = $value; } } return $fields; } public function fetch_synonyms() { $set = vB::getDbAssertor()->assertQuery($this->table, [ 'canonicaltagid' => $this->fetch_field("tagid") ], ['field' => 'tagtext', 'direction' => vB_dB_Query::SORT_ASC] ); $synonyms = []; if ($set AND $set->valid()) { foreach ($set AS $row) { $synonym = new vB_DataManager_Tag(vB_DataManager_Constants::ERRTYPE_ARRAY); $result = $synonym->set_existing($row); $synonyms[] = $synonym; } } return $synonyms; } public function is_synonym() { return $this->fetch_field("canonicaltagid") > 0; } public function fetch_canonical_tag() { if (!$this->is_synonym()) { return false; } $tag = $synonym = new vB_DataManager_Tag(vB_DataManager_Constants::ERRTYPE_ARRAY); $tag->fetch_by_id($this->fetch_field("canonicaltagid")); return $tag; } public function attach_content($type, $id) { $tags_set = vB::getDbAssertor()->assertQuery('vBForum:InsertIgnoreTagContent2', [ 'tagid' => $this->fetch_field("tagid"), 'id' => $id, 'userid' => $this->registry->userinfo['userid'], 'time' => vB::getRequest()->getTimeNow() ]); } public function detach_content($type, $id) { vB::getDbAssertor()->assertQuery('vBForum:tagnode', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_DELETE, 'entityid' => $id, 'tagid' => $this->fetch_field("tagid"), ]); } public function make_synonym($canonical_id) { $db = vB::getDbAssertor(); foreach ($this->fetch_synonyms() as $synonym) { $synonym->make_synonym($canonical_id); } $this->set("canonicaltagid", $canonical_id); $this->save(); $replace_threads = []; $associated_content = $this->fetch_associated_content(); foreach ($associated_content as $nodeid) { $replace_threads[] = [ 'tagid' => $canonical_id, 'nodeid' => $nodeid, 'userid' => $this->registry->userinfo['userid'], 'dateline' => vB::getRequest()->getTimeNow() ]; } if (sizeof($replace_threads)) { $tags_set = $db->assertQuery('vBForum:replaceIntoTagContent', ['values' => $replace_threads]); } $db->assertQuery('vBForum:tagnode', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_DELETE, 'tagid' => $this->fetch_field('tagid') ]); $db->assertQuery('vBForum:tagsearch', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_UPDATE, 'tagid' => $canonical_id, vB_dB_Query::CONDITIONS_KEY => [ 'tagid' => $this->fetch_field('tagid') ] ]); return true; } public function make_independent() { $this->set('canonicaltagid', 0); $this->save(); } public function pre_save($doquery = true) { if (!$this->condition AND !$this->fetch_field('dateline')) { $this->set('dateline', vB::getRequest()->getTimeNow()); } return true; } public function pre_delete($doquery = true) { if ($doquery) { foreach ($this->fetch_synonyms() as $synonym) { $synonym->delete(); } $db = vB::getDbAssertor(); $db->assertQuery('vBForum:tagnode', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_DELETE, 'tagid' => $this->fetch_field('tagid') ]); $db->assertQuery('vBForum:tagsearch', [ vB_dB_Query::TYPE_KEY => vB_dB_Query::QUERY_DELETE, 'tagid' => $this->fetch_field('tagid') ]); } return true; } private function fetch_associated_content() { $result = vB::getDbAssertor()->assertQuery('vBForum:tagnode', ['tagid' => $this->fetch_field('tagid')]); $associated_content = []; if ($result AND $result->valid()) { foreach ($result AS $row) { $associated_content[] = $row['nodeid']; } } return $associated_content; } protected function load_existing() { if ($this->condition) { $result = vB::getDbAssertor()->getRow('vBForum:tag', $this->condition); if ($result) { $this->set_existing($result); $this->set_condition([]); } else { $this->condition = null; } return $result; } else { throw new Exception("Fetch existing requires a condition"); } } } 