Ejemplo n.º 1
0
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getPageTags()
 {
     return $this->hasMany(PageTag::className(), ['tag_id' => 'id']);
 }
Ejemplo n.º 2
0
 public static function cloneTree($page, $parent_id)
 {
     /* This will hold new id of root of cloned tree. */
     static $new_root_id = false;
     /* Clone passed in page. */
     $clone = Record::findByIdFrom('Page', $page->id);
     $clone->parent_id = (int) $parent_id;
     $clone->id = null;
     if (!$new_root_id) {
         $clone->title .= " (copy)";
         $clone->slug .= "-copy";
     }
     $clone->save();
     /* Also clone the page parts. */
     $page_part = PagePart::findByPageId($page->id);
     if (count($page_part)) {
         foreach ($page_part as $part) {
             $part->page_id = $clone->id;
             $part->id = null;
             $part->save();
         }
     }
     /* Also clone the page tags. */
     $page_tags = $page->getTags();
     if (count($page_tags)) {
         foreach ($page_tags as $tag_id => $tag_name) {
             // create the relation between the page and the tag
             $tag = new PageTag(array('page_id' => $clone->id, 'tag_id' => $tag_id));
             $tag->save();
         }
     }
     /* This gets set only once even when called recursively. */
     if (!$new_root_id) {
         $new_root_id = $clone->id;
     }
     /* Clone and update childrens parent_id to clones new id. */
     if (Page::hasChildren($page->id)) {
         foreach (Page::childrenOf($page->id) as $child) {
             Page::cloneTree($child, $clone->id);
         }
     }
     return $new_root_id;
 }
Ejemplo n.º 3
0
 public function saveTags($tags)
 {
     if (is_string($tags)) {
         $tags = explode(',', $tags);
     }
     $tags = array_map('trim', $tags);
     $current_tags = $this->getTags();
     // no tag before! no tag now! ... nothing to do!
     if (count($tags) == 0 && count($current_tags) == 0) {
         return;
     }
     // delete all tags
     if (count($tags) == 0) {
         $tablename = self::tableNameFromClassName('Tag');
         // update count (-1) of those tags
         foreach ($current_tags as $tag) {
             self::$__CONN__->exec("UPDATE {$tablename} SET count = count - 1 WHERE name = '{$tag}'");
         }
         return Record::deleteWhere('PageTag', 'page_id=?', array($this->id));
     } else {
         $old_tags = array_diff($current_tags, $tags);
         $new_tags = array_diff($tags, $current_tags);
         // insert all tags in the tag table and then populate the page_tag table
         foreach ($new_tags as $index => $tag_name) {
             if (!empty($tag_name)) {
                 // try to get it from tag list, if not we add it to the list
                 if (!($tag = Record::findOneFrom('Tag', 'name=?', array($tag_name)))) {
                     $tag = new Tag(array('name' => trim($tag_name)));
                 }
                 $tag->count++;
                 $tag->save();
                 // create the relation between the page and the tag
                 $tag = new PageTag(array('page_id' => $this->id, 'tag_id' => $tag->id));
                 $tag->save();
             }
         }
         // remove all old tag
         foreach ($old_tags as $index => $tag_name) {
             // get the id of the tag
             $tag = Record::findOneFrom('Tag', 'name=?', array($tag_name));
             Record::deleteWhere('PageTag', 'page_id=? AND tag_id=?', array($this->id, $tag->id));
             $tag->count--;
             $tag->save();
         }
     }
 }
Ejemplo n.º 4
0
 public function saveTags($tags)
 {
     if (is_string($tags)) {
         $tags = explode(',', $tags);
     }
     $tags = array_map('trim', $tags);
     $tags = array_filter($tags);
     $current_tags = array();
     foreach ($this->tags() as $tag) {
         $current_tags[] = $tag->name();
     }
     // no tag before! no tag now! ... nothing to do!
     if (count($tags) == 0 && count($current_tags) == 0) {
         return;
     }
     // delete all tags
     if (count($tags) == 0) {
         return Record::deleteWhere('PageTag', 'page_id=?', array($this->id));
     } else {
         $old_tags = array_diff($current_tags, $tags);
         $new_tags = array_diff($tags, $current_tags);
         // insert all tags in the tag table and then populate the page_tag table
         foreach ($new_tags as $index => $tag_name) {
             if (!empty($tag_name)) {
                 // try to get it from tag list, if not we add it to the list
                 if (!($tag = Tag::findByName($tag_name))) {
                     $tag = new Tag(array('name' => trim($tag_name)));
                 }
                 $tag->save();
                 // create the relation between the page and the tag
                 $tag = new PageTag(array('page_id' => $this->id, 'tag_id' => $tag->id));
                 $tag->save();
             }
         }
         // remove all old tag
         foreach ($old_tags as $index => $tag_name) {
             // get the id of the tag
             $tag = Tag::findByName($tag_name);
             Record::deleteWhere('PageTag', 'page_id=? AND tag_id=?', array($this->id, $tag->id));
             $tag->save();
         }
     }
 }