nggAdmin

NggAdmin - Class for admin operation.

Defined (1)

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

/products/photocrati_nextgen/modules/ngglegacy/admin/functions.php  
  1. class nggAdmin{ 
  2.  
  3. /** 
  4. * create a new gallery & folder 
  5. * @class nggAdmin 
  6. * @param string $name of the gallery 
  7. * @param string $defaultpath 
  8. * @param bool $output if the function should show an error messsage or not 
  9. * @return 
  10. */ 
  11. static function create_gallery($title, $defaultpath, $output = true) { 
  12.  
  13. global $user_ID; 
  14. $fs = C_Fs::get_instance(); 
  15. $storage = C_Gallery_Storage::get_instance(); 
  16.  
  17. // get the current user ID 
  18. wp_get_current_user(); 
  19.  
  20. //cleanup pathname 
  21. $name = sanitize_file_name( sanitize_title($title) ); 
  22. $name = apply_filters('ngg_gallery_name', $name); 
  23. $txt = ''; 
  24.  
  25. $galleryObj = new stdClass; 
  26. $galleryObj->path = ''; 
  27. $nggRoot = $storage->get_gallery_abspath($galleryObj); 
  28.  
  29. // No gallery name ? 
  30. if ( empty($name) ) { 
  31. if ($output) nggGallery::show_error( __('No valid gallery name!', 'nggallery') ); 
  32. return false; 
  33.  
  34. $galleryObj = new stdClass; 
  35. $galleryObj->path = $fs->join_paths($defaultpath, $name); 
  36. $gallery_path = $storage->get_gallery_abspath($galleryObj); 
  37.  
  38. // check for main folder 
  39. if ( !is_dir($nggRoot) ) { 
  40. if ( !wp_mkdir_p( $nggRoot ) ) { 
  41. $txt = __('Directory', 'nggallery').' <strong>' . esc_html( $nggRoot ) . '</strong> '.__('didn\'t exist. Please create first the main gallery folder ', 'nggallery').'!<br />'; 
  42. $txt .= __('Check this link, if you didn\'t know how to set the permission :', 'nggallery').' <a href="http://codex.wordpress.org/Changing_File_Permissions">http://codex.wordpress.org/Changing_File_Permissions</a> '; 
  43. if ($output) nggGallery::show_error($txt); 
  44. return false; 
  45.  
  46. // 1. Check for existing folder 
  47. if ( is_dir($gallery_path) && !(SAFE_MODE) ) { 
  48. $suffix = 1; 
  49. do { 
  50. $alt_name = substr ($name, 0, 200 - ( strlen( $suffix ) + 1 ) ) . "_$suffix"; 
  51. $galleryObj->path = $fs->join_paths($defaultpath, $alt_name); 
  52. $gallery_path = $storage->get_gallery_abspath($galleryObj); 
  53. $dir_check = is_dir($gallery_path); 
  54. $suffix++; 
  55. } while ( $dir_check ); 
  56. $name = $alt_name; 
  57.  
  58. $thumb_path = $fs->join_paths($gallery_path, 'thumbs'); 
  59.  
  60. // 2. Create new gallery folder 
  61. if ( !wp_mkdir_p ($gallery_path) ) 
  62. $txt = __('Unable to create directory ', 'nggallery') . esc_html($gallery_path) . '!<br />'; 
  63.  
  64. // 3. Check folder permission 
  65. if ( !is_writeable($gallery_path) ) 
  66. $txt .= __('Directory', 'nggallery').' <strong>' . esc_html($gallery_path) . '</strong> '.__('is not writeable !', 'nggallery').'<br />'; 
  67.  
  68. // 4. Now create thumbnail folder inside 
  69. if ( !is_dir($thumb_path) ) { 
  70. if ( !wp_mkdir_p ($thumb_path) ) 
  71. $txt .= __('Unable to create directory ', 'nggallery').' <strong>' . esc_html($thumb_path) . '/thumbs !</strong>'; 
  72.  
  73. if (SAFE_MODE) { 
  74. $help = __('The server setting Safe-Mode is on !', 'nggallery'); 
  75. $help .= '<br />'.__('If you have problems, please create directory', 'nggallery').' <strong>' . esc_html($gallery_path) . '</strong> '; 
  76. $help .= __('and the thumbnails directory', 'nggallery').' <strong>' . esc_html($thumb_path) . '</strong> '.__('with permission 777 manually !', 'nggallery'); 
  77. if ($output) nggGallery::show_message($help); 
  78.  
  79. // show a error message 
  80. if ( !empty($txt) ) { 
  81. if (SAFE_MODE) { 
  82. // for safe_mode , better delete folder, both folder must be created manually 
  83. @rmdir($thumb_path); 
  84. @rmdir($gallery_path); 
  85. if ($output) nggGallery::show_error($txt); 
  86. return false; 
  87.  
  88. // now add the gallery to the database 
  89. $galleryID = nggdb::add_gallery($title, $defaultpath . $name, '', 0, 0, $user_ID ); 
  90. // here you can inject a custom function 
  91. do_action('ngg_created_new_gallery', $galleryID); 
  92.  
  93. // return only the id if defined 
  94. if ($output == false) 
  95. return $galleryID; 
  96.  
  97. if ($galleryID != false && !isset($_REQUEST['attach_to_post'])) { 
  98. $message = __('Gallery ID %1$s successfully created. You can show this gallery in your post or page with the shortcode %2$s.<br/>', 'nggallery'); 
  99. $message = sprintf($message, $galleryID, '<strong>[nggallery id=' . $galleryID . ']</strong>'); 
  100. $message .= '<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $galleryID . '" >'; 
  101. $message .= __('Edit gallery', 'nggallery'); 
  102. $message .= '</a>'; 
  103.  
  104. if ($output) nggGallery::show_message($message, 'gallery_created_msg'); 
  105. return true; 
  106.  
  107. /** 
  108. * Scan folder for new images 
  109. * @class nggAdmin 
  110. * @param string $dirname 
  111. * @return array $files list of image filenames 
  112. */ 
  113. static function scandir( $dirname = '.' ) { 
  114. $ext = apply_filters('ngg_allowed_file_types', array('jpeg', 'jpg', 'png', 'gif') ); 
  115.  
  116. $files = array(); 
  117. if( $handle = opendir( $dirname ) ) { 
  118. while( false !== ( $file = readdir( $handle ) ) ) { 
  119. $info = M_I18n::mb_pathinfo( $file ); 
  120. // just look for images with the correct extension 
  121. if ( isset($info['extension']) ) { 
  122. if ( in_array( strtolower($info['extension']), $ext) ) { 
  123. if (!seems_utf8($file)) { 
  124. $file = utf8_encode( $file ); 
  125.  
  126. $files[] = $file; 
  127.  
  128. closedir( $handle ); 
  129. sort( $files ); 
  130. return ( $files ); 
  131.  
  132. /** 
  133. * nggAdmin::createThumbnail() - function to create or recreate a thumbnail 
  134. * @class nggAdmin 
  135. * @param object | int $image contain all information about the image or the id 
  136. * @return string result code 
  137. * @since v1.0.0 
  138. */ 
  139. static function create_thumbnail($image) { 
  140.  
  141. if (is_object($image)) { 
  142. if (isset($image->id)) { 
  143. $image = $image->id; 
  144. elseif (isset($image->pid)) { 
  145. $image = $image->pid; 
  146. $storage = C_Gallery_Storage::get_instance(); 
  147.  
  148. // XXX NextGEN Legacy wasn't handling watermarks or reflections at this stage, so we're forcefully disabling them to maintain compatibility 
  149. $params = array('watermark' => false, 'reflection' => false); 
  150. $result = $storage->generate_thumbnail($image, $params); 
  151.  
  152. if (!$result) 
  153. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  154. return __('Error while creating thumbnail.', 'nggallery'); 
  155.  
  156. // success 
  157. return '1'; 
  158.  
  159. /** 
  160. * nggAdmin::resize_image() - create a new image, based on the height /width 
  161. * @class nggAdmin 
  162. * @param object | int $image contain all information about the image or the id 
  163. * @param integer $width optional 
  164. * @param integer $height optional 
  165. * @return string result code 
  166. */ 
  167. static function resize_image($image, $width = 0, $height = 0) 
  168. if (is_object($image)) { 
  169. if (isset($image->id)) { 
  170. $image = $image->id; 
  171. elseif (isset($image->pid)) { 
  172. $image = $image->pid; 
  173.  
  174. $storage = C_Gallery_Storage::get_instance(); 
  175. // XXX maybe get rid of this...it's needed to get width/height defaults, placing these directly in generate_image_size could have unwanted consequences 
  176. $settings = C_NextGen_Settings::get_instance(); 
  177.  
  178. // XXX NextGEN Legacy wasn't handling watermarks or reflections at this stage, so we're forcefully disabling them to maintain compatibility 
  179. $params = array('watermark' => false, 'reflection' => false); 
  180.  
  181. if ($width > 0) { 
  182. $params['width'] = $width; 
  183. else { 
  184. $params['width'] = $settings->imgWidth; 
  185.  
  186. if ($height > 0) { 
  187. $params['height'] = $height; 
  188. else { 
  189. $params['height'] = $settings->imgHeight; 
  190.  
  191. $result = $storage->generate_image_size($image, 'full', $params); 
  192.  
  193. if (!$result) 
  194. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  195. return __('Error while resizing image.', 'nggallery'); 
  196.  
  197. // success 
  198. return '1'; 
  199.  
  200. /** 
  201. * Rotated/Flip an image based on the orientation flag or a definded angle 
  202. * @param int|object $image 
  203. * @param string (optional) $dir, CW (clockwise)or CCW (counter clockwise), if set to false, the exif flag will be used 
  204. * @param string (optional) $flip, could be either false | V (flip vertical) | H (flip horizontal) 
  205. * @return string result code 
  206. */ 
  207. static function rotate_image($image, $dir = false, $flip = false) 
  208. if (is_object($image)) { 
  209. if (isset($image->id)) $image = $image->id; 
  210. elseif (isset($image->pid)) $image = $image->pid; 
  211. $storage = C_Gallery_Storage::get_instance(); 
  212.  
  213. // XXX NextGEN Legacy wasn't handling watermarks or reflections at this stage, so we're forcefully disabling them to maintain compatibility 
  214. $params = array('watermark' => false, 'reflection' => false); 
  215. $rotation = null; 
  216.  
  217. if ($dir === 'CW') { 
  218. $rotation = 90; 
  219. else if ($dir === 'CCW') { 
  220. $rotation = -90; 
  221. // if you didn't define a rotation, we look for the orientation flag in EXIF 
  222. else if ($dir === false) { 
  223. $meta = new nggMeta( $image); 
  224. $exif = $meta->get_EXIF(); 
  225.  
  226. if (isset($exif['Orientation'])) { 
  227.  
  228. switch ($exif['Orientation']) { 
  229. case 5 : // vertical flip + 90 rotate right 
  230. $flip = 'V'; 
  231. case 6 : // 90 rotate right 
  232. $rotation = 90; 
  233. break; 
  234. case 7 : // horizontal flip + 90 rotate right 
  235. $flip = 'H'; 
  236. case 8 : // 90 rotate left 
  237. $rotation = -90; 
  238. break; 
  239. case 4 : // vertical flip 
  240. $flip = 'V'; 
  241. break; 
  242. case 3 : // 180 rotate left 
  243. $rotation = -180; 
  244. break; 
  245. case 2 : // horizontal flip 
  246. $flip = 'H'; 
  247. break; 
  248. case 1 : // no action in the case it doesn't need a rotation 
  249. default: 
  250. return '0'; 
  251. break; 
  252. } else 
  253. return '0'; 
  254.  
  255. if ($rotation != null) { 
  256. $params['rotation'] = $rotation; 
  257.  
  258. if ($flip != null) { 
  259. $params['flip'] = $flip; 
  260.  
  261. $result = $storage->generate_image_size($image, 'full', $params); 
  262.  
  263. if (!$result) 
  264. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  265. return __('Error while rotating image.', 'nggallery'); 
  266.  
  267. // success 
  268. return '1'; 
  269.  
  270. /** 
  271. * nggAdmin::set_watermark() - set the watermark for the image 
  272. * @class nggAdmin 
  273. * @param object | int $image contain all information about the image or the id 
  274. * @return string result code 
  275. */ 
  276. static function set_watermark($image) { 
  277.  
  278. if (is_object($image)) { 
  279. if (isset($image->id)) { 
  280. $image = $image->id; 
  281. elseif (isset($image->pid)) { 
  282. $image = $image->pid; 
  283.  
  284. $storage = C_Gallery_Storage::get_instance(); 
  285.  
  286. // XXX NextGEN Legacy was only handling watermarks at this stage, so we're forcefully disabling all else 
  287. $params = array('watermark' => true, 'reflection' => false, 'crop' => false); 
  288. $result = $storage->generate_image_size($image, 'full', $params); 
  289.  
  290. if (!$result) 
  291. // XXX there isn't any error handling unfortunately at the moment in the generate_thumbnail functions, need a way to return proper error status 
  292. return __('Error while applying watermark to image.', 'nggallery'); 
  293.  
  294. // success 
  295. return '1'; 
  296.  
  297. /** 
  298. * Recover image from backup copy and reprocess it 
  299. * @class nggAdmin 
  300. * @since 1.5.0 
  301. * @param object | int $image contain all information about the image or the id 
  302. * @return string result code 
  303. */ 
  304.  
  305. static function recover_image($image) { 
  306.  
  307. return C_Gallery_Storage::get_instance()->recover_image($image); 
  308.  
  309. /** 
  310. * Add images to database 
  311. * @class nggAdmin 
  312. * @param int $galleryID 
  313. * @param array $imageslist 
  314. * @return array $image_ids Id's which are sucessful added 
  315. */ 
  316. static function add_Images($galleryID, $imageslist) { 
  317.  
  318. global $wpdb, $ngg; 
  319.  
  320. $image_ids = array(); 
  321.  
  322. if ( is_array($imageslist) ) { 
  323. foreach($imageslist as $picture) { 
  324.  
  325. // filter function to rename/change/modify image before 
  326. $picture = apply_filters('ngg_pre_add_new_image', $picture, $galleryID); 
  327.  
  328. // strip off the extension of the filename 
  329. $path_parts = M_I18n::mb_pathinfo( $picture ); 
  330. $alttext = ( !isset($path_parts['filename']) ) ? substr($path_parts['basename'], 0, strpos($path_parts['basename'], '.')) : $path_parts['filename']; 
  331. // save it to the database 
  332. $pic_id = nggdb::add_image( $galleryID, $picture, '', $alttext ); 
  333.  
  334. if (C_NextGen_Settings::get_instance()->imgBackup && !empty($pic_id)) 
  335. $storage = C_Gallery_Storage::get_instance(); 
  336. $storage->backup_image($pic_id); 
  337.  
  338. if ( !empty($pic_id) ) 
  339. $image_ids[] = $pic_id; 
  340.  
  341. // add the metadata 
  342. nggAdmin::import_MetaData( $pic_id ); 
  343.  
  344. // auto rotate 
  345. nggAdmin::rotate_image( $pic_id ); 
  346.  
  347. // Autoresize image if required 
  348. if ($ngg->options['imgAutoResize']) { 
  349. $imagetmp = nggdb::find_image( $pic_id ); 
  350. $sizetmp = @getimagesize ( $imagetmp->imagePath ); 
  351. $widthtmp = $ngg->options['imgWidth']; 
  352. $heighttmp = $ngg->options['imgHeight']; 
  353. if (($sizetmp[0] > $widthtmp && $widthtmp) || ($sizetmp[1] > $heighttmp && $heighttmp)) { 
  354. nggAdmin::resize_image( $pic_id ); 
  355.  
  356. // action hook for post process after the image is added to the database 
  357. $image = array( 'id' => $pic_id, 'filename' => $picture, 'galleryID' => $galleryID); 
  358. do_action('ngg_added_new_image', $image); 
  359.  
  360. } // is_array 
  361.  
  362. // delete dirsize after adding new images 
  363. delete_transient( 'dirsize_cache' ); 
  364.  
  365. do_action('ngg_after_new_images_added', $galleryID, $image_ids ); 
  366.  
  367. return $image_ids; 
  368.  
  369.  
  370. /** 
  371. * Import some meta data into the database (if avialable) 
  372. * @class nggAdmin 
  373. * @param array|int $imagesIds 
  374. * @return string result code 
  375. */ 
  376. static function import_MetaData($imagesIds) { 
  377.  
  378. global $wpdb; 
  379.  
  380. require_once(NGGALLERY_ABSPATH . '/lib/image.php'); 
  381.  
  382. if (!is_array($imagesIds)) 
  383. $imagesIds = array($imagesIds); 
  384.  
  385. foreach($imagesIds as $imageID) { 
  386.  
  387. // Get the image 
  388. $image = NULL; 
  389. if (is_int($imageID)) { 
  390. $image = C_Image_Mapper::get_instance()->find($imageID); 
  391. else $image = $imageID; 
  392.  
  393. if ($image) { 
  394.  
  395. $meta = nggAdmin::get_MetaData( $image); 
  396.  
  397. // get the title 
  398. $alttext = empty( $meta['title'] ) ? $image->alttext : $meta['title']; 
  399.  
  400. // get the caption / description field 
  401. $description = empty( $meta['caption'] ) ? $image->description : $meta['caption']; 
  402.  
  403. // get the file date/time from exif 
  404. $timestamp = $meta['timestamp']; 
  405.  
  406. // first update database 
  407. $result = $wpdb->query( 
  408. $wpdb->prepare("UPDATE $wpdb->nggpictures SET 
  409. alttext = %s,  
  410. description = %s,  
  411. imagedate = %s 
  412. WHERE pid = %d", $alttext, $description, $timestamp, $image->pid) ); 
  413.  
  414. if ($result === false) 
  415. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update data base)', 'nggallery') . '</strong>'; 
  416.  
  417. //this flag will inform us that the import is already one time performed 
  418. $meta['common']['saved'] = true; 
  419. $result = nggdb::update_image_meta($image->pid, $meta['common']); 
  420.  
  421. if ($result === false) 
  422. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not update meta data)', 'nggallery') . '</strong>'; 
  423.  
  424. // add the tags if we found some 
  425. if ($meta['keywords']) { 
  426. $taglist = explode(', ', $meta['keywords']); 
  427. wp_set_object_terms($image->pid, $taglist, 'ngg_tag'); 
  428.  
  429. } else 
  430. return ' <strong>' . esc_html( $image->filename ) . ' ' . __('(Error : Couldn\'t not find image)', 'nggallery') . '</strong>';// error check 
  431.  
  432. return '1'; 
  433.  
  434. /** 
  435. * nggAdmin::get_MetaData() 
  436. * @class nggAdmin 
  437. * @require NextGEN Meta class 
  438. * @param int $id image ID 
  439. * @return array metadata 
  440. */ 
  441. static function get_MetaData($image_or_id) { 
  442.  
  443. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  444.  
  445. $meta = array(); 
  446.  
  447. $pdata = new nggMeta($image_or_id); 
  448.  
  449. $meta['title'] = trim ( $pdata->get_META('title') ); 
  450. $meta['caption'] = trim ( $pdata->get_META('caption') ); 
  451. $meta['keywords'] = trim ( $pdata->get_META('keywords') ); 
  452. $meta['timestamp'] = $pdata->get_date_time(); 
  453. // this contain other useful meta information 
  454. $meta['common'] = $pdata->get_common_meta(); 
  455. // hook for addon plugin to add more meta fields 
  456. $meta = apply_filters('ngg_get_image_metadata', $meta, $pdata); 
  457.  
  458. return $meta; 
  459.  
  460.  
  461. /** 
  462. * Maybe import some meta data to the database. The functions checks the flag 'saved' 
  463. * and if based on compat reason (pre V1.4.0) we save then some meta datas to the database 
  464. * @since V1.4.0 
  465. * @param int $id 
  466. * @return result 
  467. */ 
  468. function maybe_import_meta( $image_or_id ) { 
  469.  
  470. require_once(NGGALLERY_ABSPATH . '/lib/meta.php'); 
  471. $id = is_int($image_or_id) ? $image_or_id : $image_or_id->{$image_or_id->id_field}; 
  472. $meta_obj = new nggMeta( $image_or_id ); 
  473.  
  474. if ( $meta_obj->image->meta_data['saved'] != true ) { 
  475. $common = $meta_obj->get_common_meta(); 
  476. //this flag will inform us that the import is already one time performed 
  477. $common['saved'] = true; 
  478. $result = nggdb::update_image_meta($id, $common); 
  479. } else 
  480. return false; 
  481.  
  482. return $result; 
  483.  
  484.  
  485. /** 
  486. * nggAdmin::import_gallery() 
  487. * TODO: Check permission of existing thumb folder & images 
  488. * @class nggAdmin 
  489. * @param string $galleryfolder contains relative path to the gallery itself 
  490. * @return void 
  491. */ 
  492. static function import_gallery($galleryfolder, $gallery_id=NULL) { 
  493.  
  494. global $wpdb, $user_ID; 
  495.  
  496. // get the current user ID 
  497. wp_get_current_user(); 
  498.  
  499. $created_msg = ''; 
  500.  
  501. // remove trailing slash at the end, if somebody use it 
  502. $galleryfolder = untrailingslashit($galleryfolder); 
  503.  
  504. $fs = C_Fs::get_instance(); 
  505. if (is_null($gallery_id)) { 
  506. $gallerypath = $fs->join_paths($fs->get_document_root('content'), $galleryfolder); 
  507. } else { 
  508. $storage = C_Gallery_Storage::get_instance(); 
  509. $gallerypath = $storage->get_gallery_abspath($gallery_id); 
  510.  
  511. if (!is_dir($gallerypath)) { 
  512. nggGallery::show_error(sprintf(__("Directory <strong>%s</strong> doesn`t exist!", 'nggallery'), esc_html($gallerypath))); 
  513. return ; 
  514.  
  515. // read list of images 
  516. $new_imageslist = nggAdmin::scandir($gallerypath); 
  517.  
  518. if (empty($new_imageslist)) { 
  519. nggGallery::show_message(sprintf(__("Directory <strong>%s</strong> contains no pictures", 'nggallery'), esc_html($gallerypath))); 
  520. return; 
  521.  
  522. // take folder name as gallery name 
  523. $galleryname = basename($galleryfolder); 
  524. $galleryname = apply_filters('ngg_gallery_name', $galleryname); 
  525.  
  526. // check for existing gallery folder 
  527. if (is_null($gallery_id)) 
  528. $gallery_id = $wpdb->get_var("SELECT gid FROM $wpdb->nggallery WHERE path = '$galleryfolder' "); 
  529.  
  530. if (!$gallery_id) { 
  531. // now add the gallery to the database 
  532. $gallery_id = nggdb::add_gallery( $galleryname, $galleryfolder, '', 0, 0, $user_ID ); 
  533. if (!$gallery_id) { 
  534. nggGallery::show_error(__('Database error. Could not add gallery!', 'nggallery')); 
  535. return; 
  536. else { 
  537. do_action('ngg_created_new_gallery', $gallery_id); 
  538. $created_msg = sprintf( 
  539. _n("Gallery <strong>%s</strong> successfully created!", 'Galleries <strong>%s</strong> successfully created!', 1, 'nggallery'),  
  540. esc_html($galleryname) 
  541. ); 
  542.  
  543. // Look for existing image list 
  544. $old_imageslist = $wpdb->get_col("SELECT filename FROM $wpdb->nggpictures WHERE galleryid = '$gallery_id' "); 
  545.  
  546. // if no images are there, create empty array 
  547. if ($old_imageslist == NULL) 
  548. $old_imageslist = array(); 
  549.  
  550. // check difference 
  551. $new_images = array_diff($new_imageslist, $old_imageslist); 
  552.  
  553. // all images must be valid files 
  554. foreach($new_images as $key => $picture) { 
  555.  
  556. // filter function to rename/change/modify image before 
  557. $picture = apply_filters('ngg_pre_add_new_image', $picture, $gallery_id); 
  558. $new_images[$key] = $picture; 
  559.  
  560. if (!@getimagesize($gallerypath . '/' . $picture) ) { 
  561. unset($new_images[$key]); 
  562. @unlink($gallerypath . '/' . $picture); 
  563.  
  564. // add images to database 
  565. $image_ids = nggAdmin::add_Images($gallery_id, $new_images); 
  566. do_action('ngg_after_new_images_added', $gallery_id, $image_ids); 
  567.  
  568. //add the preview image if needed 
  569. nggAdmin::set_gallery_preview ( $gallery_id ); 
  570.  
  571. // now create thumbnails 
  572. nggAdmin::do_ajax_operation( 'create_thumbnail' , $image_ids, __('Create new thumbnails', 'nggallery') ); 
  573.  
  574. //TODO:Message will not shown, because AJAX routine require more time, message should be passed to AJAX 
  575. $message = $created_msg . sprintf(_n('%s picture successfully added', '%s pictures successfully added', count($image_ids), 'nggallery'), count($image_ids)); 
  576. $message .= ' [<a href="' . admin_url() . 'admin.php?page=nggallery-manage-gallery&mode=edit&gid=' . $gallery_id . '" >'; 
  577. $message .= __('Edit gallery', 'nggallery'); 
  578. $message .= '</a>]'; 
  579.  
  580. nggGallery::show_message($message); 
  581.  
  582. return; 
  583.  
  584. /** 
  585. * Capability check. Check is the ID fit's to the user_ID 
  586. * @class nggAdmin 
  587. * @param int $check_ID is the user_id 
  588. * @return bool $result 
  589. */ 
  590. static function can_manage_this_gallery($check_ID) { 
  591.  
  592. global $user_ID, $wp_roles; 
  593.  
  594. if ( !current_user_can('NextGEN Manage others gallery') ) { 
  595. // get the current user ID 
  596. wp_get_current_user(); 
  597.  
  598. if ( $user_ID != $check_ID) 
  599. return false; 
  600.  
  601. return true; 
  602.  
  603.  
  604. /** 
  605. * Initate the Ajax operation 
  606. * @class nggAdmin 
  607. * @param string $operation name of the function which should be executed 
  608. * @param array $image_array 
  609. * @param string $title name of the operation 
  610. * @return string the javascript output 
  611. */ 
  612. static function do_ajax_operation( $operation, $image_array, $title = '' ) { 
  613.  
  614. if ( !is_array($image_array) || empty($image_array) ) 
  615. return; 
  616.  
  617. $js_array = implode('", "', $image_array); 
  618.  
  619. // send out some JavaScript, which initate the ajax operation 
  620. ?> 
  621. <script type="text/javascript"> 
  622.  
  623. Images = new Array("<?php echo $js_array; ?>"); 
  624.  
  625. nggAjaxOptions = { 
  626. operation: "<?php echo $operation; ?>",  
  627. ids: Images,  
  628. header: "<?php echo $title; ?>",  
  629. maxStep: Images.length 
  630. }; 
  631.  
  632. jQuery(document).ready( function() { 
  633. nggProgressBar.init( nggAjaxOptions ); 
  634. nggAjax.init( nggAjaxOptions ); 
  635. } ); 
  636. </script> 
  637.  
  638. <?php 
  639.  
  640. /** 
  641. * nggAdmin::set_gallery_preview() - define a preview pic after the first upload, can be changed in the gallery settings 
  642. * @class nggAdmin 
  643. * @param int $galleryID 
  644. * @return void 
  645. */ 
  646. static function set_gallery_preview( $galleryID ) 
  647. $gallery_mapper = C_Gallery_Mapper::get_instance(); 
  648. if (($gallery = $gallery_mapper->find($galleryID))) { 
  649. if (!$gallery->previewpic) { 
  650. $image_mapper = C_Image_Mapper::get_instance(); 
  651. if (($image = $image_mapper->select()->where(array('galleryid = %d', $galleryID))->where(array('exclude != 1'))-> 
  652. order_by($image_mapper->get_primary_key_column())->limit(1)->run_query())) { 
  653. $gallery->previewpic = $image->{$image->id_field}; 
  654. $gallery_mapper->save($gallery); 
  655.  
  656. /** 
  657. * Return a JSON coded array of Image ids for a requested gallery 
  658. * @class nggAdmin 
  659. * @param int $galleryID 
  660. * @return arry (JSON) 
  661. */ 
  662. static function get_image_ids( $galleryID ) { 
  663.  
  664. if ( !function_exists('json_encode') ) 
  665. return(-2); 
  666.  
  667. $gallery = nggdb::get_ids_from_gallery($galleryID, 'pid', 'ASC', false); 
  668.  
  669. header('Content-Type: text/plain; charset=' . get_option('blog_charset'), true); 
  670. $output = json_encode($gallery); 
  671.  
  672. return $output; 
  673.  
  674. /** 
  675. * Deprecated function, restored to fix compatibility with "NextGen Public Uploader" 
  676. * @deprecated 
  677. * @class nggAdmin 
  678. * @param string $filename 
  679. * @return bool $result 
  680. */ 
  681. function chmod($filename = '') 
  682. $stat = @stat(dirname($filename)); 
  683. $perms = $stat['mode'] & 0000666; 
  684. if (@chmod($filename, $perms)) 
  685. return true; 
  686. return false; 
  687.  
  688. } // END class nggAdmin