PageRenderTime 21ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/core/Option/Option.php

https://gitlab.com/vanthanhhoh/devlovebook
PHP | 307 lines | 283 code | 22 blank | 2 comment | 74 complexity | 00d2aa83ef28129c5f46075e7c2ec8e5 MD5 | raw file
  1. <?php
  2. class Option {
  3. const OPTION_TABLE = 'options';
  4. static $Options = array();
  5. function __construct() {
  6. }
  7. static function getOptionsOfType($type = 'global', $extra = '') {
  8. //if(isset(self::$Options[$type])) return self::$Options[$type];
  9. if($type != '') {
  10. $getOptions = DB::Query(self::OPTION_TABLE);
  11. $getOptions = $getOptions->Where('type', '=', $type);
  12. if(!empty($extra))
  13. $getOptions = $getOptions->_AND()->Where('extra', '=', $extra);
  14. $getOptions = $getOptions->Get('name');
  15. }
  16. else
  17. $getOptions = DB::Query(self::OPTION_TABLE)->Get('name');
  18. if($getOptions->status && $getOptions->num_rows > 0)
  19. self::$Options = array_merge(self::$Options, $getOptions->Result);
  20. return $getOptions->Result;
  21. }
  22. static function getFull() {
  23. return self::$Options;
  24. }
  25. static function getPairs() {
  26. $pairs = array();
  27. foreach(self::$Options as $opt)
  28. $pairs[$opt['name']] = $opt['value'];
  29. return $pairs;
  30. }
  31. static function get($name, $type = 'global', $extra = '') {
  32. if(empty(self::$Options)) {
  33. self::getOptionsOfType();
  34. if(!empty($type) && $type != 'global')
  35. self::getOptionsOfType($type, $extra);
  36. }
  37. if(isset(self::$Options[$name]))
  38. return self::$Options[$name]['value'];
  39. else {
  40. $getOption = DB::Query(self::OPTION_TABLE)->Where('name', '=', $name)->Get();
  41. if($getOption->status && $getOption->num_rows > 0) {
  42. self::$Options[$name] = $getOption->Result[0];
  43. return $getOption->Result[0]['value'];
  44. }
  45. else return false;
  46. }
  47. }
  48. static function add($name = '', $value = '', $title = '', $type = '', $extra = '') {
  49. if(empty($name)) {
  50. Error::Set('Option name không thể để trống');
  51. return false;
  52. }
  53. $addOpts = DB::Query(self::OPTION_TABLE)
  54. ->Insert(
  55. array( 'name' => $name,
  56. 'value' => $value,
  57. 'title' => $title,
  58. 'type' => $type,
  59. 'extra' => $extra)
  60. );
  61. if($addOpts->status && $addOpts->insert_id) return true;
  62. else {
  63. Error::Set('Cannot add option ' . $name);
  64. return false;
  65. }
  66. }
  67. static function set($name = '', $value = '', $title = '', $type = '', $extra = '', $status = 1) {
  68. if(is_array($name))
  69. $name = array_merge(
  70. array( 'name' => '',
  71. 'value' => '',
  72. 'title' => '',
  73. 'type' => '',
  74. 'extra' => '',
  75. 'status'=> ''
  76. ),
  77. $name
  78. );
  79. else
  80. $name = array(
  81. 'name' => $name,
  82. 'value' => $value,
  83. 'title' => $title,
  84. 'type' => $type,
  85. 'extra' => $extra,
  86. 'status'=> $status
  87. );
  88. if(empty($name) || (isset($name['name']) && empty($name['name']))) {
  89. Error::Set('Option name không thể để trống');
  90. return false;
  91. }
  92. $setOpts = DB::Query(self::OPTION_TABLE)
  93. ->Replace($name);
  94. if($setOpts->status) return true;
  95. else {
  96. Error::Set('Cannot add option ' . $name);
  97. return false;
  98. }
  99. }
  100. static function update($name = '', $data = array()) {
  101. if(empty($name)) {
  102. Error::Set('Option name không thể để trống');
  103. return false;
  104. }
  105. $delOpts = DB::Query(self::OPTION_TABLE)->Where('name', '=', $name)->Update($data);
  106. if($delOpts->status) return true;
  107. else {
  108. Error::Set('Cannot update option ' . $name);
  109. return false;
  110. }
  111. }
  112. static function delete($name = '') {
  113. if(empty($name)) {
  114. Error::Set('Option name không thể để trống');
  115. return false;
  116. }
  117. $delOpts = DB::Query(self::OPTION_TABLE)->Where('name', '=', $name)->Delete();
  118. if($delOpts->status) return true;
  119. else {
  120. Error::Set('Cannot delete option ' . $name);
  121. return false;
  122. }
  123. }
  124. static function formFromConfig($formName, $fields = array(), $hook = array(), $rebuild = false) {
  125. $hook = array_merge(array(
  126. 'form_action' => '',
  127. 'form_name' => '',
  128. 'form_class' => '',
  129. 'form_id' => '',
  130. 'form_before' => '',
  131. 'form_after' => ''
  132. ), $hook);
  133. $return = array('submitedValues' => '', 'form' => '');
  134. if(Input::Post('saveOptions') == 1) {
  135. //$return['submitedValues'] = Input::Post('Field');
  136. $__fs = Input::Post('Field');
  137. foreach($__fs as $_fk => $_fv) {
  138. if($fields[$_fk]['type'] == 'referer' && in_array($fields[$_fk]['display'], array('multi_selectbox', 'checkbox')))
  139. $return['submitedValues'][$_fk] = implode(',', array_filter($_fv));
  140. elseif($fields[$_fk]['type'] == 'attributes')
  141. $return['submitedValues'][$_fk] = serialize(array_filter($_fv));
  142. elseif($fields[$_fk]['type'] == 'multi_image')
  143. $return['submitedValues'][$_fk] = implode(',', array_filter($_fv));
  144. elseif($fields[$_fk]['type'] == 'tags')
  145. $return['submitedValues'][$_fk] = implode(',', array_filter($_fv));
  146. elseif($fields[$_fk]['type'] == 'list')
  147. $return['submitedValues'][$_fk] = implode(',', array_filter($_fv));
  148. elseif($fields[$_fk]['type'] == 'multi_value')
  149. $return['submitedValues'][$_fk] = implode(',', array_filter($_fv));
  150. else $return['submitedValues'][$_fk] = $_fv;
  151. $fields[$_fk]['value'] = $return['submitedValues'][$_fk];
  152. }
  153. }
  154. Theme::AddCssComponent('Forms,InputGroups');
  155. $FormElements = array(
  156. 'text' => 'input',
  157. 'number' => 'input',
  158. 'textarea' => 'textarea',
  159. 'html' => 'html',
  160. 'image' => 'image',
  161. 'multi_image' => 'multi_image',
  162. 'attributes' => 'attributes',
  163. 'tags' => 'tags',
  164. 'list' => 'list_select',
  165. 'meta_description' => 'textarea'
  166. );
  167. $AdapterFunctions = array();
  168. $FormVariables = $PrepareOptions = array();
  169. $Sortable = $TagsTextText = '';
  170. $Form = new Form($formName, $rebuild);
  171. $Form->SetArrayVar('Field');
  172. foreach($fields as $fname => $Field) {
  173. if(empty($fname) || empty($Field['label'])) continue;
  174. if(!isset($Field['require'])) $Field['require'] = false;
  175. $FieldTemplate = isset($FormElements[$Field['type']]) ? $FormElements[$Field['type']] : 'input';
  176. $FieldObj = $Form->$FieldTemplate($fname)
  177. ->Label($Field['label'])
  178. ->Value($Field['value'])
  179. ->Required($Field['require'])
  180. ->FieldClass('FieldType_' . $Field['type'] . ' Field_' . $fname);
  181. if(in_array($Field['type'], array('number', 'text', 'file'))) {
  182. $FieldObj->Type($Field['type']);
  183. }
  184. if(in_array($Field['type'], array('single_value', 'multi_value'))) {
  185. if(!isset($Field['display'])) $Field['display'] = 'single_selectbox';
  186. if($Field['display'] == 'single_selectbox') $T = 'select';
  187. if($Field['display'] == 'multi_selectbox') $T = 'multi_select';
  188. if($Field['display'] == 'radio') $T = 'radio';
  189. if($Field['display'] == 'checkbox') $T = 'checkbox';
  190. foreach($Field['options'] as $fk => $fv) {
  191. $Field['options'][] = array('value' => $fk, 'text' => $fv);
  192. unset($Field['options'][$fk]);
  193. }
  194. if(!$Field['require']) array_unshift($Field['options'], array('text' => 'Select', 'value' => ''));
  195. $FieldObj = $Form->$T($fname)
  196. ->Label($Field['label'])
  197. ->Value($Field['value'])
  198. ->FieldClass('FieldType_' . $Field['type'] . ' Field_' . $fname)
  199. ;
  200. $FieldObj->Options($Field['options'])->StaticOptions(true);
  201. }
  202. if($Field['type'] == 'referer') {
  203. if(!isset($Field['display'])) $Field['display'] = 'single_selectbox';
  204. $T = $Field['display'];
  205. if($Field['display'] == 'single_selectbox') $T = 'select';
  206. if($Field['display'] == 'multi_selectbox') $T = 'multi_select';
  207. $FieldObj = $Form->$T($fname)
  208. ->Label($Field['label'])
  209. ->Value($Field['value'])
  210. ->FieldClass('FieldType_' . $Field['type'] . ' Field_' . $fname);
  211. $RefererTable = $Field['referer']['node_type'];
  212. $DisplayField = $Field['referer']['node_field'];
  213. $AdapterFunctionName = 'vnp_' . $DisplayField . '_' . $RefererTable;
  214. if(!in_array($AdapterFunctionName, $AdapterFunctions)) {
  215. $AdapterFunctions[] = $AdapterFunctionName;
  216. $PrepareOptions[] = '$' . $AdapterFunctionName . ' = NodeBase::getNodes_Option(\'' . $RefererTable . '\', \'' . $DisplayField . '\', \'' . $RefererTable . '_id\');';
  217. }
  218. $PrepareOptions[] = '$Fields[\'' . $fname . '\'][\'Options\'] = $' . $AdapterFunctionName .';';
  219. $FieldObj->Options(array());
  220. $FieldObj->StaticOptions(false);
  221. }
  222. if($Field['type'] == 'list') {
  223. $RefererTable = $Field['referer']['node_type'];
  224. $DisplayField = $Field['referer']['node_field'];
  225. $FieldObj->AddVar(array('ref_table' => $RefererTable, 'ref_field' => $DisplayField));
  226. }
  227. if($Field['type'] == 'html')
  228. $HtmlEditor[] = 'Editor::AddEditor(\'#ID_Field_' . $fname . '\');';
  229. if($Field['type'] == 'attributes')
  230. $Sortable = 'Theme::JqueryUI(\'sortable\');';
  231. if($Field['type'] == 'tags') {
  232. $TagsTextText = 'Theme::JqueryUI(\'autocomplete\');' . PHP_EOL;
  233. $TagsTextText .= "\t\t" . 'Theme::JqueryPlugin(\'TagsInput\');' . PHP_EOL;
  234. $TagsTextText .= "\t\t" . 'Theme::CssHeader(\'jquery-ui-autocomplete\', GLOBAL_DATA_DIR . \'library/jquery-ui/jquery-ui-1.10.4.autocomplete.css\');' . PHP_EOL;
  235. $TagsTextText .= "\t\t" . 'Theme::CssHeader(\'jquery-plugin-TagsInput\', GLOBAL_DATA_DIR . \'library/jquery-plugins/TagsInput/jquery.tagsinput.css\');' . PHP_EOL;
  236. $TagsTextText .= "\t\t" . '$TagsIniStr = \'$(\\\'.VNP_TagsField\\\').tagsInput({
  237. autocomplete_url: function(request, response) {
  238. console.log(request.term);
  239. VNP.Ajax({
  240. type: \\\'post\\\',
  241. url: \\\'Node/GetTags\\\',
  242. dataType: "jsonp",
  243. data: {
  244. TagQ: request.term
  245. },
  246. success: function( data ) {
  247. response( data );
  248. },
  249. error: function( data ) {
  250. console.log(data);
  251. },
  252. complete: function() {
  253. VNP.Loader.hide();
  254. }
  255. }, \\\'json\\\');
  256. }
  257. });\';' . PHP_EOL;
  258. $TagsTextText .= "\t\t" . 'Theme::JsFooter(\'IniTagsObject-\', $TagsIniStr, \'inline\');';
  259. }
  260. $Form->AddFormElement($FieldObj);
  261. }
  262. if(!empty($HtmlEditor)) {
  263. $PrepareOptions[] = 'Boot::Library(\'Editor\');';
  264. $PrepareOptions = array_merge($PrepareOptions, $HtmlEditor);
  265. $PrepareOptions[] = 'Editor::Replace();';
  266. }
  267. $PrepareOptions[] = $Sortable;
  268. $PrepareOptions[] = $TagsTextText;
  269. $returnString = '<?php ' . implode(PHP_EOL, $PrepareOptions) . '?>
  270. <form action="' . $hook['form_action'] . '" class="' . $hook['form_class'] . '" id="' . $hook['form_id'] . '" method="post">
  271. <input type="hidden" name="saveOptions" value="1" />' .
  272. $hook['form_before'] .
  273. $Form->Render() .
  274. $hook['form_after'] . '
  275. <div style="text-align:center"><input type="submit" value="Lưu lại" class="btn btn-primary" /></div>
  276. </form>';
  277. $optionFile = CACHE_PATH . 'compiled' . DIRECTORY_SEPARATOR . $formName . '.php';
  278. if($rebuild) File::Create($optionFile, $returnString);
  279. ob_start();
  280. $Fields = $fields;
  281. include($optionFile);
  282. $return['form'] = ob_get_clean();
  283. return $return;
  284. }
  285. }