Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewwheal/4318964 to your computer and use it in GitHub Desktop.
Save andrewwheal/4318964 to your computer and use it in GitHub Desktop.
Propeller Best Practices: Admin CRUD Controller and Views
<?php
namespace Example;
class Controller_Foo extends \Admin\Controller_Template
{
public function action_index()
{
$foo = Model_Foo::find('all');
$datatable_fields = array(
array('id', 'string', 'ID'),
array('actions', 'sprintf', 'Actions', array(
'format' => '<a href="/admin/example/foo/view/%d">View</a> | <a href="/admin/example/foo/edit/%d">Edit</a> | <a href="/admin/example/foo/delete/%d">Delete</a>',
'fields' => array('id', 'id', 'id'),
)),
);
$datatable_table = new \DataTable\DataTable($datatable_fields, 20, $foo);
$datatable = \Request::forge('datatable/datatable/build_table', false)->execute(array($datatable_table));
$this->template->set('title', 'Foo | Admin');
$this->template->content = \View::forge('foo/index');
$this->template->content->set_safe('datatable', $datatable);
$this->template->content->set_safe('foo', $foo);
}
public function action_view($id = false)
{
$foo = Model_Foo::query()
->where('id', $id)
->get_one();
if (!$foo)
{
\Session::set_flash('warning', "Could not find Foo #{$id} to view");
\Response::redirect('/admin/example/foo');
}
$this->template->title = "View #{$foo->id} | Foo | Admin";
$this->template->content = \View::forge('foo/view');
$this->template->content->set_safe('foo', $foo);
}
public function action_delete($id = false)
{
$foo = Model_Foo::find($id);
if (!$foo)
{
\Session::set_flash('error', "Could not find Foo #{$id} to delete");
\Response::redirect('/admin/example/foo');
}
$fieldset = \Fieldset::forge(get_class($foo), array('class' => 'form-horizontal'));
$fieldset->add('confirm', 'Confirm')
->set_type('checkbox')
->set_value(1)
->add_rule('required');
if (\Input::method() == 'POST')
{
if ($fieldset->validation()->run())
{
try
{
$foo->delete();
\Session::set_flash('success', 'Foo was deleted');
\Response::redirect('/admin/example/foo');
}
catch (\Database_Exception $e)
{
\Session::set_flash('error', "A database error occurred when deleting Foo #{$foo->id}, sorry for the inconvenience please try again later");
\Log::error("Could not delete Foo #{$foo->id}: Database Exception - ".$e->getMessage(), __METHOD__);
}
catch (\Exception $e)
{
\Session::set_flash('error', "Could not delete Foo #{$foo->id}, sorry for the inconvenience please try again later");
\Log::error("Could not delete Foo #{$foo->id}: Unknown Exception - ".$e->getMessage(), __METHOD__);
}
}
else
{
\Session::set_flash('warning', "Please confirm that you want to delete Foo #{$foo->id}");
}
}
$buttons = \Fieldset::forge('buttons', array('form_attributes' => array('class' => 'form-actions'), 'field_template' => '{field}'));
$buttons->add('submit', null, array('type' => 'submit', 'value' => 'Delete Customer', 'class' => 'btn btn-danger'));
$buttons->add('cancel', null, array('type' => 'button', 'value' => 'Cancel', 'class' => 'btn', 'onclick' => "window.location.href ='/admin/example/foos'"));
$fieldset->add($buttons);
$this->template->set('title', "Delete #{$foo->id} | Foo | Admin");
$this->template->content = \View::forge('foo/delete');
$this->template->content->set_safe('foo', $foo);
$this->template->content->set_safe('fieldset', $fieldset);
}
public function action_create()
{
$foo = Model_Foo::forge();
$this->template->title = 'Create | Foo | Admin';
$this->_form($foo);
}
public function action_edit($id = false)
{
$foo = Model_Foo::query()
->where('id', $id)
->get_one();
if (!$foo)
{
\Session::set_flash('error', "Could not find Foo #{$id} to edit");
\Response::redirect('/admin/example/foo');
}
$this->template->title = "Edit #{$foo->id} | Foo | Admin";
$this->_form($foo);
}
protected function _form(Model_Foo $foo)
{
$fieldset = \Fieldset::forge(get_class($foo))
->add_model($foo, $foo);
$view = \View::forge('foo/form');
$view->bind_safe('foo', $foo);
$view->bind_safe('fieldset', $fieldset);
if (\Input::method() == 'POST')
{
$foo->data = \Input::post('data');
try
{
$foo->save();
\Session::set_flash('success', "Foo #{$foo->id} saved");
\Response::redirect('/admin/example/foo');
}
catch (\Orm\ValidationFailed $e)
{
\Session::set_flash('error', 'A validation error occurred, correct this below and try again');
}
catch (\Database_Exception $e)
{
\Session::set_flash('error', "Could not save Foo #{$foo->id}, sorry for the inconvenience please try again later");
\Log::error("Could not save Foo #{$foo->id}: Database Exception - ".$e->getMessage(), __METHOD__);
}
catch (\Exception $e)
{
\Session::set_flash('error', $e->getMessage());
\Log::error("Could not save Foo #{$foo->id}: Unknown Exception - ".$e->getMessage(), __METHOD__);
}
}
$fieldset->populate($foo);
$buttons = \Fieldset::forge('buttons', array('form_attributes' => array('class' => 'form-actions'), 'field_template' => '{field}'));
$buttons->add('submit', null, array('type' => 'submit', 'value' => 'Save Foo', 'class' => 'btn btn-primary'));
$buttons->add('cancel', null, array('type' => 'button', 'value' => 'Cancel', 'class' => 'btn', 'onclick' => "window.location.href ='/admin/example/foo'"));
$fieldset->add($buttons);
$this->template->set('content', $view);
}
}
<ul class="breadcrumb">
<li><a href="/admin/">Admin</a></li>
<li class="divider">/</li>
<li>Example</li>
<li class="divider">/</li>
<li><a href="/admin/example/foos/">Foos</a></li>
<li class="divider">/</li>
<li class="active">Delete Foo</li>
</ul>
<header class="page-header" style="overflow:hidden">
<h1 class="pull-left">Are you sure you want to delete this Foo?</h1>
</header>
<?= $fieldset ?>
<ul class="breadcrumb">
<li><a href="/admin/">Admin</a></li>
<li class="divider">/</li>
<li>Example</li>
<li class="divider">/</li>
<li><a href="/admin/example/foos/">Foos</a></li>
<li class="divider">/</li>
<li class="active"><?= $user->is_new() ? 'Create' : 'Edit' ?> Foo</li>
</ul>
<header class="page-header" style="overflow:hidden">
<h1 class="pull-left"><?= $foo->is_new() ? 'Create' : 'Edit' ?> Foo</h1>
</header>
<?= $fieldset->build() ?>
<ul class="breadcrumb">
<li><a href="/admin/">Admin</a></li>
<li class="divider">/</li>
<li>Example</li>
<li class="divider">/</li>
<li class="active">Foos</li>
</ul>
<header class="page-header" style="overflow:hidden">
<h1 class="pull-left">Manage Foos</h1>
<a href="/admin/example/foos/create" class="btn btn-primary pull-right">Create Foo</a>
</header>
<?= $datatable ?>
<ul class="breadcrumb">
<li><a href="/admin/">Admin</a></li>
<li class="divider">/</li>
<li>Example</li>
<li class="divider">/</li>
<li><a href="/admin/example/foos">Foos</a></li>
<li class="divider">/</li>
<li class="active">View Foo</li>
</ul>
<header class="page-header" style="overflow:hidden">
<h1 class="pull-left">View Foo</h1>
</header>
<table class="table table-bordered">
<tr>
<th>Data</th>
<td><?= $foo->data ?></td>
</tr>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment