PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Weblab/Maestro/Controller/Maestro.php

https://bitbucket.org/schtr4jh/dummy
PHP | 484 lines | 391 code | 87 blank | 6 comment | 59 complexity | 98b7fd63773b3b74d34b896985ce4631 MD5 | raw file
  1. <?php
  2. namespace Weblab\Maestro\Controller;
  3. use LFW;
  4. use LFW\Controller;
  5. use LFW\Validate;
  6. use LFW\View\Twig AS View;
  7. use LFW\Router;
  8. use LFW\Assets;
  9. use LFW\Status;
  10. use LFW\Settings;
  11. use LFW\Helpers\JSON;
  12. use LFW\Helpers\Auth;
  13. use LFW\DB;
  14. use LFW\Lang;
  15. use LFW\Debug;
  16. use Weblab\Logs\Record\Log;
  17. class Maestro extends Controller {
  18. protected $view = null;
  19. protected $data = array();
  20. protected $extends = 'weblab/admin/views/admin.twig';
  21. function __construct() {
  22. parent::__construct();
  23. }
  24. function setView($view) {
  25. $this->view = $view;
  26. }
  27. function buildMaestro() {
  28. $view = new View($this->view);
  29. $view->addData($this->data);
  30. return $view;
  31. }
  32. // old
  33. function indexAction() {
  34. return new View("index");
  35. }
  36. function all($table, $conf = array()) {
  37. if (!isset($conf['ctrl'])) {
  38. $db = debug_backtrace();
  39. $spacename = explode("\\", get_class($db[1]['object']));
  40. $ctrl = strtolower(
  41. $spacename[count($spacename)-1] == $spacename[count($spacename)-3]
  42. ? $spacename[count($spacename)-3]
  43. : ($spacename[count($spacename)-3] . "/" . $spacename[count($spacename)-1])
  44. );
  45. } else {
  46. $ctrl = $conf['ctrl'];
  47. }
  48. $conf["url"]["sort"] = isset($conf["url"]["sort"])
  49. ? $conf["url"]["sort"]
  50. : ("/maestro/" . $ctrl . "/sort");
  51. $conf["url"]["add"] = isset($conf["url"]["add"])
  52. ? $conf["url"]["add"]
  53. : ("/maestro/" . $ctrl . "/add");
  54. $conf["url"]["edit"] = isset($conf["url"]["edit"])
  55. ? $conf["url"]["edit"]
  56. : ("/maestro/" . $ctrl . "/edit");
  57. $conf["url"]["view"] = isset($conf["url"]["view"])
  58. ? $conf["url"]["view"]
  59. : ("/maestro/" . $ctrl . "/view");
  60. $conf["url"]["delete"] = isset($conf["url"]["delete"])
  61. ? $conf["url"]["delete"]
  62. : ("/maestro/" . $ctrl . "/delete");
  63. $conf["rowbtn"] = isset($conf["rowbtn"])
  64. ? $conf["rowbtn"]
  65. : array("edit", "delete", "view");
  66. $conf["headbtn"] = isset($conf["headbtn"])
  67. ? $conf["headbtn"]
  68. : array("add");
  69. $finalTable = array();
  70. foreach ($table AS $i => $row) {
  71. $temp = array();
  72. if (!is_array($row)){
  73. $rowArr = $row->__toArray();
  74. foreach ($rowArr AS $key => $val) {
  75. if (substr($key, -3) == "_id" && in_array($key, array_keys($conf['heading']))) {
  76. $v = $row->{"get".\LFW\Core::toCamel(substr(ucfirst($key), 0, -3))}();
  77. $rowArr[$key] = $v ? $v->__getMain() : $v;
  78. }
  79. }
  80. } else {
  81. $rowArr = $row;
  82. }
  83. foreach ($conf["heading"] AS $hkey => $hval) {
  84. $temp[$hkey] = $rowArr[$hkey];
  85. }
  86. if (is_array($row) && isset($row["rowbtn"]))
  87. $temp["rowbtn"] = $row["rowbtn"];
  88. $finalTable[] = $temp;
  89. }
  90. return new View("all", array(
  91. "ext" => array_key_exists("extends", $conf) ? $conf["extends"] : false,
  92. "data" => $finalTable,
  93. "conf" => $conf,
  94. "breadcrumbs" => array(
  95. array(
  96. "title" => "Maestro",
  97. "href" => "/maestro",
  98. ),
  99. array(
  100. "title" => \LFW\Core::toCamel($ctrl),
  101. ),
  102. ),
  103. ));
  104. }
  105. function allForeign($table, $conf = array()) {
  106. if (!isset($conf['ctrl'])) {
  107. $db = debug_backtrace();
  108. $spacename = explode("\\", get_class($db[1]['object']));
  109. $ctrl = strtolower(
  110. $spacename[count($spacename)-1] == $spacename[count($spacename)-3]
  111. ? $spacename[count($spacename)-3]
  112. : ($spacename[count($spacename)-3] . "/" . $spacename[count($spacename)-1])
  113. );
  114. } else {
  115. $ctrl = $conf['ctrl'];
  116. }
  117. $conf["url"]["sort"] = isset($conf["url"]["sort"])
  118. ? $conf["url"]["sort"]
  119. : ("/maestro/" . $ctrl . "/sort");
  120. $conf["url"]["add"] = isset($conf["url"]["add"])
  121. ? $conf["url"]["add"]
  122. : ("/maestro/" . $ctrl . "/add");
  123. $conf["url"]["edit"] = isset($conf["url"]["edit"])
  124. ? $conf["url"]["edit"]
  125. : ("/maestro/" . $ctrl . "/edit");
  126. $conf["url"]["delete"] = isset($conf["url"]["delete"])
  127. ? $conf["url"]["delete"]
  128. : ("/maestro/" . $ctrl . "/delete");
  129. $conf["rowbtn"] = isset($conf["rowbtn"])
  130. ? $conf["rowbtn"]
  131. : array("edit", "delete");
  132. $conf["headbtn"] = isset($conf["headbtn"])
  133. ? $conf["headbtn"]
  134. : array("add");
  135. $finalTable = array();
  136. foreach ($table AS $i => $row) {
  137. $temp = array();
  138. if (!is_array($row)){
  139. $rowArr = $row->__toArray();
  140. foreach ($rowArr AS $key => $val) {
  141. if (substr($key, -3) == "_id" && in_array($key, array_keys($conf['heading']))) {
  142. $v = $row->{"get".\LFW\Core::toCamel(substr(ucfirst($key), 0, -3))}();
  143. $rowArr[$key] = $v ? $v->__getMain() : $v;
  144. }
  145. }
  146. } else {
  147. $rowArr = $row;
  148. }
  149. foreach ($conf["heading"] AS $hkey => $hval) {
  150. $temp[$hkey] = $rowArr[$hkey];
  151. }
  152. if (is_array($row) && isset($row["rowbtn"]))
  153. $temp["rowbtn"] = $row["rowbtn"];
  154. $finalTable[] = $temp;
  155. }
  156. return new View("all_foreign", array(
  157. "ext" => array_key_exists("extends", $conf) ? $conf["extends"] : false,
  158. "data" => $finalTable,
  159. "conf" => $conf,
  160. "breadcrumbs" => array(
  161. array(
  162. "title" => "Maestro",
  163. "href" => "/maestro",
  164. ),
  165. array(
  166. "title" => \LFW\Core::toCamel($ctrl),
  167. ),
  168. ),
  169. ));
  170. }
  171. function sort($table, $conf = array()) {
  172. $ctrl = end(explode("\\", Router::get("controller")));
  173. foreach ($table AS $i => $row) {
  174. $temp = array();
  175. foreach ($conf["heading"] AS $hkey => $hval) {
  176. $temp[$hkey] = $row[$hkey];
  177. }
  178. $table[$i] = $temp;
  179. }
  180. return new View("sort", array(
  181. "ext" => isset($conf["extends"]) ? $conf["extends"] : false,
  182. "data" => $table,
  183. "conf" => $conf,
  184. "breadcrumbs" => array(
  185. array(
  186. "title" => "Maestro",
  187. "href" => "/maestro",
  188. ),
  189. array(
  190. "title" => \LFW\Core::toCamel($ctrl),
  191. "href" => "/maestro/" . $ctrl,
  192. ),
  193. array(
  194. "title" => __("sort"),
  195. ),
  196. ),
  197. ));
  198. }
  199. function updatesort($data, $model, $pos = "position") {
  200. foreach ($data AS $position => $id) {
  201. $sql = "UPDATE " . $model->getTable() . " " .
  202. "SET " . $pos . " = " . ((int)$position + 1) . " " .
  203. "WHERE id = " . ((int)$id);
  204. DB::query($sql);
  205. }
  206. return JSON::to(array(
  207. "success" => TRUE,
  208. ));
  209. }
  210. function add($conf = array()) {
  211. $ctrl = substr(strrchr(Router::get("controller"), "\\"), 1);
  212. $conf["url"]["insert"] = "/dev.php" . (isset($conf["url"]["insert"])
  213. ? $conf["url"]["insert"]
  214. : ("/maestro/" . $ctrl . "/insert"));
  215. return new View("add", array(
  216. "ext" => isset($conf["extends"]) ? $conf["extends"] : false,
  217. "conf" => $conf,
  218. "breadcrumbs" => array(
  219. array(
  220. "title" => "Maestro",
  221. "href" => "/maestro",
  222. ),
  223. array(
  224. "title" => \LFW\Core::toCamel($ctrl),
  225. "href" => "/maestro/" . $ctrl,
  226. ),
  227. array(
  228. "title" => __("add"),
  229. ),
  230. ),
  231. ));
  232. }
  233. function i18nAddPrepare($i18n) {
  234. $arri18n = array();
  235. $lang = array(1 => "Slo", 2 => "Eng", 3 => "Ita");
  236. foreach ($lang AS $langId => $langTitle) {
  237. $obj = new $i18n;
  238. $obj->forceSet(array("lang_id" => $langId, "_tablang" => array("id" => $langId, "title" => $langTitle)));
  239. $arri18n[] = $obj;
  240. }
  241. return $arri18n;
  242. }
  243. function i18nAdd($conf) {
  244. return new View("i18nadd", array(
  245. "conf" => $conf,
  246. ));
  247. }
  248. function insert($record, $plural, $redirect = TRUE, $callback = NULL) {
  249. if (!$record || ($validate = $record->validate()) && ($insert = $record->insert())) {
  250. if ($redirect)
  251. Debug::addSuccess(__("alert_success_insert"));
  252. //$log = new Log;
  253. //$log->set(array("content" => "User " . Auth::getUser()->getEmail() . " created " . $plural . "#" . $record->id()));
  254. //$log->insert();
  255. }
  256. if ($redirect && !$validate)
  257. Debug::addError(__("alert_error_insert") . " (validate)");
  258. else if ($redirect && !$insert)
  259. Debug::addError(__("alert_error_insert") . " (insert)");
  260. else if (is_callable($callback))
  261. $callback($record);
  262. if (isset($_POST['ajax']))
  263. return JSON::to(array(
  264. "success" => !!$insert,
  265. ));
  266. if ($redirect)
  267. Status::redirect("/maestro/" . $plural . ($insert ? "/edit/" . $insert->id() : "/add"));
  268. return $insert;
  269. }
  270. function edit($conf = array()) {
  271. $ctrl = substr(strrchr(Router::get("controller"), "\\"), 1);
  272. $conf["url"]["update"] = isset($conf["url"]["update"])
  273. ? $conf["url"]["update"]
  274. : ("/maestro/" . $ctrl . "/update");
  275. return new View("edit", array(
  276. "ext" => isset($conf["extends"]) ? $conf["extends"] : false,
  277. "conf" => $conf,
  278. "breadcrumbs" => array(
  279. array(
  280. "title" => "Maestro",
  281. "href" => "/maestro",
  282. ),
  283. array(
  284. "title" => \LFW\Core::toCamel($ctrl),
  285. "href" => "/maestro/" . $ctrl,
  286. ),
  287. array(
  288. "title" => __("edit"),
  289. ),
  290. ),
  291. ));
  292. }
  293. function i18nEditPrepare($parentEntity, $record, $entity) {
  294. $arri18n = array();
  295. $lang = array(1 => "Slo", 2 => "Eng", 3 => "Ita");
  296. foreach ($lang AS $langId => $langTitle) {
  297. $one = $entity->where(array("lang_id" => $langId, $parentEntity->getI18nForeign() => $record->id()))->findOne();
  298. if ($one)
  299. $arri18n[] = $one->forceSet(array("_tablang" => array("id" => $langId, "title" => $langTitle)));
  300. }
  301. return $arri18n;
  302. }
  303. function i18nEdit($conf) {
  304. return new View("i18nedit", array(
  305. "conf" => $conf,
  306. ));
  307. }
  308. function update($record, $plural, $redirect = TRUE, $callback = NULL) {
  309. if (!$record || !Validate::isInt($record->getID())) {
  310. Debug::addWarning(__("invalid_id") . " " . $record->getID());
  311. $log = new Log(array("content" => "User " . Auth::getUser()->getEmail() . " failed editing " . $plural . "#" . $record->getId() . " (invalid ID)"));
  312. $log->insert();
  313. if ($redirect) {
  314. Status::redirect(-1);
  315. }
  316. } else if (($validate = $record->validate()) && ($update = $record->update())) {
  317. Debug::addSuccess(__("alert_success_update"));
  318. //$log = new Log(array("content" => "User " . Auth::getUser()->getEmail() . " edited " . $plural . "#" . $record->getId()));
  319. //$log->insert();
  320. }
  321. if (!$validate)
  322. Debug::addError(__("alert_error_update") . " " . __("check") . $model->getValidateErrorsHTML());
  323. else if ($update === false)
  324. Debug::addError("Update? " . print_r(DB::inst()->errorInfo(), true));
  325. else if (is_callable($callback))
  326. $callback($record);
  327. if (isset($_POST['ajax']))
  328. return JSON::to(array(
  329. "success" => !!$update,
  330. ));
  331. if ($redirect)
  332. Status::redirect("/maestro/" . $plural . "/edit/" . $record->getId());
  333. return $update;
  334. }
  335. function delete($record, $plural, $redirect = FALSE, $callback = NULL, $precall = NULL) {
  336. if (is_callable($precall))
  337. $precall($record);
  338. if (!$record || !Validate::isInt($record->getId())) {
  339. Debug::addWarning(__("invalid_id") . print_r($record, TRUE));
  340. Status::redirect(-1);
  341. } else if ($delete = $record->delete()) {
  342. if ($redirect)
  343. Debug::addSuccess(__("alert_success_delete"));
  344. }
  345. if (!$delete) {
  346. if ($redirect)
  347. Debug::addError(__("alert_error_delete"));
  348. } else if (is_callable($callback))
  349. $callback($record);
  350. if ($this->getRequest()->isAjax())
  351. return JSON::to(array(
  352. "success" => !!$delete,
  353. ));
  354. if ($redirect)
  355. Status::redirect("/maestro/" . $plural);
  356. return $delete;
  357. }
  358. function view($row, $conf = array()) {
  359. $data = array(
  360. "ext" => isset($conf["extends"]) ? $conf["extends"] : false,
  361. "data" => $row,
  362. "conf" => $conf,
  363. "breadcrumbs" => array(
  364. array(
  365. "title" => "Maestro",
  366. "href" => "/maestro",
  367. ),
  368. ),
  369. );
  370. $allControllers = array();
  371. $explodeController = explode("\\", Router::get("controller"));
  372. foreach (explode(":", end($explodeController)) AS $controller) {
  373. $allControllers[] = $controller;
  374. $data["breadcrumbs"][] = array(
  375. "title" => \LFW\Core::toCamel($controller),
  376. "href" => "/maestro/" . strtolower(implode("/", $allControllers)),
  377. );
  378. }
  379. $data["breadcrumbs"][] = array(
  380. "title" => __("view"),
  381. );
  382. return new View("view", $data);
  383. }
  384. function custom($content) {
  385. return new View("custom", $content);
  386. }
  387. function block($conf = array()) {
  388. $ctrl = end(explode("\\", Router::get("controller")));
  389. $conf["url"] = isset($conf["url"])
  390. ? $conf["url"]
  391. : "#";
  392. return new View("block", array(
  393. "conf" => $conf,
  394. ));
  395. }
  396. }
  397. ?>