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
|
<?php
/**
* This is a trait to add autocover functionality to organisers
*
* 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
*
* @author Jeroen Roos
* @package Zoph
*/
namespace organiser;
use db\selectHelper;
use db\select;
use db\clause;
use db\param;
use PDO;
use photo;
/**
* Autocover for organisers
*
* @author Jeroen Roos
* @package Zoph
*/
trait autocoverTrait {
/**
* Get autocover for this organiser
* @param string how to select a coverphoto: oldest, newest, first, last, random, highest
* @param bool choose autocover from this organiser AND children
* @return photo coverphoto
*/
public function getAutoCover($autocover=null, $children=false) : ?photo {
$coverphoto=$this->getCoverphoto();
if (!($coverphoto instanceof photo)) {
$qry=$this->getAutoCoverQuery($children);
$qry = selectHelper::expandQueryForUser($qry);
$qry=selectHelper::getAutoCoverOrder($qry, $autocover);
$coverphotos=photo::getRecordsFromQuery($qry);
$coverphoto=array_shift($coverphotos);
if (!($coverphoto instanceof photo) && !$children) {
// No photos found in this organiser, let's retry, but also looking in sub-organisers
return $this->getAutoCover($autocover, true);
}
}
return $coverphoto;
}
/**
* Get query to select coverphoto for this organiser.
* @param bool choose autocover from this organiser AND children
* @return select SQL query
*/
protected function getAutoCoverBaseQuery($id, $children) : select {
$qry=new select(array("p" => "photos"));
$qry->addFunction(array("photo_id" => "DISTINCT ar.photo_id"));
$qry->join(array("ar" => "view_photo_avg_rating"), "p.photo_id = ar.photo_id");
if ($children) {
$ids=new param(":ids", $this->getBranchIdArray(), PDO::PARAM_INT);
$qry->addParam($ids);
$where=clause::InClause($id, $ids);
} else {
$where=new clause($id . "=:id");
$qry->addParam(new param(":id", $this->getId(), PDO::PARAM_INT));
}
$qry->where($where);
return $qry;
}
}
|