tidypics.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * Elgg tidypics library of common functions
  4. *
  5. * @package TidypicsCommon
  6. */
  7. /**
  8. * Get images for display on front page
  9. *
  10. * @param int number of images
  11. * @param array (optional) array of owner guids
  12. * @param string (optional) context of view to display
  13. * @return string of html for display
  14. */
  15. function tp_get_latest_photos($num_images, array $owner_guids = NULL, $context = 'front') {
  16. $prev_context = elgg_get_context();
  17. elgg_set_context($context);
  18. $image_html = elgg_list_entities(array(
  19. 'type' => 'object',
  20. 'subtype' => 'image',
  21. 'owner_guids' => $owner_guids,
  22. 'limit' => $num_images,
  23. 'full_view' => false,
  24. 'list_type_toggle' => false,
  25. 'list_type' => 'gallery',
  26. 'pagination' => false,
  27. 'gallery_class' => 'tidypics-gallery-widget',
  28. ));
  29. elgg_set_context($prev_context);
  30. return $image_html;
  31. }
  32. /**
  33. * Get albums for display on front page
  34. *
  35. * @param int number of albums
  36. * @param array (optional) array of container_guids
  37. * @param string (optional) context of view to display
  38. * @return string of html for display
  39. */
  40. function tp_get_latest_albums($num_albums, array $container_guids = NULL, $context = 'front') {
  41. $prev_context = elgg_get_context();
  42. elgg_set_context($context);
  43. $image_html = elgg_list_entities(array(
  44. 'type' => 'object',
  45. 'subtype' => 'album',
  46. 'container_guids' => $container_guids,
  47. 'limit' => $num_albums,
  48. 'full_view' => false,
  49. 'pagination' => false,
  50. ));
  51. elgg_set_context($prev_context);
  52. return $image_html;
  53. }
  54. /**
  55. * Get image directory path
  56. *
  57. * Each album gets a subdirectory based on its container id
  58. *
  59. * @return string path to image directory
  60. */
  61. function tp_get_img_dir($album_guid) {
  62. $file = new ElggFile();
  63. $file->setFilename("image/$album_guid");
  64. return $file->getFilenameOnFilestore($file);
  65. }
  66. /**
  67. * Prepare vars for a form, pulling from an entity or sticky forms.
  68. *
  69. * @param type $entity
  70. * @return type
  71. */
  72. function tidypics_prepare_form_vars($entity = null) {
  73. // input names => defaults
  74. $values = array(
  75. 'title' => '',
  76. 'description' => '',
  77. 'access_id' => ACCESS_DEFAULT,
  78. 'tags' => '',
  79. 'container_guid' => elgg_get_page_owner_guid(),
  80. 'guid' => null,
  81. 'entity' => $entity,
  82. );
  83. if ($entity) {
  84. foreach (array_keys($values) as $field) {
  85. if (isset($entity->$field)) {
  86. $values[$field] = $entity->$field;
  87. }
  88. }
  89. }
  90. if (elgg_is_sticky_form('tidypics')) {
  91. $sticky_values = elgg_get_sticky_values('tidypics');
  92. foreach ($sticky_values as $key => $value) {
  93. $values[$key] = $value;
  94. }
  95. }
  96. elgg_clear_sticky_form('tidypics');
  97. return $values;
  98. }
  99. /**
  100. * Returns available image libraries.
  101. *
  102. * @return string
  103. */
  104. function tidypics_get_image_libraries() {
  105. $options = array();
  106. if (extension_loaded('gd')) {
  107. $options['GD'] = 'GD';
  108. }
  109. if (extension_loaded('imagick')) {
  110. $options['ImageMagickPHP'] = 'imagick PHP extension';
  111. }
  112. $disablefunc = explode(',', ini_get('disable_functions'));
  113. if (is_callable('exec') && !in_array('exec', $disablefunc)) {
  114. $options['ImageMagick'] = 'ImageMagick executable';
  115. }
  116. return $options;
  117. }
  118. /**
  119. * Are there upgrade scripts to be run?
  120. *
  121. * @return bool
  122. */
  123. function tidypics_is_upgrade_available() {
  124. // sets $version based on code
  125. require_once elgg_get_plugins_path() . "tidypics/version.php";
  126. $local_version = elgg_get_plugin_setting('version', 'tidypics');
  127. if ($local_version === false) {
  128. // no version set so either new install or really old one
  129. if (!get_subtype_class('object', 'image') || !get_subtype_class('object', 'album')) {
  130. $local_version = 0;
  131. } else {
  132. // set initial version for new install
  133. elgg_set_plugin_setting('version', $version, 'tidypics');
  134. $local_version = $version;
  135. }
  136. } elseif ($local_version === '1.62') {
  137. // special work around to handle old upgrade system
  138. $local_version = 2010010101;
  139. elgg_set_plugin_setting('version', $local_version, 'tidypics');
  140. }
  141. if ($local_version == $version) {
  142. return false;
  143. } else {
  144. return true;
  145. }
  146. }
  147. /**
  148. * This lists the photos in an album as sorted by metadata
  149. *
  150. * @todo this only supports a single album. The only case for use a
  151. * procedural function like this instead of TidypicsAlbum::viewImgaes() is to
  152. * fetch images across albums as a helper to elgg_get_entities().
  153. * This should function be deprecated or fixed to work across albums.
  154. *
  155. * @param array $options
  156. * @return string
  157. */
  158. function tidypics_list_photos(array $options = array()) {
  159. global $autofeed;
  160. $autofeed = true;
  161. $defaults = array(
  162. 'offset' => (int) max(get_input('offset', 0), 0),
  163. 'limit' => (int) max(get_input('limit', 10), 0),
  164. 'full_view' => true,
  165. 'list_type_toggle' => false,
  166. 'pagination' => true,
  167. );
  168. $options = array_merge($defaults, $options);
  169. $options['count'] = true;
  170. $count = elgg_get_entities($options);
  171. $album = get_entity($options['container_guid']);
  172. if ($album) {
  173. $guids = $album->getImageList();
  174. // need to pass all the guids and handle the limit / offset in sql
  175. // to avoid problems with the navigation
  176. //$guids = array_slice($guids, $options['offset'], $options['limit']);
  177. $options['guids'] = $guids;
  178. unset($options['container_guid']);
  179. }
  180. $options['count'] = false;
  181. $entities = elgg_get_entities($options);
  182. $keys = array();
  183. foreach ($entities as $entity) {
  184. $keys[] = $entity->guid;
  185. }
  186. $entities = array_combine($keys, $entities);
  187. $sorted_entities = array();
  188. foreach ($guids as $guid) {
  189. if (isset($entities[$guid])) {
  190. $sorted_entities[] = $entities[$guid];
  191. }
  192. }
  193. // for this function count means the total number of entities
  194. // and is required for pagination
  195. $options['count'] = $count;
  196. return elgg_view_entity_list($sorted_entities, $options);
  197. }
  198. /**
  199. * Returns just a guid from a database $row. Used in elgg_get_entities()'s callback.
  200. *
  201. * @param stdClass $row
  202. * @return type
  203. */
  204. function tp_guid_callback($row) {
  205. return ($row->guid) ? $row->guid : false;
  206. }
  207. /**
  208. * the functions below replace broken core functions or add functions
  209. * that could/should exist in the core
  210. */
  211. /**
  212. * Is the request from a known browser
  213. *
  214. * @return true/false
  215. */
  216. function tp_is_person() {
  217. $known = array('msie', 'mozilla', 'firefox', 'safari', 'webkit', 'opera', 'netscape', 'konqueror', 'gecko');
  218. $agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  219. foreach ($known as $browser) {
  220. if (strpos($agent, $browser) !== false) {
  221. return true;
  222. }
  223. }
  224. return false;
  225. }