private function _set($photo_id, $tag_ids = array()) { $tag_model = new photosTagModel(); $delete_photo_tags = $this->getByField('photo_id', $photo_id, 'tag_id'); foreach ($tag_ids as $tag_id) { if (!isset($delete_photo_tags[$tag_id])) { $this->insert(array('photo_id' => $photo_id, 'tag_id' => $tag_id)); $tag_model->query('UPDATE ' . $tag_model->getTableName() . ' SET count = count + 1 WHERE id = i:id', array('id' => $tag_id)); } else { unset($delete_photo_tags[$tag_id]); } } $delete_tag_ids = array_keys($delete_photo_tags); $this->deleteByField(array('tag_id' => $delete_tag_ids, 'photo_id' => $photo_id)); $tag_model->decreaseCounters($delete_tag_ids); return true; }
protected function tagPrepareIntersection($tag_names) { $tag_model = new photosTagModel(); $in = array(); $title = array(); foreach (explode(',', $tag_names) as $tag_name) { $tag = $tag_model->getByName($tag_name); if ($tag) { $in[] = (int) $tag['id']; $title[] = $tag['name']; } } if (!$in) { $this->where[] = "0"; } else { $sql = "SELECT photo_id, COUNT(tag_id) cnt FROM `photos_photo_tags` \n WHERE tag_id IN (" . implode(',', $in) . ")\n GROUP BY photo_id\n HAVING cnt = " . count($in); $photo_id = array_keys($tag_model->query($sql)->fetchAll('photo_id')); if ($photo_id) { $this->where[] = "p.id IN (" . implode(',', $photo_id) . ")"; $this->addTitle(sprintf(_w('Tagged “%s”'), implode(',', $title))); } else { $this->where[] = "0"; } } }