PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/3.0/modules/themeroller/helpers/themeroller_task.php

http://github.com/gallery/gallery3-contrib
PHP | 285 lines | 258 code | 8 blank | 19 comment | 32 complexity | a7f173fdad1a17af8e14aae2d2147dee MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, LGPL-2.1
  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-2013 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 themeroller_task_Core {
  21. static function available_tasks() {
  22. // Return empty array so nothing appears in the maintenance screen
  23. return array();
  24. }
  25. static function create_theme($task) {
  26. try {
  27. $mode = $task->get("mode", "init");
  28. $start = microtime(true);
  29. $theme_name = $task->get("theme_name");
  30. $is_admin = $task->get("is_admin", false);
  31. $theme_path = THEMEPATH . "$theme_name/";
  32. $parameters = Cache::instance()->get("create_theme_cache:{$task->id}");
  33. if ($parameters) {
  34. $parameters = unserialize($parameters);
  35. }
  36. $completed = $task->get("completed", 0);
  37. switch ($mode) {
  38. case "init":
  39. $task->log(t("Starting theme '%theme' creation", array("theme" => $task->get("display_name"))));
  40. $task->set("mode", "create_directory");
  41. $parameters = themeroller::get_theme_parameters($task->get("original_name"),
  42. $task->get("path"),
  43. $is_admin);
  44. $task->set("total_activites",
  45. 7 // number of directories to create
  46. + 3 // screen.css, theme.info, thumbnail
  47. + count($parameters["standard_css"]) // number of standard css to copy
  48. + count($parameters["views"]) // number of views to copy
  49. + count($parameters["js"]) // number of javascript files to copy
  50. + count($parameters["masks"]) // number of images to generate
  51. + count($parameters["icons"]) // number of icon images to generate
  52. + count($parameters["css_files"]) // number of css files
  53. + count($parameters["gifs"]) // number of static files
  54. + count($parameters["images"])); // number of image files to copy
  55. $task->status = t("Starting up");
  56. break;
  57. case "create_directory":
  58. $completed = $task->get("completed");
  59. foreach (array("", "css", "css/themeroller", "css/themeroller/images", "images",
  60. "js", "views") as $dir) {
  61. $path = "{$theme_path}$dir";
  62. $completed++;
  63. if (!file_exists($path)) {
  64. mkdir($path);
  65. chmod($path, 0755);
  66. $task->log(t("Created directory: %path", array("path" => $path)));
  67. }
  68. }
  69. $task->status = t("Directories created");
  70. $task->set("mode", "copy_views");
  71. break;
  72. case "copy_views":
  73. $task->status = t("Copying views");
  74. while (!empty($parameters["views"]) && microtime(true) - $start < 1.5) {
  75. $view = array_shift($parameters["views"]);
  76. $target = "{$theme_path}views/" . basename($view);
  77. if (!file_exists($target)) {
  78. copy($view, $target);
  79. $task->log(t("Copied view: %path", array("path" => basename($view))));
  80. }
  81. $completed++;
  82. }
  83. if (empty($parameters["views"])){
  84. $task->status = t("Views copied");
  85. $task->set("mode", "copy_themeroller_images");
  86. }
  87. break;
  88. case "copy_themeroller_images":
  89. $task->status = t("Copying themeroller images");
  90. while (!empty($parameters["images"]) && microtime(true) - $start < 1.5) {
  91. $image = array_shift($parameters["images"]);
  92. $target = "{$theme_path}css/themeroller/images/" . basename($image);
  93. if (!file_exists($target)) {
  94. copy($image, $target);
  95. $task->log(t("Copied themeroller image: %path", array("path" => basename($image))));
  96. }
  97. $completed++;
  98. }
  99. if (empty($parameters["views"])){
  100. $task->status = t("Themeroller images copied");
  101. $task->set("mode", "copy_gif_images");
  102. }
  103. break;
  104. case "copy_gif_images":
  105. $task->status = t("Copying gif images");
  106. while (!empty($parameters["gifs"]) && microtime(true) - $start < 1.5) {
  107. $gif = array_shift($parameters["gifs"]);
  108. $target = "{$theme_path}images/" . basename($gif);
  109. if (!file_exists($target)) {
  110. copy($gif, $target);
  111. $task->log(t("Copied gif image: %path", array("path" => basename($gif))));
  112. }
  113. $completed++;
  114. }
  115. if (empty($parameters["gifs"])){
  116. $task->status = t("Gif images copied");
  117. $task->set("mode", "copy_css");
  118. }
  119. break;
  120. case "copy_css":
  121. $task->status = t("Copying themeroller css");
  122. $target = "{$theme_path}css/themeroller/ui.base.css";
  123. copy($parameters["css_files"][0], $target);
  124. $completed++;
  125. $task->log(t("Copied themeroller css: themeroller/ui.base.css"));
  126. $task->status = t("Themeroller css copied");
  127. $task->set("mode", "generate_images");
  128. break;
  129. case "generate_images":
  130. $task->status = t("Generating gallery images");
  131. $target_dir = "{$theme_path}images/";
  132. $colors = $task->get("colors");
  133. $image_color = $parameters["colors"]["iconColorContent"];
  134. while (!empty($parameters["masks"]) && microtime(true) - $start < 1.5) {
  135. $mask = array_shift($parameters["masks"]);
  136. $basename = basename($mask);
  137. if (preg_match("/(.*)_mask(\[(\w*)\])?(\.png)$/", $basename, $matches)) {
  138. $basename = "{$matches[1]}{$matches[4]}";
  139. $image_color = empty($matches[3]) ? $parameters["colors"]["iconColorContent"] :
  140. $parameters["colors"][$matches[3]];
  141. } else {
  142. $image_color = $parameters["colors"]["iconColorContent"];
  143. }
  144. $image_file = "{$target_dir}$basename";
  145. themeroller::generate_image($mask, $image_file, $image_color);
  146. $completed++;
  147. $task->log(t("Generated image: %path", array("path" => $image_file)));
  148. }
  149. if (empty($parameters["masks"])) {
  150. $task->set("mode", "generate_icons");
  151. $task->status = t("Gallery images generated");
  152. }
  153. break;
  154. case "generate_icons":
  155. $task->status = t("Generating icons");
  156. $target_dir = "{$theme_path}css/themeroller/images/";
  157. $mask_file = $parameters["icon_mask"];
  158. while (!empty($parameters["icons"]) && microtime(true) - $start < 1.5) {
  159. $color = array_shift($parameters["icons"]);
  160. $icon_file = $target_dir . str_replace("mask", $color, basename($mask_file));
  161. themeroller::generate_image($mask_file, $icon_file, $color);
  162. $completed++;
  163. $task->log(t("Generated themeroller icon: %path", array("path" => $icon_file)));
  164. }
  165. if (empty($parameters["icons"])) {
  166. $task->set("mode", "copy_standard_css");
  167. $task->status = t("Icons generated");
  168. }
  169. break;
  170. case "copy_standard_css":
  171. $task->status = t("Copying standard css");
  172. while (!empty($parameters["standard_css"]) && microtime(true) - $start < 1.5) {
  173. $css = array_shift($parameters["standard_css"]);
  174. $target = "{$theme_path}css/" . basename($css);
  175. if (!file_exists($target)) {
  176. copy($css, $target);
  177. $task->log(t("Copied css file: %path", array("path" => basename($target))));
  178. }
  179. $completed++;
  180. }
  181. if (empty($parameters["standard_css"])){
  182. $task->status = t("Standard css copied");
  183. $task->set("mode", "copy_javascript");
  184. }
  185. break;
  186. case "copy_javascript":
  187. $task->status = t("Copying javascript");
  188. while (!empty($parameters["js"]) && microtime(true) - $start < 1.5) {
  189. $js = array_shift($parameters["js"]);
  190. $target = "{$theme_path}js/" . str_replace(array("admin_", "site_"), "", basename($js));
  191. if (!file_exists($target)) {
  192. copy($js, $target);
  193. $task->log(t("Copied js file: %path", array("path" => basename($target))));
  194. }
  195. $completed++;
  196. }
  197. if (empty($parameters["js"])){
  198. $task->status = t("Javascript copied");
  199. $task->set("mode", "generate_screen_css");
  200. }
  201. break;
  202. case "generate_screen_css":
  203. $file = "{$theme_path}/css/screen.css";
  204. foreach (array("screen", "screen-rtl") as $file) {
  205. $css_file = "{$theme_path}/css/$file.css";
  206. $v = new View(($is_admin ? "admin" : "site") . "_{$file}.css");
  207. $v->display_name = $task->get("display_name");
  208. foreach ($parameters["colors"] as $color => $value) {
  209. $v->$color = $value;
  210. }
  211. ob_start();
  212. print $v->render();
  213. file_put_contents($css_file, ob_get_contents());
  214. ob_end_clean();
  215. }
  216. $completed++;
  217. $task->log(t("Generated screen css: %path", array("path" => $file)));
  218. $task->status = t("Screen css generated");
  219. $task->set("mode", "generate_thumbnail");
  220. break;
  221. case "generate_thumbnail":
  222. themeroller::generate_thumbnail($parameters["thumbnail"],
  223. $parameters["thumbnail_parts"],
  224. "{$theme_path}thumbnail.png");
  225. $task->status = t("Thumbnail generated");
  226. $task->set("mode", "generate_theme_info");
  227. $completed++;
  228. $task->log(t("Generated theme thumbnail: %path",
  229. array("path" => "{$theme_path}thumbnail.png")));
  230. break;
  231. case "generate_theme_info":
  232. $file = "{$theme_path}/theme.info";
  233. $v = new View("theme.info");
  234. $v->display_name = $task->get("display_name");
  235. $v->description = $task->get("description");
  236. $v->user_name = $task->get("user_name");
  237. $v->author_url = $task->get("author_url");
  238. $v->info_url = $task->get("info_url");
  239. $v->discuss_url = $task->get("discuss_url");
  240. $v->is_admin = $is_admin;
  241. $v->definition = json_encode($parameters["colors"]);
  242. ob_start();
  243. print $v->render();
  244. file_put_contents($file, ob_get_contents());
  245. ob_end_clean();
  246. $completed++;
  247. $task->log(t("Generated theme info: %path", array("path" => "{$theme_path}theme.info")));
  248. $task->status = t("Theme info generated");
  249. $task->set("mode", "done");
  250. break;
  251. case "done":
  252. themeroller::recursive_directory_delete($task->get("path"));
  253. $display_name = $task->get("display_name");
  254. $task->done = true;
  255. $task->state = "success";
  256. $task->percent_complete = 100;
  257. $completed = $task->get("total_activites");
  258. Cache::instance()->delete("create_theme_cache:{$task->id}");
  259. $message = t("Successfully generated: %name", array("name" => $display_name));
  260. message::info($message);
  261. $task->log($message);
  262. $task->status = t("'%name' generated", array("name" => $display_name));
  263. }
  264. $task->set("completed", $completed);
  265. if (!$task->done) {
  266. Cache::instance()->set("create_theme_cache:{$task->id}", serialize($parameters));
  267. $task->percent_complete = ($completed / $task->get("total_activites")) * 100;
  268. }
  269. } catch (Exception $e) {
  270. Kohana_Log::add("error",(string)$e);
  271. $task->done = true;
  272. $task->state = "error";
  273. $task->status = $e->getMessage();
  274. $task->log((string)$e);
  275. }
  276. }
  277. }