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
<?php
/**
***********************************************************************************************
* Class manages access to database table adm_photos
*
* @copyright 2004-2016 The Admidio Team
* @see http://www.admidio.org/
* @license https://www.gnu.org/licenses/gpl-2.0.html GNU General Public License v2.0 only
***********************************************************************************************
*/
/**
* @class TablePhotos
* Diese Klasse dient dazu ein Fotoveranstaltungsobjekt zu erstellen.
* Eine Fotoveranstaltung kann ueber diese Klasse in der Datenbank verwaltet werden.
*
* Beside the methods of the parent class there are the following additional methods:
*
* countImages($pho_id = 0) - Rekursive Funktion gibt die Anzahl aller Bilder
* inkl. der Unteralben zurueck
* shuffleImage($pho_id = 0) - Rekursive Funktion zum Auswaehlen eines
* Beispielbildes aus einem moeglichst hohen Album
* createFolder() - erzeugt den entsprechenden Ordner unter adm_my_files/photos
* deleteInDatabase($photo_id) - Rekursive Funktion die die uebergebene Veranstaltung
* und alle Unterveranstaltungen loescht
* deleteInFilesystem($folder) - Rekursive Funktion die alles innerhalb des uebergebenen
* Ordners mit Unterordnern und allen Dateien loescht
*/
class TablePhotos extends TableAccess
{
protected $hasChildAlbums; ///< Flag if this album has child albums
/**
* Constructor that will create an object of a recordset of the table adm_photos.
* If the id is set than the specific photo album will be loaded.
* @param \Database $database Object of the class Database. This should be the default global object @b $gDb.
* @param int $photoId The recordset of the photo album with this id will be loaded. If id isn't set than an empty object of the table is created.
*/
public function __construct(&$database, $photoId = 0)
{
$this->hasChildAlbums = null;
parent::__construct($database, TBL_PHOTOS, 'pho', $photoId);
}
/**
* Rekursive Funktion gibt die Anzahl aller Bilder inkl. der Unteralben zurueck
* pho_id noetig fuer rekursiven Aufruf
* @param int $phoId
* @return int
*/
public function countImages($phoId = 0)
{
$totalImages = 0;
// wurde keine ID uebergeben, dann Anzahl Bilder des aktuellen Albums ermitteln
if($phoId == 0)
{
$phoId = $this->getValue('pho_id');
$totalImages = $this->getValue('pho_quantity');
}
// alle Unteralben ermitteln
$sql = 'SELECT pho_id, pho_quantity
FROM '.TBL_PHOTOS.'
WHERE pho_pho_id_parent = '.$phoId.'
AND pho_locked = 0';
$childAlbumsStatement = $this->db->query($sql);
while($pho_row = $childAlbumsStatement->fetch())
{
$totalImages = $totalImages + $pho_row['pho_quantity'] + $this->countImages($pho_row['pho_id']);
}
return $totalImages;
}
/**
* Legt den Ordner fuer die Veranstaltung im Dateisystem an
* @return string[]
*/
public function createFolder()
{
$error = array('code' => '0', 'text' => '');
// Pfad in adm_my_files pruefen und ggf. anlegen
$myFilesPhotos = new MyFiles('PHOTOS');
if(!$myFilesPhotos->checkSettings())
{
$error['text'] = $myFilesPhotos->errorText;
$error['path'] = $myFilesPhotos->errorPath;
return $error;
}
// nun den Ordner fuer die Veranstaltung anlegen
$folderName = $this->getValue('pho_begin', 'Y-m-d'). '_'. $this->getValue('pho_id');
if(!$myFilesPhotos->createFolder($folderName, true))
{
$error['text'] = 'SYS_FOLDER_NOT_CREATED';
$error['path'] = 'adm_my_files/photos/'.$folderName;
}
return $error;
}
/**
* Deletes the selected photo album and all sub photo albums.
* After that the class will be initialize.
* @return bool @b true if no error occurred
*/
public function delete()
{
if($this->deleteInDatabase($this->getValue('pho_id')))
{
return parent::delete();
}
return false;
}
/**
* Rekursive Funktion die die uebergebene Veranstaltung und alle Unterveranstaltungen loescht
* @param int $photoId
* @return bool
*/
public function deleteInDatabase($photoId)
{
$returnValue = true;
$this->db->startTransaction();
// erst einmal rekursiv zur tiefsten Tochterveranstaltung gehen
$sql = 'SELECT pho_id
FROM '.TBL_PHOTOS.'
WHERE pho_pho_id_parent = '.$photoId;
$childAlbumStatement = $this->db->query($sql);
while($phoId = $childAlbumStatement->fetchColumn())
{
if($returnValue)
{
$returnValue = $this->deleteInDatabase((int) $phoId);
}
}
// nun DB-Eintrag und Ordner loeschen
if($returnValue)
{
// Ordnerpfad zusammensetzen
$folder = SERVER_PATH. '/adm_my_files/photos/'.$this->getValue('pho_begin', 'Y-m-d').'_'.$photoId;
// aktuellen Ordner incl. Unterordner und Dateien loeschen, falls er existiert
if(file_exists($folder))
{
// nun erst rekursiv den Ordner im Dateisystem loeschen
$myFilesPhotos = new MyFiles('PHOTOS');
$myFilesPhotos->setFolder($folder);
$returnValue = $myFilesPhotos->delete($folder);
}
if($returnValue)
{
// Veranstaltung jetzt in DB loeschen
$sql = 'DELETE FROM '.TBL_PHOTOS.'
WHERE pho_id = '.$photoId;
$this->db->query($sql);
}
}
$this->db->endTransaction();
return $returnValue;
}
/**
* Check if this album has one or more child albums.
* @return bool Return @b true if child albums exists.
*/
public function hasChildAlbums()
{
if($this->hasChildAlbums === null)
{
$sql = 'SELECT COUNT(*) AS count
FROM '.TBL_PHOTOS.'
WHERE pho_pho_id_parent = '.$this->getValue('pho_id');
$countChildAlbums = $this->db->query($sql);
if($countChildAlbums->fetchColumn() > 0)
{
$this->hasChildAlbums = true;
}
else
{
$this->hasChildAlbums = false;
}
}
return $this->hasChildAlbums;
}
/**
* Save all changed columns of the recordset in table of database. Therefore the class remembers if it's
* a new record or if only an update is necessary. The update statement will only update
* the changed columns. If the table has columns for creator or editor than these column
* with their timestamp will be updated.
* The current organization will be set per default.
* @param bool $updateFingerPrint Default @b true. Will update the creator or editor of the recordset if table has columns like @b usr_id_create or @b usr_id_changed
* @return bool If an update or insert into the database was done then return true, otherwise false.
*/
public function save($updateFingerPrint = true)
{
global $gCurrentOrganization;
if($this->new_record)
{
$this->setValue('pho_org_id', $gCurrentOrganization->getValue('org_id'));
}
return parent::save($updateFingerPrint);
}
/**
* Rekursive Funktion zum Auswaehlen eines Beispielbildes aus einem moeglichst hohen Album
* Rueckgabe eines Arrays mit allen noetigen Infos um den Link zu erstellen
* @param int $phoId
* @return array
*/
public function shuffleImage($phoId = 0)
{
$shuffleImage = array('shuffle_pho_id' => 0, 'shuffle_img_nr' => 0, 'shuffle_img_begin' => '');
// wurde keine ID uebergeben, dann versuchen das Zufallsbild aus dem aktuellen Album zu nehmen
if($phoId === 0)
{
$phoId = $this->getValue('pho_id');
$shuffleImage['shuffle_pho_id'] = $this->getValue('pho_id');
$shuffleImage['shuffle_img_begin'] = $this->getValue('pho_begin', 'Y-m-d');
if($this->getValue('pho_quantity') > 0)
{
$shuffleImage['shuffle_img_nr'] = mt_rand(1, $this->getValue('pho_quantity'));
}
}
if($shuffleImage['shuffle_img_nr'] == 0)
{
// kein Bild vorhanden, dann in einem Unteralbum suchen
$sql = 'SELECT *
FROM '.TBL_PHOTOS.'
WHERE pho_pho_id_parent = '.$phoId.'
AND pho_locked = 0
ORDER BY pho_quantity DESC';
$childAlbumsStatement = $this->db->query($sql);
while($phoRow = $childAlbumsStatement->fetch())
{
if($shuffleImage['shuffle_img_nr'] == 0)
{
$shuffleImage['shuffle_pho_id'] = $phoRow['pho_id'];
$shuffleImage['shuffle_img_begin'] = $phoRow['pho_begin'];
if($phoRow['pho_quantity'] > 0)
{
$shuffleImage['shuffle_img_nr'] = mt_rand(1, $phoRow['pho_quantity']);
}
else
{
$shuffleImage = $this->shuffleImage($phoRow['pho_id']);
}
}
}
}
return $shuffleImage;
}
}