1
0

Update the developer module so that it actually works. Derive the available

events by scanning the available modules. Corrected the generation of the
hidden csrf value.
This commit is contained in:
Tim Almdal 2010-06-01 11:37:43 -07:00
parent 680643c4f9
commit f658f994c9
7 changed files with 91 additions and 133 deletions

View File

@ -32,6 +32,7 @@ $config["methods"] = array(
"admin_page_bottom" => t("Bottom of administration page"), "admin_page_bottom" => t("Bottom of administration page"),
"admin_page_top" => t("Top of administration page"), "admin_page_top" => t("Top of administration page"),
"admin_head" => t("Adminstration page head"), "admin_head" => t("Adminstration page head"),
"body_attributes" => t("Body Attributes"),
"credits" => t("Album or photo page credits"), "credits" => t("Album or photo page credits"),
"dynamic_bottom" => t("Bottom of dynamic page content"), "dynamic_bottom" => t("Bottom of dynamic page content"),
"dynamic_top" => t("Top of dynamic page content"), "dynamic_top" => t("Top of dynamic page content"),
@ -44,27 +45,10 @@ $config["methods"] = array(
"photo_blocks" => t("Photo block"), "photo_blocks" => t("Photo block"),
"photo_bottom" => t("Bottom of photo content"), "photo_bottom" => t("Bottom of photo content"),
"photo_top" => t("Top of photo content"), "photo_top" => t("Top of photo content"),
"resize_bottom" => t("Bottom of the resize view"),
"resize_top" => t("Top of the resize view"),
"sidebar_bottom" => t("Bottom of sidebar"), "sidebar_bottom" => t("Bottom of sidebar"),
"sidebar_top" => t("Top of sidebar"), "sidebar_top" => t("Top of sidebar"),
"thumb_bottom" => t("Bottom of thumbnail"), "thumb_bottom" => t("Bottom of thumbnail"),
"thumb_info" => t("Thumbnail information"), "thumb_info" => t("Thumbnail information"),
"thumb_top" => t("Top of thumbnail display")), "thumb_top" => t("Top of thumbnail display")));
"event" => array("admin_menu" => t("Add an admin menu element"),
"album_menu" => t("Add an album menu element"),
"batch_complete" => t("Batch completion"),
"comment_add_form" => t("Comment add form creation"),
"comment_created" => t("Comment created"),
"comment_updated" => t("Comment updated"),
"group_before_delete" => t("Before delete group"),
"group_created" => t("Group created"),
"item_before_delete" => t("Before album or photo deletion"),
"item_created" => t("Album or photo created"),
"item_related_update" => t("Photo meta data update"),
"item_related_update_batch" => t("Photo meta data update"),
"item_updated" => t("Album or photo update"),
"photo_menu" => t("Add a photo menu element"),
"site_menu" => t("Add a site menu element"),
"user_before_delete" => t("Before user deletion"),
"user_created" => t("User created"),
"user_login" => t("User login"),
"user_logout" => t("User logout")));

View File

