PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/developer/controllers/admin_developer.php

https://github.com/ChrisRut/gallery3-contrib
PHP | 269 lines | 213 code | 38 blank | 18 comment | 12 complexity | aa0fa830c13810b57847f22ec0e66650 MD5 | raw file
  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. class Admin_Developer_Controller extends Admin_Controller {
  21. public function module() {
  22. $view = new Admin_View("admin.html");
  23. $view->content = new View("admin_developer.html");
  24. $view->content->title = t("Generate module");
  25. if (!is_writable(MODPATH)) {
  26. message::warning(
  27. t("The module directory is not writable. Please ensure that it is writable by the web server"));
  28. }
  29. list ($form, $errors) = $this->_get_module_form();
  30. $view->content->developer_content = $this->_get_module_create_content($form, $errors);
  31. print $view;
  32. }
  33. public function test_data() {
  34. $v = new Admin_View("admin.html");
  35. $v->content = new View("admin_developer.html");
  36. $v->content->title = t("Generate Test Data");
  37. list ($form, $errors) = $this->_get_module_form();
  38. $v->content->developer_content = $this->_get_test_data_view($form, $errors);
  39. print $v;
  40. }
  41. public function module_create() {
  42. access::verify_csrf();
  43. list ($form, $errors) = $this->_get_module_form();
  44. $post = new Validation($_POST);
  45. $post->add_rules("name", "required");
  46. $post->add_rules("display_name", "required");
  47. $post->add_rules("description", "required");
  48. $post->add_callbacks("name", array($this, "_is_module_defined"));
  49. if ($post->validate()) {
  50. $task_def = Task_Definition::factory()
  51. ->callback("developer_task::create_module")
  52. ->description(t("Create a new module"))
  53. ->name(t("Create Module"));
  54. $task = task::create($task_def, array_merge(array("step" => 0), $post->as_array()));
  55. $success_msg = t("Generation of %module completed successfully",
  56. array("module" => $post->name));
  57. $error_msg = t("Generation of %module failed.", array("module" => $post->name));
  58. print json_encode(array("result" => "started",
  59. "max_iterations" => 15,
  60. "success_msg" => $success_msg, "error_msg" => $error_msg,
  61. "url" => url::site("admin/developer/run_task/{$task->id}?csrf=" .
  62. access::csrf_token()),
  63. "task" => $task->as_array()));
  64. } else {
  65. $v = $this->_get_module_create_content(arr::overwrite($form, $post->as_array()),
  66. arr::overwrite($errors, $post->errors()));
  67. print json_encode(array("result" => "error",
  68. "form" => $v->__toString()));
  69. }
  70. }
  71. public function session($key) {
  72. access::verify_csrf();
  73. $input = Input::instance();
  74. Session::instance()->set($key, $input->get("value"));
  75. url::redirect($input->server("HTTP_REFERER"));
  76. }
  77. public function test_data_create() {
  78. access::verify_csrf();
  79. list ($form, $errors) = $this->_get_test_data_form();
  80. $post = new Validation($_POST);
  81. $post->add_rules("albums", "numeric");
  82. $post->add_rules("photos", "numeric");
  83. $post->add_rules("comments", "numeric");
  84. $post->add_rules("tags", "numeric");
  85. $post->add_callbacks("albums", array($this, "_set_default"));
  86. $post->add_callbacks("photos", array($this, "_set_default"));
  87. $post->add_callbacks("comments", array($this, "_set_default"));
  88. $post->add_callbacks("tags", array($this, "_set_default"));
  89. if ($post->validate()) {
  90. $task_def = Task_Definition::factory()
  91. ->callback("developer_task::create_content")
  92. ->description(t("Create test content"))
  93. ->name(t("Create Test Data"));
  94. $total = $post->albums + $post->photos + $post->comments + $post->tags;
  95. $success_msg = t("Successfully generated test data");
  96. $error_msg = t("Problems with test data generation was encountered");
  97. $task = task::create($task_def, array("total" => $total, "batch" => (int)ceil($total / 10),
  98. "success_msg" => $success_msg,
  99. "current" => 0, "error_msg" => $error_msg,
  100. "albums" => $post->albums, "photos" => $post->photos,
  101. "comments" => $post->comments, "tags" => $post->tags));
  102. batch::start();
  103. print json_encode(array("result" => "started",
  104. "max_iterations" => $total + 5,
  105. "url" => url::site("admin/developer/run_task/{$task->id}?csrf=" .
  106. access::csrf_token()),
  107. "task" => $task->as_array()));
  108. } else {
  109. $v = $this->_get_test_data_view(arr::overwrite($form, $post->as_array()),
  110. arr::overwrite($errors, $post->errors()));
  111. print json_encode(array("result" => "error",
  112. "form" => $v->__toString()));
  113. }
  114. }
  115. public function run_task($task_id) {
  116. access::verify_csrf();
  117. try {
  118. $task = task::run($task_id);
  119. } catch (Exception $e) {
  120. $error_msg = $e->getMessage();
  121. $task->done = true;
  122. }
  123. if ($task->done) {
  124. batch::stop();
  125. $context = unserialize($task->context);
  126. switch ($task->state) {
  127. case "success":
  128. message::success($context["success_msg"]);
  129. break;
  130. case "error":
  131. message::success(empty($error_msg) ? $context["error_msg"] : $error_msg);
  132. break;
  133. }
  134. print json_encode(array("result" => "success",
  135. "task" => $task->as_array()));
  136. } else {
  137. print json_encode(array("result" => "in_progress",
  138. "task" => $task->as_array()));
  139. }
  140. }
  141. function mptt() {
  142. $v = new Admin_View("admin.html");
  143. $v->content = new View("mptt_tree.html");
  144. $v->content->tree = $this->_build_tree();
  145. if (exec("which /usr/bin/dot")) {
  146. $v->content->url = url::site("admin/developer/mptt_graph");
  147. } else {
  148. $v->content->url = null;
  149. message::warning(t("The package 'graphviz' is not installed, degrading to text view"));
  150. }
  151. print $v;
  152. }
  153. function mptt_graph() {
  154. $items = ORM::factory("item")->orderby("id")->find_all();
  155. $data = $this->_build_tree();
  156. $proc = proc_open("/usr/bin/dot -Tsvg",
  157. array(array("pipe", "r"),
  158. array("pipe", "w")),
  159. $pipes,
  160. VARPATH . "tmp");
  161. fwrite($pipes[0], $data);
  162. fclose($pipes[0]);
  163. header("Content-Type: image/svg+xml");
  164. print(stream_get_contents($pipes[1]));
  165. fclose($pipes[1]);
  166. proc_close($proc);
  167. }
  168. private function _build_tree() {
  169. $items = ORM::factory("item")->orderby("id")->find_all();
  170. $data = "digraph G {\n";
  171. foreach ($items as $item) {
  172. $data .= " $item->parent_id -> $item->id\n";
  173. $data .=
  174. " $item->id [label=\"$item->id [$item->level] <$item->left_ptr, $item->right_ptr>\"]\n";
  175. }
  176. $data .= "}\n";
  177. return $data;
  178. }
  179. public function _is_module_defined(Validation $post, $field) {
  180. $module_name = strtolower(strtr($post[$field], " ", "_"));
  181. if (file_exists(MODPATH . "$module_name/module.info")) {
  182. $post->add_error($field, "module_exists");
  183. }
  184. }
  185. public function _set_default(Validation $post, $field) {
  186. if (empty($post->$field)) {
  187. $post->$field = 0;
  188. }
  189. }
  190. private function _get_module_form() {
  191. $form = array("name" => "", "display_name" => "", "description" => "", "theme[]" => array(),
  192. "event[]" => array());
  193. $errors = array_fill_keys(array_keys($form), "");
  194. return array($form, $errors);
  195. }
  196. private function _get_module_create_content($form, $errors) {
  197. $config = Kohana::config("developer.methods");
  198. $v = new View("developer_module.html");
  199. $v->action = "admin/developer/module_create";
  200. $v->hidden = array("csrf" => access::csrf_token());
  201. $v->theme = $config["theme"];
  202. $v->event = $config["event"];
  203. $v->form = $form;
  204. $v->errors = $errors;
  205. return $v;
  206. }
  207. private function _get_test_data_form() {
  208. $form = array("albums" => "10", "photos" => "10", "comments" => "10", "tags" => "10",
  209. "generate_albums" => "");
  210. $errors = array_fill_keys(array_keys($form), "");
  211. return array($form, $errors);
  212. }
  213. private function _get_test_data_view($form, $errors) {
  214. $v = new View("admin_developer_test_data.html");
  215. $v->action = "admin/developer/test_data_create";
  216. $v->hidden = array("csrf" => access::csrf_token());
  217. $album_count = ORM::factory("item")->where("type", "album")->count_all();
  218. $photo_count = ORM::factory("item")->where("type", "photo")->count_all();
  219. $v->comment_installed = module::is_active("comment");
  220. $comment_count = empty($v->comment_installed) ? 0 : ORM::factory("comment")->count_all();
  221. $v->tag_installed = module::is_active("tag");
  222. $tag_count = empty($v->tag_installed) ? 0 : ORM::factory("tag")->count_all();
  223. $v->album_count = t2("%count album", "%count albums", $album_count);
  224. $v->photo_count = t2("%count photo", "%count photos", $photo_count);
  225. $v->comment_count = t2("%count comment", "%count comments", $comment_count);
  226. $v->tag_count = t2("%count tag", "%count tags", $tag_count);
  227. $v->form = $form;
  228. $v->errors = $errors;
  229. return $v;
  230. }
  231. }