AppadminbaseBlockEdit.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 fecshop\app\appadmin\modules;
  10. use fec\helpers\CRequest;
  11. use fecshop\app\appadmin\interfaces\base\AppadminbaseBlockEditInterface;
  12. use Yii;
  13. use yii\base\BaseObject;
  14. /**
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class AppadminbaseBlockEdit extends BaseObject
  19. {
  20. public $_param;
  21. public $_primaryKey;
  22. public $_one;
  23. public $_service;
  24. public $_textareas;
  25. public $_lang_attr;
  26. /**
  27. * html input or text etc. , html name like: <input name="XXXX" />.
  28. */
  29. protected $_editFormData;
  30. public function init()
  31. {
  32. if (!($this instanceof AppadminbaseBlockEditInterface)) {
  33. echo json_encode([
  34. 'statusCode'=>'300',
  35. 'message'=>'Manageredit must implements fecshop\app\appadmin\interfaces\base\AppadminbaseBlockEditInterface',
  36. ]);
  37. exit;
  38. }
  39. $this->_editFormData = 'editFormData';
  40. $this->setService();
  41. $this->_param = CRequest::param();
  42. $this->_primaryKey = $this->_service->getPrimaryKey();
  43. $id = $this->_param[$this->_primaryKey];
  44. $this->_one = $this->_service->getByPrimaryKey($id);
  45. }
  46. public function getVal($name, $column){
  47. return ($this->_one[$name] || $this->_one[$name] === 0) ? $this->_one[$name] : $column['default'];
  48. }
  49. public function getEditBar($editArr = [])
  50. {
  51. $langs = Yii::$service->fecshoplang->getAllLangCode();
  52. $defaultLangCode = Yii::$service->fecshoplang->defaultLangCode;
  53. // xhEditor编辑器里面上传图片和其他的类型的url以及允许的文件类型
  54. // fecshop只实现了image的上传,其他类型的自己实现。
  55. $upImgUrl = Yii::$service->admin->getXhEditorUploadImgUrl();
  56. $upImgFormat = Yii::$service->admin->getXhEditorUploadImgForamt();
  57. $upFlashUrl = Yii::$service->admin->getXhEditorUploadFlashUrl();
  58. $upFlashFormat = Yii::$service->admin->getXhEditorUploadFlashFormat();
  59. $upLinkUrl = Yii::$service->admin->getXhEditorUploadLinkUrl();
  60. $upLinkFormat = Yii::$service->admin->getXhEditorUploadLinkFormat();
  61. $upMediaUrl = Yii::$service->admin->getXhEditorUploadMediaUrl();
  62. $upMediaFormat = Yii::$service->admin->getXhEditorUploadMediaFormat();
  63. if (empty($editArr)) {
  64. $editArr = $this->getEditArr();
  65. }
  66. $str = '';
  67. $langAndTextarea = '';
  68. if ($this->_param[$this->_primaryKey]) {
  69. $str = <<<EOF
  70. <input type="hidden" value="{$this->_param[$this->_primaryKey]}" size="30" name="{$this->_editFormData}[{$this->_primaryKey}]" class="textInput ">
  71. EOF;
  72. }
  73. $idsj = md5(time());
  74. $idsji = 0;
  75. foreach ($editArr as $column) {
  76. $name = $column['name'];
  77. $require = $column['require'] ? 'required' : '';
  78. $label = $column['label'] ? $column['label'] : $this->_one->getAttributeLabel($name);
  79. $display = isset($column['display']) ? $column['display'] : '';
  80. if (empty($display)) {
  81. $display = ['type' => 'inputString'];
  82. }
  83. //var_dump($this->_one['id']);
  84. $value = $this->getVal($name, $column);
  85. $display_type = isset($display['type']) ? $display['type'] : 'inputString';
  86. if ($display_type == 'inputString') {
  87. $isLang = isset($display['lang']) ? $display['lang'] : false;
  88. if ($isLang && is_array($langs) && !empty($langs)) {
  89. $tabLangTitle = '';
  90. $tabLangInput = '';
  91. foreach ($langs as $lang) {
  92. if ($require && $defaultLangCode === $lang) {
  93. $inputStringLangRequire = 'required';
  94. } else {
  95. $inputStringLangRequire = 0;
  96. }
  97. $tabLangTitle .= '<li><a href="javascript:;"><span>'.$lang.'</span></a></li>';
  98. $langAttrName = Yii::$service->fecshoplang->getLangAttrName($name, $lang);
  99. $t_val = isset($value[$langAttrName]) ? $value[$langAttrName] : '';
  100. // 对于含有 " 的字符串进行处理
  101. $t_val = str_replace('"', '&quot;', $t_val) ;
  102. $tabLangInput .= '<div>
  103. <p class="edit_p">
  104. <label>'.$label.'['.$lang.']:</label>
  105. <input type="text" value="'.$t_val.'" size="30" name="'.$this->_editFormData.'['.$name.']['.$langAttrName.']" class="textInput '.$inputStringLangRequire.' ">
  106. </p>
  107. </div>';
  108. }
  109. $this->_lang_attr .= <<<EOF
  110. <div class="tabs" currentIndex="0" eventType="click" style="margin:10px 0;">
  111. <div class="tabsHeader">
  112. <div class="tabsHeaderContent">
  113. <ul>
  114. {$tabLangTitle}
  115. </ul>
  116. </div>
  117. </div>
  118. <div class="tabsContent" style="">
  119. {$tabLangInput}
  120. </div>
  121. <div class="tabsFooter">
  122. <div class="tabsFooterContent"></div>
  123. </div>
  124. </div>
  125. EOF;
  126. } else {
  127. // 对于含有 " 的字符串进行处理
  128. $value = str_replace('"', '&quot;', $value) ;
  129. $str .= <<<EOF
  130. <p class="edit_p">
  131. <label>{$label}:</label>
  132. <input type="text" value="{$value}" size="30" name="{$this->_editFormData}[{$name}]" class="textInput {$require} ">
  133. </p>
  134. EOF;
  135. }
  136. } elseif ($display_type == 'inputDate') {
  137. if ($value && !is_numeric($value)) {
  138. $value = strtotime($value);
  139. }
  140. $valueData = $value ? date('Y-m-d', $value) : '';
  141. $str .= <<<EOF
  142. <p class="edit_p">
  143. <label>{$label}:</label>
  144. <input type="text" value="{$valueData}" size="30" name="{$this->_editFormData}[{$name}]" class="date textInput {$require} ">
  145. </p>
  146. EOF;
  147. } elseif ($display_type == 'inputDateTime') {
  148. if ($value && !is_numeric($value)) {
  149. $value = strtotime($value);
  150. }
  151. $valueData = $value ? date('Y-m-d H:i:s', $value) : '';
  152. $str .= <<<EOF
  153. <p class="edit_p">
  154. <label>{$label}:</label>
  155. <input type="text" datefmt="yyyy-MM-dd HH:mm:ss" value="{$valueData}" size="30" name="{$this->_editFormData}[{$name}]" class="date textInput {$require} ">
  156. </p>
  157. EOF;
  158. } elseif ($display_type == 'inputEmail') {
  159. $str .= <<<EOF
  160. <p class="edit_p">
  161. <label>{$label}:</label>
  162. <input type="text" value="{$value}" size="30" name="{$this->_editFormData}[{$name}]" class="email textInput {$require} ">
  163. </p>
  164. EOF;
  165. } elseif ($display_type == 'stringText') {
  166. $str .= <<<EOF
  167. <p class="edit_p">
  168. <label>{$label}:</label>
  169. {$value}
  170. </p>
  171. EOF;
  172. } elseif ($display_type == 'inputPassword') {
  173. $str .= <<<EOF
  174. <p class="edit_p">
  175. <label>{$label}:</label>
  176. <input type="password" value="" size="30" name="{$this->_editFormData}[{$name}]" class=" textInput {$require} ">
  177. </p>
  178. EOF;
  179. } elseif ($display_type == 'select') {
  180. $data = isset($display['data']) ? $display['data'] : '';
  181. //var_dump($data);
  182. //echo $value;
  183. $select_str = '';
  184. if (is_array($data)) {
  185. $select_str .= <<<EOF
  186. <select class="combox {$require}" name="{$this->_editFormData}[{$name}]" >
  187. EOF;
  188. $select_str .= '<option value="">'.$label.'</option>';
  189. foreach ($data as $k => $v) {
  190. if ($value == $k) {
  191. //echo $value."#".$k;
  192. $select_str .= '<option selected="selected" value="'.$k.'">'.$v.'</option>';
  193. } else {
  194. $select_str .= '<option value="'.$k.'">'.$v.'</option>';
  195. }
  196. }
  197. $select_str .= '</select>';
  198. }
  199. $str .= <<<EOF
  200. <p class="edit_p">
  201. <label>{$label}:</label>
  202. {$select_str}
  203. </p>
  204. EOF;
  205. } elseif ($display_type == 'editSelect') {
  206. $data = isset($display['data']) ? $display['data'] : '';
  207. //var_dump($data);
  208. //echo $value;
  209. $select_str = '';
  210. if (is_array($data)) {
  211. $idsji++;
  212. $selectId = $idsj.$idsji;
  213. $select_str .= <<<EOF
  214. <select id="{$selectId}" class=" {$require}" name="{$this->_editFormData}[{$name}]" >
  215. EOF;
  216. $select_str .= '<option value="">'.$label.'</option>';
  217. $editSelectChosen = false;
  218. foreach ($data as $k => $v) {
  219. if ($value == $k) {
  220. //echo $value."#".$k;
  221. $select_str .= '<option selected value="'.$k.'">'.$v.'</option>';
  222. $editSelectChosen = true;
  223. } else {
  224. $select_str .= '<option value="'.$k.'">'.$v.'</option>';
  225. }
  226. }
  227. if (!$editSelectChosen) {
  228. $select_str .= '<option selected value="'.$value.'">'.$value.'</option>';
  229. }
  230. $select_str .= '</select>';
  231. }
  232. $str .= <<<EOF
  233. <p class="edit_p">
  234. <label>{$label}:</label>
  235. {$select_str}
  236. </p>
  237. <script type="text/javascript">
  238. $('#{$selectId}').editableSelect(
  239. { filter: false }
  240. );
  241. </script>
  242. EOF;
  243. } elseif ($display_type == 'textarea') {
  244. $rows = isset($display['rows']) ? $display['rows'] : 15;
  245. $cols = isset($display['cols']) ? $display['cols'] : 110;
  246. $isLang = isset($display['lang']) ? $display['lang'] : false;
  247. $uploadImgUrl = 'upimgurl="'.Yii::$service->url->getUrl($upImgUrl).'" upimgext="' . $upImgFormat . '"';
  248. $uploadFlashUrl = 'upflashurl="'.Yii::$service->url->getUrl($upFlashUrl).'" upflashext="' . $upFlashFormat . '"';
  249. $uploadLinkUrl = 'uplinkurl="'.Yii::$service->url->getUrl($upLinkUrl).'" uplinkext="' . $upLinkFormat . '"';
  250. $uploadMediaUrl = 'upmediaurl="'.Yii::$service->url->getUrl($upMediaUrl).'" upmediaext:"' . $upMediaFormat . '" ';
  251. if ($isLang && is_array($langs) && !empty($langs)) {
  252. $tabLangTitle = '';
  253. $tabLangTextarea = '';
  254. foreach ($langs as $lang) {
  255. $langAttrName = Yii::$service->fecshoplang->getLangAttrName($name, $lang);
  256. if ($require && $defaultLangCode === $lang) {
  257. $inputStringLangRequire = 'required';
  258. } else {
  259. $inputStringLangRequire = 0;
  260. }
  261. $tabLangTitle .= '<li><a href="javascript:;"><span>'.$lang.'</span></a></li>';
  262. $tabLangTextarea .= '
  263. <div>
  264. <fieldset id="fieldset_table_qbe">
  265. <legend style="color:#cc0000">'.$label.'['.$lang.']:</legend>
  266. <div>
  267. <div class="unit">
  268. <textarea '.$uploadImgUrl.' '.$uploadFlashUrl.' '.$uploadLinkUrl.' '.$uploadMediaUrl.' class="editor '.$inputStringLangRequire.'" rows="'.$rows.'" cols="'.$cols.'" name="'.$this->_editFormData.'['.$name.']['.$langAttrName.']" >'.$value[$langAttrName].'</textarea>
  269. </div>
  270. </div>
  271. </fieldset>
  272. </div>';
  273. }
  274. $this->_textareas .= <<<EOF
  275. <div class="tabs" currentIndex="0" eventType="click" style="margin:10px 0;">
  276. <div class="tabsHeader">
  277. <div class="tabsHeaderContent">
  278. <ul>
  279. {$tabLangTitle}
  280. </ul>
  281. </div>
  282. </div>
  283. <div class="tabsContent" style="">
  284. {$tabLangTextarea}
  285. </div>
  286. <div class="tabsFooter">
  287. <div class="tabsFooterContent"></div>
  288. </div>
  289. </div>
  290. EOF;
  291. } else {
  292. $this->_textareas .= <<<EOF
  293. <fieldset id="fieldset_table_qbe">
  294. <legend style="color:#cc0000">{$label}:</legend>
  295. <div>
  296. <textarea class="editor" name="{$this->_editFormData}[{$name}]" rows="{$rows}" cols="{$cols}" name="{$this->_editFormData}[{$name}]" {$uploadImgUrl} {$uploadFlashUrl} {$uploadLinkUrl} {$uploadMediaUrl} >{$value}</textarea>
  297. </div>
  298. </fieldset>
  299. EOF;
  300. }
  301. }
  302. }
  303. return $str;
  304. }
  305. }