WordsController.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <?php
  2. namespace Admin\Controller;
  3. use Common\Controller\AdminController;
  4. class WordsController extends AdminController {
  5. public function __construct(){
  6. parent::__construct();
  7. }
  8. //单词编辑
  9. public function edit() {
  10. $step = I('step','');
  11. $wordsId = I('words_id','');
  12. $courseId = I('course_id','');
  13. $wordsKeys = array('n'=>'words_n','pron'=>'words_pron','adj'=>'words_adj','num'=>'words_num','v'=>'words_v','adv'=>'words_adv','art'=>'words_art',
  14. 'prep'=>'words_prep','conj'=>'words_conj','int'=>'words_int','vt'=>'words_vt','vi'=>'words_vi','other'=>'words_other',);
  15. if (empty($step)){
  16. $info = array();
  17. $wordsPreId = '';
  18. $wordsNextId = '';
  19. if (!empty($courseId)){
  20. $info = M('Words')->where(array('words_id'=>$wordsId))->find();
  21. $wordsPreInfo = M('Words')->where(array('course_id'=>$courseId,'words_id'=>array('LT',$wordsId)))->order('words_id desc')->field('words_id')->find();
  22. if ($wordsPreInfo!=null){
  23. $wordsPreId = $wordsPreInfo['words_id'];
  24. }
  25. $wordsNextInfo = M('Words')->where(array('course_id'=>$courseId,'words_id'=>array('GT',$wordsId)))->order('words_id asc')->field('words_id')->find();
  26. if ($wordsNextInfo!=null){
  27. $wordsNextId = $wordsNextInfo['words_id'];
  28. }
  29. if($info['words_img']){
  30. $arr=explode(',',$info['words_img']);
  31. $info['words_img1']=$arr[0];
  32. $info['words_img2']=$arr[1];
  33. $info['words_img3']=$arr[2];
  34. }
  35. $ttt='';
  36. if($info['words_voice1']){
  37. if($ttt){
  38. $ttt.=",".$info['words_voice1'];
  39. }else{
  40. $ttt=$info['words_voice1'];
  41. }
  42. }
  43. if($info['words_voice2']){
  44. if($ttt){
  45. $ttt.=",".$info['words_voice2'];
  46. }else{
  47. $ttt=$info['words_voice2'];
  48. }
  49. }
  50. if($info['words_voice3']){
  51. if($ttt){
  52. $ttt.=",".$info['words_voice3'];
  53. }else{
  54. $ttt=$info['words_voice3'];
  55. }
  56. }
  57. $info['words_voice']=$ttt;
  58. $ttt='';
  59. if($info['words_read1']){
  60. if($ttt){
  61. $ttt.=",".$info['words_read1'];
  62. }else{
  63. $ttt=$info['words_read1'];
  64. }
  65. }
  66. if($info['words_read2']){
  67. if($ttt){
  68. $ttt.=",".$info['words_read2'];
  69. }else{
  70. $ttt=$info['words_read2'];
  71. }
  72. }
  73. if($info['words_read3']){
  74. if($ttt){
  75. $ttt.=",".$info['words_read3'];
  76. }else{
  77. $ttt=$info['words_read3'];
  78. }
  79. }
  80. $info['words_read']=$ttt;
  81. }
  82. $this->assign('info',$info);
  83. $this->assign('courseId', $courseId);
  84. $this->assign('wordsKeys', $wordsKeys);
  85. $this->assign('wordsPreId', $wordsPreId);
  86. $this->assign('wordsNextId', $wordsNextId);
  87. $this->display();
  88. }else if($step==2){
  89. $data['course_id'] = $courseId;
  90. $data['words_name'] = trim(I('words_name',''));
  91. $wordsVoice = I('words_voice','');
  92. $wordsRead = I('words_read','');
  93. if (substr($wordsVoice,-1)==','){
  94. $wordsVoice = substr($wordsVoice, 0, -1);
  95. }
  96. $wordsVoiceArr = explode(',', $wordsVoice);
  97. $wordsReadArr = explode(',', $wordsRead);
  98. for ($i=0;$i<count($wordsVoiceArr);$i++){
  99. $j = $i+1;
  100. $data['words_read'.$j] = $wordsReadArr[$i];
  101. $data['words_voice'.$j] = $wordsVoiceArr[$i];
  102. }
  103. $wordsVals = array_values($wordsKeys);
  104. $checkWords = true;
  105. foreach ($wordsVals as $keys){
  106. $data[$keys] = I($keys,'');
  107. if (!empty($data[$keys])){
  108. $checkWords = false;
  109. }
  110. }
  111. if (empty($data['words_name'])){
  112. $this->error('单词不能为空');
  113. }
  114. /* if (empty($data['words_voice1'])){
  115. $this->error('读音不能为空');
  116. }
  117. if (empty($data['words_read1'])){
  118. $this->error('音标不能为空');
  119. } */
  120. if ($checkWords){
  121. $this->error('中文释义不能为空');
  122. }
  123. $data['jz_en1'] = I('jz_en1','');
  124. $data['jz_cn1'] = I('jz_cn1','');
  125. $data['jz_en2'] = I('jz_en2','');
  126. $data['jz_cn2'] = I('jz_cn2','');
  127. $data['jz_en3'] = I('jz_en3','');
  128. $data['jz_cn3'] = I('jz_cn3','');
  129. $data['words_similar'] = I('words_similar','');
  130. $data['annotation_cn'] = I('annotation_cn','');
  131. $data['similar_cn'] = I('similar_cn','');
  132. $words_img1=I('words_img1','');
  133. $words_img2=I('words_img2','');
  134. $words_img3=I('words_img3','');
  135. $ttt='';
  136. if($words_img1){
  137. if($ttt){
  138. $ttt.=",".$words_img1;
  139. }else{
  140. $ttt=$words_img1;
  141. }
  142. }
  143. if($words_img2){
  144. if($ttt){
  145. $ttt.=",".$words_img2;
  146. }else{
  147. $ttt=$words_img2;
  148. }
  149. }
  150. if($words_img3){
  151. if($ttt){
  152. $ttt.=",".$words_img3;
  153. }else{
  154. $ttt=$words_img3;
  155. }
  156. }
  157. $data['words_img']=$ttt;
  158. /*
  159. if (!empty($data['jz_en1']) && strpos(strtolower($data['jz_en1']), strtolower($data['words_name']))===FALSE){
  160. $this->error('您添加的例句1不包含对应单词,请仔细核对后提交');
  161. }
  162. if (!empty($data['jz_en2']) && strpos(strtolower($data['jz_en2']), strtolower($data['words_name']))===FALSE){
  163. $this->error('您添加的例句2不包含对应单词,请仔细核对后提交');
  164. }
  165. if (!empty($data['jz_en3']) && strpos(strtolower($data['jz_en3']), strtolower($data['words_name']))===FALSE){
  166. $this->error('您添加的例句3不包含对应单词,请仔细核对后提交');
  167. }
  168. */
  169. if (!empty($data['jz_en1']) && empty($data['jz_cn1'])){
  170. $this->error('您添加的例句1释义不能为空,请仔细核对后提交');
  171. }
  172. if (!empty($data['jz_en2']) && empty($data['jz_cn2'])){
  173. $this->error('您添加的例句1释义不能为空,请仔细核对后提交');
  174. }
  175. if (!empty($data['jz_en3']) && empty($data['jz_cn3'])){
  176. $this->error('您添加的例句1释义不能为空,请仔细核对后提交');
  177. }
  178. if (!empty($data['words_similar']) && strlen($data['words_similar'])>50){
  179. $this->error('近义词不能大于50个字节');
  180. }
  181. if (!empty($data['similar_cn']) && strlen($data['similar_cn'])>500){
  182. $this->error('英文释义不能大于500个字节');
  183. }
  184. $data['operate_dt'] = time();
  185. if ($wordsId){
  186. $jumpType = 1;
  187. M('Words')->where(array('words_id'=>$wordsId))->save($data);
  188. }else{
  189. $jumpType = 2;
  190. $wordsId = M('Words')->add($data);
  191. M('Course')->where(array('course_id'=>$courseId))->setInc('course_num');
  192. }
  193. //$this->success('单词操作成功','/admin/course/detail?course_id='.$courseId);
  194. $jumpId = I('jumpId');
  195. if ($jumpId==1){
  196. if ($jumpType==1){
  197. $this->success('单词操作成功','/admin/words/edit?course_id='.$courseId.'&words_id='.$wordsId);
  198. }else{
  199. $this->success('单词操作成功','/admin/words/edit?course_id='.$courseId);
  200. }
  201. }else if($jumpId==2){
  202. $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'words_id'=>array('LT',$wordsId)))->order('words_id desc')->field('words_id')->find();
  203. if ($wordsInfo==NULL){
  204. $this->success('单词操作成功,暂无上一个单词','/admin/words/edit?course_id='.$courseId);
  205. }
  206. $this->success('单词操作成功,跳转至上一个单词','/admin/words/edit?course_id='.$courseId.'&words_id='.$wordsInfo['words_id']);
  207. }else if($jumpId==3){
  208. $wordsInfo = M('Words')->where(array('course_id'=>$courseId,'words_id'=>array('GT',$wordsId)))->order('words_id asc')->field('words_id')->find();
  209. if ($wordsInfo==NULL){
  210. $this->success('单词操作成功,暂无下一个单词','/admin/words/edit?course_id='.$courseId);
  211. }
  212. $this->success('单词操作成功,跳转至下一个单词','/admin/words/edit?course_id='.$courseId.'&words_id='.$wordsInfo['words_id']);
  213. }
  214. }
  215. }
  216. //删除单词
  217. public function del(){
  218. $wordsId = I('words_id','');
  219. $courseId = I('course_id','');
  220. //M('Words')->where(array('words_id'=>$wordsId))->save(array('is_delete'=>2));
  221. M('Words')->where(array('words_id'=>$wordsId))->delete();
  222. M('Course')->where(array('course_id'=>$courseId))->setDec('course_num');
  223. $this->success('单词删除成功','/admin/course/detail/index?course_id='.$courseId);
  224. }
  225. //获取单词信息接口
  226. public function dict(){
  227. $wordsFields = array('n','pron','adj','num','v','adv','art','prep','conj','int','vt','vi','other',);
  228. $wordsName = I('words_name','');
  229. if (empty($wordsName)){
  230. $data = array('code'=>1,'msg'=>'单词不能为空');
  231. echo json_encode($data);
  232. }
  233. $httpCurl = new \Org\Net\HttpCurl();
  234. $url = C('TRANS').'&w='.$wordsName;
  235. $res = $httpCurl->get($url,'xml');
  236. $res = json_decode(json_encode(simplexml_load_string($res, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  237. /* $url = C('TRANS').'&type=json&w='.$wordsName;
  238. $res = $httpCurl->get($url); */
  239. if (!isset($res['key'])){
  240. $data = array('code'=>1,'msg'=>'单词翻译出错');
  241. echo json_encode($data);
  242. }
  243. if (isset($res['ps'])){
  244. if (!is_array($res['ps'])){
  245. $data['data']['ps'][] = $res['ps'];
  246. }else{
  247. $data['data']['ps'] = $res['ps'];
  248. }
  249. }else{
  250. $data['data']['ps'] = array();
  251. }
  252. if (isset($res['pron'])){
  253. if (!is_array($res['pron'])){
  254. $data['data']['pron'][] = $res['pron'];
  255. }else{
  256. $data['data']['pron'] = $res['pron'];
  257. }
  258. /* $data['data']['pron'] = array(
  259. 'http://res.iciba.com/resource/amp3/0/0/c4/7d/c47d187067c6cf953245f128b5fde62a.mp3',
  260. 'http://res.iciba.com/resource/amp3/0/0/34/d1/34d1f91fb2e514b8576fab1a75a89a6b.mp3'
  261. ); */
  262. }else{
  263. $data['data']['pron'] = array();
  264. }
  265. if (isset($res['pos']) && !empty($res['pos'])){
  266. if (is_array($res['pos'])){
  267. foreach ($res['pos'] as $k=>$v){
  268. $key = str_replace('.', '', $v);
  269. if (!in_array($key, $wordsFields)){
  270. $pos[] = array('other', trim($res['acceptation'][$k]));
  271. }else{
  272. $pos[] = array($key, trim($res['acceptation'][$k]));
  273. }
  274. }
  275. }else{
  276. $key = str_replace('.', '', $res['pos']);
  277. //$pos[] = array(str_replace('.', '', $res['pos']),trim($res['acceptation']));
  278. if (!in_array($key, $wordsFields)){
  279. $pos[] = array('other', trim($res['acceptation']));
  280. }else{
  281. $pos[] = array($key, trim($res['acceptation']));
  282. }
  283. /* if (isset($wordsKeys[str_replace('.', '', $res['pos'])])){
  284. $pos[] = array(str_replace('.', '', $res['pos']),trim($res['acceptation']));
  285. }else{
  286. $pos[] = array('words_other',trim($res['acceptation']));
  287. } */
  288. }
  289. $data['data']['pos'] = $pos;
  290. }else{
  291. $pos[] = array('other', trim($res['acceptation']));
  292. $data['data']['pos'] = $pos;
  293. }
  294. if (isset($res['sent'])){
  295. if (empty($res['sent'][0])){
  296. $sent[] = array(trim($res['sent']['orig']),trim($res['sent']['trans']));
  297. }else{
  298. foreach ($res['sent'] as $k=>$v){
  299. if ($k>2){
  300. continue;
  301. }
  302. $sent[] = array(trim($v['orig']),trim($v['trans']));
  303. }
  304. }
  305. $data['data']['sent'] = $sent;
  306. }
  307. $data['code'] = 0;
  308. $this->ajaxReturn($data);exit;
  309. }
  310. }