/** * Delete by id with taking into account case of stack. * * If deleting photo is single photo then just delete photo. * If deleting photo is parent of stack then first of all make unstack * If deleting photo is children photo in stack then after deleting photo decrease stack * * @param int $id */ public function delete($id) { $id = (int) $id; if (!$id) { return; } $parent = $this->getStackParent($id); if ($parent && $parent['id'] == $id) { $this->unstack($id); } // first of all try delete from disk $photo = $this->getById($id); $path = photosPhoto::getPhotoPath($photo); $thumb_dir = photosPhoto::getPhotoThumbDir($photo); waFiles::delete(dirname($path)); waFiles::delete($thumb_dir); // delete some related models $related_models = array('AlbumPhotos', 'PhotoExif', 'PhotoRights'); foreach ($related_models as $name) { $model_name = 'photos' . $name . 'Model'; $model = new $model_name(); $model->deleteByField('photo_id', $id); } // especial deleting rest models: // tags $photo_tags_model = new photosPhotoTagsModel(); $tags_model = new photosTagModel(); $tag_ids = array_keys($photo_tags_model->getByField('photo_id', $id, 'tag_id')); $photo_tags_model->deleteByField('photo_id', $id); $tags_model->decreaseCounters($tag_ids); // delete photo(s) itself $this->deleteById($id); // if deleted just one photo in stack (not stack itself) if ($parent && $parent['id'] != $id) { $stack_count = $parent['stack_count'] - 1; $stack_count = $stack_count > 1 ? $stack_count : 0; $sql = "UPDATE {$this->table} SET stack_count = i:stack_count WHERE id = i:id"; $this->exec($sql, array('id' => $parent['id'], 'stack_count' => $stack_count)); } }
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; }