Beispiel #1
0
 /**
  * 
  * @param integer|Model_Page_Front $page_id
  * @param array $tags
  */
 public static function save_by_page($page_id, $tags)
 {
     if (is_string($tags)) {
         $tags = explode(Model_Tag::SEPARATOR, $tags);
     }
     $tags = array_unique(array_map('trim', $tags));
     $current_tags = Model_Page_Tag::find_by_page($page_id);
     if ($page_id instanceof Model_Page_Front) {
         $page_id = $page_id->id();
     }
     // no tag before! no tag now! ... nothing to do!
     if (empty($tags) and empty($current_tags)) {
         return NULL;
     }
     // delete all tags
     if (empty($tags)) {
         // update count (-1) of those tags
         foreach ($current_tags as $tag) {
             DB::update(Model_Tag::tableName())->set(array('count' => DB::expr('count - 1')))->where('name', '=', $tag)->execute();
         }
         Record::deleteWhere(self::tableName(), array('where' => array(array('page_id', '=', (int) $page_id))));
         Cache::instance()->delete_tag('page_tags');
     } 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)) {
                 continue;
             }
             $tag = Record::findOneFrom('Model_Tag', array('where' => array(array('name', '=', $tag_name))));
             // try to get it from tag list, if not we add it to the list
             if (!$tag instanceof Model_Tag) {
                 $tag = new Model_Tag(array('name' => trim($tag_name)));
             }
             $tag->count++;
             $tag->save();
             // create the relation between the page and the tag
             $page_tag = new Model_Page_Tag(array('page_id' => (int) $page_id, 'tag_id' => $tag->id));
             $page_tag->save();
         }
         // remove all old tag
         foreach ($old_tags as $index => $tag_name) {
             // get the id of the tag
             $tag = Record::findOneFrom('Model_Tag', array('where' => array(array('name', '=', $tag_name))));
             Record::deleteWhere(self::tableName(), array('where' => array(array('page_id', '=', (int) $page_id), array('tag_id', '=', $tag->id))));
             $tag->count--;
             $tag->save();
         }
         Cache::instance()->delete_tag('page_tags');
     }
 }
Beispiel #2
0
<?php

defined('SYSPATH') or die('No direct script access.');
// При сохранении страницы обновление тегов
Observer::observe(array('page_add_after_save', 'page_edit_after_save'), function ($page) {
    $tags = Request::current()->post('page_tags');
    if ($tags !== NULL) {
        Model_Page_Tag::save_by_page($page->id, $tags);
    }
});
// Загрузка шаблона с тегами в блок с метатегами в редактор страницы
Observer::observe('view_page_edit_meta', function ($page) {
    echo View::factory('page/tags', array('tags' => Model_Page_Tag::find_by_page($page->id)));
});
Observer::observe('layout_backend_head_before', function () {
    echo '<script type="text/javascript">var TAG_SEPARATOR = "' . Model_Tag::SEPARATOR . '";</script>';
});
// При выводе списка стран запускается метод custom_filter и передача в него
// Database_query_builder, в этом обсервере можно дополнять этот запрос
Observer::observe('frontpage_custom_filter', function ($sql, $page) {
    $tags = Context::instance()->get('tag');
    if (empty($tags)) {
        return;
    }
    $sql->join(array(Model_Page_Tag::TABLE_NAME, 'pts'), 'inner')->distinct(TRUE)->on('pts.page_id', '=', 'page.id')->join(array(Model_Tag::TABLE_NAME, 'ts'))->on('pts.tag_id', '=', 'ts.id')->where('ts.name', 'in', explode(',', $tags));
});