--- /dev/null
+<?php
+
+interface IPF_Admin_ListFilter
+{
+ function title();
+ function setParams($params);
+ function render($extraParams);
+}
+
+abstract class IPF_Admin_Component
+{
+ public $perPage = 50;
+ public $app = null, $request = null;
+
+ public function slug()
+ {
+ $slug = get_class($this);
+ $slug = preg_replace('/^Admin([A-Z])/', '\1', $slug);
+ $slug = preg_replace('/Admin$/', '', $slug);
+ $slug = preg_replace('/([a-z0-9])([A-Z0-9])/', '\1-\2', $slug);
+ $slug = preg_replace('/\W+/', '-', $slug);
+ return strtolower($slug);
+ }
+
+ public abstract function verbose_name();
+ public function titleList() { return $this->verbose_name().' List'; }
+ public function titleAdd() { return 'Add ' . $this->verbose_name(); }
+ public function titleEdit() { return 'Edit ' . $this->verbose_name(); }
+ public function titleDelete() { return 'Delete ' . $this->verbose_name(); }
+
+ public function getPerms($request)
+ {
+ return array('view', 'add', 'change', 'delete');
+ }
+
+ public function isAccessible($what, $request=null)
+ {
+ if (!$request)
+ $request = $this->request;
+ if (!$request)
+ throw new IPF_Exception('No request.');
+ return IPF_Admin_App::isAccessible($request, $this, $what);
+ }
+
+ protected function context($request)
+ {
+ return array();
+ }
+
+ protected function renderToResponse($template, $context, $request)
+ {
+ $context = array_merge(
+ array(
+ 'component' => $this,
+ 'app' => $this->app,
+ ),
+ $this->context($request),
+ $context);
+ return IPF_Admin_App::RenderToResponse($template, $context, $request);
+ }
+
+ protected function _setupEditForm($form)
+ {
+ $this->_setupForm($form);
+ }
+
+ protected function _setupAddForm($form)
+ {
+ $this->_setupForm($form);
+ }
+
+ protected function _setupForm($form)
+ {
+ }
+
+ public abstract function list_display();
+
+ public function list_display_links()
+ {
+ return null;
+ }
+
+ protected function columnTitle($column)
+ {
+ return IPF_Utils::humanTitle($column);
+ }
+
+ public abstract function renderCell($object, $column);
+
+ protected abstract function getItems($searchValue, $filters, $page, $pageSize);
+ protected abstract function itemsCount($searchValue, $filters);
+ public abstract function getObjectByID($id);
+ public abstract function saveObject($form, $object);
+ public abstract function deleteObject($object);
+
+ public function searcheable()
+ {
+ return false;
+ }
+
+ protected abstract function _getForm($obj, $data);
+
+ protected function _getEditForm($obj, $data)
+ {
+ return $this->_getForm($obj, $data);
+ }
+
+ protected function _getAddForm($obj, $data)
+ {
+ return $this->_getForm($obj, $data);
+ }
+
+ protected function _getListTemplate()
+ {
+ return 'admin/items.html';
+ }
+
+ protected function _getAddTemplate()
+ {
+ return 'admin/change.html';
+ }
+
+ protected function _getChangeTemplate()
+ {
+ return 'admin/change.html';
+ }
+
+ protected function extraMedia($form)
+ {
+ return array(
+ 'js' => $form->extra_js(),
+ 'css' => array(),
+ );
+ }
+
+ protected function objectTools($obj)
+ {
+ return array();
+ }
+
+ // Views Function
+ public function AddItem($request)
+ {
+ $errors = false;
+ if ($request->method == 'POST') {
+ $form = $this->_getAddForm(null, $request->POST + $request->FILES);
+ $this->_setupAddForm($form);
+
+ if ($form->isValid()) {
+ list($id, $object) = $this->saveObject($form, null);
+
+ IPF_Admin_Log::logObject($this, 'add', $object, $id);
+
+ $url = @$request->POST['ipf_referrer'];
+ if (!$url)
+ $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($this->app->slug(), $this->slug()));
+ return new IPF_HTTP_Response_Redirect($url);
+ }
+ $errors = true;
+ } else {
+ $form = $this->_getAddForm(null, null);
+ $this->_setupAddForm($form);
+ }
+
+ $extraMedia = $this->extraMedia($form);
+
+ $context = array(
+ 'mode'=>'add',
+ 'page_title'=>$this->titleAdd(),
+ 'classname'=>$this->verbose_name(),
+ 'form' => $form,
+ 'form_html' => $form->renderLayout(new IPF_Admin_Form_Layout),
+ 'extra_js' => $extraMedia['js'],
+ 'extra_css' => $extraMedia['css'],
+ 'errors' => $errors,
+ 'objecttools' => array(),
+ );
+ return $this->renderToResponse($this->_getAddTemplate(), $context, $request);
+ }
+
+ public function DeleteItem($request, $id)
+ {
+ $object = $this->getObjectByID($id);
+ if (!$object)
+ throw new IPF_HTTP_Error404;
+
+ if ($request->method == 'POST') {
+ IPF_Admin_Log::logObject($this, 'delete', $object);
+
+ $this->deleteObject($object);
+
+ $url = @$request->POST['ipf_referrer'];
+ if (!$url)
+ $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($this->app->slug(), $this->slug()));
+ return new IPF_HTTP_Response_Redirect($url);
+ }
+
+ $context = array(
+ 'page_title' => $this->titleDelete(),
+ 'classname' => $this->verbose_name(),
+ 'object' => $object,
+ 'object_id' => $id,
+ 'ipf_referrer' => @$request->GET['ipf_referrer'],
+ );
+ return $this->renderToResponse('admin/delete.html', $context, $request);
+ }
+
+ public function EditItem($request, $id)
+ {
+ $object = $this->getObjectByID($id);
+ if (!$object)
+ throw new IPF_HTTP_Error404;
+
+ $errors = false;
+ if ($request->method == 'POST') {
+ $form = $this->_getEditForm($object, $request->POST + $request->FILES);
+ $this->_setupEditForm($form);
+
+ if ($form->isValid()) {
+ list($id, $object) = $this->saveObject($form, $object);
+
+ IPF_Admin_Log::logObject($this, 'change', $object, $id);
+
+ $url = @$request->POST['ipf_referrer'];
+ if (!$url)
+ $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($this->app->slug(), $this->slug()));
+
+ return new IPF_HTTP_Response_Redirect($url);
+ }
+ $errors = true;
+ } else {
+ $form = $this->_getEditForm($object, null);
+ $this->_setupEditForm($form);
+ }
+
+ $objecttools = $this->objectTools($object);
+ $extraMedia = $this->extraMedia($form);
+
+ $context = array(
+ 'mode'=>'change',
+ 'page_title'=>$this->titleEdit(),
+ 'classname'=>$this->verbose_name(),
+ 'object' => $object,
+ 'object_id' => $id,
+ 'form' => $form,
+ 'form_html' => $form->renderLayout(new IPF_Admin_Form_Layout),
+ 'extra_js' => $extraMedia['js'],
+ 'extra_css' => $extraMedia['css'],
+ 'errors' => $errors,
+ 'objecttools' => $objecttools,
+ );
+ return $this->renderToResponse($this->_getChangeTemplate(), $context, $request);
+ }
+
+ public function ListItems($request)
+ {
+ $searchValue = @$request->GET['q'];
+
+ $filters = $this->listFilters();
+ foreach ($filters as $f)
+ $f->setParams($request->GET);
+
+ $currentPage = (int)@$request->GET['page'];
+ if (!$currentPage)
+ $currentPage = 1;
+
+ $count = $this->itemsCount($searchValue, $filters);
+ $objects = $this->getItems($searchValue, $filters, $currentPage, $this->perPage);
+
+
+ $header = array();
+ foreach ($this->list_display() as $name) {
+ if (is_array($name)) {
+ list($name, $title) = $name;
+ } else {
+ $title = $this->columnTitle($name);
+ }
+
+ $header[] = array(
+ 'title' => $title,
+ 'name' => $name,
+ );
+ }
+
+ $links_display = $this->list_display_links();
+ if (!$links_display && count($header))
+ $links_display = array($header[0]['name']);
+
+ $rows = array();
+ foreach ($objects as $id => $o) {
+ $cells = array();
+ foreach ($header as $h) {
+ $listMethod = 'column_'.$h['name'];
+ if (method_exists($this,$listMethod)) {
+ $cell = $this->$listMethod($o);
+ } else {
+ $cell = $this->renderCell($o, $h['name']);
+ }
+
+ if (array_search($h['name'], $links_display) !== false)
+ $cell = '<a href="'.$id.'/">'.$cell.'</a>';
+
+ $cells[] = $cell;
+ }
+ $rows[] = array(
+ 'cells' => $cells,
+ 'object' => $o,
+ 'object_id' => $id,
+ );
+ }
+
+ $pagerLayout = new IPF_Pager_Layout;
+ $pages = $pagerLayout->layout($currentPage, ceil($count / $this->perPage));
+
+ $context = array(
+ 'orderable'=>$this->_orderable(),
+ 'page_title'=>$this->titleList(),
+ 'header' => $header,
+ 'rows' => $rows,
+ 'count' => $count,
+ 'pages' => $pages,
+ 'current_page' => $currentPage,
+ 'classname'=>$this->verbose_name(),
+ 'title_add'=>$this->titleAdd(),
+ 'filters' => $filters,
+ 'is_search' => $this->searcheable(),
+ 'search_value' => $searchValue,
+ );
+ return $this->renderToResponse($this->_getListTemplate(), $context, $request);
+ }
+
+ public function listFilters()
+ {
+ return array();
+ }
+
+ public function _orderable()
+ {
+ return false;
+ }
+
+ public function reorder($ids)
+ {
+ throw new IPF_Exception('Reordering is not implemented.');
+ }
+}
+
+++ /dev/null
-<?php
-
-interface IPF_Admin_ListFilter
-{
- function title();
- function setParams($params);
- function render($extraParams);
-}
-
-abstract class IPF_Admin_Component
-{
- public $perPage = 50;
- public $app = null, $request = null;
-
- public function slug()
- {
- $slug = get_class($this);
- $slug = preg_replace('/^Admin([A-Z])/', '\1', $slug);
- $slug = preg_replace('/Admin$/', '', $slug);
- $slug = preg_replace('/([a-z0-9])([A-Z0-9])/', '\1-\2', $slug);
- $slug = preg_replace('/\W+/', '-', $slug);
- return strtolower($slug);
- }
-
- public abstract function verbose_name();
- public function titleList() { return $this->verbose_name().' List'; }
- public function titleAdd() { return 'Add ' . $this->verbose_name(); }
- public function titleEdit() { return 'Edit ' . $this->verbose_name(); }
- public function titleDelete() { return 'Delete ' . $this->verbose_name(); }
-
- public function getPerms($request)
- {
- return array('view', 'add', 'change', 'delete');
- }
-
- public function isAccessible($what, $request=null)
- {
- if (!$request)
- $request = $this->request;
- if (!$request)
- throw new IPF_Exception('No request.');
- return IPF_Admin_App::isAccessible($request, $this, $what);
- }
-
- protected function context($request)
- {
- return array();
- }
-
- protected function renderToResponse($template, $context, $request)
- {
- $context = array_merge(
- array(
- 'component' => $this,
- 'app' => $this->app,
- ),
- $this->context($request),
- $context);
- return IPF_Admin_App::RenderToResponse($template, $context, $request);
- }
-
- protected function _setupEditForm($form)
- {
- $this->_setupForm($form);
- }
-
- protected function _setupAddForm($form)
- {
- $this->_setupForm($form);
- }
-
- protected function _setupForm($form)
- {
- }
-
- public abstract function list_display();
-
- public function list_display_links()
- {
- return null;
- }
-
- protected function columnTitle($column)
- {
- return IPF_Utils::humanTitle($column);
- }
-
- public abstract function renderCell($object, $column);
-
- protected abstract function getItems($searchValue, $filters, $page, $pageSize);
- protected abstract function itemsCount($searchValue, $filters);
- public abstract function getObjectByID($id);
- public abstract function saveObject($form, $object);
- public abstract function deleteObject($object);
-
- public function searcheable()
- {
- return false;
- }
-
- protected abstract function _getForm($obj, $data);
-
- protected function _getEditForm($obj, $data)
- {
- return $this->_getForm($obj, $data);
- }
-
- protected function _getAddForm($obj, $data)
- {
- return $this->_getForm($obj, $data);
- }
-
- protected function _getListTemplate()
- {
- return 'admin/items.html';
- }
-
- protected function _getAddTemplate()
- {
- return 'admin/change.html';
- }
-
- protected function _getChangeTemplate()
- {
- return 'admin/change.html';
- }
-
- protected function extraMedia($form)
- {
- return array(
- 'js' => $form->extra_js(),
- 'css' => array(),
- );
- }
-
- protected function objectTools($obj)
- {
- return array();
- }
-
- // Views Function
- public function AddItem($request)
- {
- $errors = false;
- if ($request->method == 'POST') {
- $form = $this->_getAddForm(null, $request->POST + $request->FILES);
- $this->_setupAddForm($form);
-
- if ($form->isValid()) {
- list($id, $object) = $this->saveObject($form, null);
-
- IPF_Admin_Log::logObject($this, 'add', $object, $id);
-
- $url = @$request->POST['ipf_referrer'];
- if (!$url)
- $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($this->app->slug(), $this->slug()));
- return new IPF_HTTP_Response_Redirect($url);
- }
- $errors = true;
- } else {
- $form = $this->_getAddForm(null, null);
- $this->_setupAddForm($form);
- }
-
- $extraMedia = $this->extraMedia($form);
-
- $context = array(
- 'mode'=>'add',
- 'page_title'=>$this->titleAdd(),
- 'classname'=>$this->verbose_name(),
- 'form' => $form,
- 'form_html' => $form->renderLayout(new IPF_Admin_Form_Layout),
- 'extra_js' => $extraMedia['js'],
- 'extra_css' => $extraMedia['css'],
- 'errors' => $errors,
- 'objecttools' => array(),
- );
- return $this->renderToResponse($this->_getAddTemplate(), $context, $request);
- }
-
- public function DeleteItem($request, $id)
- {
- $object = $this->getObjectByID($id);
- if (!$object)
- throw new IPF_HTTP_Error404;
-
- if ($request->method == 'POST') {
- IPF_Admin_Log::logObject($this, 'delete', $object);
-
- $this->deleteObject($object);
-
- $url = @$request->POST['ipf_referrer'];
- if (!$url)
- $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($this->app->slug(), $this->slug()));
- return new IPF_HTTP_Response_Redirect($url);
- }
-
- $context = array(
- 'page_title' => $this->titleDelete(),
- 'classname' => $this->verbose_name(),
- 'object' => $object,
- 'object_id' => $id,
- 'ipf_referrer' => @$request->GET['ipf_referrer'],
- );
- return $this->renderToResponse('admin/delete.html', $context, $request);
- }
-
- public function EditItem($request, $id)
- {
- $object = $this->getObjectByID($id);
- if (!$object)
- throw new IPF_HTTP_Error404;
-
- $errors = false;
- if ($request->method == 'POST') {
- $form = $this->_getEditForm($object, $request->POST + $request->FILES);
- $this->_setupEditForm($form);
-
- if ($form->isValid()) {
- list($id, $object) = $this->saveObject($form, $object);
-
- IPF_Admin_Log::logObject($this, 'change', $object, $id);
-
- $url = @$request->POST['ipf_referrer'];
- if (!$url)
- $url = IPF_HTTP_URL::urlForView('IPF_Admin_Views_ListItems', array($this->app->slug(), $this->slug()));
-
- return new IPF_HTTP_Response_Redirect($url);
- }
- $errors = true;
- } else {
- $form = $this->_getEditForm($object, null);
- $this->_setupEditForm($form);
- }
-
- $objecttools = $this->objectTools($object);
- $extraMedia = $this->extraMedia($form);
-
- $context = array(
- 'mode'=>'change',
- 'page_title'=>$this->titleEdit(),
- 'classname'=>$this->verbose_name(),
- 'object' => $object,
- 'object_id' => $id,
- 'form' => $form,
- 'form_html' => $form->renderLayout(new IPF_Admin_Form_Layout),
- 'extra_js' => $extraMedia['js'],
- 'extra_css' => $extraMedia['css'],
- 'errors' => $errors,
- 'objecttools' => $objecttools,
- );
- return $this->renderToResponse($this->_getChangeTemplate(), $context, $request);
- }
-
- public function ListItems($request)
- {
- $searchValue = @$request->GET['q'];
-
- $filters = $this->listFilters();
- foreach ($filters as $f)
- $f->setParams($request->GET);
-
- $currentPage = (int)@$request->GET['page'];
- if (!$currentPage)
- $currentPage = 1;
-
- $count = $this->itemsCount($searchValue, $filters);
- $objects = $this->getItems($searchValue, $filters, $currentPage, $this->perPage);
-
-
- $header = array();
- foreach ($this->list_display() as $name) {
- if (is_array($name)) {
- list($name, $title) = $name;
- } else {
- $title = $this->columnTitle($name);
- }
-
- $header[] = array(
- 'title' => $title,
- 'name' => $name,
- );
- }
-
- $links_display = $this->list_display_links();
- if (!$links_display && count($header))
- $links_display = array($header[0]['name']);
-
- $rows = array();
- foreach ($objects as $id => $o) {
- $cells = array();
- foreach ($header as $h) {
- $listMethod = 'column_'.$h['name'];
- if (method_exists($this,$listMethod)) {
- $cell = $this->$listMethod($o);
- } else {
- $cell = $this->renderCell($o, $h['name']);
- }
-
- if (array_search($h['name'], $links_display) !== false)
- $cell = '<a href="'.$id.'/">'.$cell.'</a>';
-
- $cells[] = $cell;
- }
- $rows[] = array(
- 'cells' => $cells,
- 'object' => $o,
- 'object_id' => $id,
- );
- }
-
- $pagerLayout = new IPF_Pager_Layout;
- $pages = $pagerLayout->layout($currentPage, ceil($count / $this->perPage));
-
- $context = array(
- 'orderable'=>$this->_orderable(),
- 'page_title'=>$this->titleList(),
- 'header' => $header,
- 'rows' => $rows,
- 'count' => $count,
- 'pages' => $pages,
- 'current_page' => $currentPage,
- 'classname'=>$this->verbose_name(),
- 'title_add'=>$this->titleAdd(),
- 'filters' => $filters,
- 'is_search' => $this->searcheable(),
- 'search_value' => $searchValue,
- );
- return $this->renderToResponse($this->_getListTemplate(), $context, $request);
- }
-
- public function listFilters()
- {
- return array();
- }
-
- public function _orderable()
- {
- return false;
- }
-
- public function reorder($ids)
- {
- throw new IPF_Exception('Reordering is not implemented.');
- }
-}
-