PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/public/admin-console/application/controllers/base.php

https://gitlab.com/vince.omega/mcb-nov-build
PHP | 283 lines | 238 code | 37 blank | 8 comment | 36 complexity | 24df13cba87238630276ca34aee1b3d8 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class Base_Controller extends Controller {
  3. // the fields are to be overriden in descendants
  4. public $fields = array();
  5. public $objectName = '';
  6. public $title = '';
  7. protected $db;
  8. public function renderView(){
  9. $view = new View('admin');
  10. $view->header = new View('header');
  11. $view->content = new View( $this->objectName );
  12. $view->footer = new View('footer');
  13. $view->header->title = $this->title;
  14. $view->render(TRUE);
  15. }
  16. public function colModel(){
  17. $colModel = array();
  18. foreach($this->tableCols as $column){
  19. $col = "{label:'".$this->fields[$column]['title']."', name:'$column', align:'left'";
  20. //if (isset($this->fields[$column]['index'])) $col .= ", index:'".$this->fields[$column]['index']."'";
  21. if (isset($this->fields[$column]['width'])) $col .= ", width:'".$this->fields[$column]['width']."'";
  22. if (isset($this->fields[$column]['align'])) $col .= ", align:'".$this->fields[$column]['align']."'";
  23. if (isset($this->fields[$column]['stype'])) $col .= ", stype:'".$this->fields[$column]['stype']."'";
  24. if (isset($this->fields[$column]['options'])) $col .= ", editoptions: { value:".$this->getJSList( $this->fields[$column]['options'] )."}";
  25. $col .= "}\n";
  26. $colModel[] = $col;
  27. }
  28. return '['.join(',', $colModel).']';
  29. }
  30. public function index(){
  31. $this->msg = (isset($_GET['msg'])) ? $_GET['msg'] : false;
  32. $this->renderView();
  33. }
  34. public function getQuery(){
  35. $db = new Database();
  36. return $db->select('id,'.join(',', $this->tableCols))->from($this->objectName);
  37. }
  38. public function getWhere(){
  39. return array();
  40. }
  41. public function view(){
  42. $page = $_GET['page'];
  43. $limit = $_GET['rows'];
  44. $sidx = $_GET['sidx'];
  45. $sord = $_GET['sord'];
  46. $query = $this->getQuery();
  47. $where = $this->getWhere();
  48. if (isset($_GET['_search']) && $_GET['_search']=='true'){
  49. $fields = array_intersect(array_keys($this->fields), array_keys($_GET));
  50. foreach ($fields as $field){
  51. $index = (isset($this->fields[$field]['index'])) ? $this->fields[$field]['index'] : $field;
  52. $type = (isset($this->fields[$field]['type'])) ? $this->fields[$field]['type'] : 'text';
  53. switch ($type){
  54. case 'select':
  55. if ($_GET[$field] != 0){
  56. $where[] = $index . ' = "' . $_GET[$field] . '"';
  57. }
  58. break;
  59. case 'date':
  60. $where[] = 'FROM_UNIXTIME( '.$index.', "%m-%d-%Y" ) LIKE "' . $_GET[$field] . '%"';
  61. break;
  62. default:
  63. $where[] = $index . ' LIKE "' . $_GET[$field] . '%"';
  64. }
  65. }
  66. }
  67. if (count($where)) {
  68. $query->where( join(' AND ', $where) );
  69. }
  70. //$rows = $query->limit($limit)->offset(($page-1)*$limit)->orderby(array($sidx => $sord))->find_all();
  71. $rows = $query->limit($limit)->offset(($page-1)*$limit)->orderby(array($sidx => $sord))->get();
  72. //die($query->last_query());
  73. $count = $query->count_last_query();
  74. $responce->page = $page;
  75. $responce->total = ( $count >0 ) ? ceil($count/$limit) : 1;
  76. $responce->records = $count;
  77. $i = 0;
  78. foreach ($rows as $row) {
  79. $responce->rows[$i]['id']=$row->id;
  80. $ceil = array();
  81. foreach ($this->tableCols as $field){
  82. if (isset($this->fields[$field]['date']))
  83. $row->$field = date("m-d-Y", ($row->$field));
  84. if (isset($this->fields[$field]['price']))
  85. $row->$field = number_format($row->$field, 2);
  86. $ceil[] = $row->$field;
  87. }
  88. $responce->rows[$i]['cell'] = $ceil;
  89. $i++;
  90. }
  91. echo json_encode($responce);
  92. }
  93. public function getForm(){
  94. $out = '<form name="'.$this->objectName.'" id="'.$this->objectName.'" style="display:none;">
  95. <table border="0" width="100%">'."\n";
  96. $js = false;
  97. foreach ($this->fields as $name => $element){
  98. switch ($element['type']){
  99. case 'text':
  100. $html = '<input type="text" name="'.$name.'" id="'.$name.'">';
  101. break;
  102. case 'textarea':
  103. $html = '<textarea name="'.$name.'" id="'.$name.'"></textarea>';
  104. break;
  105. case 'select':
  106. $selection = $this->getList($element['options']);
  107. $html = form::dropdown($element['index'], $selection);
  108. break;
  109. case 'enum':
  110. $options = array();
  111. foreach ($element['enum']['options'] as $key => $value) {
  112. if (FALSE == is_int($key))
  113. $options[$key] = $value;
  114. else
  115. $options[ $value ] = ucfirst($value);
  116. }
  117. $default = FALSE;
  118. if (isset($element['enum']['default']))
  119. $default = $element['enum']['default'];
  120. $html = form::dropdown($name, $options, $default);
  121. break;
  122. case 'wysiwyg':
  123. $html = '<textarea class="ckeditor" cols="80" id="'.$name.'" name="'.$name.'" rows="10"></textarea>';
  124. $html .= "
  125. <script>
  126. $(function(){
  127. var oFCKeditor = new FCKeditor( '$name' ) ;
  128. oFCKeditor.Width = '100%' ;
  129. oFCKeditor.Height = '400' ;
  130. oFCKeditor.BasePath = '".url::base()."media/js/fckeditor/' ;
  131. oFCKeditor.ReplaceTextarea() ;
  132. fckEditors.push('$name');
  133. });
  134. </script>\n";
  135. break;
  136. case 'date':
  137. $html = '<input type="text" name="'.$name.'" id="'.$name.'">';
  138. $js .= "$('#$name').datepicker({dateFormat: 'yy-mm-dd'});\n";
  139. break;
  140. default:
  141. $html = false;
  142. }
  143. $required = (isset($element['required']) && $element['required']===true) ? '*' : '';
  144. $desc = (isset($element['description'])) ? '<br/><small>'.$element['description'].'</small>' : '';
  145. if ($html) $out .= "<tr><td>".$element['title']."$required $desc</td><td width=\"70%\">$html</td></tr>\n";
  146. }
  147. $out .= "</table>
  148. <input type=\"hidden\" name=\"id\" value=\"\" id=\"id\">
  149. <button onClick=\"submitForm(); return false;\">Save</button>
  150. <button onClick=\"toggleForm(); return false;\">Cancel</button>
  151. </form>\n";
  152. if ($js) $out .= "\n<script>\n$js\n</script>\n";
  153. return $out;
  154. }
  155. public function save(){
  156. $tableName = inflector::singular($this->objectName);
  157. $row = (isset($_POST['id']) && $_POST['id']!='') ? ORM::factory($tableName, (int)$_POST['id']) : ORM::factory($tableName);
  158. $errors = array();
  159. foreach ($this->fields as $name => $element){
  160. $error = false;
  161. if(isset($element['index'])) $name = $element['index'];
  162. // check if the field is required
  163. if (isset($element['required']) && $element['required']===true && (!isset($_POST[$name]) || $_POST[$name]=='')){
  164. $error = $element['title'].' required';
  165. }
  166. // check by mask
  167. if (!$error && $_POST[$name]!='' && isset($element['mask']) && !preg_match ( '/' . $element['mask'] . '/', $_POST[$name] )){
  168. $error = $element['title'].' has wrong format';
  169. }
  170. if (!$error){
  171. $row->$name = $_POST[$name];
  172. }else{
  173. $errors[] = $error;
  174. }
  175. }
  176. if (count($errors)>0){
  177. echo(join(', ', $errors));
  178. }else{
  179. try{
  180. // call custom method to let descandent classes do something when saving
  181. $this->customBeforeSave($row);
  182. $row->save();
  183. $this->customAfterSave($row);
  184. }catch (Exception $e){
  185. die($e->getMessage());
  186. }
  187. echo 'OK';
  188. }
  189. }
  190. public function edit(){
  191. if (!isset($_GET['id'])){
  192. throw new Exception('Id required');
  193. }
  194. $tableName = inflector::singular($this->objectName);
  195. $row = ORM::factory($tableName, (int)$_GET['id']);
  196. header('Content-type: application/json');
  197. echo json_encode($row->as_array());
  198. }
  199. public function delete(){
  200. if (!isset($_GET['id'])){
  201. throw new Exception('Id required');
  202. }
  203. $tableName = inflector::singular($this->objectName);
  204. $row = ORM::factory($tableName, (int)$_GET['id']);
  205. // call custom method to let descandent classes do something when deleting
  206. $this->customBeforeDelete($row);
  207. $row->delete();
  208. $this->customAfterDelete($row);
  209. url::redirect('/'.$this->objectName);
  210. }
  211. public function customBeforeSave(&$row){
  212. }
  213. public function customAfterSave(&$row){
  214. }
  215. public function customBeforeDelete($row){
  216. }
  217. public function customAfterDelete($row){
  218. }
  219. protected function getList($table, $index = 'id', $value = 'name'){
  220. if (is_array($table)){
  221. list($table, $index, $value) = $table;
  222. }
  223. $db = new Database();
  224. $query = $db->select($index.' AS id', $value.' AS value')->from($table)->orderby('id');
  225. $out = array();
  226. $rows = $query->get();
  227. foreach ($rows as $row){
  228. $out[ $row->id ] = $row->value;
  229. }
  230. return $out;
  231. }
  232. protected function getJSList($table, $index = 'id', $value = 'name', $where = false){
  233. if (is_array($table)){
  234. list($table, $index, $value) = $table;
  235. }
  236. $db = new Database();
  237. $query = $db->select($index.' AS id', $value.' AS value')->from($table);
  238. if ($where) $query->where($where);
  239. $out = array("0:'All'");
  240. $rows = $query->get();
  241. foreach ($rows as $row){
  242. $out[] = $row->id.":'".$row->value."'";
  243. }
  244. return '{'.join(',', $out).'}';
  245. }
  246. }