A_Attach_To_Post_Ajax

Provides AJAX actions for the Attach To Post interface TODO: Need to add authorization checks to each action.

Defined (1)

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

/products/photocrati_nextgen/modules/attach_to_post/package.module.attach_to_post.php  
  1. class A_Attach_To_Post_Ajax extends Mixin 
  2. var $attach_to_post = NULL; 
  3. /** 
  4. * Retrieves the attach to post controller 
  5. */ 
  6. function get_attach_to_post() 
  7. if (is_null($this->attach_to_post)) { 
  8. $this->attach_to_post = C_Attach_Controller::get_instance(); 
  9. return $this->attach_to_post; 
  10. /** 
  11. * Returns a list of image sources for the Attach to Post interface 
  12. * @return type 
  13. */ 
  14. function get_attach_to_post_sources_action() 
  15. $response = array(); 
  16. if ($this->object->validate_ajax_request('nextgen_edit_displayed_gallery')) { 
  17. $response['sources'] = $this->get_attach_to_post()->get_sources(); 
  18. return $response; 
  19. /** 
  20. * Gets existing galleries 
  21. * @return array 
  22. */ 
  23. function get_existing_galleries_action() 
  24. $this->debug = TRUE; 
  25. $response = array(); 
  26. if ($this->object->validate_ajax_request('nextgen_edit_displayed_gallery')) { 
  27. $limit = $this->object->param('limit'); 
  28. $offset = $this->object->param('offset'); 
  29. // We return the total # of galleries, so that the client can make 
  30. // pagination requests 
  31. $mapper = C_Gallery_Mapper::get_instance(); 
  32. $response['total'] = $mapper->count(); 
  33. $response['limit'] = $limit = $limit ? $limit : 0; 
  34. $response['offset'] = $offset = $offset ? $offset : 0; 
  35. // Get the galleries 
  36. $mapper->select(); 
  37. if ($limit) { 
  38. $mapper->limit($limit, $offset); 
  39. $response['items'] = $mapper->run_query(); 
  40. } else { 
  41. $response['error'] = 'insufficient access'; 
  42. $this->debug = FALSE; 
  43. return $response; 
  44. /** 
  45. * Gets existing albums 
  46. * @return array 
  47. */ 
  48. function get_existing_albums_action() 
  49. $response = array(); 
  50. if ($this->object->validate_ajax_request('nextgen_edit_displayed_gallery')) { 
  51. $limit = $this->object->param('limit'); 
  52. $offset = $this->object->param('offset'); 
  53. // We return the total # of albums, so that the client can make pagination requests 
  54. $mapper = C_Album_Mapper::get_instance(); 
  55. $response['total'] = $mapper->count(); 
  56. $response['limit'] = $limit = $limit ? $limit : 0; 
  57. $response['offset'] = $offset = $offset ? $offset : 0; 
  58. // Get the albums 
  59. $mapper->select(); 
  60. if ($limit) { 
  61. $mapper->limit($limit, $offset); 
  62. $response['items'] = $mapper->run_query(); 
  63. return $response; 
  64. /** 
  65. * Gets existing image tags 
  66. * @return array 
  67. */ 
  68. function get_existing_image_tags_action() 
  69. $response = array(); 
  70. if ($this->object->validate_ajax_request('nextgen_edit_displayed_gallery')) { 
  71. $limit = $this->object->param('limit'); 
  72. $offset = $this->object->param('offset'); 
  73. $response['limit'] = $limit = $limit ? $limit : 0; 
  74. $response['offset'] = $offset = $offset ? $offset : 0; 
  75. $response['items'] = array(); 
  76. $params = array('number' => $limit, 'offset' => $offset, 'fields' => 'names'); 
  77. foreach (get_terms('ngg_tag', $params) as $term) { 
  78. $response['items'][] = array('id' => $term, 'title' => $term, 'name' => $term); 
  79. $response['total'] = count(get_terms('ngg_tag', array('fields' => 'ids'))); 
  80. return $response; 
  81. /** 
  82. * Gets entities (such as images) for a displayed gallery (attached gallery) 
  83. */ 
  84. function get_displayed_gallery_entities_action() 
  85. $response = array(); 
  86. if ($this->object->validate_ajax_request('nextgen_edit_displayed_gallery') && ($params = $this->object->param('displayed_gallery'))) { 
  87. $limit = $this->object->param('limit'); 
  88. $offset = $this->object->param('offset'); 
  89. $factory = C_Component_Factory::get_instance(); 
  90. $displayed_gallery = $factory->create('displayed_gallery'); 
  91. foreach ($params as $key => $value) { 
  92. $displayed_gallery->{$key} = $value; 
  93. $response['limit'] = $limit = $limit ? $limit : 0; 
  94. $response['offset'] = $offset = $offset ? $offset : 0; 
  95. $response['total'] = $displayed_gallery->get_entity_count('both'); 
  96. $response['items'] = $displayed_gallery->get_entities($limit, $offset, FALSE, 'both'); 
  97. $controller = C_Display_Type_Controller::get_instance(); 
  98. $storage = C_Gallery_Storage::get_instance(); 
  99. $image_mapper = C_Image_Mapper::get_instance(); 
  100. $settings = C_NextGen_Settings::get_instance(); 
  101. foreach ($response['items'] as &$entity) { 
  102. $image = $entity; 
  103. if (in_array($displayed_gallery->source, array('album', 'albums'))) { 
  104. // Set the alttext of the preview image to the 
  105. // name of the gallery or album 
  106. if ($image = $image_mapper->find($entity->previewpic)) { 
  107. if ($entity->is_album) { 
  108. $image->alttext = sprintf(__('Album: %s', 'nggallery'), $entity->name); 
  109. } else { 
  110. $image->alttext = sprintf(__('Gallery: %s', 'nggallery'), $entity->title); 
  111. // Prefix the id of an album with 'a' 
  112. if ($entity->is_album) { 
  113. $id = $entity->{$entity->id_field}; 
  114. $entity->{$entity->id_field} = 'a' . $id; 
  115. // Get the thumbnail 
  116. $entity->thumb_url = $storage->get_image_url($image, 'thumb', TRUE); 
  117. $entity->thumb_html = $storage->get_image_html($image, 'thumb'); 
  118. } else { 
  119. $response['error'] = __('Missing parameters', 'nggallery'); 
  120. return $response; 
  121. /** 
  122. * Saves the displayed gallery 
  123. */ 
  124. function save_displayed_gallery_action() 
  125. $response = array(); 
  126. $mapper = C_Displayed_Gallery_Mapper::get_instance(); 
  127. // Do we have fields to work with? 
  128. if ($this->object->validate_ajax_request('nextgen_edit_displayed_gallery', true) && ($params = json_decode($this->object->param('displayed_gallery')))) { 
  129. // Existing displayed gallery ? 
  130. if ($id = $this->object->param('id')) { 
  131. $displayed_gallery = $mapper->find($id, TRUE); 
  132. if ($displayed_gallery) { 
  133. foreach ($params as $key => $value) { 
  134. $displayed_gallery->{$key} = $value; 
  135. } else { 
  136. $factory = C_Component_Factory::get_instance(); 
  137. $displayed_gallery = $factory->create('displayed_gallery', $params, $mapper); 
  138. // Save the changes 
  139. if ($displayed_gallery) { 
  140. if ($displayed_gallery->save()) { 
  141. $response['displayed_gallery'] = $displayed_gallery->get_entity(); 
  142. } else { 
  143. $response['validation_errors'] = $this->get_attach_to_post()->show_errors_for($displayed_gallery, TRUE); 
  144. } else { 
  145. $response['error'] = __('Displayed gallery does not exist', 'nggallery'); 
  146. } else { 
  147. $response['error'] = __('Invalid request', 'nggallery'); 
  148. return $response;