Pages.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. /**
  3. * @author Giancarlo Voci
  4. * @date 04/12/2018
  5. * @time 15:22
  6. */
  7. namespace OpenMind\Admin;
  8. use Base;
  9. use Exception;
  10. use OpenMind\FindBox\FindBoxCoreSystem;
  11. use OpenMind\FindBox\SmartESL;
  12. use OpenMind\System\Notifications;
  13. use OpenMind\System\User;
  14. use OpenMind\Web\Controller;
  15. use Template;
  16. class Pages extends Controller
  17. {
  18. private $findBoxCore = null;
  19. /**
  20. * Pages constructor.
  21. * @param Base $f3
  22. */
  23. public function __construct($f3)
  24. {
  25. parent::__construct($f3);
  26. try {
  27. $this->findBoxCore = new FindBoxCoreSystem(...array_values($f3->get('CORE.fb')));
  28. } catch (Exception $e) {
  29. $ns = new Notifications();
  30. $ns->notify(Notifications::FB_ADMIN, $f3->clean($e->getMessage()));
  31. // IF can't connect to FINDBOX, reroute to OMLINK Page Settings to fast change settings
  32. if (!$f3->get('AJAX') && ($f3->get('PATTERN') != '/admin/omlsettings'))
  33. $f3->reroute('/admin/omlsettings');
  34. }
  35. // Set Admin F3 UI folder
  36. $f3->set('UI', 'ui/admin/');
  37. }
  38. public function Admin($f3)
  39. {
  40. echo Template::instance()->render('masterpage.tpl');
  41. }
  42. public function ESLStatus(Base $f3)
  43. {
  44. $eslStatus = $this->findBoxCore->GetAllESLStatus();
  45. $f3->set('STATUS_BADGE_COLOR', [
  46. 'OFFLINE' => 'warning',
  47. 'BLOCKED,FC' => 'danger',
  48. 'NO_STATUS' => 'light',
  49. 'OK' => 'success'
  50. ]);
  51. $f3->set('BATLEV_ICONS', [
  52. 'GOOD' => 'full', // 100% - 60%
  53. 'LOW' => 'half', // 60% - 30%
  54. 'BAD' => 'empty', // 30% - 0%
  55. ]);
  56. $f3->set('ESL_STATUS', $eslStatus);
  57. $f3->set('PAGENAME', 'Dashboard');
  58. $f3->set('CONTENT', 'labelstatus.tpl');
  59. echo Template::instance()->render('masterpage.tpl');
  60. }
  61. public function OML_Settings(Base $f3)
  62. {
  63. global $frameworkUtils;
  64. $path = 'cfg/config.bin';
  65. if (!file_exists($path))
  66. throw new Exception('Config file not found');
  67. $data = file_get_contents($path);
  68. $config = $frameworkUtils->Decrypt($data);
  69. $f3->set('PAGENAME', 'OM-Link Settings');
  70. $f3->set('CONTENT', 'omlsettings.tpl');
  71. $f3->set('CONFIG_TEXT', $config);
  72. echo Template::instance()->render('masterpage.tpl');
  73. }
  74. public function Config(Base $f3)
  75. {
  76. global $frameworkUtils;
  77. $config = $f3->get('BODY');
  78. if (strlen($config) == 0) {
  79. echo json_encode([
  80. "err" => "Empty body command"
  81. ]);
  82. return;
  83. }
  84. $configOBJ = json_decode($config);
  85. switch ($configOBJ->method) {
  86. case 'saveconfig':
  87. $newConfig = $frameworkUtils->Encrypt($configOBJ->value);
  88. @rename('cfg/config.bin', 'cfg/config.bin.old');
  89. @file_put_contents('cfg/config.bin', $newConfig);
  90. echo json_encode([
  91. "err" => "Configuration saved"
  92. ]);
  93. break;
  94. case 'loadconfig':
  95. $config = $frameworkUtils->Decrypt(file_get_contents('cfg/config.bin'));
  96. echo json_encode([
  97. "err" => "Configuration reloaded successfully",
  98. "value" => $config
  99. ]);
  100. break;
  101. case 'geteslstatus':
  102. $labelID = $configOBJ->label_id;
  103. $esl = new SmartESL($this->findBoxCore, [$labelID]);
  104. $status = $esl->GetStatus(true);
  105. $svgPath = $this->findBoxCore->GetTemplateImage($status[$configOBJ->label_id]['data']['template_id']);
  106. echo json_encode([
  107. "err" => "Status reloaded successfully",
  108. "value" => $status[$configOBJ->label_id],
  109. "svgPath" => $svgPath
  110. ]);
  111. break;
  112. case 'blinklabels':
  113. $esl = new SmartESL($this->findBoxCore, [$configOBJ->labelid]);
  114. $ret = $esl->BlinkSpecial(2, 1, 1000, 5, 0);
  115. if ($ret['err'] == 0 && $ret['value']['code'] == 0) {
  116. if ($ret['value']['value'][$configOBJ->labelid])
  117. echo json_encode([
  118. "err" => 0,
  119. "value" => "{$configOBJ->labelid} blink successfull"
  120. ]);
  121. else
  122. echo json_encode([
  123. "err" => 1,
  124. "value" => "{$configOBJ->labelid} blink error"
  125. ]);
  126. }
  127. break;
  128. case 'unbind':
  129. $ret = $this->findBoxCore->UnBind($configOBJ->labelid);
  130. if ($ret['err'] != 0) {
  131. echo json_encode($ret);
  132. return;
  133. }
  134. echo json_encode([
  135. "err" => 0,
  136. "value" => "{$configOBJ->labelid} unbind successfull"
  137. ]);
  138. break;
  139. case 'resetfailcounter':
  140. $URI = "/1.0.0/index.php/smartesl.resetfailcounter/1.0.0/[[\"{$configOBJ->labelid}\"]]";
  141. $this->findBoxCore->CallAPI($URI, "");
  142. echo json_encode([
  143. "err" => 0,
  144. "value" => "{$configOBJ->labelid} Reset Fail Counter successfull"
  145. ]);
  146. break;
  147. case 'TemplateSelector':
  148. $this->TemplateSelector($f3, $configOBJ);
  149. break;
  150. case 'SetDisplay':
  151. foreach($configOBJ->labels as $labelID) {
  152. $esl = new SmartESL($this->findBoxCore, [$labelID]);
  153. $esl->Display($configOBJ->id, $labelID);
  154. }
  155. echo json_encode([
  156. "err" => "Display command successfull"
  157. ]);
  158. break;
  159. }
  160. return;
  161. }
  162. private function TemplateSelector(Base $f3, $Command)
  163. {
  164. global $frameworkUtils;
  165. $fb_template = $this->findBoxCore->CallAPI('/1.0.0/index.php/smartesl.gettemplates/1.0.0/', '[]');
  166. $templates = $this->findBoxCore->getLabelsFromResponse($fb_template);
  167. if ($Command->clearCache == 1)
  168. $frameworkUtils->rrmdir("fbtpl");
  169. $tplFileNames = [];
  170. if ($templates != null) {
  171. foreach($templates as $item) {
  172. if (!in_array($item['resolution'], $Command->resolution))
  173. continue;
  174. if (!file_exists("fbtpl/{$item['template_id']}.svg"))
  175. $this->findBoxCore->GetTemplateImage($item['template_id']);
  176. $tplFileNames[] = [
  177. "id" => $item['template_id'],
  178. "name" => $item['name'],
  179. "resolution" => $item['resolution'],
  180. "title" => $item['name'],
  181. "svg" => file_get_contents("fbtpl/{$item['template_id']}.svg")
  182. ];
  183. }
  184. }
  185. $f3->set('TPLOBJ', $tplFileNames);
  186. $tpl = Template::instance()->render('templates.tpl');
  187. echo json_encode([
  188. "err" => 0,
  189. "value" => $tpl
  190. ]);
  191. }
  192. public function Users(Base $f3)
  193. {
  194. $listaUtenti = new User('');
  195. $f3->set('USERLIST', $listaUtenti->find());
  196. $f3->set('PAGENAME', 'User Management');
  197. $f3->set('CONTENT', 'users.tpl');
  198. echo Template::instance()->render('masterpage.tpl');
  199. }
  200. }