1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
|
<?php
/**
* A photo album class corresponding to the album table.
*
* This file is part of Zoph.
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @package Zoph
* @author Jason Geiger
* @author Jeroen Roos
*/
use conf\conf;
use db\select;
use db\param;
use db\insert;
use db\delete;
use db\db;
use db\clause;
use db\selectHelper;
use organiser\autocoverTrait;
use organiser\showPageTrait;
use organiser\organiserInterface;
use template\template;
/**
* Photo album
*
* @package Zoph
* @author Jason Geiger
* @author Jeroen Roos
*/
class album extends zophTreeTable implements organiserInterface {
use showPageTrait;
use autocoverTrait;
/** @param Name of the root node in XML responses */
const XMLROOT="albums";
/** @param Name of the leaf nodes in XML responses */
const XMLNODE="album";
/** @var string The name of the database table */
protected static $tableName="albums";
/** @var array List of primary keys */
protected static $primaryKeys=array("album_id");
/** @var array Fields that may not be empty */
protected static $notNull=array("album");
/** @var bool keep keys with insert. In most cases the keys
are set by the db with auto_increment */
protected static $keepKeys = false;
/** @var string URL for this class */
protected static $url="album?album_id=";
/** @var int Cache the count of photos */
private $photoCount;
/**
* lookup this album in the db
*/
public function lookup() {
$user=user::getCurrent();
$id = $this->getId();
if (!is_numeric($id)) {
die("album_id must be numeric");
}
if (!$id) {
return;
}
$qry=new select(array("a" => "albums"));
$distinct=true;
$qry->addFields(array("*"), $distinct);
$where=new clause("a.album_id=:albumid");
$qry->addParam(new param(":albumid", (int) $this->getId(), PDO::PARAM_INT));
if (!$user->canSeeAllPhotos()) {
$qry->join(array("gp" => "group_permissions"), "a.album_id=gp.album_id")
->join(array("gu" => "groups_users"), "gp.group_id=gu.group_id");
$clause=new clause("gu.user_id=:userid");
$where->addAnd($clause);
$qry->addParam(new param(":userid", (int) $user->getId(), PDO::PARAM_INT));
if ($user->canEditOrganisers()) {
$subqry=new select(array("a" => "albums"));
$distinct=true;
$subqry->addFields(array("*"), $distinct);
$subwhere=new clause("album_id=:subalbum_id");
$subqry->addParam(new param(":subalbum_id", (int) $this->getId(), PDO::PARAM_INT));
$subwhere->addAnd(new clause("a.createdby=:ownerid"));
$subqry->addParam(new param(":ownerid", (int) $user->getId(), PDO::PARAM_INT));
$subqry->where($subwhere);
$qry->union($subqry);
}
}
$qry->where($where);
return $this->lookupFromSQL($qry);
}
/**
* Insert a new album in the db
*/
public function insert() {
parent::insert();
$parentId=$this->get("parent_album_id");
$qry=new select(array("gp" => "group_permissions"));
$qry->addParam(new param(":albumId", (int) $parentId, PDO::PARAM_INT));
$where=new clause("album_id=:albumId");
$where->addAnd(new clause("subalbums=1"));
$qry->where($where);
$perms=permissions::getRecordsFromQuery($qry);
foreach ($perms as $perm) {
$perm->set("album_id", $this->getId());
$perm->insert();
}
}
/**
* Add a photo to this album
* @param photo Photo to add
*/
public function addPhoto(photo $photo) {
$user=user::getCurrent();
if ($this->isWritableBy($user)) {
$qry=new insert(array("photo_albums"));
$qry->addParam(new param(":photo_id", (int) $photo->getId(), PDO::PARAM_INT));
$qry->addParam(new param(":album_id", (int) $this->getId(), PDO::PARAM_INT));
$qry->execute();
}
}
/**
* Remove a photo from this album
* @param photo Photo to remove
*/
public function removePhoto(photo $photo) {
$user=user::getCurrent();
if ($this->isWritableBy($user)) {
$qry=new delete("photo_albums");
$where=new clause("photo_id=:photo_id");
$where->addAnd(new clause("album_id=:album_id"));
$qry->where($where);
$qry->addParams(array(
new param(":photo_id", (int) $photo->getId(), PDO::PARAM_INT),
new param(":album_id", (int) $this->getId(), PDO::PARAM_INT)
));
$qry->execute();
}
}
/**
* Delete this album
*/
public function delete() {
parent::delete(array("photo_albums", "group_permissions"));
$users = user::getRecords("user_id", array("lightbox_id" => $this->get("album_id")));
if ($users) {
foreach ($users as $user) {
$user->lookup();
$user->setFields(array("lightbox_id" => null));
$user->update();
}
}
}
/**
* Get the name of this album
*/
public function getName() {
return $this->get("album");
}
/**
* Get the subalbums of this album
* @param string optional order
* @return array of albums
*/
public function getChildren($order=null) {
$user=user::getCurrent();
$qry=new select(array("a" => "albums"));
$qry->addFields(array("*", "name"=>"album"));
$where=new clause("parent_album_id=:album_id");
$qry->addGroupBy("a.album_id");
$qry->addParam(new param(":album_id", (int) $this->getId(), PDO::PARAM_INT));
$qry=selectHelper::addOrderToQuery($qry, $order);
if ($order!="name") {
$qry->addOrder("name");
}
if (!$user->canSeeAllPhotos()) {
$qry->join(array("gp" => "group_permissions"), "a.album_id=gp.album_id")
->join(array("gu" => "groups_users"), "gp.group_id=gu.group_id");
$where->addAnd(new clause("gu.user_id=:userid"));
$qry->addParam(new param(":userid", (int) $user->getId(), PDO::PARAM_INT));
if ($user->canEditOrganisers()) {
$subqry=new select(array("a" => "albums"));
$subqry->addFields(array("*", "name"=>"album"));
$subwhere=new clause("parent_album_id=:subalbum_id");
$subqry->addParam(new param(":subalbum_id", (int) $this->getId(), PDO::PARAM_INT));
$subwhere->addAnd(new clause("a.createdby=:ownerid"));
$subqry->addParam(new param(":ownerid", (int) $user->getId(), PDO::PARAM_INT));
$subqry->where($subwhere);
$qry->union($subqry);
}
}
$qry->where($where);
$this->children=static::getRecordsFromQuery($qry);
return $this->children;
}
/**
* Get details (statistics) about this album from db
* @return array Array with statistics
*/
public function getDetails() {
$user=user::getCurrent();
$qry=new select(array("vad" => "view_albums_details"));
$where=new clause("vad.album_id=:albid");
$qry->addParam(new param(":albid", $this->getId(), PDO::PARAM_INT));
if (!$user->canSeeAllPhotos()) {
$qry->join(array("gp" => "group_permissions"), "vad.album_id=gp.album_id")
->join(array("gu" => "groups_users"), "gp.group_id=gu.group_id");
$where->addAnd(new clause("gu.user_id=:userid"));
$qry->addParam(new param(":userid", (int) $user->getId(), PDO::PARAM_INT));
}
$qry->where($where);
$result=db::query($qry);
if ($result) {
return $result->fetch(PDO::FETCH_ASSOC);
} else {
return array();
}
}
/**
* Turn the array from @see getDetails() into XML
* @param array Don't fetch details, but use the given array
*/
public function getDetailsXML(array $details = array()) {
if (empty($details)) {
$details=$this->getDetails();
}
$details["title"]=translate("In this album:", false);
return parent::getDetailsXML($details);
}
/**
* Get count of photos in this album
*/
public function getPhotoCount() {
if ($this->photoCount) {
return $this->photoCount;
}
$qry=new select(array("pa" => "photo_albums"));
$qry->join(array("p" => "photos"), "pa.photo_id = p.photo_id");
$qry->addFunction(array("count" => "count(distinct pa.photo_id)"));
$where=new clause("pa.album_id = :alb_id");
$qry->addParam(new param(":alb_id", $this->getId(), PDO::PARAM_INT));
$qry = selectHelper::expandQueryForUser($qry);
$qry->where($where);
$count=$qry->getCount();
$this->photoCount=$count;
return $count;
}
/**
* Return the amount of photos in this album and it's children
*/
public function getTotalPhotoCount() {
// Without the lookup, parent_album_id is not available!
$this->lookup();
$qry=new select(array("pa" => "photo_albums"));
$qry->addFunction(array("count" => "COUNT(DISTINCT pa.photo_id)"));
$idList = $this->getBranchIdArray();
$ids=new param(":alb_id", $idList, PDO::PARAM_INT);
$qry->addParam($ids);
$where=clause::InClause("pa.album_id", $ids);
$qry=selectHelper::expandQueryForUser($qry);
$qry->where($where);
return $qry->getCount();
}
/**
* Get the photos in this album
* Does NOT check user permissions!
*/
public function getPhotos() {
$qry=new select(array("pa" => "photo_albums"));
$qry->addFields(array("photo_id" => "pa.photo_id"));
$qry->where(new clause("pa.album_id = :alb_id"));
$qry->addParam(new param(":alb_id", $this->getId(), PDO::PARAM_INT));
return photo::getRecordsFromQuery($qry);
}
/**
* Get array of fields/values to create an edit form
* @return array fields/values
*/
public function getEditArray() {
if ($this->isRoot()) {
$parent=array (
translate("parent album"),
translate("Albums"));
} else {
$parent=array (
translate("parent album"),
static::createDropdown("parent_album_id", $this->get("parent_album_id")));
}
return array(
"album" =>
array(
translate("album name"),
template::createInput("album", $this->get("album"), 64, null, 40)),
"parent_album_id" => $parent,
"album_description" =>
array(
translate("album description"),
template::createInput("album_description", $this->get("album_description"), 128, null, 40)),
"pageset" =>
array(
translate("pageset"),
template::createDropdown("pageset", $this->get("pageset"),
template::createSelectArray(pageset::getRecords("title"), array("title"), true))),
"sortname" =>
array(
translate("sort name"),
template::createInput("sortname", $this->get("sortname"))),
"sortorder" =>
array(
translate("album sort order"),
template::createPhotoFieldDropdown("sortorder", $this->get("sortorder")))
);
}
/**
* Create an array describing permissions for all groups
* for display or edit
* @param bool Return array of groups instead of array of permissions
* @return array permissions
*/
public function getPermissionArray($getGroup=false) {
$groups = group::getAll();
$perms=array();
foreach ($groups as $group) {
$permissions = $group->getGroupPermissions($this);
if ($permissions) {
if ($getGroup) {
$perms[]=$group;
} else {
$perms[]=$permissions;
}
}
}
return $perms;
}
/**
* Can the user write to this album?
* @param user user
* @return bool
*/
public function isWritableBy(user $user) {
return $user->isAdmin() || $user->getAlbumPermissions($this)->get("writable");
}
/**
* Get query to select coverphoto for this album.
* @param bool choose autocover from this album AND children
* @return select SQL query
*/
protected function getAutoCoverQuery($children=false) : select {
$qry = $this->getAutoCoverBaseQuery("pa.album_id", $children);
$qry->join(array("pa" => "photo_albums"), "pa.photo_id = ar.photo_id");
return $qry;
}
/**
* Lookup album by name
* @param string name
* @param bool do a "LIKE" comparison instead of "equals"
*/
public static function getByName($name, $like=false) {
if (empty($name)) {
return false;
}
$qry=new select(array("a" => "albums"));
$qry->addFields(array("album_id"));
if ($like) {
$qry->where(new clause("lower(album) LIKE :name"));
$qry->addParam(new param(":name", "%" . strtolower($name) . "%", PDO::PARAM_STR));
} else {
$qry->where(new clause("lower(album)=:name"));
$qry->addParam(new param(":name", strtolower($name), PDO::PARAM_STR));
}
return static::getRecordsFromQuery($qry);
}
/**
* Get Top N albums
*/
public static function getTopN() {
$user=user::getCurrent();
$qry=new select(array("a" => "albums"));
$qry->addFields(array("album_id", "album"));
$qry->addFunction(array("count" => "count(distinct pa.photo_id)"));
$qry->join(array("pa" => "photo_albums"), "pa.album_id=a.album_id");
$qry->addGroupBy("a.album_id");
$qry->addOrder("count DESC")->addOrder("a.album");
$qry->addLimit((int) $user->prefs->get("reports_top_n"));
if (!$user->canSeeAllPhotos()) {
$qry->join(array("gp" => "group_permissions"), "a.album_id=gp.album_id")
->join(array("gu" => "groups_users"), "gp.group_id=gu.group_id");
$qry->where(new clause("gu.user_id=:userid"));
$qry->addParam(new param(":userid", (int) $user->getId(), PDO::PARAM_INT));
}
return parent::getTopNfromSQL($qry);
}
/**
* Get autocomplete preference for albums for the current user
*/
public static function getAutocompPref() {
$user=user::getCurrent();
return ($user->prefs->get("autocomp_albums") && conf::get("interface.autocomplete"));
}
/**
* Return all albums
* @return array array of albums
*/
public static function getAll(string $order=null) {
$user=user::getCurrent();
$qry=new select(array("a" => "albums"));
$qry->addFields(array("album_id"), true);
$qry->addFields(array("album"));
$qry->addOrder($order ?? "album");
if (!$user->canSeeAllPhotos()) {
$qry->join(array("gp" => "group_permissions"), "gp.album_id = a.album_id");
$qry->join(array("gu" => "groups_users"), "gp.group_id = gu.group_id");
$qry->where(new clause("gu.user_id=:userid"));
$qry->addParam(new param(":userid", $user->getId(), PDO::PARAM_INT));
}
return static::getRecordsFromQuery($qry);
}
/**
* Get albums newer than a certain date
* @param user get albums for this user
* @param string date
* @return array array of albums
*/
public static function getNewer(user $user, $date) {
$qry=new select(array("a" => "albums"));
$qry->addFields(array("album_id"), true);
$qry->join(array("gp" => "group_permissions"), "a.album_id=gp.album_id")
->join(array("gu" => "groups_users"), "gp.group_id=gu.group_id");
$where=new clause("user_id=:userid");
$where->addAnd(new clause("gp.changedate>:changedate"));
$qry->addParams(array(
new param(":userid", $user->getId(), PDO::PARAM_INT),
new param(":changedate", $date, PDO::PARAM_STR)));
$qry->where($where)
->addOrder("a.album_id");
return static::getRecordsFromQuery($qry);
}
/**
* Get number of albums for the currently logged on user
* @return int count
*/
public static function getCount() {
$user=user::getCurrent();
$qry=new select(array("a" => "albums"));
$qry->addFunction(array("count" => "COUNT(DISTINCT a.album_id)"));
if (!$user->canSeeAllPhotos()) {
$qry->join(array("gp" => "group_permissions"), "a.album_id=gp.album_id")
->join(array("gu" => "groups_users"), "gp.group_id=gu.group_id");
$where=new clause("user_id=:userid");
$qry->addParam(new param(":userid", $user->getId(), PDO::PARAM_INT));
$qry->where($where);
}
return $qry->getCount();
}
}
?>
|