Manageredit.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecadmin\block\config;
  10. use Yii;
  11. use fec\helpers\CRequest;
  12. use fec\helpers\CUrl;
  13. use fec\helpers\CModel;
  14. use fecadmin\helpers\CConfig;
  15. use fecadmin\models\AdminConfig;
  16. /**
  17. * @author Terry Zhao <2358269014@qq.com>
  18. * @since 1.0
  19. */
  20. class Manageredit{
  21. public $_param;
  22. public $_paramKey;
  23. public $_one;
  24. public function __construct(){
  25. $this->_paramKey = 'id';
  26. }
  27. # 初始化数据
  28. public function initParam(){
  29. $val = $this->_param[$this->_paramKey];
  30. if($val){
  31. $this->_one = AdminConfig::find()->where([$this->_paramKey => $val])->one();
  32. }else{
  33. $this->_one = new AdminConfig;
  34. }
  35. //$obj = $this->_obj;
  36. }
  37. # 传递给前端的数据 显示编辑form
  38. public function getLastData(){
  39. $request_param = CRequest::param();
  40. $this->_param = $request_param;
  41. $this->initParam();
  42. return [
  43. 'editBar' => $this->getEditBar(),
  44. 'saveUrl' => CUrl::getUrl('fecadmin/config/managereditsave'),
  45. ];
  46. }
  47. # 保存
  48. public function save(){
  49. $request_param = CRequest::param();
  50. $this->_param = $request_param['editFormData'];
  51. $this->initParam();
  52. $model = $this->_one;
  53. $model->attributes = $this->_param;
  54. if($model[$this->_paramKey]){
  55. if ($model->validate()) {
  56. CConfig::setCacheConfig($model['key'],$model['value']);
  57. $model->save();
  58. echo json_encode(array(
  59. "statusCode"=>"200",
  60. "message"=>"update success",
  61. ));
  62. exit;
  63. }
  64. }else{
  65. if ($model->validate()) {
  66. CConfig::setCacheConfig($model['key'],$model['value']);
  67. $model->save();
  68. echo json_encode(array(
  69. "statusCode"=>"200",
  70. "message"=>"insert success",
  71. ));
  72. exit;
  73. }
  74. }
  75. $errors = $model->errors;
  76. echo json_encode(["statusCode"=>"300",
  77. "message" => CModel::getErrorStr($errors),
  78. ]);
  79. exit;
  80. }
  81. # 批量删除
  82. public function delete(){
  83. //$request_param = CRequest::param();
  84. //$this->_param = $request_param;
  85. //$this->initParam();
  86. # admin 用户不能删除
  87. if($id = CRequest::param('id')){
  88. $model = AdminConfig::findOne(['id' => $id]);
  89. if($model->id){
  90. $model->delete();
  91. echo json_encode(["statusCode"=>"200",
  92. "message" => 'Delete Success!',
  93. ]);
  94. exit;
  95. }else{
  96. echo json_encode(["statusCode"=>"300",
  97. "message" => "id => $id , is not exist",
  98. ]);
  99. exit;
  100. }
  101. }else if($ids = CRequest::param('ids')){
  102. $id_arr = explode(",",$ids);
  103. AdminConfig::deleteAll(['in','id',$id_arr]);
  104. echo json_encode(["statusCode"=>"200",
  105. "message" => "$ids Delete Success!",
  106. ]);
  107. exit;
  108. }
  109. echo json_encode(["statusCode"=>"300",
  110. "message" => "id or ids Param is not Exist!",
  111. ]);
  112. exit;
  113. }
  114. public function getEditArr(){
  115. return [
  116. [
  117. 'label'=>'Label',
  118. 'name'=>'label',
  119. 'display'=>[
  120. 'type' => 'inputString',
  121. ],
  122. 'require' => 1,
  123. ],
  124. [
  125. 'label'=>'KEY',
  126. 'name'=>'key',
  127. 'display'=>[
  128. 'type' => 'inputString',
  129. ],
  130. 'require' => 1,
  131. ],
  132. [
  133. 'label'=>'Value',
  134. 'name'=>'value',
  135. 'display'=>[
  136. 'type' => 'inputString',
  137. ],
  138. 'require' => 1,
  139. ],
  140. [
  141. 'label'=>'描述',
  142. 'name'=>'description',
  143. 'display'=>[
  144. 'type' => 'inputString',
  145. ],
  146. 'require' => 0,
  147. ],
  148. ];
  149. }
  150. public function getEditBar(){
  151. $editArr = $this->getEditArr();
  152. $str = '';
  153. if($this->_param[$this->_paramKey]){
  154. $str = '<input type="hidden" value="'.$this->_param[$this->_paramKey].'" size="30" name="editFormData['.$this->_paramKey.']" class="textInput ">';
  155. }
  156. foreach($editArr as $column){
  157. $name = $column['name'];
  158. $require = $column['require'] ? 'required' : '';
  159. $label = $column['label'] ? $column['label'] : $this->_one->getAttributeLabel($name);
  160. $display = isset($column['display']) ? $column['display'] : '';
  161. if(empty($display)){
  162. $display = ['type' => 'inputString'];
  163. }
  164. //var_dump($this->_one['id']);
  165. $value = $this->_one[$name] ? $this->_one[$name] : $column['default'];
  166. $display_type = isset($display['type']) ? $display['type'] : 'inputString';
  167. if($display_type == 'inputString'){
  168. $str .='<p>
  169. <label>'.$label.':</label>
  170. <input type="text" value="'.$value.'" size="30" name="editFormData['.$name.']" class="textInput '.$require.' ">
  171. </p>';
  172. }else if($display_type == 'inputDate'){
  173. $str .='<p>
  174. <label>'.$label.':</label>
  175. <input type="text" value="'.($value ? date("Y-m-d",strtotime($value)) : '').'" size="30" name="editFormData['.$name.']" class="date textInput '.$require.' ">
  176. </p>';
  177. }else if($display_type == 'inputEmail'){
  178. $str .='<p>
  179. <label>'.$label.':</label>
  180. <input type="text" value="'.$value.'" size="30" name="editFormData['.$name.']" class="email textInput '.$require.' ">
  181. </p>';
  182. }else if($display_type == 'inputPassword'){
  183. $str .='<p>
  184. <label>'.$label.':</label>
  185. <input type="password" value="" size="30" name="editFormData['.$name.']" class=" textInput '.$require.' ">
  186. </p>';
  187. }else if($display_type == 'select'){
  188. $data = isset($display['data']) ? $display['data'] : '';
  189. //var_dump($data);
  190. //echo $value;
  191. $select_str = '';
  192. if(is_array($data)){
  193. $select_str .= '<select class="combox '.$require.'" name="editFormData['.$name.']" >';
  194. $select_str .='<option value="">'.$label.'</option>';
  195. foreach($data as $k => $v){
  196. if($value == $k){
  197. //echo $value."#".$k;
  198. $select_str .='<option selected="selected" value="'.$k.'">'.$v.'</option>';
  199. }else{
  200. $select_str .='<option value="'.$k.'">'.$v.'</option>';
  201. }
  202. }
  203. $select_str .= '</select>';
  204. }
  205. $str .='<p>
  206. <label>'.$label.':</label>
  207. '.$select_str.'
  208. </p>';
  209. }
  210. }
  211. return $str;
  212. }
  213. }