summaryrefslogtreecommitdiff
path: root/modules/developer/controllers/admin_developer.php
blob: b961959a780ad2993fd686db1125c33d995ea626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php defined("SYSPATH") or die("No direct script access.");
/**
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2008 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
class Admin_Developer_Controller extends Admin_Controller {
  public function index() {
    $view = new Admin_View("admin.html");
    $view->content = new View("admin_developer.html");
    if (!is_writable(MODPATH)) {
      message::warning(
        t("The module directory is not writable. Please insure that it is writable by the web server"));
    }
    list ($form, $errors) = $this->_get_module_form();
    $view->content->module_create = $this->_get_module_create_content($form, $errors);
    print $view;
  }

  public function module_create() {
    access::verify_csrf();
      
    list ($form, $errors) = $this->_get_module_form();

    $post = new Validation($_POST);
    $post->add_rules("name", "required");
    $post->add_rules("description", "required");
    $post->add_callbacks("name", array($this, "_is_module_defined"));
    
    if ($post->validate()) {
      $task_def = Task_Definition::factory()
        ->callback("developer_task::create_module")
        ->description(t("Create a new module"))
        ->name(t("Create Module"));
      $task = task::create($task_def, array_merge(array("step" => 0), $post->as_array()));

      print json_encode(array("result" => "started",
                            "url" => url::site("admin/developer/run_create/{$task->id}?csrf=" .
                                               access::csrf_token()),
                            "task" => $task->as_array()));
    } else {
      $v = $this->_get_module_create_content(arr::overwrite($form, $post->as_array()),
        arr::overwrite($errors, $post->errors()));
      print json_encode(array("result" => "error",
                              "form" => $v->__toString()));
    }
  }

  public function run_create($task_id) {
    access::verify_csrf();

    $task = task::run($task_id);

    if ($task->done) {
      $context = unserialize($task->context);
      switch ($task->state) {
      case "success":
        message::success(t("Generation of %module completed successfully",
                           array("module" => $context["name"])));
        break;

      case "error":
        message::success(t("Generation of %module failed.",
                           array("module" => $context["name"])));
        break;
      }
      print json_encode(array("result" => "success",
                              "task" => $task->as_array()));

    } else {
      print json_encode(array("result" => "in_progress",
                              "task" => $task->as_array()));
    }
  }

  public function session($key) {
    if (!(user::active()->admin)) {
      throw new Exception("@todo UNAUTHORIZED", 401);
    }

    Session::instance()->set($key, $this->input->get("value", false));
    $this->auto_render = false;
    url::redirect($_SERVER["HTTP_REFERER"]);
  }

  private function _get_module_create_content($form, $errors) {
    $config = Kohana::config("developer.methods");

    $v = new View("developer_module.html");
    $v->action = "admin/developer/module_create";
    $v->hidden = array("csrf" => access::csrf_token());
    $v->theme = $config["theme"];
    $v->event = $config["event"];
    $v->menu = $config["menu"];
    $v->form = $form;
    $v->errors = $errors;
    return $v;
  }

  public function _is_module_defined(Validation $post, $field) {
    $module_name = $post[$field];
    if (file_exists(MODPATH . "$module_name/module.info")) {
      $post->add_error($field, "module_exists");
    }
  }
  
  private function _get_module_form($name="", $description="") {
    $form = array("name" => "", "description" => "", "theme[]" => array(), "menu[]" => array(),
                  "event[]" => array());
    $errors = array_fill_keys(array_keys($form), "");
    
    return array($form, $errors);
  }
}