@ -18,6 +18,8 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
class Admin_Developer_Controller extends Admin_Controller { class Admin_Developer_Controller extends Admin_Controller {
static $event_list = array();
public function module() { public function module() {
$view = new Admin_View("admin.html"); $view = new Admin_View("admin.html");
$view->content = new View("admin_developer.html"); $view->content = new View("admin_developer.html");
@ -51,6 +53,8 @@ class Admin_Developer_Controller extends Admin_Controller {
$post->add_rules("name", "required"); $post->add_rules("name", "required");
$post->add_rules("display_name", "required"); $post->add_rules("display_name", "required");
$post->add_rules("description", "required"); $post->add_rules("description", "required");
$post->add_callbacks("theme", array($this, "_noop_validation"));
$post->add_callbacks("event", array($this, "_noop_validation"));
$post->add_callbacks("name", array($this, "_is_module_defined")); $post->add_callbacks("name", array($this, "_is_module_defined"));
if ($post->validate()) { if ($post->validate()) {
@ -58,14 +62,14 @@ class Admin_Developer_Controller extends Admin_Controller {
->callback("developer_task::create_module") ->callback("developer_task::create_module")
->description(t("Create a new module")) ->description(t("Create a new module"))
->name(t("Create Module")); ->name(t("Create Module"));
$task = task::create($task_def, array_merge(array("step" => 0), $post->as_array()));
$success_msg = t("Generation of %module completed successfully", $success_msg = t("Generation of %module completed successfully",
array("module" => $post->name)); array("module" => $post->name));
$error_msg = t("Generation of %module failed.", array("module" => $post->name)); $error_msg = t("Generation of %module failed.", array("module" => $post->name));
$task_context = array("step" => 0, "success_msg" => $success_msg, "error_msg" => $error_msg);
$task = task::create($task_def, array_merge($task_context, $post->as_array()));
print json_encode(array("result" => "started", print json_encode(array("result" => "started",
"max_iterations" => 15, "max_iterations" => 15,
"success_msg" => $success_msg, "error_msg" => $error_msg,
"url" => url::site("admin/developer/run_task/{$task->id}?csrf=" . "url" => url::site("admin/developer/run_task/{$task->id}?csrf=" .
access::csrf_token()), access::csrf_token()),
"task" => $task->as_array())); "task" => $task->as_array()));
@ -77,6 +81,9 @@ class Admin_Developer_Controller extends Admin_Controller {
} }
} }
public function _noop_validation(Validation $array, $field) {
}
public function session($key) { public function session($key) {
access::verify_csrf(); access::verify_csrf();
$input = Input::instance(); $input = Input::instance();
@ -85,8 +92,6 @@ class Admin_Developer_Controller extends Admin_Controller {
} }
public function test_data_create() { public function test_data_create() {
access::verify_csrf();
list ($form, $errors) = $this->_get_test_data_form(); list ($form, $errors) = $this->_get_test_data_form();
$post = new Validation($_POST); $post = new Validation($_POST);
@ -128,8 +133,6 @@ class Admin_Developer_Controller extends Admin_Controller {
} }
public function run_task($task_id) { public function run_task($task_id) {
access::verify_csrf();
try { try {
$task = task::run($task_id); $task = task::run($task_id);
} catch (Exception $e) { } catch (Exception $e) {
@ -229,14 +232,50 @@ class Admin_Developer_Controller extends Admin_Controller {
$v = new View("developer_module.html"); $v = new View("developer_module.html");
$v->action = "admin/developer/module_create"; $v->action = "admin/developer/module_create";
$v->hidden = array("csrf" => access::csrf_token());
$v->theme = $config["theme"]; $v->theme = $config["theme"];
$v->event = $config["event"]; $v->event = $this->_get_events();
$v->form = $form; $v->form = $form;
$v->errors = $errors; $v->errors = $errors;
$submit_attributes = array(
"id" => "g-generate-module",
"name" => "generate",
"class" => "ui-state-default ui-corner-all",
"style" => "clear:both!important");
if (!is_writable(MODPATH)) {
$submit_attributes["class"] .= " ui-state-disabled";
$submit_attributes["disabled"] = "disabled";
}
$v->submit_attributes = $submit_attributes;
return $v; return $v;
} }
private function _get_events() {
if (empty(self::$event_list)) {
$dir = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(MODPATH));
foreach ($dir as $file) {
$file_as_string = file_get_contents($file);
if (preg_match_all('#module::event\("(.*?)"(.*)\);#mU', $file_as_string, $matches, PREG_SET_ORDER) > 0) {
foreach ($matches as $match) {
$event_name = $match[1];
$parameters = array();
if (!empty($match[2]) &&
preg_match_all('#\$[a-zA-Z_]*#', $match[2], $param_names)) {
foreach ($param_names[0] as $name) {
$parameters[] = $name != '$this' ? $name : '$' . $event_name;
}
}
self::$event_list["static function $event_name(" . implode(", ", $parameters) . ")"] = $event_name;
ksort(self::$event_list);
}
}
}
}
return self::$event_list;
}
private function _get_test_data_form() { private function _get_test_data_form() {
$form = array("albums" => "10", "photos" => "10", "comments" => "10", "tags" => "10", $form = array("albums" => "10", "photos" => "10", "comments" => "10", "tags" => "10",
"generate_albums" => ""); "generate_albums" => "");
@ -248,7 +287,6 @@ class Admin_Developer_Controller extends Admin_Controller {
private function _get_test_data_view($form, $errors) { private function _get_test_data_view($form, $errors) {
$v = new View("admin_developer_test_data.html"); $v = new View("admin_developer_test_data.html");
$v->action = "admin/developer/test_data_create"; $v->action = "admin/developer/test_data_create";
$v->hidden = array("csrf" => access::csrf_token());
$album_count = ORM::factory("item")->where("type", "=", "album")->count_all(); $album_count = ORM::factory("item")->where("type", "=", "album")->count_all();
$photo_count = ORM::factory("item")->where("type", "=", "photo")->count_all(); $photo_count = ORM::factory("item")->where("type", "=", "photo")->count_all();

View File

@ -25,6 +25,7 @@ class developer_task_Core {
static function create_module($task) { static function create_module($task) {
$context = unserialize($task->context); $context = unserialize($task->context);
Kohana_Log::add("error", "task context:\n" . Kohana::debug($context));
if (empty($context["module"])) { if (empty($context["module"])) {
$context["class_name"] = strtr($context["name"], " ", "_"); $context["class_name"] = strtr($context["name"], " ", "_");
@ -34,11 +35,11 @@ class developer_task_Core {
switch ($context["step"]) { switch ($context["step"]) {
case 0: // Create directory tree case 0: // Create directory tree
foreach (array("", "controllers", "helpers", "js", "views") as $dir) { foreach (array("", "controllers", "helpers", "views") as $dir) {
$path = "{$context['module_path']}/$dir"; $path = "{$context['module_path']}/$dir";
if (!file_exists($path)) { if (!file_exists($path)) {
mkdir($path); mkdir($path);
chmod($path, 0777); chmod($path, 0755);
} }
} }
break; break;
@ -103,7 +104,19 @@ class developer_task_Core {
file_put_contents($file, ob_get_contents()); file_put_contents($file, ob_get_contents());
ob_end_clean(); ob_end_clean();
break; break;
case 9: // Generate module.info (do last) case 9: // Generate dashboard block view
$file = "{$context['module_path']}/views/admin_{$context['module']}_block.html.php";
ob_start();
$v = new View("dashboard_block_html.txt");
$v->name = $context["name"];
$v->module = $context["module"];
$v->class_name = $context["class_name"];
$v->css_id = preg_replace("#\s+#", "", $context["name"]);
print $v->render();
file_put_contents($file, ob_get_contents());
ob_end_clean();
break;
case 10: // Generate module.info (do last)
$file = "{$context["module_path"]}/module.info"; $file = "{$context["module_path"]}/module.info";
ob_start(); ob_start();
$v = new View("module_info.txt"); $v = new View("module_info.txt");
@ -115,7 +128,7 @@ class developer_task_Core {
break; break;
} }
if (isset($file)) { if (isset($file)) {
chmod($file, 0666); chmod($file, 0765);
} }
$task->done = (++$context["step"]) >= 11; $task->done = (++$context["step"]) >= 11;
$task->context = serialize($context); $task->context = serialize($context);
@ -128,7 +141,6 @@ class developer_task_Core {
$config = Kohana::config("developer.methods"); $config = Kohana::config("developer.methods");
$file = "{$context["module_path"]}/helpers/{$context["module"]}_{$helper}.php"; $file = "{$context["module_path"]}/helpers/{$context["module"]}_{$helper}.php";
touch($file); touch($file);
chmod($file, 0666);
ob_start(); ob_start();
$v = new View("$helper.txt"); $v = new View("$helper.txt");
$v->helper = $helper; $v->helper = $helper;
@ -184,7 +196,7 @@ class developer_task_Core {
private static function _add_album_or_photo($desired_type=null) { private static function _add_album_or_photo($desired_type=null) {
srand(time()); srand(time());
$parents = ORM::factory("item")->where("type", "=", "album")->find_all()->as_array(); $parents = ORM::factory("item")->where("type", "=", "album")->find_all()->as_array();
$owner_id = user::active()->id; $owner_id = identity::active_user()->id;
$test_images = glob(dirname(dirname(__FILE__)) . "/data/*.[Jj][Pp][Gg]"); $test_images = glob(dirname(dirname(__FILE__)) . "/data/*.[Jj][Pp][Gg]");
@ -207,7 +219,7 @@ class developer_task_Core {
$parents[] = $item->save(); $parents[] = $item->save();
} else { } else {
$photo_index = rand(0, count($test_images) - 1); $photo_index = rand(0, count($test_images) - 1);
$item = ORM:factory("item"); $item = ORM::factory("item");
$item->type = "photo"; $item->type = "photo";
$item->parent_id = $parent->id; $item->parent_id = $parent->id;
$item->set_data_file($test_images[$photo_index]); $item->set_data_file($test_images[$photo_index]);
@ -240,6 +252,7 @@ class developer_task_Core {
$comment = ORM::factory("comment"); $comment = ORM::factory("comment");
$comment->author_id = $author->id; $comment->author_id = $author->id;
$comment->item_id = $photo->id;
$comment->text = self::_random_phrase(rand(8, 500)); $comment->text = self::_random_phrase(rand(8, 500));
$comment->guest_name = $guest_name; $comment->guest_name = $guest_name;
$comment->guest_email = $guest_email; $comment->guest_email = $guest_email;

View File

@ -19,7 +19,7 @@
<? endif ?> <? endif ?>
}); });
</script> </script>
<?= form::open($action, array("method" => "post", "id" => "g-generate-test-data"), $hidden) ?> <?= form::open($action, array("method" => "post", "id" => "g-generate-test-data")) ?>
<? if (!empty($album_count)): ?> <? if (!empty($album_count)): ?>
<p><?= t("Currently:") ?><br /> <p><?= t("Currently:") ?><br />
@ -29,6 +29,7 @@
<fieldset> <fieldset>
<ul> <ul>
<li><?= access::csrf_form_field() ?></li>
<li <? if (!empty($errors["albums"])): ?> class="g-error"<? endif ?>> <li <? if (!empty($errors["albums"])): ?> class="g-error"<? endif ?>>
<fieldset> <fieldset>
<?= form::label("g-generate-albums", t("Generate Albums")) ?> <?= form::label("g-generate-albums", t("Generate Albums")) ?>

View File

@ -0,0 +1,10 @@
<?php defined("SYSPATH") or die("No direct script access.") ?>
<?= "<?php defined(\"SYSPATH\") or die(\"No direct script access.\") ?>" ?>
<div class="g-<?= $css_id ?>-block">
<?= "<a href=\"<?= \$item->url() ?>\">" ?>
<?= "<?= \$item->thumb_tag(array(\"class\" => \"g-thumbnail\")) ?>" ?>
</a>
</div>

View File

@ -1,8 +1,9 @@
<?php defined("SYSPATH") or die("No direct script access.") ?> <?php defined("SYSPATH") or die("No direct script access.") ?>
<?= form::open($action, array("method" => "post"), $hidden) ?> <?= form::open($action, array("method" => "post")) ?>
<fieldset> <fieldset>
<ul> <ul>
<li><?= access::csrf_form_field() ?></li>
<li <? if (!empty($errors["name"])): ?> class="g-error"<? endif ?>> <li <? if (!empty($errors["name"])): ?> class="g-error"<? endif ?>>
<?= form::label("name", t("Name")) ?> <?= form::label("name", t("Name")) ?>
<?= form::input("name", $form["name"]) ?> <?= form::input("name", $form["name"]) ?>
@ -33,14 +34,14 @@
<?= form::label("theme[]", t("Theme callbacks")) ?> <?= form::label("theme[]", t("Theme callbacks")) ?>
<?= form::dropdown(array("name" => "theme[]", "multiple" => true, "size" => 6), $theme, $form["theme[]"]) ?> <?= form::dropdown(array("name" => "theme[]", "multiple" => true, "size" => 6), $theme, $form["theme[]"]) ?>
</li> </li>
<li> <li style="padding-left: 1em" >
<?= form::label("event[]", t("Gallery event handlers")) ?> <?= form::label("event[]", t("Gallery event handlers")) ?>
<?= form::dropdown(array("name" => "event[]", "multiple" => true, "size" => 6), $event, $form["event[]"]) ?> <?= form::dropdown(array("name" => "event[]", "multiple" => true, "size" => 6), $event, $form["event[]"]) ?>
</li> </li>
</ul> </ul>
</li> </li>
<li> <li>
<?= form::submit(array("id" => "g-generate-module", "name" => "generate", "class" => "submit", "style" => "clear:both!important"), t("Generate")) ?> <?= form::submit($submit_attributes, t("Generate")) ?>
</li> </li>
</ul> </ul>
</fieldset> </fieldset>

View File

@ -19,98 +19,9 @@
* Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
*/ */
class <?= $module ?>_event { class <?= $module ?>_event {
<? if (!empty($callbacks["admin_menu"])): ?> <? foreach ($callbacks as $callback => $unused): ?>
static function admin_menu($menu, $theme) { <?= $callback ?> {
} }
<? endif ?> <? endforeach ?>
<? if (!empty($callbacks["album_menu"])): ?>
static function album_menu($menu, $theme) {
}
<? endif ?>
<? if (!empty($callbacks["batch_complete"])): ?>
static function batch_complete() {
}
<? endif ?>
<? if (!empty($callbacks["comment_add_form"])): ?>
static function comment_add_form($form) {
}
<? endif ?>
<? if (!empty($callbacks["comment_created"])): ?>
static function comment_created($theme, $args) {
}
<? endif ?>
<? if (!empty($callbacks["comment_updated"])): ?>
static function comment_updated($old, $new) {
}
<? endif ?>
<? if (!empty($callbacks["group_before_delete"])): ?>
static function group_before_delete($group) {
}
<? endif ?>
<? if (!empty($callbacks["group_created"])): ?>
static function group_created($group) {
}
<? endif ?>
<? if (!empty($callbacks["item_before_delete"])): ?>
static function item_before_delete($item) {
}
<? endif ?>
<? if (!empty($callbacks["item_created"])): ?>
static function item_created($item) {
}
<? endif ?>
<? if (!empty($callbacks["item_related_update"])): ?>
static function item_related_update($item) {
}
<? endif ?>
<? if (!empty($callbacks["item_related_update_batch"])): ?>
static function item_related_update_batch($sql) {
}
<? endif ?>
<? if (!empty($callbacks["item_updated"])): ?>
static function item_updated($old, $new) {
}
<? endif ?>
<? if (!empty($callbacks["photo_menu"])): ?>
static function photo_menu($menu, $theme) {
}
<? endif ?>
<? if (!empty($callbacks["site_menu"])): ?>
static function site_menu($menu, $theme) {
}
<? endif ?>
<? if (!empty($callbacks["user_before_delete"])): ?>
static function user_before_delete($user) {
}
<? endif ?>
<? if (!empty($callbacks["user_created"])): ?>
static function user_created($user) {
}
<? endif ?>
<? if (!empty($callbacks["user_login"])): ?>
static function user_login($user) {
}
<? endif ?>
<? if (!empty($callbacks["user_logout"])): ?>
static function user_logout($user) {
}
<? endif ?>
} }