protected function _execute(array $options) { $module_name = $options['module']; $module_dir = Arr::get(Kohana::modules(), $module_name); $author = $options['author']; switch ($options['type']) { case 'number': $parent = 'Stats_Widget_Number'; break; case 'chart': $parent = 'Stats_Widget_Chart'; break; default: $parent = 'Stats_Widget'; } $name = $options['name']; $title = Jam::capitalize_class_name(str_replace('_', ' ', $name)); $path = str_replace('_', DIRECTORY_SEPARATOR, $title); $dir = $module_dir . 'classes' . DIRECTORY_SEPARATOR . 'Stats' . DIRECTORY_SEPARATOR . 'Widget'; $file = $dir . DIRECTORY_SEPARATOR . $path . EXT; $class = 'Stats_Widget_' . str_replace(' ', '_', $title); if (!is_dir(dirname($file))) { mkdir(dirname($file), 0777, TRUE); } $widget_content = <<<WIDGET <?php defined('SYSPATH') OR die('No direct script access.'); /** * Stats Widget: {$title} * * @package {$module_name} * @author {$author} * @copyright (c) 2011-2013 Despark Ltd. */ class {$class} extends {$parent} { \tprotected \$_title = '{$title}'; \tpublic function retrieve(\$start_date, \$end_date) \t{ \t\t// return 0; \t} } WIDGET; Minion_Jam_Generate::modify_file($file, $widget_content, $options['force'] !== FALSE); }
protected function _execute(array $options) { $module_name = $options['module']; $dir = Arr::get(Kohana::modules(), $module_name); $meta = Jam::meta($options['model']); $controller = $options['controller'] ?: Inflector::plural($meta->model()); $item_name = $meta->model(); $item_title = ucwords(Inflector::humanize($meta->model())); $plural_name = str_replace('_', ' ', $controller); $author = $options['author']; $controller_option = $options['controller'] ? '->controller(\'' . $controller . '\')' : NULL; $controller_title = Jam::capitalize_class_name($plural_name); $controller_path = str_replace('_', DIRECTORY_SEPARATOR, $controller_title); $controller_dir = $dir . 'classes' . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR . 'Tart'; $controller_file = $controller_dir . DIRECTORY_SEPARATOR . $controller_path . EXT; $controller_class = 'Controller_Tart_' . str_replace(' ', '_', $controller_title); $views_path = 'tart' . DIRECTORY_SEPARATOR . strtolower($controller_path); $views_dir = $dir . 'views' . DIRECTORY_SEPARATOR . $views_path . DIRECTORY_SEPARATOR; $extra_controller = $options['controller'] ? ", array('controller' => '{$controller}')" : ''; $extra_controller_delete = $options['controller'] ? "array('controller' => '{$controller}', 'action' => 'delete')" : "'delete'"; $batch_index = NULL; $batch_delete = NULL; $batch_modify = NULL; if (!is_dir($views_dir)) { mkdir($views_dir, 0777, TRUE); } if (!is_dir(dirname($controller_file))) { mkdir(dirname($controller_file), 0777, TRUE); } if ($options['batch_delete'] !== FALSE or $options['batch_modify'] !== FALSE) { $actions = ''; if ($options['batch_delete'] !== FALSE) { $actions .= "\n\t\t\t\t'delete' => 'Delete',"; $batch_delete = <<<BATCH_DELETE \tpublic function batch_delete(\$ids) \t{ \t\tif (\$this->request->method() == Request::POST) \t\t{ \t\t\t\$result = array(); \t\t\tforeach (Jam::all('{$meta->model()}')->where_key(\$ids) as \${$item_name}) \t\t\t{ \t\t\t\t\$result[] = \${$item_name}->delete(); \t\t\t} \t\t\t\$this->notify('success', count(\$result).' {$plural_name} deleted'); \t\t\t\$this->redirect(Tart::uri('{$controller}')); \t\t} \t\telse \t\t{ \t\t\t\$table = Tart::table(Jam::all('{$meta->model()}')->where_key(\$ids)) \t\t\t\t->selected(\$ids) \t\t\t\t->columns('{$meta->name_key()}', Tart::column()); \t\t\t\$this->template->content = View::factory('{$views_path}/batch_delete', array('table' => \$table)); \t\t} \t} BATCH_DELETE; } if ($options['batch_modify'] !== FALSE) { $actions .= "\n\t\t\t\t'modify' => 'Modify',"; $batch_modify = <<<BATCH_MODIFY \tpublic function batch_modify(\$ids) \t{ \t\t\$params = array('{$meta->name_key()}'); \t\tif (\$this->request->method() == Request::POST) \t\t{ \t\t\t\$modified = Tart_Request::modified_params(\$this->request->post(), \$params); \t\t\tforeach (Jam::all('{$meta->model()}')->where_key(\$ids) as \$item) \t\t\t{ \t\t\t\t\$item->set(\$modified)->save(); \t\t\t} \t\t\t\$this->notify('success', count(\$ids).' {$plural_name} modified: '.Tart_Request::to_modifications(\$modified)); \t\t\t\$this->redirect(Tart::uri('{$controller}')); \t\t} \t\telse \t\t{ \t\t\t\$table = Tart::table(Jam::all('{$meta->model()}')->where_key(\$ids)) \t\t\t\t->selected(\$ids) \t\t\t\t->columns('{$meta->name_key()}', Tart::column()); \t\t\t\$item = Jam::build('{$meta->model()}', Jam_Form::common_params(\$table->collection(), \$params)); \t\t\t\$this->template->content = View::factory('{$views_path}/batch_modify', array('table' => \$table, 'form' => Jam::form(\$item, 'tart_general')->validation(FALSE))); \t\t} \t} BATCH_MODIFY; } $batch_index = <<<BATCH_INDEX \t\t\t->batch_actions(array({$actions} \t\t\t)) BATCH_INDEX; } $controller_content = <<<CONTROLLER <?php defined('SYSPATH') OR die('No direct script access.'); /** * Tart Controller: {$controller_title} * * @package {$module_name} * @author {$author} * @copyright (c) 2011-2013 Despark Ltd. */ class {$controller_class} extends Controller_Tart_Layout { \tpublic function action_index() \t{ \t\t\$collection = Jam::all('{$meta->model()}'); \t\t\$filter = Tart::filter(\$this->request->query()) \t\t\t{$controller_option} \t\t\t->entries(array( \t\t\t\t'q' => Tart::entry('search'), \t\t\t)) \t\t\t->apply(\$collection); \t\t\$index = Tart::index(\$collection, \$this->request->query('offset')) \t\t\t{$controller_option} {$batch_index} \t\t\t->columns(array( \t\t\t\t'{$meta->name_key()}' => Tart::column(), \t\t\t\t'actions' => Tart::column('actions'), \t\t\t)); \t\t\$this->template->set(array( \t\t\t'content' => View::factory('{$views_path}/index', array('index' => \$index, 'filter' => \$filter)), \t\t\t'sidebar' => View::factory('{$views_path}/sidebar', array('filter' => \$filter)), \t\t)); \t} \tpublic function action_edit() \t{ \t\t\$item = Jam::find_insist('{$meta->model()}', \$this->request->param('id')); \t\tif (\$this->request->method() === Request::POST AND \$item->set(\$this->post())->check()) \t\t{ \t\t\t\$this->notify('success', '{$item_title} Updated'); \t\t\t\$item->save(); \t\t} \t\t\$this->template->content = View::factory('{$views_path}/edit', array('item' => \$item)); \t} \tpublic function action_new() \t{ \t\t\$item = Jam::build('{$meta->model()}'); \t\tif (\$this->request->method() === Request::POST AND \$item->set(\$this->post())->check()) \t\t{ \t\t\t\$item->save(); \t\t\t\$this->notify('success', '{$item_title} Created'); \t\t\t\$this->redirect(Tart::uri(\$item{$extra_controller})); \t\t} \t\t\$this->template->content = View::factory('{$views_path}/new', array('item' => \$item)); \t} \tpublic function action_delete() \t{ \t\t\$item = Jam::find_insist('{$meta->model()}', \$this->request->param('id')); \t\t\$item->delete(); \t\t\$this->notify('success', "{$item_title} \\{\$item->name()\\} deleted"); \t\t\$this->redirect(Tart::uri('{$controller}')); \t} {$batch_delete} {$batch_modify} } CONTROLLER; Minion_Jam_Generate::modify_file($controller_file, $controller_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); $index_file = $views_dir . 'index' . EXT; $index_content = <<<VIEW_INDEX <ul class="breadcrumb"> \t<li class="active"> \t\t{$controller_title} <?php echo \$filter->render_active(); ?> \t</li> \t<li class="pull-right"> \t\t<?php echo \$index->pagination()->pager(); ?> \t</li> </ul> <?php echo \$index->render(); ?> VIEW_INDEX; Minion_Jam_Generate::modify_file($index_file, $index_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); $sidebar_file = $views_dir . 'sidebar' . EXT; $sidebar_content = <<<VIEW_SIDEBAR <ul class="nav nav-tabs nav-stacked"> \t<li> \t\t<?php echo Tart_Html::anchor(Tart::uri(Jam::build('{$meta->model()}'){$extra_controller}), '<i class="icon-plus"></i> Add {$item_title}'); ?> \t</li> </ul> <?php echo \$filter->render() ?> VIEW_SIDEBAR; Minion_Jam_Generate::modify_file($sidebar_file, $sidebar_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); $new_file = $views_dir . 'new' . EXT; $new_content = <<<VIEW_NEW <ul class="breadcrumb"> \t<li> \t\t<?php echo Tart_Html::anchor(Tart::uri('{$controller}'), '{$controller_title}'); ?> \t\t<span class="divider">/</span> \t</li> \t<li class="active"> \t\tCreate New {$item_title} \t</li> </ul> <?php echo Form::open(Tart::uri(\$item{$extra_controller}), array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')) ?> \t<?php echo View::factory('{$views_path}/form', array('item' => \$item)) ?> \t<div class="form-actions"> \t\t<?php echo Tart_Html::anchor(Tart::uri('{$controller}'), 'Cancel', array('class' => 'btn btn-link')); ?> \t\t<button type="submit" class="btn btn-primary">Create {$item_title}</button> \t</div> <?php echo Form::close() ?> VIEW_NEW; Minion_Jam_Generate::modify_file($new_file, $new_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); $edit_file = $views_dir . 'edit' . EXT; $edit_content = <<<VIEW_EDIT <ul class="breadcrumb"> \t<li> \t\t<a href="<?php echo Tart::uri('{$controller}') ?>">{$controller_title}</a> \t\t<span class="divider">/</span> \t</li> \t<li class="active"> \t\tEdit {$item_name} <strong><?php echo \$item->name() ?></strong> \t</li> </ul> <?php echo Form::open(Tart::uri(\$item{$extra_controller}), array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')) ?> \t<?php echo View::factory('{$views_path}/form', array('item' => \$item)) ?> \t<div class="form-actions"> \t\t<?php echo Tart_Html::anchor(Tart::uri('{$controller}'), 'Cancel', array('class' => 'btn btn-link')); ?> \t\t<button type="submit" class="btn btn-primary">Save changes</button> \t\t<?php echo Tart_Html::anchor(Tart::uri(\$item, {$extra_controller_delete}), 'Delete {$item_title}', array('class' => 'btn btn-danger pull-right', 'data-confirm' => 'Are you sure you want to delete this {$item_title}?')); ?> \t</div> <?php echo Form::close() ?> VIEW_EDIT; Minion_Jam_Generate::modify_file($edit_file, $edit_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); $form_file = $views_dir . 'form' . EXT; $form_content = <<<VIEW_FORM <?php \$form = Jam::form(\$item, 'tart_general') ?> <fieldset> \t<legend>Information</legend> \t<?php echo \$form->row('input', '{$meta->name_key()}') ?> </fieldset> VIEW_FORM; Minion_Jam_Generate::modify_file($form_file, $form_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); if ($options['batch_delete'] !== FALSE) { $batch_delete_file = $views_dir . 'batch_delete' . EXT; $batch_delete_content = <<<VIEW_BATCH_DELETE <ul class="breadcrumb"> \t<li> \t\t<?php echo Tart_Html::anchor(Tart::uri('{$controller}'), '{$controller_title}'); ?> \t\t<span class="divider">/</span> \t</li> \t<li class="active"> \t\tBatch Delete \t</li> </ul> <?php echo Form::open(Tart::uri('{$controller}', 'batch'), array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')) ?> \t<?php echo \$table->render() ?> \t<div class="form-actions"> \t\t<?php echo Tart_Html::anchor(Tart::uri('{$controller}'), 'Cancel', array('class' => 'btn btn-link')); ?> \t\t<button type="submit" class="btn btn-primary" name="action" value="delete">Delete Selected</button> \t</div> <?php echo Form::close() ?> VIEW_BATCH_DELETE; Minion_Jam_Generate::modify_file($batch_delete_file, $batch_delete_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); } if ($options['batch_modify'] !== FALSE) { $batch_modify_file = $views_dir . 'batch_modify' . EXT; $batch_modify_content = <<<VIEW_BATCH_MODIFY <ul class="breadcrumb"> \t<li> \t\t<?php echo Tart_Html::anchor(Tart::uri('{$controller}'), '{$controller_title}'); ?> \t\t<span class="divider">/</span> \t</li> \t<li class="active"> \t\tBatch Modify \t</li> </ul> <?php echo Form::open(Tart::uri('{$controller}', 'batch'), array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')) ?> \t<?php echo \$table->render() ?> \t<fieldset> \t\t<legend>Modify Selected Items</legend> \t\t<?php echo \$form->row('input', '{$meta->name_key()}', array('clear' => TRUE)); ?> \t</fieldset> \t<div class="form-actions"> \t\t<?php echo Tart_Html::anchor(Tart::uri('{$controller}'), 'Cancel', array('class' => 'btn btn-link')); ?> \t\t<button type="submit" class="btn btn-primary" name="action" value="modify">Modify Selected Items</button> \t</div> <?php echo Form::close() ?> VIEW_BATCH_MODIFY; Minion_Jam_Generate::modify_file($batch_modify_file, $batch_modify_content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); } }
protected function _execute(array $options) { $module_name = $options['module'] ?: 'applicaiton'; $module_dir = $options['module'] ? Arr::get(Kohana::modules(), $module_name) : APPPATH; $author = $options['author'] ?: '-'; $name = $options['name']; $title = Jam::capitalize_class_name(str_replace('_', ' ', $name)); $path = str_replace('_', DIRECTORY_SEPARATOR, $title); $dir = $module_dir . 'classes' . DIRECTORY_SEPARATOR . 'Model'; $file = $dir . DIRECTORY_SEPARATOR . $path . EXT; $class = 'Model_' . str_replace(' ', '_', $title); if (!is_dir(dirname($file))) { mkdir(dirname($file), 0777, TRUE); } $content = <<<MODEL <?php defined('SYSPATH') OR die('No direct script access.'); /** * Jam Model: {$title} * * @package {$module_name} * @author {$author} * @copyright (c) 2011-2013 Despark Ltd. */ class {$class} extends Jam_Model { \tpublic static function initialize(Jam_Meta \$meta) \t{ \t\t\$meta \t\t\t->associations(array( \t\t\t)) \t\t\t->fields(array( \t\t\t\t'id' => Jam::field('primary'), \t\t\t\t'name' => Jam::field('string'), \t\t\t)) \t\t\t->validator('name', array('present' => TRUE)); \t} } MODEL; Minion_Jam_Generate::modify_file($file, $content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); if ($options['collection'] !== FALSE) { $dir = $module_dir . 'classes' . DIRECTORY_SEPARATOR . 'model' . DIRECTORY_SEPARATOR . 'collection'; $file = $dir . DIRECTORY_SEPARATOR . $path . EXT; $class = 'Model_Collection_' . str_replace(' ', '_', $title); if (!is_dir(dirname($file))) { mkdir(dirname($file), 0777, TRUE); } $content = <<<COLLECTION <?php defined('SYSPATH') OR die('No direct script access.'); /** * Collection for {$title} * * @package {$module_name} * @author {$author} * @copyright (c) 2011-2013 Despark Ltd. */ class {$class} extends Jam_Query_Builder_Collection { } COLLECTION; Minion_Jam_Generate::modify_file($file, $content, $options['force'] !== FALSE, $options['unlink'] !== FALSE); } }