InputService.class.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace Home\Service;
  3. /**
  4. * InputService
  5. */
  6. class InputService extends CommonService {
  7. /**
  8. * 添加input
  9. * @param array input
  10. * @return
  11. */
  12. public function add($input) {
  13. $Input = $this->getD();
  14. $input = $Input->create($input);
  15. $ret = $Input->add($input);
  16. if (false === $ret) {
  17. return $this->resultReturn(false);
  18. }
  19. return $this->resultReturn(true);
  20. }
  21. /**
  22. * 更新input
  23. * @param array $input
  24. * @return array
  25. */
  26. public function update($input) {
  27. if (!D('Field', 'Service')->existField($input['field_id'])) {
  28. return $this->resultReturn(false);
  29. }
  30. $Input = $this->getD();
  31. if (!$this->existInput($input['id'])) {
  32. return $this->resultReturn(false);
  33. }
  34. // 更新html
  35. $old = $Input->getById($input['id']);
  36. $field = M('Field')->getById($input['field_id']);
  37. if ($input['type'] != $old['type']
  38. || $input['width'] != $old['width']
  39. || $input['height'] != $old['height']
  40. || $input['value'] != $old['value']
  41. || $input['opt_value'] != $old['opt_value']
  42. || $input['editor'] != $old['editor']
  43. || false === strpos($input['html'], $field['name'])) {
  44. $field['model'] = $this->getInputModelName($field['model_id']);
  45. D('Input', 'Logic')->genHtml($input, $field);
  46. }
  47. $input = $Input->create($input);
  48. if (false === $Input->save($input)) {
  49. return $this->resultReturn(false);
  50. }
  51. return $this->resultReturn(true);
  52. }
  53. /**
  54. * 检查表单域是否可用
  55. * @param array $input Input数组
  56. * @param int $id 需要更新input的id
  57. * @return mixed
  58. */
  59. public function checkInput($input, $id) {
  60. $Input = $this->getD();
  61. if ($Input->isValid($input, $id)) {
  62. return $this->resultReturn(true);
  63. }
  64. return $this->errorResultReturn($Input->getError());
  65. }
  66. /**
  67. * 创建input
  68. * @param array $input
  69. * @param array $field
  70. * @return array
  71. */
  72. public function create(&$input, $field) {
  73. $inputLogic = D('Input', 'Logic');
  74. // 处理表单域长度
  75. $inputLogic->genSize($input);
  76. // 生成表单域html
  77. $field['model'] = $this->getInputModelName($field['model_id']);
  78. if (!isset($input['html']) || '' == $input['html']) {
  79. $inputLogic->genHtml($input, $field);
  80. }
  81. return $this->getD()->create($input);
  82. }
  83. /**
  84. * input是否存在
  85. * @param int $id
  86. * @return boolean
  87. */
  88. public function existInput($id) {
  89. if ($this->getM()->where("id = {$id}")->count() > 0) {
  90. return true;
  91. }
  92. return false;
  93. }
  94. /**
  95. * 是否file表单域
  96. * @param string $type 类型
  97. * @return boolean
  98. */
  99. public function isFileInput($type) {
  100. if ('file' == $type) {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /**
  106. * 是否checkbox
  107. * @param string $type 类型
  108. * @return boolean
  109. */
  110. public function isCheckbox($type) {
  111. if ('checkbox' == $type) {
  112. return true;
  113. }
  114. return false;
  115. }
  116. /**
  117. * 得到添加表单域
  118. * @param string $tblName 模型数据表
  119. * @return array
  120. */
  121. public function getAddInputsByTblName($tblName) {
  122. $model = M('Model')->getByTblName($tblName);
  123. // 得到模型对应的非系统字段
  124. $where = array(
  125. 'model_id' => $model['id'],
  126. 'is_system' => 0,
  127. );
  128. $fields = M('Field')->where($where)->select();
  129. // 得到字段对应的表单域
  130. $inputs = array();
  131. $orders = array();
  132. foreach ($fields as $key => $field) {
  133. $input = M('Input')->getByFieldId($field['id']);
  134. if ($input['is_show']) {
  135. $inputs[$key] = $input;
  136. $orders[$key] = $inputs[$key]['show_order'];
  137. }
  138. }
  139. // 排序表单域
  140. array_multisort($orders, $inputs);
  141. return $inputs;
  142. }
  143. /**
  144. * 得到带值编辑的表单域
  145. * @param string $tblName 模型数据表
  146. * @param array $data 表单域值
  147. * @return array
  148. */
  149. public function getEditInputsByTblName($tblName, $data) {
  150. $types = array('checkbox', 'select', 'radio', 'relation_select');
  151. $inputs = $this->getAddInputsByTblName($tblName);
  152. $model = M('Model')->getByTblName($tblName);
  153. // 生成带有默认值的表单域
  154. foreach ($inputs as $key => $input) {
  155. $field = M('Field')->getById($input['field_id']);
  156. $field['model'] = $this->getInputModelName($model['id']);
  157. if (in_array($input['type'], $types)) {
  158. // 处理opt_value中的默认项
  159. $i = 0;
  160. $inputLogic = D('Input', 'Logic');
  161. $opts = $inputLogic->optValueToArray($input['opt_value']);
  162. foreach ($opts['opt_value'] as $opt) {
  163. if ($opt == $data[$field['name']]) {
  164. $opts['selected'] = $i;
  165. break ;
  166. }
  167. $i += 1;
  168. }
  169. if ('checkbox' == $input['type']) {
  170. $selected = explode(',', $data[$field['name']]);
  171. $opts['selected'] = '';
  172. foreach ($selected as $item) {
  173. $pos = array_pos($opts['opt_value'], $item);
  174. if (false !== $pos) {
  175. $opts['selected'] .= "{$pos},";
  176. }
  177. }
  178. $opts = $inputLogic->optArrayToString($opts, true);
  179. } else {
  180. $opts = $inputLogic->optArrayToString($opts);
  181. }
  182. // 已得到realtion_select的可选项
  183. if ('relation_select' == $input['type']) {
  184. $inputs[$key]['type'] = 'select';
  185. }
  186. $inputs[$key]['opt_value'] = $opts;
  187. } else {
  188. $value = strip_sql_injection($data[$field['name']]);
  189. $inputs[$key]['value'] = $value;
  190. }
  191. // 生成带值的表单域
  192. D('Input', 'Logic')->genHtml($inputs[$key], $field);
  193. }
  194. return $inputs;
  195. }
  196. /**
  197. * 更新关联表单域
  198. * @param array $data 更新的数据
  199. * @param int $modelId 模型id
  200. * @return
  201. */
  202. public function updateRalationInput(array $data, $modelId) {
  203. $fieldService = D('Field', 'Service');
  204. $inputLogic = D('Input', 'Logic');
  205. $fields = $fieldService->getByModelId($modelId);
  206. $updKeys = array_keys($data);
  207. $updFields = array();
  208. foreach ($fields as $field) {
  209. if (!in_array($field['name'], $updKeys)) {
  210. continue ;
  211. }
  212. $rFields=$fieldService->getRelatedFields($modelId, $field['name']);
  213. foreach ($rFields as $rField) {
  214. $updFields[] = $rField;
  215. }
  216. }
  217. foreach ($updFields as $updField) {
  218. $opts = $inputLogic->getRelationOpts($updField);
  219. $fn = $this->getInputModelName($updField['model_id'])
  220. . "[$updField[name]]";
  221. $udpInput = array(
  222. 'opt_value' => $inputLogic->optArrayToString($opts),
  223. 'html' => genSelect($fn, $opts['opt_value'])
  224. );
  225. // update
  226. $this->getM()
  227. ->where("id={$updField['input']['id']}")
  228. ->save($udpInput);
  229. }
  230. return ;
  231. }
  232. /**
  233. * 得到模型小写作为表单域的name
  234. * @param int $modelId 模型的id
  235. * @return string
  236. */
  237. protected function getInputModelName($modelId) {
  238. $model = M('Model')->field('tbl_name')->getById($modelId);
  239. $ctrlName = D('Model', 'Service')->getCtrlName($model['tbl_name']);
  240. return strtolower($ctrlName);
  241. }
  242. protected function getModelName() {
  243. return 'Input';
  244. }
  245. }