/framework/core/db/DbZone.php

http://zoop.googlecode.com/ · PHP · 91 lines · 80 code · 11 blank · 0 comment · 17 complexity · 642b7bc77af88e088e4b81229447844e MD5 · raw file

  1. <?php
  2. class DbZone extends Object
  3. {
  4. private $class;
  5. private $fieldInfo = array();
  6. private $orderby;
  7. private $conditions;
  8. public function init($params)
  9. {
  10. $this->getMixinOwner()->setBaseDir('dbzone');
  11. $this->class = $params['class'];
  12. if(isset($params['orderby']))
  13. $this->orderby = $params['orderby'];
  14. else
  15. $this->orderby = 'id';
  16. if(isset($params['conditions']))
  17. $this->conditions = $params['conditions'];
  18. else
  19. $this->conditions = null;
  20. }
  21. public function setFieldType($field, $type)
  22. {
  23. $this->fieldInfo[$field]['type'] = $type;
  24. }
  25. public function pageDefault($p, $z)
  26. {
  27. $this->redirect('list');
  28. }
  29. public function pageList($p, $z)
  30. {
  31. $table = DbObject::_getTableSchema($this->class);
  32. $objects = DbObject::_find($this->class, $this->conditions, array('orderby' => $this->orderby . ', id'));
  33. $this->assign('table', $table);
  34. $this->assign('objects', $objects);
  35. }
  36. public function postList($p, $z)
  37. {
  38. $action = $_POST['action'];
  39. if($action == 'edit')
  40. $this->redirect('edit/' . $_POST['id']);
  41. else if($action == 'view')
  42. $this->redirect('view/' . $_POST['id']);
  43. else if($action == 'add')
  44. $this->redirect('edit');
  45. else if($action == 'delete')
  46. {
  47. $object = new $this->class($_POST['id']);
  48. $object->destroy();
  49. Redirect();
  50. }
  51. }
  52. public function pageEdit($p, $z)
  53. {
  54. if(isset($p[1]) && $p[1])
  55. $object = new $this->class($p[1]);
  56. else
  57. $object = new $this->class();
  58. $object->forceFields();
  59. $this->assign('object', $object);
  60. $this->assign('fieldInfo', $this->fieldInfo);
  61. }
  62. public function postEdit($p, $z)
  63. {
  64. if(isset($p[1]) && $p[1])
  65. $object = new $this->class($p[1]);
  66. else
  67. $object = new $this->class();
  68. foreach($_POST['fields'] as $field => $value)
  69. $object->$field = $value;
  70. $object->save();
  71. $this->redirect('list');
  72. }
  73. public function pageView($p, $z)
  74. {
  75. $object = new $this->class($p[1]);
  76. $object->forceFields();
  77. $this->assign('object', $object);
  78. }
  79. }
  80. AddTemplateDir(dirname(__file__) . '/templates');