NGG_Album_Manager

The NextCellent Gallery NGG Album Manager class.

Defined (1)

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

/admin/class-ngg-album-manager.php  
  1. class NGG_Album_Manager implements NGG_Displayable { 
  2.  
  3. /** 
  4. * The selected album ID. 
  5. * @var int 
  6. */ 
  7. private $currentID = 0; 
  8.  
  9. /** 
  10. * The array for the galleries. 
  11. * @var array|bool 
  12. */ 
  13. private $galleries = false; 
  14.  
  15. /** 
  16. * The array for the albums. 
  17. * @var array|bool 
  18. */ 
  19. private $albums = false; 
  20.  
  21. /** 
  22. * The amount of all galleries. 
  23. * @var int|boolean 
  24. */ 
  25. private $num_galleries = false; 
  26.  
  27. /** 
  28. * The amount of all albums 
  29. * @var int|boolean 
  30. */ 
  31. var $num_albums = false; 
  32.  
  33. /** 
  34. * FZSM: small tweak to pas current Album Id to output. 
  35. */ 
  36. public function display() { 
  37.  
  38. /** 
  39. * @global nggdb $nggdb 
  40. */ 
  41. global $nggdb; 
  42.  
  43. $this->currentID = isset( $_REQUEST['act_album'] ) ? (int) $_REQUEST['act_album'] : 0; 
  44.  
  45. if ( isset ( $_POST['update'] ) || isset( $_POST['delete'] ) || isset( $_POST['add'] ) ) { 
  46. $this->processor(); 
  47.  
  48. if ( isset ( $_POST['update_album'] ) ) { 
  49. $this->update_album(); 
  50.  
  51. // get first all galleries & albums 
  52. $this->albums = $nggdb->find_all_album(); 
  53. $this->galleries = $nggdb->find_all_galleries(); 
  54. $this->num_albums = count( $this->albums ); 
  55. $this->num_galleries = count( $this->galleries ); 
  56. $this->output( $this->currentID ); 
  57.  
  58.  
  59. /** 
  60. * Handle the updates. 
  61. */ 
  62. private function processor() { 
  63. global $wpdb; 
  64.  
  65. check_admin_referer( 'ngg_album' ); 
  66.  
  67. if ( isset( $_POST['add'] ) && isset ( $_POST['newalbum'] ) ) { 
  68.  
  69. if ( ! nggGallery::current_user_can( 'NextGEN Add/Delete album' ) ) { 
  70. wp_die( __( 'Cheatin’ uh?' ) ); 
  71.  
  72. $result = nggdb::add_album( $_POST['newalbum'] ); 
  73. $this->currentID = ( $result ) ? $result : 0; 
  74.  
  75. //hook for other plugins 
  76. do_action( 'ngg_add_album', $this->currentID ); 
  77.  
  78. if ( $result ) { 
  79. nggGallery::show_message( __( 'Updated successfully', 'nggallery' ) ); 
  80.  
  81. if ( isset( $_POST['update'] ) && ( $this->currentID > 0 ) ) { 
  82.  
  83. $gid = ''; 
  84.  
  85. // get variable galleryContainer 
  86. parse_str( $_POST['sortorder'] ); 
  87. if ( is_array( $gid ) ) { 
  88. $serial_sort = serialize( $gid ); 
  89. $wpdb->query( "UPDATE $wpdb->nggalbum SET sortorder = '$serial_sort' WHERE id = $this->currentID " ); 
  90. } else { 
  91. $wpdb->query( "UPDATE $wpdb->nggalbum SET sortorder = '0' WHERE id = $this->currentID " ); 
  92.  
  93. //hook for other plugins 
  94. do_action( 'ngg_update_album_sortorder', $this->currentID ); 
  95.  
  96. nggGallery::show_message( __( 'Updated successfully', 'nggallery' ) ); 
  97.  
  98.  
  99. if ( isset( $_POST['delete'] ) ) { 
  100.  
  101. if ( ! nggGallery::current_user_can( 'NextGEN Add/Delete album' ) ) { 
  102. wp_die( __( 'Cheatin’ uh?' ) ); 
  103.  
  104. $result = nggdb::delete_album( $this->currentID ); 
  105.  
  106. //hook for other plugins 
  107. do_action( 'ngg_delete_album', $this->currentID ); 
  108.  
  109. // jump back to main selection 
  110. $this->currentID = 0; 
  111.  
  112. if ( $result ) { 
  113. nggGallery::show_message( __( 'Album deleted', 'nggallery' ) ); 
  114.  
  115.  
  116. private function update_album() { 
  117. global $wpdb; 
  118.  
  119. check_admin_referer( 'ngg_thickbox_form' ); 
  120.  
  121. if ( ! nggGallery::current_user_can( 'NextGEN Edit album settings' ) ) { 
  122. wp_die( __( 'Cheatin’ uh?' ) ); 
  123.  
  124. $name = $_POST['album_name']; 
  125. $desc = $_POST['album_desc']; 
  126. $prev = (int) $_POST['previewpic']; 
  127. $link = (int) $_POST['pageid']; 
  128.  
  129. // slug must be unique, we use the title for that 
  130. $slug = nggdb::get_unique_slug( sanitize_title( $name ), 'album', $this->currentID ); 
  131.  
  132. $result = $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->nggalbum SET slug= '%s', name= '%s', albumdesc= '%s', previewpic= %d, pageid= %d WHERE id = '%d'",  
  133. $slug, $name, $desc, $prev, $link, $this->currentID ) ); 
  134.  
  135. //hook for other plugin to update the fields 
  136. do_action( 'ngg_update_album', $this->currentID, $_POST ); 
  137.  
  138. if ( $result ) { 
  139. nggGallery::show_message( __( 'Updated successfully', 'nggallery' ) ); 
  140.  
  141. /** 
  142. * FZSM: Added Album to autocomplete 
  143. * @param $currentAlbum 
  144. */ 
  145. private function output( $currentAlbum ) { 
  146.  
  147. //TODO:Code MUST be optimized, how to flag a used gallery better? 
  148. $used_list = $this->get_used_galleries(); 
  149.  
  150. ?> 
  151.  
  152. <script type="text/javascript"> 
  153.  
  154. jQuery(document).ready( 
  155. function() { 
  156. jQuery("#previewpic").nggAutocomplete({ 
  157. type: 'image',  
  158. domain: "<?php echo home_url('index.php', is_ssl() ? 'https' : 'http'); ?>",  
  159. width: "95%",  
  160. term: <?php echo $currentAlbum; ?> 
  161. }); 
  162.  
  163. var selectContainer = jQuery('#selectContainer'); 
  164.  
  165. selectContainer.sortable({ 
  166. items: '.groupItem',  
  167. placeholder: 'sort_placeholder',  
  168. opacity: 0.7,  
  169. tolerance: 'intersect',  
  170. distance: 2,  
  171. forcePlaceholderSize: true,  
  172. connectWith: ['#galleryContainer'] 
  173. }); 
  174.  
  175. jQuery('#galleryContainer').sortable({ 
  176. items: '.groupItem',  
  177. placeholder: 'sort_placeholder',  
  178. opacity: 0.7,  
  179. tolerance: 'intersect',  
  180. distance: 2,  
  181. forcePlaceholderSize: true,  
  182. connectWith: ['#selectContainer', '#albumContainer'] 
  183. }); 
  184.  
  185. jQuery('#albumContainer').sortable({ 
  186. items: '.groupItem',  
  187. placeholder: 'sort_placeholder',  
  188. opacity: 0.7,  
  189. tolerance: 'intersect',  
  190. distance: 2,  
  191. forcePlaceholderSize: true,  
  192. connectWith: ['#galleryContainer'] 
  193. }); 
  194.  
  195. var min = jQuery('a.min'); 
  196.  
  197. min.bind('click', toggleContent); 
  198.  
  199. // Hide used galleries 
  200. jQuery('a#toggle_used').click(function() { 
  201. selectContainer.find('div.inUse').toggle(); 
  202. return false; 
  203. ); 
  204.  
  205. // Maximize All Portlets (whole site, no differentiation) 
  206. jQuery('a#all_max').click(function() { 
  207. jQuery('div.itemContent:hidden').show(); 
  208. return false; 
  209. ); 
  210.  
  211. // Minimize All Portlets (whole site, no differentiation) 
  212. jQuery('a#all_min').click(function() { 
  213. jQuery('div.itemContent:visible').hide(); 
  214. return false; 
  215. ); 
  216. // Auto Minimize if more than 4 (whole site, no differentiation) 
  217. if (min.length > 4) { 
  218. min.html('╋'); 
  219. jQuery('div.itemContent:visible').hide(); 
  220. selectContainer.find('div.inUse').toggle(); 
  221. ); 
  222.  
  223. var toggleContent = function() { 
  224. var targetContent = jQuery('div.itemContent', this.parentNode.parentNode); 
  225. if (targetContent.css('display') == 'none') { 
  226. targetContent.slideDown(300); 
  227. jQuery(this).html('━'); 
  228. } else { 
  229. targetContent.slideUp(300); 
  230. jQuery(this).html('╋'); 
  231. return false; 
  232. }; 
  233.  
  234. function ngg_serialize() { 
  235. //serial = jQuery.SortSerialize(s); 
  236. var serial = jQuery('#galleryContainer').sortable('serialize'); 
  237. jQuery('input[name=sortorder]').val(serial); 
  238.  
  239. function showDialog() { 
  240. var edit = jQuery("#editalbum"); 
  241. edit.dialog({ 
  242. width: 640,  
  243. resizable: false,  
  244. modal: true,  
  245. title: '<?php echo esc_js( __('Edit Album', 'nggallery') ); ?>' 
  246. }); 
  247. edit.find('.dialog-cancel').click(function() { 
  248. jQuery("#editalbum").dialog("close"); 
  249. }); 
  250.  
  251. </script> 
  252. <div class="wrap album" id="wrap"> 
  253. <h2><?php _e( 'Albums', 'nggallery' ) ?></h2> 
  254.  
  255. <form id="selectalbum" method="POST" onsubmit="ngg_serialize()" accept-charset="utf-8"> 
  256. <?php wp_nonce_field( 'ngg_album' ) ?> 
  257. <input name="sortorder" type="hidden"/> 
  258.  
  259. <div class="albumnav tablenav"> 
  260. <div class="alignleft actions"> 
  261. <?php esc_html_e( 'Select album', 'nggallery' ) ?> 
  262. <select id="act_album" name="act_album" onchange="this.form.submit();"> 
  263. <option value="0"><?php esc_html_e( 'No album selected', 'nggallery' ) ?></option> 
  264. <?php 
  265. if ( is_array( $this->albums ) ) { 
  266. foreach ( $this->albums as $album ) { 
  267. $selected = ( $this->currentID == $album->id ) ? 'selected="selected" ' : ''; 
  268. echo '<option value="' . $album->id . '" ' . $selected . '>' . $album->id . ' - ' . esc_attr( $album->name ) . '</option>' . "\n"; 
  269. ?> 
  270. </select> 
  271. <?php if ( $this->currentID > 0 ) { ?> 
  272. <input class="button-primary" type="submit" name="update" value="<?php esc_attr_e( 'Update',  
  273. 'nggallery' ); ?>"/> 
  274. <?php if ( nggGallery::current_user_can( 'NextGEN Edit album settings' ) ) { ?> 
  275. <input class="button-secondary" type="submit" name="showThickbox" value="<?php esc_attr_e( 'Edit album',  
  276. 'nggallery' ); ?>" onclick="showDialog(); return false;"/> 
  277. <?php } ?> 
  278. <?php if ( nggGallery::current_user_can( 'NextGEN Add/Delete album' ) ) { ?> 
  279. <input class="button-secondary action " type="submit" name="delete" value="<?php esc_attr_e( 'Delete',  
  280. 'nggallery' ); ?>" onclick="javascript:check=confirm('<?php echo esc_js( 'Delete album?',  
  281. 'nggallery' ); ?>');if(check==false) return false;"/> 
  282. <?php } ?> 
  283. <?php } else { ?> 
  284. <?php if ( nggGallery::current_user_can( 'NextGEN Add/Delete album' ) ) { ?> 
  285. <span><?php esc_html_e( 'Add new album', 'nggallery' ); ?> </span> 
  286. <input class="search-input" id="newalbum" name="newalbum" type="text" value=""/> 
  287. <input class="button-secondary action" type="submit" name="add" value="<?php esc_attr_e( 'Add',  
  288. 'nggallery' ); ?>"/> 
  289. <?php } ?> 
  290. <?php } ?> 
  291. </div> 
  292. </div> 
  293. </form> 
  294.  
  295. <br class="clear"/> 
  296.  
  297. <div> 
  298. <div style="float:right;"> 
  299. <a href="#" title="<?php esc_attr_e( 'Show / hide used galleries',  
  300. 'nggallery' ); ?>" id="toggle_used"><?php esc_html_e( '[Show all]', 'nggallery' ); ?></a> 
  301. | <a href="#" title="<?php esc_attr_e( 'Maximize the widget content',  
  302. 'nggallery' ); ?>" id="all_max"><?php esc_html_e( '[Maximize]', 'nggallery' ); ?></a> 
  303. | <a href="#" title="<?php esc_attr_e( 'Minimize the widget content',  
  304. 'nggallery' ); ?>" id="all_min"><?php esc_html_e( '[Minimize]', 'nggallery' ); ?></a> 
  305. </div> 
  306. <?php esc_html_e( 'After you create and select a album, you can drag and drop a gallery or another album into your new album below',  
  307. 'nggallery' ); ?> 
  308. </div> 
  309.  
  310. <br class="clear"> 
  311.  
  312. <div class="container"> 
  313.  
  314. <!-- /#album container --> 
  315. <div class="widget widget-right"> 
  316. <div class="widget-top"> 
  317. <h3><?php esc_html_e( 'Select album', 'nggallery' ); ?></h3> 
  318. </div> 
  319. <div id="albumContainer" class="widget-holder"> 
  320. <?php 
  321. if ( is_array( $this->albums ) ) { 
  322. foreach ( $this->albums as $album ) { 
  323. $this->get_container( 'a' . $album->id ); 
  324. ?> 
  325. </div> 
  326. </div> 
  327.  
  328. <!-- /#select container --> 
  329. <div class="widget widget-right"> 
  330. <div class="widget-top"> 
  331. <h3><?php esc_html_e( 'Select gallery', 'nggallery' ); ?></h3> 
  332. </div> 
  333. <div id="selectContainer" class="widget-holder"> 
  334. <?php 
  335.  
  336. if ( is_array( $this->galleries ) ) { 
  337. //get the array of galleries 
  338. $sort_array = $this->currentID > 0 ? (array) $this->albums[ $this->currentID ]->galleries : array(); 
  339. foreach ( $this->galleries as $gallery ) { 
  340. if ( ! in_array( $gallery->gid, $sort_array ) ) { 
  341. if ( in_array( $gallery->gid, $used_list ) ) { 
  342. $this->get_container( $gallery->gid, true ); 
  343. } else { 
  344. $this->get_container( $gallery->gid, false ); 
  345. ?> 
  346. </div> 
  347. </div> 
  348.  
  349. <!-- /#target-album --> 
  350. <div class="widget target-album widget-left"> 
  351.  
  352. <?php 
  353. if ( $this->currentID > 0 ) { 
  354. $album = $this->albums[ $this->currentID ]; 
  355. ?> 
  356. <div class="widget-top"> 
  357. <h3><?php esc_html_e( 'Album ID',  
  358. 'nggallery' ); ?><?php echo $album->id . ': ' . esc_html( $album->name ); ?> </h3> 
  359. </div> 
  360. <div id="galleryContainer" class="widget-holder target"> 
  361. <?php 
  362. $sort_array = (array) $this->albums[ $this->currentID ]->galleries; 
  363. foreach ( $sort_array as $galleryid ) { 
  364. $this->get_container( $galleryid, false ); 
  365. else 
  366. ?> 
  367. <div class="widget-top"> 
  368. <h3><?php esc_html_e( 'No album selected!', 'nggallery' ); ?></h3> 
  369. </div> 
  370. <div class="widget-holder target"> 
  371. <?php 
  372. ?> 
  373. </div> 
  374. </div> 
  375. <!-- /#target-album --> 
  376.  
  377. </div> 
  378. <!-- /#container --> 
  379. </div> 
  380. <!-- /#wrap --> 
  381.  
  382. <?php if ( $this->currentID > 0 ) : ?> 
  383. <!-- #editalbum --> 
  384. <div id="editalbum" style="display: none;"> 
  385. <form id="form-edit-album" method="POST" accept-charset="utf-8"> 
  386. <?php wp_nonce_field( 'ngg_thickbox_form' ) ?> 
  387. <input type="hidden" id="current_album" name="act_album" value="<?php echo $this->currentID; ?>"/> 
  388. <table width="100%"> 
  389. <tr> 
  390. <th> 
  391. <?php esc_html_e( 'Album name:', 'nggallery' ); ?><br/> 
  392. <input class="search-input" id="album_name" name="album_name" type="text" value="<?php echo esc_attr( $album->name ); ?>" style="width:95%"/> 
  393. </th> 
  394. </tr> 
  395. <tr> 
  396. <th> 
  397. <?php esc_html_e( 'Album description:', 'nggallery' ); ?><br/> 
  398. <textarea class="search-input" id="album_desc" name="album_desc" cols="50" rows="2" style="width:95%"><?php echo esc_attr( $album->albumdesc ); ?></textarea> 
  399. </th> 
  400. </tr> 
  401. <tr> 
  402. <th> 
  403. <?php esc_html_e( 'Select a preview image:', 'nggallery' ); ?><br/> 
  404. <select id="previewpic" name="previewpic" style="width:95%"> 
  405. <?php if ( $album->previewpic == 0 ) ?> 
  406. <option value="0"><?php esc_html_e( 'No picture', 'nggallery' ); ?></option> 
  407. <?php 
  408. if ( $album->previewpic == 0 ) { 
  409. echo '<option value="0" selected="selected">' . __( 'No picture',  
  410. 'nggallery' ) . '</option>'; 
  411. } else { 
  412. $picture = nggdb::find_image( $album->previewpic ); 
  413. echo '<option value="' . $picture->pid . '" selected="selected" >' . $picture->pid . ' - ' . ( empty( $picture->alltext ) ? esc_attr( $picture->filename ) : esc_attr( $picture->alltext ) ) . ' </option>' . "\n"; 
  414. ?> 
  415. </select> 
  416. </th> 
  417. </tr> 
  418. <tr> 
  419. <th> 
  420. <?php esc_html_e( 'Page Link to', 'nggallery' ) ?><br/> 
  421. <select name="pageid" style="width:95%"> 
  422. <option value="0"><?php esc_html_e( 'Not linked', 'nggallery' ) ?></option> 
  423. <?php 
  424. if ( ! isset( $album->pageid ) ) { 
  425. $album->pageid = 0; 
  426. parent_dropdown( $album->pageid ); ?> 
  427. </select> 
  428. </th> 
  429. </tr> 
  430.  
  431. <?php do_action( 'ngg_edit_album_settings', $this->currentID ); ?> 
  432.  
  433. <tr> 
  434. <td class="submit"> 
  435. <input type="submit" class="button-primary" name="update_album" value="<?php esc_attr_e( 'OK',  
  436. 'nggallery' ); ?>"/> 
  437.   
  438. <input class="button-secondary dialog-cancel" type="reset" value="<?php esc_attr_e( 'Cancel',  
  439. 'nggallery' ); ?>"/> 
  440. </td> 
  441. </tr> 
  442. </table> 
  443. </form> 
  444. </div> 
  445. <!-- /#editalbum --> 
  446. <?php endif; ?> 
  447.  
  448. <?php 
  449.  
  450.  
  451. /** 
  452. * Create the album or gallery container 
  453. * @param integer $id (the prefix 'a' indidcates that you look for a album 
  454. * @param bool $used (object will be hidden) 
  455. */ 
  456. private function get_container( $id = 0, $used = false ) { 
  457.  
  458. /** 
  459. * @global nggdb $nggdb 
  460. */ 
  461. global $nggdb; 
  462.  
  463. $obj = array(); 
  464. $preview_image = ''; 
  465. $class = ''; 
  466.  
  467. // if the id started with a 'a', then it's a sub album 
  468. if ( substr( $id, 0, 1 ) == 'a' ) { 
  469.  
  470. if ( ! $album = $this->albums[ substr( $id, 1 ) ] ) { 
  471. return; 
  472.  
  473. $obj['id'] = $album->id; 
  474. $obj['name'] = $obj['title'] = $album->name; 
  475. $obj['type'] = 'album'; 
  476. $class = 'album_obj'; 
  477.  
  478. // get the post name 
  479. $post = get_post( $album->pageid ); 
  480. $obj['pagenname'] = ( $post == null ) ? '---' : $post->post_title; 
  481.  
  482. // for speed reason we limit it to 50 
  483. if ( $this->num_albums < 50 ) { 
  484. if ( $album->previewpic != 0 ) { 
  485. $image = $nggdb->find_image( $album->previewpic ); 
  486. $preview_image = ( ! is_null( $image->thumbURL ) ) ? '<div class="inlinepicture"><img src="' . esc_url( $image->thumbURL ) . '" /></div>' : ''; 
  487.  
  488. // this indicates that we have a album container 
  489. $prefix = 'a'; 
  490.  
  491. } else { 
  492. if ( ! $gallery = $nggdb->find_gallery( $id ) ) { 
  493. return; 
  494.  
  495. $obj['id'] = $gallery->gid; 
  496. $obj['name'] = $gallery->name; 
  497. $obj['title'] = $gallery->title; 
  498. $obj['type'] = 'gallery'; 
  499.  
  500. // get the post name 
  501. $post = get_post( $gallery->pageid ); 
  502. $obj['pagenname'] = ( $post == null ) ? '---' : $post->post_title; 
  503.  
  504. // for spped reason we limit it to 50 
  505. if ( $this->num_galleries < 50 ) { 
  506. // set image url 
  507. $image = $nggdb->find_image( $gallery->previewpic ); 
  508. $preview_image = isset( $image->thumbURL ) ? '<div class="inlinepicture"><img src="' . esc_url( $image->thumbURL ) . '" /></div>' : ''; 
  509.  
  510. $prefix = ''; 
  511.  
  512. // add class if it's in use in other albums 
  513. $used = $used ? ' inUse' : ''; 
  514.  
  515. echo '<div id="gid-' . $prefix . $obj['id'] . '" class="groupItem' . $used . '"> 
  516. <div class="innerhandle"> 
  517. <div class="item_top ' . $class . '"> 
  518. <a href="#" class="min" title="close">━</a> 
  519. ID: ' . $obj['id'] . ' | ' . wp_html_excerpt( esc_html( nggGallery::i18n( $obj['title'] ) ),  
  520. 25 ) . ' 
  521. </div> 
  522. <div class="itemContent"> 
  523. ' . $preview_image . ' 
  524. <p><strong>' . __( 'Name',  
  525. 'nggallery' ) . ': </strong>' . esc_html( nggGallery::i18n( $obj['name'] ) ) . '</p> 
  526. <p><strong>' . __( 'Title',  
  527. 'nggallery' ) . ': </strong>' . esc_html( nggGallery::i18n( $obj['title'] ) ) . '</p> 
  528. <p><strong>' . __( 'Page',  
  529. 'nggallery' ) . ': </strong>' . esc_html( nggGallery::i18n( $obj['pagenname'] ) ) . '</p> 
  530. ' . apply_filters( 'ngg_display_album_item_content', '', $obj ) . ' 
  531. </div> 
  532. </div> 
  533. </div>'; 
  534.  
  535. /** 
  536. * get all used galleries from all albums 
  537. * @return array $used_galleries_ids 
  538. */ 
  539. private function get_used_galleries() { 
  540.  
  541. $used = array(); 
  542.  
  543. if ( $this->albums ) { 
  544. foreach ( $this->albums as $key => $value ) { 
  545. $sort_array = $this->albums[ $key ]->galleries; 
  546. foreach ( $sort_array as $galleryid ) { 
  547. if ( ! in_array( $galleryid, $used ) ) { 
  548. $used[] = $galleryid; 
  549.  
  550. return $used;