public function test_should_bind_sql_bind_in_using_active_records() { $Tag = new Tag(); $Tag->create(array('name' => 'Tag 1')); $Tag->create(array('name' => 'Tag 2')); $this->assertTrue($Tags = $Tag->find(array('conditions' => array('name IN (?)', $Tag->find())))); $this->assertEqual($Tags[0]->name, 'Tag 1'); $this->assertEqual($Tags[1]->name, 'Tag 2'); }
public function run() { DB::table('tags')->delete(); Tag::create(array('name' => 'spring')); Tag::create(array('name' => 'summer')); Tag::create(array('name' => 'apple')); }
public function run() { $faker = Faker::create(); foreach (range(1, 10) as $index) { Tag::create(['name' => $faker->word]); } }
private function generate() { $tags = array('auth' => 'auth', 'eloquent' => 'eloquent'); foreach ($tags as $name => $slug) { Tag::create(array('name' => $name, 'slug' => $slug)); } }
public function run() { $tags = array_values(Lang::get('tags')); foreach ($tags as $tag) { Tag::create(array('name' => $tag)); } }
public function run() { DB::table('tags')->delete(); Tag::create(['name' => 'Announcements']); Tag::create(['name' => 'Engineering']); Tag::create(['name' => 'Features']); }
public function run() { $faker = Faker::create(); foreach (range(1, 10) as $index) { Tag::create(['title' => $faker->firstname()]); } }
public function run() { $faker = Faker::create(); foreach (range(1, 10) as $index) { Tag::create([]); } }
protected function createComponentPageEditForm($name) { $form = $this->createPageFormBase($name, false); if (!$form->isSubmitted()) { $id = $this->getParam("id"); $page = Page::find($id); $values = $page->getValues(); $values["tags"] = $page->Tags->fetchColumn("id"); $form->setDefaults($values); } $presenter = $this; $form->onSubmit[] = function ($form) use($presenter) { $values = $form->values; try { $page = Page::create($values); $page->Tags = array_map(function ($id) { return Tag::create($id); }, $values["tags"]); $page->save(); $presenter->flashMessage("Page '{$page->name}' was changed!"); $presenter->redirect("default", array("id" => $page->id)); } catch (ModelException $e) { $page->addErrorsToForm($form); } }; }
public function testNewRecordWithNewReferenced() { $page = Page::create(array("name" => "English article", "description" => "Description", "text" => "Text in english.", "allowed" => true)); $page->Tags[] = Tag::create(array("name" => "Society", "url" => "society")); $page->Tags[] = Tag::create(array("name" => "Previte", "url" => "previte")); $page->save(); $this->assertEquals(2, count(Page::findByName("English article")->Tags)); }
public function test_create_tag() { $t = Tag::create(array('tag_text' => $this->text, 'tag_slug' => $this->slug)); $this->assertType('Tag', $t); $this->assertEquals($t->tag_slug, $this->slug); $this->assertEquals($t->tag_text, $this->text); $t->delete(); }
public function run() { $tags = array('Winning', 'Bi-Winning', 'Win Here', 'Win There', 'Win Win Everywhere', 'Can\'t Stop', 'Won\'t Stop', 'Feel Like Losing', 'Party Hard', 'Relationships', 'Travel', 'Charlie Sheen'); foreach ($tags as $tag) { $name = array('name' => $tag); Tag::create($name); } }
/** * Store a newly created tag in storage. * * @return Response */ public function store() { $validator = Validator::make($data = Input::all(), Tag::$rules); if ($validator->fails()) { return Redirect::back()->withErrors($validator)->withInput(); } Tag::create($data); }
/** * Store a newly created tag in storage. * * @return Response */ public function store() { $validator = Tag::validate($data = Input::all()); if ($validator->fails()) { return Redirect::back()->withErrors($validator)->withInput(); } Tag::create($data); return Redirect::route('admin.tags.index'); }
public function run() { Tag::truncate(); $template = 'app/views/admin/template.blade.php'; $info = file_get_contents($template); $values = array('Phone', 'Tablet', 'Laptop', 'Accessories'); foreach ($values as $value) { Tag::create(array('name' => $value, 'slug' => Str::slug($value), 'template' => $info, 'block' => 0)); } }
private function _saveTags($tags) { foreach ($tags as $tagName) { $tag = Tag::where('name', '=', $tagName)->first(); if (is_null($tag)) { $this->tags()->save(Tag::create(array('name' => $tagName))); } else { $this->tags()->attach($tag->id); } } }
public function run() { DB::table('tags')->delete(); Tag::create(array('id' => 1, 'name' => 'PHP')); Tag::create(array('id' => 2, 'name' => 'Javascript')); Tag::create(array('id' => 3, 'name' => 'CSS')); Tag::create(array('id' => 4, 'name' => 'MySQL')); Tag::create(array('id' => 5, 'name' => 'HTML')); Tag::create(array('id' => 6, 'name' => 'MongoDB')); DB::table('post_tag')->delete(); DB::table('post_tag')->insert(array(array('post_id' => 1, 'tag_id' => 5), array('post_id' => 1, 'tag_id' => 3), array('post_id' => 2, 'tag_id' => 1), array('post_id' => 3, 'tag_id' => 1), array('post_id' => 3, 'tag_id' => 4), array('post_id' => 4, 'tag_id' => 6), array('post_id' => 5, 'tag_id' => 2))); }
public function test_create_tag() { $t = Tag::create( array( 'term_display' => $this->text, 'term' => $this->slug ) ); $this->assert_type( 'Tag', $t ); // Check the tag's id is set. $this->assert_true((int)$t->id > 0, 'The Tag id should be greater than zero'); $this->assert_equal($this->slug, $t->term, 'The slug should equal the slug value passed in.' ); $this->assert_equal($this->text, $t->term_display, 'The text should equal the text value passed in.' ); $this->assert_true( (int)$t->mptt_left > 0, 'The Tag mptt_left should be greater than zero' ); $this->assert_true( (int)$t->mptt_right > 0, 'The Tag mptt_right should be greater than zero' ); Tags::vocabulary()->delete_term( $t ); }
/** * Run the database seeds. * * @return void */ public function run() { Eloquent::unguard(); DB::table('companies')->delete(); DB::table('items')->delete(); DB::table('categories')->delete(); $territory = Territory::where('name', 'Stockholms län')->where('county', '01')->first(); $company1 = Company::create(array('id' => 1, 'name' => '北欧工作室', 'description' => 'chenyipingsheng: 如何设计api,应该先按模块拆分原则进行初步划分。(4) api返回数据文章中所说的对null的看法... app后端设计(10)--数据增量更新 chenyipingsheng: 文章中update_time的时间粒度选择的是分钟,如果某个时间段内新增的条数/分钟,超过了单页si...', 'territory_id' => $territory->id, 'url' => 'http://www.google.com', 'address' => '啊发扩大浪费大斗进发觉啦地方', 'established_year' => '2011')); $company2 = Company::create(array('id' => 2, 'name' => '阿呆姆斯工作室', 'description' => 'chenyipingsheng: 如何设计api,应该先按模块拆分原则进行初步划分。(4) api返回数据文章中所说的对null的看法... app后端设计(10)--数据增量更新 chenyipingsheng: 文章中update_time的时间粒度选择的是分钟,如果某个时间段内新增的条数/分钟,超过了单页si...', 'territory_id' => $territory->id, 'url' => 'http://www.google.com', 'address' => '饿我热哦我iooewpp', 'established_year' => '2013')); $item1 = Item::create(array('id' => 1, 'company_id' => 1, 'name' => 'Kayala投资项目', 'description' => '做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经历过手机网页端,android客户端,iphone客户端,现就职于app云后端平台bmob(想了解bmob点击这里)。其中的乐与苦,得与失,仰首问天有谁知?我觉得是时候来个总结,把相关的技术和心得记录下来。', 'territory_id' => $territory->id, 'url' => 'http://www.google.com', 'min_investment' => 20000000)); $item2 = Item::create(array('id' => 2, 'company_id' => 1, 'name' => '棕熊城堡出售', 'description' => '做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经历过手机网页端,android客户端,iphone客户端,现就职于app云后端平台bmob(想了解bmob点击这里)。其中的乐与苦,得与失,仰首问天有谁知?我觉得是时候来个总结,把相关的技术和心得记录下来。', 'territory_id' => $territory->id, 'url' => 'http://www.google.com', 'min_investment' => 30000000, 'max_investment' => 30000000)); $item3 = Item::create(array('id' => 3, 'company_id' => 2, 'name' => '布鲁艾尔空气净化器', 'description' => '做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经历过手机网页端,android客户端,iphone客户端,现就职于app云后端平台bmob(想了解bmob点击这里)。其中的乐与苦,得与失,仰首问天有谁知?我觉得是时候来个总结,把相关的技术和心得记录下来。', 'territory_id' => $territory->id, 'url' => 'http://www.google.com', 'min_investment' => 10000000, 'max_investment' => 10000000)); $item4 = Item::create(array('id' => 4, 'company_id' => 2, 'name' => 'volvo公司转让', 'description' => '做了3年app相关的系统架构,api设计,先后在3个创业公司中工作,经历过手机网页端,android客户端,iphone客户端,现就职于app云后端平台bmob(想了解bmob点击这里)。其中的乐与苦,得与失,仰首问天有谁知?我觉得是时候来个总结,把相关的技术和心得记录下来。', 'territory_id' => $territory->id, 'url' => 'http://www.google.com', 'min_investment' => 250000000, 'max_investment' => 250000000)); Category::create(array('id' => 1, 'name' => '融资项目')); Category::create(array('id' => 2, 'name' => '房产出售')); Category::create(array('id' => 3, 'name' => '产品推广')); Category::create(array('id' => 4, 'name' => '公司转让')); Category::create(array('id' => 5, 'name' => '技术出售')); Category::create(array('id' => 6, 'name' => '文化, 媒体, 旅游')); CompanyCategory::create(array('id' => 1, 'company_id' => 1, 'category_id' => 6)); CompanyCategory::create(array('id' => 2, 'company_id' => 2, 'category_id' => 6)); ItemCategory::create(array('id' => 1, 'item_id' => 1, 'category_id' => 1)); ItemCategory::create(array('id' => 2, 'item_id' => 1, 'category_id' => 2)); ItemCategory::create(array('id' => 3, 'item_id' => 2, 'category_id' => 2)); ItemCategory::create(array('id' => 4, 'item_id' => 3, 'category_id' => 4)); ItemCategory::create(array('id' => 5, 'item_id' => 3, 'category_id' => 5)); ItemCategory::create(array('id' => 6, 'item_id' => 3, 'category_id' => 6)); ItemCategory::create(array('id' => 7, 'item_id' => 4, 'category_id' => 3)); ItemCategory::create(array('id' => 8, 'item_id' => 4, 'category_id' => 5)); Tag::create(array('id' => 1, 'name' => '回报率高')); Tag::create(array('id' => 2, 'name' => '绿色环保')); Tag::create(array('id' => 3, 'name' => '市场巨大')); Tag::create(array('id' => 4, 'name' => '用途广')); Tag::create(array('id' => 5, 'name' => '历史悠久')); Tag::create(array('id' => 6, 'name' => '可自雇')); Tag::create(array('id' => 7, 'name' => 'PM2.5')); ItemTag::create(array('id' => 1, 'tag_id' => 1, 'item_id' => 1)); ItemTag::create(array('id' => 2, 'tag_id' => 2, 'item_id' => 1)); ItemTag::create(array('id' => 3, 'tag_id' => 3, 'item_id' => 1)); ItemTag::create(array('id' => 4, 'tag_id' => 2, 'item_id' => 2)); ItemTag::create(array('id' => 5, 'tag_id' => 3, 'item_id' => 2)); ItemTag::create(array('id' => 6, 'tag_id' => 4, 'item_id' => 2)); ItemTag::create(array('id' => 7, 'tag_id' => 5, 'item_id' => 3)); ItemTag::create(array('id' => 8, 'tag_id' => 6, 'item_id' => 3)); ItemTag::create(array('id' => 9, 'tag_id' => 7, 'item_id' => 3)); ItemTag::create(array('id' => 10, 'tag_id' => 3, 'item_id' => 4)); ItemTag::create(array('id' => 11, 'tag_id' => 6, 'item_id' => 4)); }
protected function setUp() { $settings = array('config_dir' => dirname(__FILE__) . '/../config'); Salama::bootstrap($settings); # 1. create models, database $runner = new SalamaRunner(); $runner->build(); User::raw("CREATE DATABASE IF NOT EXISTS `salamatest`")->goraw(); $runner->syncdb(); # 2. add test data SalamaSuite::$users = array(1 => 'good_bit', 2 => 'evil_bit'); foreach (SalamaSuite::$users as $k => $username) { # transaction support #User::begin(); $user = User::create(); $user->username = $username; $user->login = $k; $user->save(); $u = UserInfo::create(); $u->total = $k; $u->user_id = $user->id; # @TODO when relational, this will be automatically set $u->save(); for ($i = 0; $i < 2; $i++) { $u = UserComment::create(); $u->user_id = $user->id; # @TODO when relational, this will be automatically set $u->comment = "Hello from {$username}!"; $u->pub_date = $i; $u->save(); } $tag = Tag::create(); $tag->name = "Blue"; $tag->save(); $tu = TagUser::create(); $tu->user_id = $user->id; $tu->tag_id = $tag->id; // @TODO $tu->tag = $t; should do the same thing $tu->golden_path = $username . " bets on " . rand(); $tu->save(); $tag2 = Tag::create(); $tag2->name = "Yellow"; $tag2->save(); $tu = TagUser::create(); $tu->user_id = $user->id; $tu->tag_id = $tag2->id; $tu->golden_path = $username . " bets on " . rand(); $tu->save(); #User::commit(); } }
public function tag($the_tags) { $ids = array(); foreach ($the_tags as &$tag_str) { $tag_str = Str::slug(trim($tag_str)); $tag = Tag::where('name', '=', $tag_str)->first(); if ($tag) { $tag->count = $tag->count + 1; $tag->save(); $ids[] = $tag->id; } else { $tag = Tag::create(array('name' => $tag_str, 'count' => 1)); $ids[] = $tag->id; } } $this->tags()->sync($ids); }
public function saveInto(DataObjectInterface $record) { if ($this->name) { $tags = explode(',', $this->dataValue()); if (!$record instanceof SummitEvent) { return; } $record->Tags()->removeAll(); foreach ($tags as $t) { $tag = Tag::get()->filter('Tag', $t)->first(); if (is_null($tag)) { $tag = Tag::create(array('Tag' => $t)); $tag->write(); } $record->Tags()->add($tag); } } }
/** * Run the migrations. * * @return void */ public function up() { Schema::create('tags', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); for ($i = 1; $i < 6; $i++) { Tag::create(['name' => 'Tag' . $i]); } //creation table de liaison posts + tags (post_tag) Schema::create('post_tag', function (Blueprint $table) { $table->increments('id'); $table->integer('post_id')->unsigned()->index(); $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade'); $table->integer('tag_id')->unsigned()->index(); $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade'); $table->timestamps(); }); }
/** * Создание новости */ public function create() { if (!User::isAdmin()) { App::abort('403'); } if (Request::isMethod('post')) { $news = new News(); $news->category_id = Request::input('category_id'); $news->user_id = User::get('id'); $news->title = Request::input('title'); $news->slug = ''; $news->text = Request::input('text'); $image = Request::file('image'); if ($image && $image->isValid()) { $ext = $image->getClientOriginalExtension(); $filename = uniqid(mt_rand()) . '.' . $ext; if (in_array($ext, ['jpeg', 'jpg', 'png', 'gif'])) { $img = new SimpleImage($image->getPathName()); $img->best_fit(1280, 1280)->save('uploads/news/images/' . $filename); $img->best_fit(200, 200)->save('uploads/news/thumbs/' . $filename); } $news->image = $filename; } if ($news->save()) { if ($tags = Request::input('tags')) { $tags = array_map('trim', explode(',', $tags)); foreach ($tags as $tag) { $tag = Tag::create(['name' => $tag]); $tag->create_news_tags(['news_id' => $news->id]); } } App::setFlash('success', 'Новость успешно создана!'); App::redirect('/' . $news->category->slug . '/' . $news->slug); } else { App::setFlash('danger', $news->getErrors()); App::setInput($_POST); } } $categories = Category::getAll(); App::view('news.create', compact('categories')); }
/** * Create Tag by quick tag name * @param string $tagName * @param array $args * @return $this|null */ public static function __callStatic($tagName, $args) { $tagName = strtolower($tagName); if (in_array($tagName, self::$quickTags)) { $attributes = count($args) ? $args[0] : array(); return Tag::create($attributes, $tagName); } return null; }
public function run() { //DB::table('tags')->delete(); // tags Tag::create(array('name' => 'L4StartKit', 'slug' => 'l4startkit')); }
/** * Update the specified resource in storage. * PUT /article/{id} * * @param int $id * @return Response */ public function update($id) { $rules = ['title' => 'required|max:100', 'content' => 'required', 'tags' => array('required', 'regex:/^\\w+$|^(\\w+,)+\\w+$/')]; $validator = Validator::make(Input::all(), $rules); if ($validator->passes()) { $article = Article::with('tags')->find($id); $article->update(Input::only('title', 'content')); $resolved_content = Markdown::parse(Input::get('content')); $article->resolved_content = $resolved_content; $tags = array_unique(explode(',', Input::get('tags'))); if (str_contains($resolved_content, '<p>')) { $start = strpos($resolved_content, '<p>'); $length = strpos($resolved_content, '</p>') - $start - 3; $article->summary = substr($resolved_content, $start + 3, $length); } elseif (str_contains($resolved_content, '</h')) { $start = strpos($resolved_content, '<h'); $length = strpos($resolved_content, '</h') - $start - 4; $article->summary = substr($resolved_content, $start + 4, $length); } $article->save(); foreach ($article->tags as $tag) { if (($index = array_search($tag->name, $tags)) !== false) { unset($tags[$index]); } else { $tag->count--; $tag->save(); $article->tags()->detach($tag->id); } } foreach ($tags as $tagName) { $tag = Tag::whereName($tagName)->first(); if (!$tag) { $tag = Tag::create(array('name' => $tagName)); } $tag->count++; $article->tags()->save($tag); } return Redirect::route('article.show', $article->id); } else { return Redirect::route('article.edit', $id)->withInput()->withErrors($validator); } }
public function action_auth_ajax_wp_import_posts() { // get the values post'd in $inputs = $_POST->filter_keys(array('db_name', 'db_host', 'db_user', 'db_pass', 'db_prefix', 'category_import', 'import_index')); $inputs = $inputs->getArrayCopy(); // make sure we have all our default values $inputs = array_merge($this->default_values, $inputs); // get the wpdb $wpdb = $this->wp_connect($inputs['db_host'], $inputs['db_name'], $inputs['db_user'], $inputs['db_pass']); // if we couldn't connect, error out if (!$wpdb) { EventLog::log(_t('Failed to import from "%s"', array($inputs['db_name']))); Session::error(_t('Failed to import from "%s"', array($inputs['db_name']))); echo '<p>' . _t('Failed to connect using the given database connection details.') . '</p>'; } // we connected just fine, let's get moving! // begin a transaction. if we error out at any point, we want to roll back to before import began DB::begin_transaction(); // fetch the number of posts from the wordpress database so we can batch things up $num_posts = $wpdb->get_value('select count(id) from ' . $inputs['db_prefix'] . 'posts'); // figure out the LIMIT we're at $min = $inputs['import_index'] * IMPORT_BATCH; $max = min($min + IMPORT_BATCH, $num_posts); // for display only echo '<p>' . _t('Importing posts %1$d - %2$d of %3$d.', array($min, $max, $num_posts)) . '</p>'; // get all the imported users so we can link old post authors to new post authors $users = DB::get_results('select user_id, value from {userinfo} where name = :name', array(':name' => 'wp_id')); // create an easy user map of old ID -> new ID $user_map = array(); foreach ($users as $info) { $user_map[$info->value] = $info->user_id; } // get all the post IDs we've imported so far to make sure we don't duplicate any $post_map = DB::get_column('select value from {postinfo} where name = :name', array(':name' => 'wp_id')); // now we're ready to start importing posts $posts = $wpdb->get_results('select id, post_author, post_date, post_content, post_title, post_status, comment_status, post_name, post_modified, guid, post_type from ' . $inputs['db_prefix'] . 'posts order by id asc limit ' . $min . ', ' . IMPORT_BATCH); foreach ($posts as $post) { // if this post is already in the list we've imported, skip it if (in_array($post->id, $post_map)) { continue; } // set up the big taxonomy sql query // if this turns out to be incredibly slow we should refactor it into a big join, but they're all keys so it seems zippy enough for me $taxonomy_query = 'select name, slug from ' . $inputs['db_prefix'] . 'terms where term_id in ( select term_id from ' . $inputs['db_prefix'] . 'term_taxonomy where taxonomy = :taxonomy and term_taxonomy_id in ( select term_taxonomy_id from ' . $inputs['db_prefix'] . 'term_relationships where object_id = :object_id ) )'; // get all the textual tag names for this post $tags = $wpdb->get_results($taxonomy_query, array(':taxonomy' => 'post_tag', ':object_id' => $post->id)); // should we import categories as tags too? if ($inputs['category_import']) { // then do the same as above for the category taxonomy $categories = $wpdb->get_results($taxonomy_query, array(':taxonomy' => 'category', ':object_id' => $post->id)); } // create the new post $p = new Post(array('title' => MultiByte::convert_encoding($post->post_title), 'content' => MultiByte::convert_encoding($post->post_content), 'user_id' => $user_map[$post->post_author], 'pubdate' => HabariDateTime::date_create($post->post_date), 'updated' => HabariDateTime::date_create($post->post_modified), 'slug' => MultiByte::convert_encoding($post->post_name))); // figure out the post type switch ($post->post_type) { case 'post': $p->content_type = Post::type('entry'); break; case 'page': $p->content_type = Post::type('page'); break; default: // we're not importing other types - continue 2 to break out of the switch and the loop and continue to the next post continue 2; } // figure out the post status switch ($post->post_status) { case 'publish': $p->status = Post::status('published'); break; case 'future': $p->status = Post::status('scheduled'); break; case 'pending': // means pending-review, not pending as in scheduled // means pending-review, not pending as in scheduled case 'draft': $p->status = Post::status('draft'); break; default: // Post::status() returns false if it doesn't recognize the status type $status = Post::status($post->post_status); // store in a temp value because if you try and set ->status to an invalid value the Post class freaks if ($status == false) { // we're not importing statuses we don't recognize - continue 2 to break out of the switch and the loop and continue to the next post continue 2; } else { $p->status = $status; } break; } // if comments are closed, disable them on the new post if ($post->comment_status == 'closed') { $p->info->comments_disabled = true; } // save the old post ID in info $p->info->wp_id = $post->id; // since we're not using it, save the old GUID too $p->info->wp_guid = $post->guid; // now that we've got all the pieces in place, save the post try { $p->insert(); // now that the post is in the db we can add tags to it // first, if we want to import categories as tags, add them to the array if ($inputs['category_import']) { $tags = array_merge($tags, $categories); } // now for the tags! foreach ($tags as $tag) { // try to get the tag by slug, which is the key and therefore the most unique $t = Tags::get_by_slug($tag->slug); // if we didn't get back a tag, create a new one if ($t == false) { $t = Tag::create(array('term' => $tag->slug, 'term_display' => $tag->name)); } // now that we have a tag (one way or the other), associate this post with it $t->associate('post', $p->id); } } catch (Exception $e) { EventLog::log($e->getMessage(), 'err'); echo '<p class="error">' . _t('There was an error importing post %s. See the EventLog for the error message.', array($post->post_title)); echo '<p>' . _t('Rolling back changes…') . '</p>'; // rollback all changes before we return so the import hasn't changed anything yet DB::rollback(); // and return so they don't get AJAX to send them on to the next step return false; } } // if we've finished without an error, commit the import DB::commit(); if ($max < $num_posts) { // if there are more posts to import // get the next ajax url $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_posts')); // bump the import index by one so we get a new batch next time $inputs['import_index']++; } else { // move on to importing comments // get the next ajax url $ajax_url = URL::get('auth_ajax', array('context' => 'wp_import_comments')); // reset the import index so we start at the first comment $inputs['import_index'] = 0; } // and spit out ajax to send them to the next step - posts! echo $this->get_ajax($ajax_url, $inputs); }
/** * TODO: be more careful * INSERT INTO {tag2post} / SELECT $master_tag->ID,post_ID FROM {tag2post} WHERE tag_id = $tag->id" and then "DELETE FROM {tag2post} WHERE tag_id = $tag->id" * Renames tags. * If the master tag exists, the tags will be merged with it. * If not, it will be created first. * * @param Array tags The tag text, slugs or ids to be renamed * @param mixed master The Tag to which they should be renamed, or the slug, text or id of it **/ public static function rename($master, $tags) { if (!is_array($tags)) { $tags = array($tags); } $tag_names = array(); // get array of existing tags first to make sure we don't conflict with a new master tag foreach ($tags as $tag) { $posts = array(); $post_ids = array(); $tag = Tags::get_one($tag); // get all the post ID's tagged with this tag $posts = DB::get_results('SELECT post_id FROM {tag2post} WHERE tag_id = ?', array($tag->id)); if (count($posts) > 0) { // build a list of all the post_id's we need for the new tag foreach ($posts as $post) { $post_ids[] = $post->post_id; } $tag_names[] = $tag->tag; } Tags::delete($tag); } // get the master tag $master_tag = Tags::get_one($master); if (!isset($master_tag->slug)) { // it didn't exist, so we assume it's tag text and create it $master_tag = Tag::create(array('tag_slug' => Utils::slugify($master), 'tag_text' => $master)); $master_ids = array(); } else { // get the posts the tag is already on so we don't duplicate them $master_posts = DB::get_results('SELECT post_id FROM {tag2post} WHERE tag_id = ?', array($master_tag->id)); $master_ids = array(); foreach ($master_posts as $master_post) { $master_ids[] = $master_post->post_id; } } if (count($post_ids) > 0) { // only try and add the master tag to posts it's not already on $post_ids = array_diff($post_ids, $master_ids); // link the master tag to each distinct post we removed tags from foreach ($post_ids as $post_id) { DB::query('INSERT INTO {tag2post} ( tag_id, post_id ) VALUES ( ?, ? )', array($master_tag->id, $post_id)); } } EventLog::log(sprintf(_n('Tag %s has been renamed to %s.', 'Tags %s have been renamed to %s.', count($tags)), implode($tag_names, ', '), $master), 'info', 'tag', 'habari'); }
/** * Updates the tags associated with the given media. * * If the image can't be resized, its web and thumbnail images will * be symlinked to the full size image to prevent broken image sources. * * @param Media $media The Media object to associate the tags with. * @param array $tags An array of tag names. */ private static function update_tags($media, $tags) { // Remove all existing tag associations Database::DELETE_FROM(TAG_RELATION_TABLE . ' WHERE image_id = ?', array($media->id)); foreach ($tags as $tag) { // Skip empty tags if (trim($tag) == '') { continue; } // Create tags which don't exist yet if (!Tag::exists($tag)) { Tag::create($tag); } $media->add_tag($tag); } }