/** * * @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'); } }
private function saveTag(Model_Tag $tag, View_Html $view) { if (isset($_REQUEST['save'])) { $tag->setData($_POST); if (!($errors = $tag->validate())) { $tag->save(); $view->redir('Admin_Tag', 'default', array('id' => $tag->getId())); return true; } $view->errors = $errors; } return false; }
/** * Checks if a given tag already exists. * The parameter $tag is a hash containing the tag name and type as below * E.g: $tag = array('tag_name' => 'bubba', tag_type => 'junk'); * * @param string $tag Has containing the tag name and tag type * @param bool $save Optionally saves the tag if does not exist * @return mixed Model_Tag if the tag exists, FALSE otherwise */ public static function get_tag_by_name($tag, $save = FALSE) { $result = ORM::factory('tag')->where('tag', '=', $tag['tag_name'])->where('tag_type', '=', $tag['tag_type'])->find(); if ($result->loaded()) { return $result; } elseif (!$result->loaded() and $save == TRUE) { try { // Save the tag $orm_tag = new Model_Tag(); $orm_tag->tag = $tag['tag_name']; $orm_tag->tag_type = $tag['tag_type']; return $orm_tag->save(); } catch (Database_Exception $e) { Kohana::$log->add(Log::ERROR, $e->getMessage()); return FALSE; } } else { return FALSE; } }
public function action_index() { require $_SERVER['DOCUMENT_ROOT'] . "/application/vendor/vimeo/autoload.php"; $client_id = ''; $client_secret = ''; $token = ''; $token_secret = ''; $vimeo = new \Vimeo\Vimeo($client_id, $client_secret, $token); $categories = $vimeo->request("/categories"); foreach ($categories['body']['data'] as $category) { // $categoryArray = array('Animation', 'Arts & Design', 'Cameras & Techniques', 'Comedy', 'Documentary', 'Experimental', 'Fashion', 'Food', 'Instructionals', 'Music', 'Narrative', 'Personal', 'Reporting & Journalism', 'Sports', 'Talks')); // if (in_array($category['name'], $categoryArray)) { // continue; // } $categoryShortName = str_replace('/categories/', '', $category['uri']); for ($i = 1; $i <= 20; $i++) { sleep(1); echo 'page ' . $i . ' of ' . $category['uri']; $videos = $vimeo->request($category['uri'] . '/videos', array('sort' => 'plays', 'per_page' => 50, 'page' => $i)); // echo '<pre>'; print_r($videos); echo '</pre>'; exit; foreach ($videos['body']['data'] as $video) { // Prepares video data array $videoSpecs['uri'] = $video['uri']; $videoSpecs['name'] = $video['name']; $videoSpecs['description'] = $video['description']; $videoSpecs['link'] = $video['link']; $videoSpecs['duration'] = $video['duration']; $videoSpecs['width'] = $video['width']; $videoSpecs['height'] = $video['height']; $videoSpecs['create_time'] = $video['created_time']; $videoSpecs['plays'] = $video['stats']['plays']; $videoSpecs['likes'] = $video['metadata']['connections']['likes']['total']; $videoSpecs['comments'] = $video['metadata']['connections']['comments']['total']; if ($video['privacy']['embed'] === 'public') { $videoSpecs['embeddable'] = true; } else { $videoSpecs['embeddable'] = false; } // Look for existing record $videoOrm = ORM::factory('Video')->where('uri', '=', $video['uri'])->find_all()->as_array(); if (sizeOf($videoOrm) === 0) { // Add record to DB if it doesn't exist $videoRecord = new Model_Video(); $videoRecord->values($videoSpecs); $videoRecord->save(); $videoId = $videoRecord->id; $videoOrm = ORM::factory('Video')->where('uri', '=', $video['uri'])->find(); // Populate tags table with video data foreach ($video['tags'] as $tag) { $tagRecord = new Model_Tag(); $tagRecord->values(array('video_id' => $videoId, 'name' => $tag['name'])); $tagRecord->save(); } } else { // Update record if it exists $videoOrm[0]->values($videoSpecs); $videoOrm[0]->save(); $videoId = $videoOrm[0]->id; } // Populate categories table category data if that video and category association is not already stored $categoryOrm = ORM::factory('Category')->where('video_id', '=', $videoId)->and_where('short_name', '=', $categoryShortName)->find_all(); if ($categoryOrm->count() === 0) { $categoryRecord = new Model_Category(); $categoryRecord->values(array('video_id' => $videoId, 'name' => $category['name'], 'short_name' => $categoryShortName)); $categoryRecord->save(); } } } } }