Album.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. <?php
  2. ###
  3. # @name Album Module
  4. # @copyright 2015 by Tobias Reich
  5. ###
  6. if (!defined('LYCHEE')) exit('Error: Direct access is not allowed!');
  7. class Album extends Module {
  8. private $database = null;
  9. private $settings = null;
  10. private $albumIDs = null;
  11. public function __construct($database, $plugins, $settings, $albumIDs) {
  12. # Init vars
  13. $this->database = $database;
  14. $this->plugins = $plugins;
  15. $this->settings = $settings;
  16. $this->albumIDs = $albumIDs;
  17. return true;
  18. }
  19. public function add($title = 'Untitled', $public = 0, $visible = 1) {
  20. # Check dependencies
  21. self::dependencies(isset($this->database));
  22. # Call plugins
  23. $this->plugins(__METHOD__, 0, func_get_args());
  24. # Parse
  25. if (strlen($title)>50) $title = substr($title, 0, 50);
  26. # Database
  27. $sysstamp = time();
  28. $query = Database::prepare($this->database, "INSERT INTO ? (title, sysstamp, public, visible) VALUES ('?', '?', '?', '?')", array(LYCHEE_TABLE_ALBUMS, $title, $sysstamp, $public, $visible));
  29. $result = $this->database->query($query);
  30. # Call plugins
  31. $this->plugins(__METHOD__, 1, func_get_args());
  32. if (!$result) {
  33. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  34. return false;
  35. }
  36. return $this->database->insert_id;
  37. }
  38. public static function prepareData($data) {
  39. # This function requires the following album-attributes and turns them
  40. # into a front-end friendly format: id, title, public, sysstamp, password
  41. # Note that some attributes remain unchanged
  42. # Check dependencies
  43. self::dependencies(isset($data));
  44. # Init
  45. $album = null;
  46. # Set unchanged attributes
  47. $album['id'] = $data['id'];
  48. $album['title'] = $data['title'];
  49. $album['public'] = $data['public'];
  50. # Parse date
  51. $album['sysdate'] = date('F Y', $data['sysstamp']);
  52. # Parse password
  53. $album['password'] = ($data['password']=='' ? '0' : '1');
  54. # Set placeholder for thumbs
  55. $album['thumbs'] = array();
  56. return $album;
  57. }
  58. public function get() {
  59. # Check dependencies
  60. self::dependencies(isset($this->database, $this->settings, $this->albumIDs));
  61. # Call plugins
  62. $this->plugins(__METHOD__, 0, func_get_args());
  63. # Get album information
  64. switch ($this->albumIDs) {
  65. case 'f': $return['public'] = '0';
  66. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE star = 1 " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  67. break;
  68. case 's': $return['public'] = '0';
  69. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE public = 1 " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  70. break;
  71. case 'r': $return['public'] = '0';
  72. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  73. break;
  74. case '0': $return['public'] = '0';
  75. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = 0 " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  76. break;
  77. default: $query = Database::prepare($this->database, "SELECT * FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  78. $albums = $this->database->query($query);
  79. $return = $albums->fetch_assoc();
  80. $return['sysdate'] = date('d M. Y', $return['sysstamp']);
  81. $return['password'] = ($return['password']=='' ? '0' : '1');
  82. $query = Database::prepare($this->database, "SELECT id, title, tags, public, star, album, thumbUrl, takestamp, url FROM ? WHERE album = '?' " . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  83. break;
  84. }
  85. # Get photos
  86. $photos = $this->database->query($query);
  87. $previousPhotoID = '';
  88. while ($photo = $photos->fetch_assoc()) {
  89. # Turn data from the database into a front-end friendly format
  90. $photo = Photo::prepareData($photo);
  91. # Set previous and next photoID for navigation purposes
  92. $photo['previousPhoto'] = $previousPhotoID;
  93. $photo['nextPhoto'] = '';
  94. # Set current photoID as nextPhoto of previous photo
  95. if ($previousPhotoID!=='') $return['content'][$previousPhotoID]['nextPhoto'] = $photo['id'];
  96. $previousPhotoID = $photo['id'];
  97. # Add to return
  98. $return['content'][$photo['id']] = $photo;
  99. }
  100. if ($photos->num_rows===0) {
  101. # Album empty
  102. $return['content'] = false;
  103. } else {
  104. # Enable next and previous for the first and last photo
  105. $lastElement = end($return['content']);
  106. $lastElementId = $lastElement['id'];
  107. $firstElement = reset($return['content']);
  108. $firstElementId = $firstElement['id'];
  109. if ($lastElementId!==$firstElementId) {
  110. $return['content'][$lastElementId]['nextPhoto'] = $firstElementId;
  111. $return['content'][$firstElementId]['previousPhoto'] = $lastElementId;
  112. }
  113. }
  114. $return['id'] = $this->albumIDs;
  115. $return['num'] = $photos->num_rows;
  116. # Call plugins
  117. $this->plugins(__METHOD__, 1, func_get_args());
  118. return $return;
  119. }
  120. public function getAll($public) {
  121. # Check dependencies
  122. self::dependencies(isset($this->database, $this->settings, $public));
  123. # Call plugins
  124. $this->plugins(__METHOD__, 0, func_get_args());
  125. # Initialize return var
  126. $return = array(
  127. 'smartalbums' => null,
  128. 'albums' => null,
  129. 'num' => 0
  130. );
  131. # Get SmartAlbums
  132. if ($public===false) $return['smartalbums'] = $this->getSmartInfo();
  133. # Albums query
  134. if ($public===false) $query = Database::prepare($this->database, 'SELECT id, title, public, sysstamp, password FROM ? ' . $this->settings['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
  135. else $query = Database::prepare($this->database, 'SELECT id, title, public, sysstamp, password FROM ? WHERE public = 1 AND visible <> 0 ' . $this->settings['sortingAlbums'], array(LYCHEE_TABLE_ALBUMS));
  136. # Execute query
  137. $albums = $this->database->query($query);
  138. if (!$albums) {
  139. Log::error($database, __METHOD__, __LINE__, 'Could not get all albums (' . $database->error . ')');
  140. exit('Error: ' . $this->database->error);
  141. }
  142. # For each album
  143. while ($album = $albums->fetch_assoc()) {
  144. # Turn data from the database into a front-end friendly format
  145. $album = Album::prepareData($album);
  146. # Thumbs
  147. if (($public===true&&$album['password']==='0')||
  148. ($public===false)) {
  149. # Execute query
  150. $query = Database::prepare($this->database, "SELECT thumbUrl FROM ? WHERE album = '?' ORDER BY star DESC, " . substr($this->settings['sortingPhotos'], 9) . " LIMIT 3", array(LYCHEE_TABLE_PHOTOS, $album['id']));
  151. $thumbs = $this->database->query($query);
  152. # For each thumb
  153. $k = 0;
  154. while ($thumb = $thumbs->fetch_object()) {
  155. $album['thumbs'][$k] = LYCHEE_URL_UPLOADS_THUMB . $thumb->thumbUrl;
  156. $k++;
  157. }
  158. }
  159. # Add to return
  160. $return['albums'][] = $album;
  161. }
  162. # Num of albums
  163. $return['num'] = $albums->num_rows;
  164. # Call plugins
  165. $this->plugins(__METHOD__, 1, func_get_args());
  166. return $return;
  167. }
  168. private function getSmartInfo() {
  169. # Check dependencies
  170. self::dependencies(isset($this->database, $this->settings));
  171. # Initialize return var
  172. $return = array(
  173. 'unsorted' => null,
  174. 'public' => null,
  175. 'starred' => null,
  176. 'recent' => null
  177. );
  178. ###
  179. # Unsorted
  180. ###
  181. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE album = 0 ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  182. $unsorted = $this->database->query($query);
  183. $i = 0;
  184. $return['unsorted'] = array(
  185. 'thumbs' => array(),
  186. 'num' => $unsorted->num_rows
  187. );
  188. while($row = $unsorted->fetch_object()) {
  189. if ($i<3) {
  190. $return['unsorted']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row->thumbUrl;
  191. $i++;
  192. } else break;
  193. }
  194. ###
  195. # Starred
  196. ###
  197. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE star = 1 ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  198. $starred = $this->database->query($query);
  199. $i = 0;
  200. $return['starred'] = array(
  201. 'thumbs' => array(),
  202. 'num' => $starred->num_rows
  203. );
  204. while($row3 = $starred->fetch_object()) {
  205. if ($i<3) {
  206. $return['starred']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  207. $i++;
  208. } else break;
  209. }
  210. ###
  211. # Public
  212. ###
  213. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE public = 1 ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  214. $public = $this->database->query($query);
  215. $i = 0;
  216. $return['public'] = array(
  217. 'thumbs' => array(),
  218. 'num' => $public->num_rows
  219. );
  220. while($row2 = $public->fetch_object()) {
  221. if ($i<3) {
  222. $return['public']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row2->thumbUrl;
  223. $i++;
  224. } else break;
  225. }
  226. ###
  227. # Recent
  228. ###
  229. $query = Database::prepare($this->database, 'SELECT thumbUrl FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) ' . $this->settings['sortingPhotos'], array(LYCHEE_TABLE_PHOTOS));
  230. $recent = $this->database->query($query);
  231. $i = 0;
  232. $return['recent'] = array(
  233. 'thumbs' => array(),
  234. 'num' => $recent->num_rows
  235. );
  236. while($row3 = $recent->fetch_object()) {
  237. if ($i<3) {
  238. $return['recent']['thumbs'][$i] = LYCHEE_URL_UPLOADS_THUMB . $row3->thumbUrl;
  239. $i++;
  240. } else break;
  241. }
  242. # Return SmartAlbums
  243. return $return;
  244. }
  245. public function getArchive() {
  246. # Check dependencies
  247. self::dependencies(isset($this->database, $this->albumIDs));
  248. # Call plugins
  249. $this->plugins(__METHOD__, 0, func_get_args());
  250. # Illicit chars
  251. $badChars = array_merge(
  252. array_map('chr', range(0,31)),
  253. array("<", ">", ":", '"', "/", "\\", "|", "?", "*")
  254. );
  255. # Photos query
  256. switch($this->albumIDs) {
  257. case 's':
  258. $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE public = 1', array(LYCHEE_TABLE_PHOTOS));
  259. $zipTitle = 'Public';
  260. break;
  261. case 'f':
  262. $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE star = 1', array(LYCHEE_TABLE_PHOTOS));
  263. $zipTitle = 'Starred';
  264. break;
  265. case 'r':
  266. $photos = Database::prepare($this->database, 'SELECT title, url FROM ? WHERE LEFT(id, 10) >= unix_timestamp(DATE_SUB(NOW(), INTERVAL 1 DAY)) GROUP BY checksum', array(LYCHEE_TABLE_PHOTOS));
  267. $zipTitle = 'Recent';
  268. break;
  269. default:
  270. $photos = Database::prepare($this->database, "SELECT title, url FROM ? WHERE album = '?'", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  271. $zipTitle = 'Unsorted';
  272. }
  273. # Get title from database when album is not a SmartAlbum
  274. if ($this->albumIDs!=0&&is_numeric($this->albumIDs)) {
  275. $query = Database::prepare($this->database, "SELECT title FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  276. $album = $this->database->query($query);
  277. # Error in database query
  278. if (!$album) {
  279. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  280. return false;
  281. }
  282. # Fetch object
  283. $album = $album->fetch_object();
  284. # Photo not found
  285. if ($album===null) {
  286. Log::error($this->database, __METHOD__, __LINE__, 'Album not found. Cannot start download.');
  287. return false;
  288. }
  289. # Set title
  290. $zipTitle = $album->title;
  291. }
  292. # Escape title
  293. $zipTitle = str_replace($badChars, '', $zipTitle);
  294. $filename = LYCHEE_DATA . $zipTitle . '.zip';
  295. # Create zip
  296. $zip = new ZipArchive();
  297. if ($zip->open($filename, ZIPARCHIVE::CREATE)!==TRUE) {
  298. Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive');
  299. return false;
  300. }
  301. # Execute query
  302. $photos = $this->database->query($photos);
  303. # Check if album empty
  304. if ($photos->num_rows==0) {
  305. Log::error($this->database, __METHOD__, __LINE__, 'Could not create ZipArchive without images');
  306. return false;
  307. }
  308. # Parse each path
  309. $files = array();
  310. while ($photo = $photos->fetch_object()) {
  311. # Parse url
  312. $photo->url = LYCHEE_UPLOADS_BIG . $photo->url;
  313. # Parse title
  314. $photo->title = str_replace($badChars, '', $photo->title);
  315. if (!isset($photo->title)||$photo->title==='') $photo->title = 'Untitled';
  316. # Check if readable
  317. if (!@is_readable($photo->url)) continue;
  318. # Get extension of image
  319. $extension = getExtension($photo->url);
  320. # Set title for photo
  321. $zipFileName = $zipTitle . '/' . $photo->title . $extension;
  322. # Check for duplicates
  323. if (!empty($files)) {
  324. $i = 1;
  325. while (in_array($zipFileName, $files)) {
  326. # Set new title for photo
  327. $zipFileName = $zipTitle . '/' . $photo->title . '-' . $i . $extension;
  328. $i++;
  329. }
  330. }
  331. # Add to array
  332. $files[] = $zipFileName;
  333. # Add photo to zip
  334. $zip->addFile($photo->url, $zipFileName);
  335. }
  336. # Finish zip
  337. $zip->close();
  338. # Send zip
  339. header("Content-Type: application/zip");
  340. header("Content-Disposition: attachment; filename=\"$zipTitle.zip\"");
  341. header("Content-Length: " . filesize($filename));
  342. readfile($filename);
  343. # Delete zip
  344. unlink($filename);
  345. # Call plugins
  346. $this->plugins(__METHOD__, 1, func_get_args());
  347. return true;
  348. }
  349. public function setTitle($title = 'Untitled') {
  350. # Check dependencies
  351. self::dependencies(isset($this->database, $this->albumIDs));
  352. # Call plugins
  353. $this->plugins(__METHOD__, 0, func_get_args());
  354. # Execute query
  355. $query = Database::prepare($this->database, "UPDATE ? SET title = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $title, $this->albumIDs));
  356. $result = $this->database->query($query);
  357. # Call plugins
  358. $this->plugins(__METHOD__, 1, func_get_args());
  359. if (!$result) {
  360. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  361. return false;
  362. }
  363. return true;
  364. }
  365. public function setDescription($description = '') {
  366. # Check dependencies
  367. self::dependencies(isset($this->database, $this->albumIDs));
  368. # Call plugins
  369. $this->plugins(__METHOD__, 0, func_get_args());
  370. # Execute query
  371. $query = Database::prepare($this->database, "UPDATE ? SET description = '?' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $description, $this->albumIDs));
  372. $result = $this->database->query($query);
  373. # Call plugins
  374. $this->plugins(__METHOD__, 1, func_get_args());
  375. if (!$result) {
  376. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  377. return false;
  378. }
  379. return true;
  380. }
  381. public function getPublic() {
  382. # Check dependencies
  383. self::dependencies(isset($this->database, $this->albumIDs));
  384. # Call plugins
  385. $this->plugins(__METHOD__, 0, func_get_args());
  386. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f') return false;
  387. # Execute query
  388. $query = Database::prepare($this->database, "SELECT public FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  389. $albums = $this->database->query($query);
  390. $album = $albums->fetch_object();
  391. # Call plugins
  392. $this->plugins(__METHOD__, 1, func_get_args());
  393. if ($album->public==1) return true;
  394. return false;
  395. }
  396. public function getDownloadable() {
  397. # Check dependencies
  398. self::dependencies(isset($this->database, $this->albumIDs));
  399. # Call plugins
  400. $this->plugins(__METHOD__, 0, func_get_args());
  401. if ($this->albumIDs==='0'||$this->albumIDs==='s'||$this->albumIDs==='f'||$this->albumIDs==='r') return false;
  402. # Execute query
  403. $query = Database::prepare($this->database, "SELECT downloadable FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  404. $albums = $this->database->query($query);
  405. $album = $albums->fetch_object();
  406. # Call plugins
  407. $this->plugins(__METHOD__, 1, func_get_args());
  408. if ($album->downloadable==1) return true;
  409. return false;
  410. }
  411. public function setPublic($public, $password, $visible, $downloadable) {
  412. # Check dependencies
  413. self::dependencies(isset($this->database, $this->albumIDs));
  414. # Call plugins
  415. $this->plugins(__METHOD__, 0, func_get_args());
  416. # Convert values
  417. $public = ($public==='1' ? 1 : 0);
  418. $visible = ($visible==='1' ? 1 : 0);
  419. $downloadable = ($downloadable==='1' ? 1 : 0);
  420. # Set public
  421. $query = Database::prepare($this->database, "UPDATE ? SET public = '?', visible = '?', downloadable = '?', password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $public, $visible, $downloadable, $this->albumIDs));
  422. $result = $this->database->query($query);
  423. if (!$result) {
  424. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  425. return false;
  426. }
  427. # Reset permissions for photos
  428. if ($public===1) {
  429. $query = Database::prepare($this->database, "UPDATE ? SET public = 0 WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  430. $result = $this->database->query($query);
  431. if (!$result) {
  432. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  433. return false;
  434. }
  435. }
  436. # Call plugins
  437. $this->plugins(__METHOD__, 1, func_get_args());
  438. # Set password
  439. if (isset($password)&&strlen($password)>0) return $this->setPassword($password);
  440. return true;
  441. }
  442. private function setPassword($password) {
  443. # Check dependencies
  444. self::dependencies(isset($this->database, $this->albumIDs));
  445. # Call plugins
  446. $this->plugins(__METHOD__, 0, func_get_args());
  447. if (strlen($password)>0) {
  448. # Get hashed password
  449. $password = getHashedString($password);
  450. # Set hashed password
  451. # Do not prepare $password because it is hashed and save
  452. # Preparing (escaping) the password would destroy the hash
  453. $query = Database::prepare($this->database, "UPDATE ? SET password = '$password' WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  454. } else {
  455. # Unset password
  456. $query = Database::prepare($this->database, "UPDATE ? SET password = NULL WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  457. }
  458. # Execute query
  459. $result = $this->database->query($query);
  460. # Call plugins
  461. $this->plugins(__METHOD__, 1, func_get_args());
  462. if (!$result) {
  463. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  464. return false;
  465. }
  466. return true;
  467. }
  468. public function checkPassword($password) {
  469. # Check dependencies
  470. self::dependencies(isset($this->database, $this->albumIDs));
  471. # Call plugins
  472. $this->plugins(__METHOD__, 0, func_get_args());
  473. # Execute query
  474. $query = Database::prepare($this->database, "SELECT password FROM ? WHERE id = '?' LIMIT 1", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  475. $albums = $this->database->query($query);
  476. $album = $albums->fetch_object();
  477. # Call plugins
  478. $this->plugins(__METHOD__, 1, func_get_args());
  479. if ($album->password=='') return true;
  480. else if ($album->password===crypt($password, $album->password)) return true;
  481. return false;
  482. }
  483. public function merge() {
  484. # Check dependencies
  485. self::dependencies(isset($this->database, $this->albumIDs));
  486. # Call plugins
  487. $this->plugins(__METHOD__, 0, func_get_args());
  488. # Convert to array
  489. $albumIDs = explode(',', $this->albumIDs);
  490. # Get first albumID
  491. $albumID = array_splice($albumIDs, 0, 1);
  492. $albumID = $albumID[0];
  493. $query = Database::prepare($this->database, "UPDATE ? SET album = ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $albumID, $this->albumIDs));
  494. $result = $this->database->query($query);
  495. if (!$result) {
  496. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  497. return false;
  498. }
  499. # $albumIDs contains all IDs without the first albumID
  500. # Convert to string
  501. $filteredIDs = implode(',', $albumIDs);
  502. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $filteredIDs));
  503. $result = $this->database->query($query);
  504. # Call plugins
  505. $this->plugins(__METHOD__, 1, func_get_args());
  506. if (!$result) {
  507. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  508. return false;
  509. }
  510. return true;
  511. }
  512. public function delete() {
  513. # Check dependencies
  514. self::dependencies(isset($this->database, $this->albumIDs));
  515. # Call plugins
  516. $this->plugins(__METHOD__, 0, func_get_args());
  517. # Init vars
  518. $error = false;
  519. # Execute query
  520. $query = Database::prepare($this->database, "SELECT id FROM ? WHERE album IN (?)", array(LYCHEE_TABLE_PHOTOS, $this->albumIDs));
  521. $photos = $this->database->query($query);
  522. # For each album delete photo
  523. while ($row = $photos->fetch_object()) {
  524. $photo = new Photo($this->database, $this->plugins, null, $row->id);
  525. if (!$photo->delete($row->id)) $error = true;
  526. }
  527. # Delete albums
  528. $query = Database::prepare($this->database, "DELETE FROM ? WHERE id IN (?)", array(LYCHEE_TABLE_ALBUMS, $this->albumIDs));
  529. $result = $this->database->query($query);
  530. # Call plugins
  531. $this->plugins(__METHOD__, 1, func_get_args());
  532. if ($error) return false;
  533. if (!$result) {
  534. Log::error($this->database, __METHOD__, __LINE__, $this->database->error);
  535. return false;
  536. }
  537. return true;
  538. }
  539. }
  540. ?>