nggdb

NextGEN Gallery Database Class.

Defined (1)

The class is defined in the following location(s).

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