publiclib.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. spl_autoload_register(function ($class_name) {
  3. $path = explode('\\', $class_name);
  4. $fileName = $path[sizeof($path) - 1];
  5. $path[sizeof($path) - 1] = '';
  6. //$path = strtolower(DIR_ROOT. implode(DIRECTORY_SEPARATOR, $path));
  7. $path = DIR_ROOT . strtolower(implode(DIRECTORY_SEPARATOR, $path));
  8. if (is_file($path . $fileName . '.php')) {
  9. return include $path . $fileName . '.php';
  10. }
  11. if (is_file(strtolower($path . $fileName . '.php'))) {
  12. return include strtolower($path . $fileName . '.php');
  13. }
  14. @include $path . $fileName . '.php';
  15. });
  16. function is_ajax_request()
  17. {
  18. if (isset($_SERVER['HTTP_X_REQUESTED_WITH'])) {
  19. return true;
  20. } else if (isset($_SERVER['HTTP_REQUEST_TYPE']) && $_SERVER['HTTP_REQUEST_TYPE'] == 'fetch') {
  21. return true;
  22. } else {
  23. return false;
  24. }
  25. }
  26. global $_LANG;
  27. $_LANG = array();
  28. class Lang
  29. {
  30. public static function getStrings($module = '', $lang = null)
  31. {
  32. global $_LANG;
  33. if ($lang == null) {
  34. $lang = APP_LANG;
  35. }
  36. if (isset($_LANG[$lang][$module])) {
  37. return $_LANG[$lang][$module];
  38. }
  39. return $module;
  40. }
  41. public static function getString($string = '', $module = null, $lang = null)
  42. {
  43. global $_LANG;
  44. if ($lang == null) {
  45. $lang = APP_LANG;
  46. }
  47. if (isset($_LANG[$lang][$string]) && is_string($_LANG[$lang][$string])) {
  48. return $_LANG[$lang][$string];
  49. }
  50. if (isset($_LANG[$lang][$module][$string])) {
  51. return $_LANG[$lang][$module][$string];
  52. }
  53. return $string;
  54. }
  55. public static function buildStrings()
  56. {
  57. global $_LANG;
  58. if (isset($_SESSION['cache']['strings']) && CACHE_LANG) {
  59. $_LANG = $_SESSION['cache']['strings'];
  60. return;
  61. }
  62. $rdi = new RecursiveDirectoryIterator(DIR_APP);
  63. foreach (new RecursiveIteratorIterator($rdi) as $file) {
  64. if (preg_match("/lang\/.*\.php$/", $file)) {
  65. $included_lang = explode('/', $file);
  66. $included_lang = end($included_lang);
  67. $included_lang = str_replace('.php', '', $included_lang);
  68. $lang = array();
  69. include_once $file;
  70. $_LANG[$included_lang][array_keys($lang)[0]] = array_values($lang)[0];
  71. }
  72. }
  73. }
  74. }
  75. abstract class Singleton
  76. {
  77. private static $instance;
  78. private function __construct()
  79. {
  80. }
  81. private static function newObj()
  82. {
  83. if (!isset(self::$instance)) {
  84. $class = get_called_class();
  85. self::$instance = new $class;
  86. }
  87. return self::$instance;
  88. }
  89. public static function getInstance()
  90. {
  91. if (!isset(self::$instance)) {
  92. return self::newObj();
  93. }
  94. return self::$instance;
  95. }
  96. }
  97. use App\Core\Datatables\Datatables as Datatables;
  98. use ORM\DBInstance;
  99. abstract class DefaultController
  100. {
  101. protected $_class;
  102. protected $_baseUrl;
  103. function table()
  104. {
  105. if (isset($_GET['selective']) && $_GET['selective'] == 'true') {
  106. echo Datatables::getTable($this->_class, $this->_baseUrl);
  107. } else {
  108. echo Datatables::getTable($this->_class, $this->_baseUrl, false);
  109. }
  110. }
  111. function searchTable()
  112. {
  113. $records = [];
  114. $total = 0;
  115. try {
  116. if ($_POST['search']['value'] != '') {
  117. $search = [];
  118. foreach ($this->_class::_searchable as $searchable) {
  119. $search[$searchable] = ['OR like', $_POST['search']['value']];
  120. }
  121. $records = $this->_class::findMany($search, $this->_class::_listable)->get(true);
  122. } else {
  123. $records = $this->_class::findAll($this->_class::_listable, ['limit' => $_POST['length'], 'offset' => $_POST['start']])->get();
  124. }
  125. $total = DBInstance::queryOne("SELECT count(*) as count FROM {" . $this->_class::_tableName . '}')->count;
  126. } catch (Exception $e) {
  127. if (APP_STATUS != 'production') {
  128. for ($i = $_POST['start']; $i <= $_POST['length'] + $_POST['start']; $i++) {
  129. $line = new stdClass();
  130. $line->id = $i;
  131. $line->name = md5($i);
  132. $records[] = $line;
  133. }
  134. $total = 10000;
  135. }
  136. }
  137. if (!is_array($records)) {
  138. $records = [$records];
  139. }
  140. foreach ($records as $record) {
  141. if ($_GET['selective'] == 'true') {
  142. $record->id = Datatables::getActionMenu($record->id, $this->_baseUrl);
  143. } else {
  144. $record->id = Datatables::getSelectMenu($record->id);
  145. }
  146. }
  147. \RR\Response::json([
  148. 'draw' => $_POST['draw'],
  149. 'recordsTotal' => $total,
  150. 'recordsFiltered' => $total,
  151. 'data' => $records
  152. ])->send();
  153. }
  154. }
  155. /**
  156. * Request obtainers helpers
  157. */
  158. function request($param, $default = false)
  159. {
  160. if (is_string($param)) {
  161. return requestString($param, $default = false);
  162. }
  163. if (is_array($param)) {
  164. return requestArray($param, $default);
  165. }
  166. }
  167. function requestString($param, $default = false)
  168. {
  169. if (isset($_POST[$param])) {
  170. return $_POST[$param];
  171. }
  172. if (isset($_GET[$param])) {
  173. return $_GET[$param];
  174. }
  175. return $default;
  176. }
  177. function requestArray($params, $default = false) : array
  178. {
  179. $return = [];
  180. foreach ($params as $param) {
  181. $return[] = requestString($param, $default);
  182. }
  183. return $return;
  184. }
  185. /**
  186. * Development helpers
  187. */
  188. function dd($param, $die = true) : void
  189. {
  190. if ($param) {
  191. echo '<pre>';
  192. var_dump($param);
  193. }
  194. if ($die) {
  195. die;
  196. }
  197. }
  198. function ddd($param, $die = true) : void
  199. {
  200. if ($param) {
  201. error_log(print_r($param, true));
  202. }
  203. if ($die) {
  204. die;
  205. }
  206. }
  207. /**
  208. * Summary of icon
  209. * @return void
  210. */
  211. function icon($icon) : String
  212. {
  213. return "<i class='fa fa-$icon'></i>";
  214. }
  215. // Just a change for git