PageRenderTime 251ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/gallery/helpers/album.php

https://github.com/ShakeNBake/gallery3
PHP | 178 lines | 130 code | 10 blank | 38 comment | 7 complexity | 189d39a09b24f855cda3ff9381c30eb8 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php defined("SYSPATH") or die("No direct script access.");
  2. /**
  3. * Gallery - a web based photo album viewer and editor
  4. * Copyright (C) 2000-2009 Bharat Mediratta
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /**
  21. * This is the API for handling albums.
  22. *
  23. * Note: by design, this class does not do any permission checking.
  24. */
  25. class album_Core {
  26. /**
  27. * Create a new album.
  28. * @param integer $parent_id id of parent album
  29. * @param string $name the name of this new album (it will become the directory name on disk)
  30. * @param integer $title the title of the new album
  31. * @param string $description (optional) the longer description of this album
  32. * @param string $slug (optional) the url component for this photo
  33. * @return Item_Model
  34. */
  35. static function create($parent, $name, $title, $description=null, $owner_id=null, $slug=null) {
  36. if (!$parent->loaded || !$parent->is_album()) {
  37. throw new Exception("@todo INVALID_PARENT");
  38. }
  39. if (strpos($name, "/")) {
  40. throw new Exception("@todo NAME_CANNOT_CONTAIN_SLASH");
  41. }
  42. // We don't allow trailing periods as a security measure
  43. // ref: http://dev.kohanaphp.com/issues/684
  44. if (rtrim($name, ".") != $name) {
  45. throw new Exception("@todo NAME_CANNOT_END_IN_PERIOD");
  46. }
  47. if (empty($slug)) {
  48. $slug = item::convert_filename_to_slug($name);
  49. }
  50. $album = ORM::factory("item");
  51. $album->type = "album";
  52. $album->title = $title;
  53. $album->description = $description;
  54. $album->name = $name;
  55. $album->owner_id = $owner_id;
  56. $album->thumb_dirty = 1;
  57. $album->resize_dirty = 1;
  58. $album->slug = $slug;
  59. $album->rand_key = ((float)mt_rand()) / (float)mt_getrandmax();
  60. $album->sort_column = "created";
  61. $album->sort_order = "ASC";
  62. // Randomize the name or slug if there's a conflict
  63. // @todo Improve this. Random numbers are not user friendly
  64. while (ORM::factory("item")
  65. ->where("parent_id", $parent->id)
  66. ->open_paren()
  67. ->where("name", $album->name)
  68. ->orwhere("slug", $album->slug)
  69. ->close_paren()
  70. ->find()->id) {
  71. $rand = rand();
  72. $album->name = "{$name}-$rand";
  73. $album->slug = "{$slug}-$rand";
  74. }
  75. $album = $album->add_to_parent($parent);
  76. mkdir($album->file_path());
  77. mkdir(dirname($album->thumb_path()));
  78. mkdir(dirname($album->resize_path()));
  79. // @todo: publish this from inside Item_Model::save() when we refactor to the point where
  80. // there's only one save() happening here.
  81. module::event("item_created", $album);
  82. return $album;
  83. }
  84. static function get_add_form($parent) {
  85. $form = new Forge("albums/{$parent->id}", "", "post", array("id" => "gAddAlbumForm"));
  86. $group = $form->group("add_album")
  87. ->label(t("Add an album to %album_title", array("album_title" => $parent->title)));
  88. $group->input("title")->label(t("Title"));
  89. $group->textarea("description")->label(t("Description"));
  90. $group->input("name")->label(t("Directory name"))
  91. ->callback("item::validate_no_slashes")
  92. ->error_messages("no_slashes", t("The directory name can't contain the \"/\" character"));
  93. $group->input("slug")->label(t("Internet Address"))
  94. ->callback("item::validate_url_safe")
  95. ->error_messages(
  96. "not_url_safe",
  97. t("The internet address should contain only letters, numbers, hyphens and underscores"));
  98. $group->hidden("type")->value("album");
  99. $group->submit("")->value(t("Create"));
  100. $form->add_rules_from(ORM::factory("item"));
  101. $form->script("")
  102. ->url(url::abs_file("modules/gallery/js/albums_form_add.js"));
  103. return $form;
  104. }
  105. static function get_edit_form($parent) {
  106. $form = new Forge("albums/{$parent->id}", "", "post", array("id" => "gEditAlbumForm"));
  107. $form->hidden("_method")->value("put");
  108. $group = $form->group("edit_item")->label(t("Edit Album"));
  109. $group->input("title")->label(t("Title"))->value($parent->title);
  110. $group->textarea("description")->label(t("Description"))->value($parent->description);
  111. if ($parent->id != 1) {
  112. $group->input("dirname")->label(t("Directory Name"))->value($parent->name)
  113. ->rules("required")
  114. ->error_messages(
  115. "name_conflict", t("There is already a movie, photo or album with this name"))
  116. ->callback("item::validate_no_slashes")
  117. ->error_messages("no_slashes", t("The directory name can't contain a \"/\""))
  118. ->callback("item::validate_no_trailing_period")
  119. ->error_messages("no_trailing_period", t("The directory name can't end in \".\""));
  120. $group->input("slug")->label(t("Internet Address"))->value($parent->slug)
  121. ->error_messages(
  122. "slug_conflict", t("There is already a movie, photo or album with this internet address"))
  123. ->callback("item::validate_url_safe")
  124. ->error_messages(
  125. "not_url_safe",
  126. t("The internet address should contain only letters, numbers, hyphens and underscores"));
  127. } else {
  128. $group->hidden("dirname")->value($parent->name);
  129. $group->hidden("slug")->value($parent->slug);
  130. }
  131. $sort_order = $group->group("sort_order", array("id" => "gAlbumSortOrder"))
  132. ->label(t("Sort Order"));
  133. $sort_order->dropdown("column", array("id" => "gAlbumSortColumn"))
  134. ->label(t("Sort by"))
  135. ->options(album::get_sort_order_options())
  136. ->selected($parent->sort_column);
  137. $sort_order->dropdown("direction", array("id" => "gAlbumSortDirection"))
  138. ->label(t("Order"))
  139. ->options(array("ASC" => t("Ascending"),
  140. "DESC" => t("Descending")))
  141. ->selected($parent->sort_order);
  142. module::event("item_edit_form", $parent, $form);
  143. $group = $form->group("buttons")->label("");
  144. $group->hidden("type")->value("album");
  145. $group->submit("")->value(t("Modify"));
  146. $form->add_rules_from(ORM::factory("item"));
  147. return $form;
  148. }
  149. /**
  150. * Return a structured set of all the possible sort orders.
  151. */
  152. static function get_sort_order_options() {
  153. return array("weight" => t("Manual"),
  154. "captured" => t("Date captured"),
  155. "created" => t("Date uploaded"),
  156. "title" => t("Title"),
  157. "updated" => t("Date modified"),
  158. "view_count" => t("Number of views"),
  159. "rand_key" => t("Random"));
  160. }
  161. }