PageRenderTime 53ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/nextgen-gallery-3/lib/ngg-db.php

https://bitbucket.org/zachisit/zachis.it-m
PHP | 1109 lines | 515 code | 210 blank | 384 comment | 87 complexity | d5d5f160205be9430d9e701b1c255ce1 MD5 | raw file
  1. <?php
  2. if ( !class_exists('nggdb') ) :
  3. /**
  4. * NextGEN Gallery Database Class
  5. *
  6. * @author Alex Rabe, Vincent Prat
  7. * @copyright 2008-2011
  8. * @since 1.0.0
  9. */
  10. class nggdb {
  11. /**
  12. * Holds the list of all galleries
  13. *
  14. * @since 1.1.0
  15. * @access public
  16. * @var object|array
  17. */
  18. var $galleries = false;
  19. /**
  20. * Holds the list of all images
  21. *
  22. * @since 1.3.0
  23. * @access public
  24. * @var object|array
  25. */
  26. var $images = false;
  27. /**
  28. * Holds the list of all albums
  29. *
  30. * @since 1.3.0
  31. * @access public
  32. * @var object|array
  33. */
  34. var $albums = false;
  35. /**
  36. * The array for the pagination
  37. *
  38. * @since 1.1.0
  39. * @access public
  40. * @var array
  41. */
  42. var $paged = false;
  43. /**
  44. * PHP4 compatibility layer for calling the PHP5 constructor.
  45. *
  46. */
  47. function nggdb() {
  48. return $this->__construct();
  49. }
  50. /**
  51. * Init the Database Abstraction layer for NextGEN Gallery
  52. *
  53. */
  54. function __construct() {
  55. global $wpdb;
  56. $this->galleries = array();
  57. $this->images = array();
  58. $this->albums = array();
  59. $this->paged = array();
  60. register_shutdown_function(array(&$this, '__destruct'));
  61. }
  62. /**
  63. * PHP5 style destructor and will run when database object is destroyed.
  64. *
  65. * @return bool Always true
  66. */
  67. function __destruct() {
  68. return true;
  69. }
  70. /**
  71. * Get all the album and unserialize the content
  72. *
  73. * @since 1.3.0
  74. * @param string $order_by
  75. * @param string $order_dir
  76. * @param int $limit number of albums, 0 shows all albums
  77. * @param int $start the start index for paged albums
  78. * @return array $album
  79. */
  80. function find_all_album( $order_by = 'id', $order_dir = 'ASC', $limit = 0, $start = 0) {
  81. global $wpdb;
  82. $order_dir = ( $order_dir == 'DESC') ? 'DESC' : 'ASC';
  83. $limit_by = ( $limit > 0 ) ? 'LIMIT ' . intval($start) . ',' . intval($limit) : '';
  84. $this->albums = $wpdb->get_results("SELECT * FROM $wpdb->nggalbum ORDER BY {$order_by} {$order_dir} {$limit_by}" , OBJECT_K );
  85. if ( !$this->albums )
  86. return array();
  87. foreach ($this->albums as $key => $value) {
  88. $this->albums[$key]->galleries = empty ($this->albums[$key]->sortorder) ? array() : (array) unserialize($this->albums[$key]->sortorder) ;
  89. $this->albums[$key]->name = stripslashes( $this->albums[$key]->name );
  90. $this->albums[$key]->albumdesc = stripslashes( $this->albums[$key]->albumdesc );
  91. wp_cache_add($key, $this->albums[$key], 'ngg_album');
  92. }
  93. return $this->albums;
  94. }
  95. /**
  96. * Get all the galleries
  97. *
  98. * @param string $order_by
  99. * @param string $order_dir
  100. * @param bool $counter (optional) Select true when you need to count the images
  101. * @param int $limit number of paged galleries, 0 shows all galleries
  102. * @param int $start the start index for paged galleries
  103. * @param bool $exclude
  104. * @return array $galleries
  105. */
  106. function find_all_galleries($order_by = 'gid', $order_dir = 'ASC', $counter = false, $limit = 0, $start = 0, $exclude = true) {
  107. global $wpdb;
  108. // Check for the exclude setting
  109. $exclude_clause = ($exclude) ? ' AND exclude<>1 ' : '';
  110. $order_dir = ( $order_dir == 'DESC') ? 'DESC' : 'ASC';
  111. $limit_by = ( $limit > 0 ) ? 'LIMIT ' . intval($start) . ',' . intval($limit) : '';
  112. $this->galleries = $wpdb->get_results( "SELECT SQL_CALC_FOUND_ROWS * FROM $wpdb->nggallery ORDER BY {$order_by} {$order_dir} {$limit_by}", OBJECT_K );
  113. // Count the number of galleries and calculate the pagination
  114. if ($limit > 0) {
  115. $this->paged['total_objects'] = intval ( $wpdb->get_var( "SELECT FOUND_ROWS()" ) );
  116. $this->paged['objects_per_page'] = max ( count( $this->galleries ), $limit );
  117. $this->paged['max_objects_per_page'] = ( $limit > 0 ) ? ceil( $this->paged['total_objects'] / intval($limit)) : 1;
  118. }
  119. if ( !$this->galleries )
  120. return array();
  121. // get the galleries information
  122. foreach ($this->galleries as $key => $value) {
  123. $galleriesID[] = $key;
  124. // init the counter values
  125. $this->galleries[$key]->counter = 0;
  126. $this->galleries[$key]->title = stripslashes($this->galleries[$key]->title);
  127. $this->galleries[$key]->galdesc = stripslashes($this->galleries[$key]->galdesc);
  128. wp_cache_add($key, $this->galleries[$key], 'ngg_gallery');
  129. }
  130. // if we didn't need to count the images then stop here
  131. if ( !$counter )
  132. return $this->galleries;
  133. // get the counter values
  134. $picturesCounter = $wpdb->get_results('SELECT galleryid, COUNT(*) as counter FROM '.$wpdb->nggpictures.' WHERE galleryid IN (\''.implode('\',\'', $galleriesID).'\') ' . $exclude_clause . ' GROUP BY galleryid', OBJECT_K);
  135. if ( !$picturesCounter )
  136. return $this->galleries;
  137. // add the counter to the gallery objekt
  138. foreach ($picturesCounter as $key => $value) {
  139. $this->galleries[$value->galleryid]->counter = $value->counter;
  140. wp_cache_add($value->galleryid, $this->galleries[$value->galleryid], 'ngg_gallery');
  141. }
  142. return $this->galleries;
  143. }
  144. /**
  145. * Get a gallery given its ID
  146. *
  147. * @param int|string $id or $slug
  148. * @return A nggGallery object (null if not found)
  149. */
  150. function find_gallery( $id ) {
  151. global $wpdb;
  152. if( is_numeric($id) ) {
  153. if ( $gallery = wp_cache_get($id, 'ngg_gallery') )
  154. return $gallery;
  155. $gallery = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->nggallery WHERE gid = %d", $id ) );
  156. } else
  157. $gallery = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->nggallery WHERE slug = %s", $id ) );
  158. // Build the object from the query result
  159. if ($gallery) {
  160. // it was a bad idea to use a object, stripslashes_deep() could not used here, learn from it
  161. $gallery->title = stripslashes($gallery->title);
  162. $gallery->galdesc = stripslashes($gallery->galdesc);
  163. $gallery->abspath = WINABSPATH . $gallery->path;
  164. //TODO:Possible failure , $id could be a number or name
  165. wp_cache_add($id, $gallery, 'ngg_gallery');
  166. return $gallery;
  167. } else
  168. return false;
  169. }
  170. /**
  171. * This function return all information about the gallery and the images inside
  172. *
  173. * @param int|string $id or $name
  174. * @param string $order_by
  175. * @param string $order_dir (ASC |DESC)
  176. * @param bool $exclude
  177. * @param int $limit number of paged galleries, 0 shows all galleries
  178. * @param int $start the start index for paged galleries
  179. * @param bool $json remove the key for associative array in json request
  180. * @return An array containing the nggImage objects representing the images in the gallery.
  181. */
  182. function get_gallery($id, $order_by = 'sortorder', $order_dir = 'ASC', $exclude = true, $limit = 0, $start = 0, $json = false) {
  183. global $wpdb;
  184. // init the gallery as empty array
  185. $gallery = array();
  186. $i = 0;
  187. // Check for the exclude setting
  188. $exclude_clause = ($exclude) ? ' AND tt.exclude<>1 ' : '';
  189. // Say no to any other value
  190. $order_dir = ( $order_dir == 'DESC') ? 'DESC' : 'ASC';
  191. $order_by = ( empty($order_by) ) ? 'sortorder' : $order_by;
  192. // Should we limit this query ?
  193. $limit_by = ( $limit > 0 ) ? 'LIMIT ' . intval($start) . ',' . intval($limit) : '';
  194. // Query database
  195. if( is_numeric($id) )
  196. $result = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS tt.*, t.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE t.gid = %d {$exclude_clause} ORDER BY tt.{$order_by} {$order_dir} {$limit_by}", $id ), OBJECT_K );
  197. else
  198. $result = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS tt.*, t.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE t.slug = %s {$exclude_clause} ORDER BY tt.{$order_by} {$order_dir} {$limit_by}", $id ), OBJECT_K );
  199. // Count the number of images and calculate the pagination
  200. if ($limit > 0) {
  201. $this->paged['total_objects'] = intval ( $wpdb->get_var( "SELECT FOUND_ROWS()" ) );
  202. $this->paged['objects_per_page'] = max ( count( $result ), $limit );
  203. $this->paged['max_objects_per_page'] = ( $limit > 0 ) ? ceil( $this->paged['total_objects'] / intval($limit)) : 1;
  204. }
  205. // Build the object
  206. if ($result) {
  207. // Now added all image data
  208. foreach ($result as $key => $value) {
  209. // due to a browser bug we need to remove the key for associative array for json request
  210. // (see http://code.google.com/p/chromium/issues/detail?id=883)
  211. if ($json) $key = $i++;
  212. $gallery[$key] = new nggImage( $value );
  213. }
  214. }
  215. // Could not add to cache, the structure is different to find_gallery() cache_add, need rework
  216. //wp_cache_add($id, $gallery, 'ngg_gallery');
  217. return $gallery;
  218. }
  219. /**
  220. * This function return all information about the gallery and the images inside
  221. *
  222. * @param int|string $id or $name
  223. * @param string $orderby
  224. * @param string $order (ASC |DESC)
  225. * @param bool $exclude
  226. * @return An array containing the nggImage objects representing the images in the gallery.
  227. */
  228. function get_ids_from_gallery($id, $order_by = 'sortorder', $order_dir = 'ASC', $exclude = true) {
  229. global $wpdb;
  230. // Check for the exclude setting
  231. $exclude_clause = ($exclude) ? ' AND tt.exclude<>1 ' : '';
  232. // Say no to any other value
  233. $order_dir = ( $order_dir == 'DESC') ? 'DESC' : 'ASC';
  234. $order_by = ( empty($order_by) ) ? 'sortorder' : $order_by;
  235. // Query database
  236. if( is_numeric($id) )
  237. $result = $wpdb->get_col( $wpdb->prepare( "SELECT tt.pid FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE t.gid = %d $exclude_clause ORDER BY tt.{$order_by} $order_dir", $id ) );
  238. else
  239. $result = $wpdb->get_col( $wpdb->prepare( "SELECT tt.pid FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE t.slug = %s $exclude_clause ORDER BY tt.{$order_by} $order_dir", $id ) );
  240. return $result;
  241. }
  242. /**
  243. * Delete a gallery AND all the pictures associated to this gallery!
  244. *
  245. * @id The gallery ID
  246. */
  247. function delete_gallery( $id ) {
  248. global $wpdb;
  249. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->nggpictures WHERE galleryid = %d", $id) );
  250. $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->nggallery WHERE gid = %d", $id) );
  251. wp_cache_delete($id, 'ngg_gallery');
  252. //TODO:Remove all tag relationship
  253. return true;
  254. }
  255. /**
  256. * Get an album given its ID
  257. *
  258. * @id The album ID or name
  259. * @return A nggGallery object (false if not found)
  260. */
  261. function find_album( $id ) {
  262. global $wpdb;
  263. // Query database
  264. if ( is_numeric($id) && $id != 0 ) {
  265. if ( $album = wp_cache_get($id, 'ngg_album') )
  266. return $album;
  267. $album = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->nggalbum WHERE id = %d", $id) );
  268. } elseif ( $id == 'all' || (is_numeric($id) && $id == 0) ) {
  269. // init the object and fill it
  270. $album = new stdClass();
  271. $album->id = 'all';
  272. $album->name = __('Album overview','nggallery');
  273. $album->albumdesc = __('Album overview','nggallery');
  274. $album->previewpic = 0;
  275. $album->sortorder = serialize( $wpdb->get_col("SELECT gid FROM $wpdb->nggallery") );
  276. } else {
  277. $album = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->nggalbum WHERE slug = %s", $id) );
  278. }
  279. // Unserialize the galleries inside the album
  280. if ( $album ) {
  281. if ( !empty( $album->sortorder ) )
  282. $album->gallery_ids = unserialize( $album->sortorder );
  283. // it was a bad idea to use a object, stripslashes_deep() could not used here, learn from it
  284. $album->albumdesc = stripslashes($album->albumdesc);
  285. $album->name = stripslashes($album->name);
  286. wp_cache_add($album->id, $album, 'ngg_album');
  287. return $album;
  288. }
  289. return false;
  290. }
  291. /**
  292. * Delete an album
  293. *
  294. * @id The album ID
  295. */
  296. function delete_album( $id ) {
  297. global $wpdb;
  298. $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->nggalbum WHERE id = %d", $id) );
  299. wp_cache_delete($id, 'ngg_album');
  300. return $result;
  301. }
  302. /**
  303. * Insert an image in the database
  304. *
  305. * @return the ID of the inserted image
  306. */
  307. function insert_image($gid, $filename, $alttext, $desc, $exclude) {
  308. global $wpdb;
  309. $result = $wpdb->query(
  310. "INSERT INTO $wpdb->nggpictures (galleryid, filename, description, alttext, exclude) VALUES "
  311. . "('$gid', '$filename', '$desc', '$alttext', '$exclude');");
  312. $pid = (int) $wpdb->insert_id;
  313. wp_cache_delete($gid, 'ngg_gallery');
  314. return $pid;
  315. }
  316. /**
  317. * nggdb::update_image() - Update an image in the database
  318. *
  319. * @param int $pid id of the image
  320. * @param (optional) string|int $galleryid
  321. * @param (optional) string $filename
  322. * @param (optional) string $description
  323. * @param (optional) string $alttext
  324. * @param (optional) int $exclude (0 or 1)
  325. * @param (optional) int $sortorder
  326. * @return bool result of update query
  327. */
  328. function update_image($pid, $galleryid = false, $filename = false, $description = false, $alttext = false, $exclude = false, $sortorder = false) {
  329. global $wpdb;
  330. $sql = array();
  331. $pid = (int) $pid;
  332. // slug must be unique, we use the alttext for that
  333. $slug = nggdb::get_unique_slug( sanitize_title( $alttext ), 'image' );
  334. $update = array(
  335. 'image_slug' => $slug,
  336. 'galleryid' => $galleryid,
  337. 'filename' => $filename,
  338. 'description' => $description,
  339. 'alttext' => $alttext,
  340. 'exclude' => $exclude,
  341. 'sortorder' => $sortorder);
  342. // create the sql parameter "name = value"
  343. foreach ($update as $key => $value)
  344. if ($value !== false)
  345. $sql[] = $key . " = '" . $value . "'";
  346. // create the final string
  347. $sql = implode(', ', $sql);
  348. if ( !empty($sql) && $pid != 0)
  349. $result = $wpdb->query( "UPDATE $wpdb->nggpictures SET $sql WHERE pid = $pid" );
  350. wp_cache_delete($pid, 'ngg_image');
  351. return $result;
  352. }
  353. /**
  354. * nggdb::update_gallery() - Update an gallery in the database
  355. *
  356. * @since V1.7.0
  357. * @param int $id id of the gallery
  358. * @param (optional) string $title or name of the gallery
  359. * @param (optional) string $path
  360. * @param (optional) string $description
  361. * @param (optional) int $pageid
  362. * @param (optional) int $previewpic
  363. * @param (optional) int $author
  364. * @return bool result of update query
  365. */
  366. function update_gallery($id, $name = false, $path = false, $title = false, $description = false, $pageid = false, $previewpic = false, $author = false) {
  367. global $wpdb;
  368. $sql = array();
  369. $id = (int) $id;
  370. // slug must be unique, we use the title for that
  371. $slug = nggdb::get_unique_slug( sanitize_title( $title ), 'gallery' );
  372. $update = array(
  373. 'name' => $name,
  374. 'slug' => $slug,
  375. 'path' => $path,
  376. 'title' => $title,
  377. 'galdesc' => $description,
  378. 'pageid' => $pageid,
  379. 'previewpic' => $previewpic,
  380. 'author' => $author);
  381. // create the sql parameter "name = value"
  382. foreach ($update as $key => $value)
  383. if ($value !== false)
  384. $sql[] = $key . " = '" . $value . "'";
  385. // create the final string
  386. $sql = implode(', ', $sql);
  387. if ( !empty($sql) && $id != 0)
  388. $result = $wpdb->query( "UPDATE $wpdb->nggallery SET $sql WHERE gid = $id" );
  389. wp_cache_delete($id, 'ngg_gallery');
  390. return $result;
  391. }
  392. /**
  393. * nggdb::update_album() - Update an album in the database
  394. *
  395. * @since V1.7.0
  396. * @param int $ id id of the album
  397. * @param (optional) string $title
  398. * @param (optional) int $previewpic
  399. * @param (optional) string $description
  400. * @param (optional) serialized array $sortorder
  401. * @param (optional) int $pageid
  402. * @return bool result of update query
  403. */
  404. function update_album($id, $name = false, $previewpic = false, $description = false, $sortorder = false, $pageid = false ) {
  405. global $wpdb;
  406. $sql = array();
  407. $id = (int) $id;
  408. // slug must be unique, we use the title for that
  409. $slug = nggdb::get_unique_slug( sanitize_title( $name ), 'album' );
  410. $update = array(
  411. 'name' => $name,
  412. 'slug' => $slug,
  413. 'previewpic' => $previewpic,
  414. 'albumdesc' => $description,
  415. 'sortorder' => $sortorder,
  416. 'pageid' => $pageid);
  417. // create the sql parameter "name = value"
  418. foreach ($update as $key => $value)
  419. if ($value !== false)
  420. $sql[] = $key . " = '" . $value . "'";
  421. // create the final string
  422. $sql = implode(', ', $sql);
  423. if ( !empty($sql) && $id != 0)
  424. $result = $wpdb->query( "UPDATE $wpdb->nggalbum SET $sql WHERE id = $id" );
  425. wp_cache_delete($id, 'ngg_album');
  426. return $result;
  427. }
  428. /**
  429. * Get an image given its ID
  430. *
  431. * @param int|string The image ID or Slug
  432. * @return object A nggImage object representing the image (false if not found)
  433. */
  434. function find_image( $id ) {
  435. global $wpdb;
  436. if( is_numeric($id) ) {
  437. if ( $image = wp_cache_get($id, 'ngg_image') )
  438. return $image;
  439. $result = $wpdb->get_row( $wpdb->prepare( "SELECT tt.*, t.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.pid = %d ", $id ) );
  440. } else
  441. $result = $wpdb->get_row( $wpdb->prepare( "SELECT tt.*, t.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.image_slug = %s ", $id ) );
  442. // Build the object from the query result
  443. if ($result) {
  444. $image = new nggImage($result);
  445. return $image;
  446. }
  447. return false;
  448. }
  449. /**
  450. * Get images given a list of IDs
  451. *
  452. * @param $pids array of picture_ids
  453. * @return An array of nggImage objects representing the images
  454. */
  455. function find_images_in_list( $pids, $exclude = false, $order = 'ASC' ) {
  456. global $wpdb;
  457. $result = array();
  458. // Check for the exclude setting
  459. $exclude_clause = ($exclude) ? ' AND t.exclude <> 1 ' : '';
  460. // Check for the exclude setting
  461. $order_clause = ($order == 'RAND') ? 'ORDER BY rand() ' : ' ORDER BY t.pid ASC' ;
  462. if ( is_array($pids) ) {
  463. $id_list = "'" . implode("', '", $pids) . "'";
  464. // Save Query database
  465. $images = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggpictures AS t INNER JOIN $wpdb->nggallery AS tt ON t.galleryid = tt.gid WHERE t.pid IN ($id_list) $exclude_clause $order_clause", OBJECT_K);
  466. // Build the image objects from the query result
  467. if ($images) {
  468. foreach ($images as $key => $image)
  469. $result[$key] = new nggImage( $image );
  470. }
  471. }
  472. return $result;
  473. }
  474. /**
  475. * Add an image to the database
  476. *
  477. * @since V1.4.0
  478. * @param int $pid id of the gallery
  479. * @param (optional) string|int $galleryid
  480. * @param (optional) string $filename
  481. * @param (optional) string $description
  482. * @param (optional) string $alttext
  483. * @param (optional) array $meta data
  484. * @param (optional) int $post_id (required for sync with WP media lib)
  485. * @param (optional) string $imagedate
  486. * @param (optional) int $exclude (0 or 1)
  487. * @param (optional) int $sortorder
  488. * @return bool result of the ID of the inserted image
  489. */
  490. function add_image( $id = false, $filename = false, $description = '', $alttext = '', $meta_data = false, $post_id = 0, $imagedate = '0000-00-00 00:00:00', $exclude = 0, $sortorder = 0 ) {
  491. global $wpdb;
  492. if ( is_array($meta_data) )
  493. $meta_data = serialize($meta_data);
  494. // slug must be unique, we use the alttext for that
  495. $slug = nggdb::get_unique_slug( sanitize_title( $alttext ), 'image' );
  496. // Add the image
  497. if ( false === $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->nggpictures (image_slug, galleryid, filename, description, alttext, meta_data, post_id, imagedate, exclude, sortorder)
  498. VALUES (%s, %d, %s, %s, %s, %s, %d, %s, %d, %d)", $slug, $id, $filename, $description, $alttext, $meta_data, $post_id, $imagedate, $exclude, $sortorder ) ) ) {
  499. return false;
  500. }
  501. $imageID = (int) $wpdb->insert_id;
  502. // Remove from cache the galley, needs to be rebuild now
  503. wp_cache_delete( $id, 'ngg_gallery');
  504. //and give me the new id
  505. return $imageID;
  506. }
  507. /**
  508. * Add an album to the database
  509. *
  510. * @since V1.7.0
  511. * @param (optional) string $title
  512. * @param (optional) int $previewpic
  513. * @param (optional) string $description
  514. * @param (optional) serialized array $sortorder
  515. * @param (optional) int $pageid
  516. * @return bool result of the ID of the inserted album
  517. */
  518. function add_album( $name = false, $previewpic = 0, $description = '', $sortorder = 0, $pageid = 0 ) {
  519. global $wpdb;
  520. // name must be unique, we use the title for that
  521. $slug = nggdb::get_unique_slug( sanitize_title( $name ), 'album' );
  522. // Add the album
  523. if ( false === $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->nggalbum (name, slug, previewpic, albumdesc, sortorder, pageid)
  524. VALUES (%s, %s, %d, %s, %s, %d)", $name, $slug, $previewpic, $description, $sortorder, $pageid ) ) ) {
  525. return false;
  526. }
  527. $albumID = (int) $wpdb->insert_id;
  528. //and give me the new id
  529. return $albumID;
  530. }
  531. /**
  532. * Add an gallery to the database
  533. *
  534. * @since V1.7.0
  535. * @param (optional) string $title or name of the gallery
  536. * @param (optional) string $path
  537. * @param (optional) string $description
  538. * @param (optional) int $pageid
  539. * @param (optional) int $previewpic
  540. * @param (optional) int $author
  541. * @return bool result of the ID of the inserted gallery
  542. */
  543. function add_gallery( $title = '', $path = '', $description = '', $pageid = 0, $previewpic = 0, $author = 0 ) {
  544. global $wpdb;
  545. // slug must be unique, we use the title for that
  546. $slug = nggdb::get_unique_slug( sanitize_title( $title ), 'gallery' );
  547. // Note : The field 'name' is deprecated, it's currently kept only for compat reason with older shortcodes, we copy the slug into this field
  548. if ( false === $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->nggallery (name, slug, path, title, galdesc, pageid, previewpic, author)
  549. VALUES (%s, %s, %s, %s, %s, %d, %d, %d)", $slug, $slug, $path, $title, $description, $pageid, $previewpic, $author ) ) ) {
  550. return false;
  551. }
  552. $galleryID = (int) $wpdb->insert_id;
  553. //and give me the new id
  554. return $galleryID;
  555. }
  556. /**
  557. * Delete an image entry from the database
  558. * @param integer $id is the Image ID
  559. */
  560. function delete_image( $id ) {
  561. global $wpdb;
  562. // Delete the image
  563. $result = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->nggpictures WHERE pid = %d", $id) );
  564. // Delete tag references
  565. wp_delete_object_term_relationships( $id, 'ngg_tag');
  566. // Remove from cache
  567. wp_cache_delete( $id, 'ngg_image');
  568. return $result;
  569. }
  570. /**
  571. * Get the last images registered in the database with a maximum number of $limit results
  572. *
  573. * @param integer $page start offset as page number (0,1,2,3,4...)
  574. * @param integer $limit the number of result
  575. * @param bool $exclude do not show exluded images
  576. * @param int $galleryId Only look for images with this gallery id, or in all galleries if id is 0
  577. * @param string $orderby is one of "id" (default, order by pid), "date" (order by exif date), sort (order by user sort order)
  578. * @return
  579. */
  580. function find_last_images($page = 0, $limit = 30, $exclude = true, $galleryId = 0, $orderby = "id") {
  581. global $wpdb;
  582. // Check for the exclude setting
  583. $exclude_clause = ($exclude) ? ' AND exclude<>1 ' : '';
  584. // a limit of 0 makes no sense
  585. $limit = ($limit == 0) ? 30 : $limit;
  586. // calculate the offset based on the pagr number
  587. $offset = (int) $page * $limit;
  588. $galleryId = (int) $galleryId;
  589. $gallery_clause = ($galleryId === 0) ? '' : ' AND galleryid = ' . $galleryId . ' ';
  590. // default order by pid
  591. $order = 'pid DESC';
  592. switch ($orderby) {
  593. case 'date':
  594. $order = 'imagedate DESC';
  595. break;
  596. case 'sort':
  597. $order = 'sortorder ASC';
  598. break;
  599. }
  600. $result = array();
  601. $gallery_cache = array();
  602. // Query database
  603. $images = $wpdb->get_results("SELECT * FROM $wpdb->nggpictures WHERE 1=1 $exclude_clause $gallery_clause ORDER BY $order LIMIT $offset, $limit");
  604. // Build the object from the query result
  605. if ($images) {
  606. foreach ($images as $key => $image) {
  607. // cache a gallery , so we didn't need to lookup twice
  608. if (!array_key_exists($image->galleryid, $gallery_cache))
  609. $gallery_cache[$image->galleryid] = nggdb::find_gallery($image->galleryid);
  610. // Join gallery information with picture information
  611. foreach ($gallery_cache[$image->galleryid] as $index => $value)
  612. $image->$index = $value;
  613. // Now get the complete image data
  614. $result[$key] = new nggImage( $image );
  615. }
  616. }
  617. return $result;
  618. }
  619. /**
  620. * nggdb::get_random_images() - Get an random image from one ore more gally
  621. *
  622. * @param integer $number of images
  623. * @param integer $galleryID optional a Gallery
  624. * @return A nggImage object representing the image (null if not found)
  625. */
  626. function get_random_images($number = 1, $galleryID = 0) {
  627. global $wpdb;
  628. $number = (int) $number;
  629. $galleryID = (int) $galleryID;
  630. $images = array();
  631. // Query database
  632. if ($galleryID == 0)
  633. $result = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.exclude != 1 ORDER by rand() limit $number");
  634. else
  635. $result = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE t.gid = $galleryID AND tt.exclude != 1 ORDER by rand() limit {$number}");
  636. // Return the object from the query result
  637. if ($result) {
  638. foreach ($result as $image) {
  639. $images[] = new nggImage( $image );
  640. }
  641. return $images;
  642. }
  643. return null;
  644. }
  645. /**
  646. * Get all the images from a given album
  647. *
  648. * @param object|int $album The album object or the id
  649. * @param string $order_by
  650. * @param string $order_dir
  651. * @param bool $exclude
  652. * @return An array containing the nggImage objects representing the images in the album.
  653. */
  654. function find_images_in_album($album, $order_by = 'galleryid', $order_dir = 'ASC', $exclude = true) {
  655. global $wpdb;
  656. if ( !is_object($album) )
  657. $album = nggdb::find_album( $album );
  658. // Get gallery list
  659. $gallery_list = implode(',', $album->gallery_ids);
  660. // Check for the exclude setting
  661. $exclude_clause = ($exclude) ? ' AND tt.exclude<>1 ' : '';
  662. // Say no to any other value
  663. $order_dir = ( $order_dir == 'DESC') ? 'DESC' : 'ASC';
  664. $order_by = ( empty($order_by) ) ? 'galleryid' : $order_by;
  665. $result = $wpdb->get_results("SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.galleryid IN ($gallery_list) $exclude_clause ORDER BY tt.$order_by $order_dir");
  666. // Return the object from the query result
  667. if ($result) {
  668. foreach ($result as $image) {
  669. $images[] = new nggImage( $image );
  670. }
  671. return $images;
  672. }
  673. return null;
  674. }
  675. /**
  676. * search for images and return the result
  677. *
  678. * @since 1.3.0
  679. * @param string $request
  680. * @param int $limit number of results, 0 shows all results
  681. * @return Array Result of the request
  682. */
  683. function search_for_images( $request, $limit = 0 ) {
  684. global $wpdb;
  685. // If a search pattern is specified, load the posts that match
  686. if ( !empty($request) ) {
  687. // added slashes screw with quote grouping when done early, so done later
  688. $request = stripslashes($request);
  689. // split the words it a array if seperated by a space or comma
  690. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $request, $matches);
  691. $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
  692. $n = '%';
  693. $searchand = '';
  694. $search = '';
  695. foreach( (array) $search_terms as $term) {
  696. $term = addslashes_gpc($term);
  697. $search .= "{$searchand}((tt.description LIKE '{$n}{$term}{$n}') OR (tt.alttext LIKE '{$n}{$term}{$n}') OR (tt.filename LIKE '{$n}{$term}{$n}'))";
  698. $searchand = ' AND ';
  699. }
  700. $term = $wpdb->escape($request);
  701. if (count($search_terms) > 1 && $search_terms[0] != $request )
  702. $search .= " OR (tt.description LIKE '{$n}{$term}{$n}') OR (tt.alttext LIKE '{$n}{$term}{$n}') OR (tt.filename LIKE '{$n}{$term}{$n}')";
  703. if ( !empty($search) )
  704. $search = " AND ({$search}) ";
  705. $limit_by = ( $limit > 0 ) ? 'LIMIT ' . intval($limit) : '';
  706. } else
  707. return false;
  708. // build the final query
  709. $query = "SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE 1=1 $search ORDER BY tt.pid ASC $limit_by";
  710. $result = $wpdb->get_results($query);
  711. // TODO: Currently we didn't support a proper pagination
  712. $this->paged['total_objects'] = $this->paged['objects_per_page'] = intval ( $wpdb->get_var( "SELECT FOUND_ROWS()" ) );
  713. $this->paged['max_objects_per_page'] = 1;
  714. // Return the object from the query result
  715. if ($result) {
  716. foreach ($result as $image) {
  717. $images[] = new nggImage( $image );
  718. }
  719. return $images;
  720. }
  721. return null;
  722. }
  723. /**
  724. * search for galleries and return the result
  725. *
  726. * @since 1.7.0
  727. * @param string $request
  728. * @param int $limit number of results, 0 shows all results
  729. * @return Array Result of the request
  730. */
  731. function search_for_galleries( $request, $limit = 0 ) {
  732. global $wpdb;
  733. // If a search pattern is specified, load the posts that match
  734. if ( !empty($request) ) {
  735. // added slashes screw with quote grouping when done early, so done later
  736. $request = stripslashes($request);
  737. // split the words it a array if seperated by a space or comma
  738. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $request, $matches);
  739. $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
  740. $n = '%';
  741. $searchand = '';
  742. $search = '';
  743. foreach( (array) $search_terms as $term) {
  744. $term = addslashes_gpc($term);
  745. $search .= "{$searchand}((title LIKE '{$n}{$term}{$n}') OR (name LIKE '{$n}{$term}{$n}') )";
  746. $searchand = ' AND ';
  747. }
  748. $term = $wpdb->escape($request);
  749. if (count($search_terms) > 1 && $search_terms[0] != $request )
  750. $search .= " OR (title LIKE '{$n}{$term}{$n}') OR (name LIKE '{$n}{$term}{$n}')";
  751. if ( !empty($search) )
  752. $search = " AND ({$search}) ";
  753. $limit = ( $limit > 0 ) ? 'LIMIT ' . intval($limit) : '';
  754. } else
  755. return false;
  756. // build the final query
  757. $query = "SELECT * FROM $wpdb->nggallery WHERE 1=1 $search ORDER BY title ASC $limit";
  758. $result = $wpdb->get_results($query);
  759. return $result;
  760. }
  761. /**
  762. * search for albums and return the result
  763. *
  764. * @since 1.7.0
  765. * @param string $request
  766. * @param int $limit number of results, 0 shows all results
  767. * @return Array Result of the request
  768. */
  769. function search_for_albums( $request, $limit = 0 ) {
  770. global $wpdb;
  771. // If a search pattern is specified, load the posts that match
  772. if ( !empty($request) ) {
  773. // added slashes screw with quote grouping when done early, so done later
  774. $request = stripslashes($request);
  775. // split the words it a array if seperated by a space or comma
  776. preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $request, $matches);
  777. $search_terms = array_map(create_function('$a', 'return trim($a, "\\"\'\\n\\r ");'), $matches[0]);
  778. $n = '%';
  779. $searchand = '';
  780. $search = '';
  781. foreach( (array) $search_terms as $term) {
  782. $term = addslashes_gpc($term);
  783. $search .= "{$searchand}(name LIKE '{$n}{$term}{$n}')";
  784. $searchand = ' AND ';
  785. }
  786. $term = $wpdb->escape($request);
  787. if (count($search_terms) > 1 && $search_terms[0] != $request )
  788. $search .= " OR (name LIKE '{$n}{$term}{$n}')";
  789. if ( !empty($search) )
  790. $search = " AND ({$search}) ";
  791. $limit = ( $limit > 0 ) ? 'LIMIT ' . intval($limit) : '';
  792. } else
  793. return false;
  794. // build the final query
  795. $query = "SELECT * FROM $wpdb->nggalbum WHERE 1=1 $search ORDER BY name ASC $limit";
  796. $result = $wpdb->get_results($query);
  797. return $result;
  798. }
  799. /**
  800. * search for a filename
  801. *
  802. * @since 1.4.0
  803. * @param string $filename
  804. * @param int (optional) $galleryID
  805. * @return Array Result of the request
  806. */
  807. function search_for_file( $filename, $galleryID = false ) {
  808. global $wpdb;
  809. // If a search pattern is specified, load the posts that match
  810. if ( !empty($filename) ) {
  811. // added slashes screw with quote grouping when done early, so done later
  812. $term = $wpdb->escape($filename);
  813. $where_clause = '';
  814. if ( is_numeric($galleryID) ) {
  815. $id = (int) $galleryID;
  816. $where_clause = " AND tt.galleryid = {$id}";
  817. }
  818. }
  819. // build the final query
  820. $query = "SELECT t.*, tt.* FROM $wpdb->nggallery AS t INNER JOIN $wpdb->nggpictures AS tt ON t.gid = tt.galleryid WHERE tt.filename = '{$term}' {$where_clause} ORDER BY tt.pid ASC ";
  821. $result = $wpdb->get_row($query);
  822. // Return the object from the query result
  823. if ($result) {
  824. $image = new nggImage( $result );
  825. return $image;
  826. }
  827. return null;
  828. }
  829. /**
  830. * Update or add meta data for an image
  831. *
  832. * @since 1.4.0
  833. * @param int $id The image ID
  834. * @param array $values An array with existing or new values
  835. * @return bool result of query
  836. */
  837. function update_image_meta( $id, $new_values ) {
  838. global $wpdb;
  839. // Query database for existing values
  840. // Use cache object
  841. $old_values = $wpdb->get_var( $wpdb->prepare( "SELECT meta_data FROM $wpdb->nggpictures WHERE pid = %d ", $id ) );
  842. $old_values = unserialize( $old_values );
  843. $meta = array_merge( (array)$old_values, (array)$new_values );
  844. $result = $wpdb->query( $wpdb->prepare("UPDATE $wpdb->nggpictures SET meta_data = %s WHERE pid = %d", serialize($meta), $id) );
  845. wp_cache_delete($id, 'ngg_image');
  846. return $result;
  847. }
  848. /**
  849. * Computes a unique slug for the gallery,album or image, when given the desired slug.
  850. *
  851. * @since 1.7.0
  852. * @author taken from WP Core includes/post.php
  853. * @param string $slug the desired slug (post_name)
  854. * @param string $type ('image', 'album' or 'gallery')
  855. * @param int (optional) $id of the object, so that it's not checked against itself
  856. * @return string unique slug for the object, based on $slug (with a -1, -2, etc. suffix)
  857. */
  858. function get_unique_slug( $slug, $type, $id = 0 ) {
  859. global $wpdb;
  860. switch ($type) {
  861. case 'image':
  862. $check_sql = "SELECT image_slug FROM $wpdb->nggpictures WHERE image_slug = %s AND NOT pid = %d LIMIT 1";
  863. break;
  864. case 'album':
  865. $check_sql = "SELECT slug FROM $wpdb->nggalbum WHERE slug = %s AND NOT id = %d LIMIT 1";
  866. break;
  867. case 'gallery':
  868. $check_sql = "SELECT slug FROM $wpdb->nggallery WHERE slug = %s AND NOT gid = %d LIMIT 1";
  869. break;
  870. default:
  871. return false;
  872. }
  873. //if you didn't give us a name we take the type
  874. $slug = empty($slug) ? $type: $slug;
  875. // Slugs must be unique across all objects.
  876. $slug_check = $wpdb->get_var( $wpdb->prepare( $check_sql, $slug, $id ) );
  877. if ( $slug_check ) {
  878. $suffix = 2;
  879. do {
  880. $alt_name = substr ($slug, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "-$suffix";
  881. $slug_check = $wpdb->get_var( $wpdb->prepare($check_sql, $alt_name, $id ) );
  882. $suffix++;
  883. } while ( $slug_check );
  884. $slug = $alt_name;
  885. }
  886. return $slug;
  887. }
  888. }
  889. endif;
  890. if ( ! isset($GLOBALS['nggdb']) ) {
  891. /**
  892. * Initate the NextGEN Gallery Database Object, for later cache reasons
  893. * @global object $nggdb Creates a new nggdb object
  894. * @since 1.1.0
  895. */
  896. unset($GLOBALS['nggdb']);
  897. $GLOBALS['nggdb'] = new nggdb() ;
  898. }
  899. ?>