Album.php 20 KB

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