Album.php 20 KB

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