CategoryAction.class.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /**
  3. *
  4. * Category(分类)
  5. *
  6. */
  7. if(!defined("Ainaphp")) exit("Access Denied");
  8. class CategoryAction extends AdminbaseAction
  9. {
  10. protected $dao,$categorys,$module;
  11. function _initialize()
  12. {
  13. parent::_initialize();
  14. foreach ((array)$this->module as $rw){
  15. if($rw['type']==1 && $rw['status']==1) $data['module'][$rw['id']] = $rw;
  16. }
  17. $this->module=$data['module'];
  18. $this->assign($data);
  19. unset($data);
  20. $this->dao = D('Admin/category');
  21. }
  22. /**
  23. * 列表
  24. *
  25. */
  26. public function index()
  27. {
  28. if($this->categorys){
  29. foreach($this->categorys as $r) {
  30. if($r['module']=='Page'){
  31. $r['str_manage'] = '<a href="?g=Admin&m=Page&a=edit&id='.$r['id'].'">'.L('edit_page').'</a> | ';
  32. }else{
  33. $r['str_manage'] = '<a href="'.U('Category/add',array( 'parentid' => $r['id'],'type'=>$r['type'])).'">'.L('add_catname').'</a> | ';
  34. }
  35. $r['str_manage'] .= '<a href="'.U('Category/edit',array( 'id' => $r['id'],'type'=>$r['type'])).'">'.L('edit').'</a> | <a href="javascript:confirm_delete(\''.U('Category/delete',array( 'id' => $r['id'])).'\')">'.L('delete').'</a> ';
  36. $r['modulename']=$this->module[$r['moduleid']]['title'] ? $this->module[$r['moduleid']]['title'] : L('Module_url');
  37. $r['dis'] = $r['ismenu'] ? '<font color="green">'.L('display_yes').'</font>' : '<font color="red">'.L('display_no').'</font>' ;
  38. $array[] = $r;
  39. }
  40. $str = "<tr>
  41. <td width='40' align='center'><input name='listorders[\$id]' type='text' size='3' value='\$listorder' class='input-text-c'></td>
  42. <td align='center'>\$id</td>
  43. <td >\$spacer\$catname &nbsp;</td>
  44. <td align='center'>\$modulename</td>
  45. <td align='center'><a href='\$url' target='_blank'>".L('fangwen')."</a></td>
  46. <td align='center'>\$str_manage</td>
  47. </tr>";
  48. import ( '@.ORG.Tree' );
  49. $tree = new Tree ($array);
  50. unset($array);
  51. $tree->icon = array('&nbsp;&nbsp;&nbsp;<span>'.L('tree_1').'</span>','&nbsp;&nbsp;&nbsp;<span>'.L('tree_2').'</span>','&nbsp;&nbsp;&nbsp;<span>'.L('tree_3').'</span>');
  52. $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
  53. $categorys = $tree->get_tree(0, $str);
  54. $this->assign('categorys', $categorys);
  55. }
  56. $this->display();
  57. }
  58. public function _before_add()
  59. {
  60. foreach((array)$this->Urlrule as $key =>$r){
  61. if($r['ishtml'])$Urlrule[$key]=$r;
  62. }
  63. $this->assign('Urlrule', $Urlrule);
  64. $yourphp_auth_key = sysmd5(C('ADMIN_ACCESS').$_SERVER['HTTP_USER_AGENT']);
  65. $yourphp_auth = authcode('1-1-0-1-jpeg,jpg,png,gif-3-0', 'ENCODE',$yourphp_auth_key);
  66. $this->assign('yourphp_auth',$yourphp_auth);
  67. $templates= template_file();
  68. $this->assign ( 'templates',$templates );
  69. $parentid = intval($_GET['parentid']);
  70. $vo['ismenu']=1;
  71. $vo['moduleid'] =$this->categorys[$parentid]['moduleid'];
  72. $this->assign('vo', $vo);
  73. foreach($this->categorys as $r) {
  74. $array[] = $r;
  75. }
  76. import ( '@.ORG.Tree' );
  77. $str = "<option value='\$id' \$selected>\$spacer \$catname</option>";
  78. $tree = new Tree ($array);
  79. $select_categorys = $tree->get_tree(0, $str,$parentid);
  80. $usergroup=F('Role');
  81. $this->assign('rlist',$usergroup);
  82. $this->assign('select_categorys', $select_categorys);
  83. }
  84. /**
  85. * 提交录入
  86. *
  87. */
  88. public function insert()
  89. {
  90. /*
  91. if($_POST['parentid']){
  92. if($_POST['moduleid']!=$this->categorys[$_POST['parentid']]['moduleid']){
  93. $this->success(L('chose_notop_module'));
  94. }
  95. }
  96. */
  97. if(empty($_POST['urlruleid']) && $_POST['ishtml']) $this->error(L('do_empty'));
  98. $_POST['readgroup'] = $_POST['readgroup'] ? implode(',',$_POST['readgroup']) : '';
  99. $_POST['postgroup'] = $_POST['postgroup'] ? implode(',',$_POST['postgroup']) : '';
  100. $_POST['module'] = $this->module[$_POST['moduleid']]['name'] ? $this->module[$_POST['moduleid']]['name'] : '';
  101. $_POST['moduleid']= intval($_POST['moduleid']);
  102. if(APP_LANG)$_POST['lang']=LANG_ID;
  103. if($this->dao->create())
  104. {
  105. $id = $this->dao->add();
  106. if($id)
  107. {
  108. if($_POST['module']=='Page'){
  109. $_POST['id']=$id;
  110. if(empty($_POST['title']))$_POST['title'] = $_POST['catname'];
  111. $Page=D('Page');
  112. if($Page->create()){
  113. $Page->add();
  114. }
  115. }
  116. if($_POST['aid']) {
  117. $Attachment =M('Attachment');
  118. $aids = implode(',',$_POST['aid']);
  119. $data['catid']= $_POST['catid'];
  120. $data['moduleid']= $_POST['moduleid'];
  121. $data['status']= '1';
  122. $Attachment->where("aid in (".$aids.")")->save($data);
  123. }
  124. $this->repair();
  125. savecache('Category');
  126. if($_POST['ishtml']){
  127. $this->categorys = F('Category');
  128. $cat = $this->categorys[$id];
  129. $arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));
  130. foreach($arrparentid as $catid) {
  131. if($this->categorys[$catid]['ishtml']) $this->clisthtml($catid);
  132. }
  133. if($this->sysConfig['HOME_ISHTML']) $this->create_index();
  134. }
  135. $this->assign ( 'jumpUrl', U(MODULE_NAME.'/index') );
  136. $this->success(L('add_ok'));
  137. }else{
  138. $this->error(L('add_error'));
  139. }
  140. }else{
  141. $this->error($this->dao->getError());
  142. }
  143. }
  144. /**
  145. * 编辑
  146. *
  147. */
  148. public function edit()
  149. {
  150. $id = intval($_GET['id']);
  151. foreach((array)$this->Urlrule as $key =>$r){
  152. if($r['ishtml'])$Urlrule[$key]=$r;
  153. }
  154. $this->assign('Urlrule', $Urlrule);
  155. $yourphp_auth_key = sysmd5(C('ADMIN_ACCESS').$_SERVER['HTTP_USER_AGENT']);
  156. $yourphp_auth = authcode('1-1-0-1-jpeg,jpg,png,gif-3-0', 'ENCODE',$yourphp_auth_key);
  157. $this->assign('yourphp_auth',$yourphp_auth);
  158. $templates= template_file();
  159. $this->assign ( 'templates',$templates );
  160. $record = $this->categorys[$id];
  161. $record['readgroup'] = explode(',',$record['readgroup']);
  162. if(empty($id) || empty($record)) $this->error(L('do_empty'));
  163. $parentid = intval($record['parentid']);
  164. import ( '@.ORG.Tree' );
  165. $result = $this->categorys;
  166. foreach($result as $r) {
  167. //if($r['type']==1) continue;
  168. $r['selected'] = $r['id'] == $parentid ? 'selected' : '';
  169. $array[] = $r;
  170. }
  171. $str = "<option value='\$id' \$selected>\$spacer \$catname</option>";
  172. $tree = new Tree ($array);
  173. $select_categorys = $tree->get_tree(0, $str,$parentid);
  174. $this->assign('select_categorys', $select_categorys);
  175. $this->assign('vo', $record);
  176. $usergroup=F('Role');
  177. $this->assign('rlist',$usergroup);
  178. $this->display ();
  179. }
  180. /**
  181. * 提交编辑
  182. *
  183. */
  184. public function update()
  185. {
  186. if(empty($_POST['urlruleid']) && $_POST['ishtml']) $this->error(L('do_empty'));
  187. $_POST['module'] = $this->module[$_POST['moduleid']]['name'];
  188. $_POST['readgroup'] = $_POST['readgroup'] ? implode(',',$_POST['readgroup']) : '';
  189. $_POST['postgroup'] = $_POST['postgroup'] ? implode(',',$_POST['postgroup']) : '';
  190. $_POST['arrparentid'] = $this->get_arrparentid($_POST['id']);
  191. if(empty($_POST['listtype']))$_POST['listtype']=0;
  192. if($_POST['type']){
  193. $_POST['moduleid']=0;
  194. $_POST['module']='';
  195. }
  196. if (false === $this->dao->create ()) {
  197. $this->error ( $this->dao->getError () );
  198. }
  199. if (false !== $this->dao->save ()) {
  200. if($_POST['aid']) {
  201. $Attachment =M('Attachment');
  202. $aids = implode(',',$_POST['aid']);
  203. $data['moduleid']= $_POST['moduleid'];
  204. $data['catid']= $_POST['id'];
  205. $data['status']= '1';
  206. $Attachment->where("aid in (".$aids.")")->save($data);
  207. }
  208. if($_POST['chage_all']){
  209. $data=array();
  210. $arrchildid = $this->get_arrchildid($_POST['id']);
  211. $data['urlruleid'] = $_POST['urlruleid'] ? $_POST['urlruleid'] : '0' ;
  212. $data['presentpoint'] = $_POST['presentpoint'];
  213. $data['postgroup'] = $_POST['postgroup'] ? $_POST['postgroup'] : '';
  214. $data['chargepoint'] = $_POST['chargepoint'];
  215. $data['paytype'] = $_POST['paytype'];
  216. $data['repeatchargedays'] = $_POST['repeatchargedays'];
  217. $data['ismenu'] = $_POST['ismenu'];
  218. $data['ishtml'] = $_POST['ishtml'];
  219. $data['pagesize'] = $_POST['pagesize'];
  220. $data['template_list'] = $_POST['template_list'];
  221. $data['template_show'] = $_POST['template_show'];
  222. $data['readgroup'] = $_POST['readgroup'] ? $_POST['readgroup'] : '';
  223. $r = $this->dao->where( ' id in ('.$arrchildid.')')->data($data)->save();
  224. }
  225. $this->repair();
  226. $this->repair();
  227. savecache('Category');
  228. if($_POST['ishtml']){
  229. $cat=$this->categorys[$_POST['id']];
  230. $arrparentid = array_filter(explode(',',$cat['arrparentid'].','.$cat['id']));
  231. foreach($arrparentid as $catid) {
  232. if($this->categorys[$catid]['ishtml']) $this->clisthtml($catid);
  233. }
  234. if($this->sysConfig['HOME_ISHTML']) $this->create_index();
  235. }
  236. $this->assign ( 'jumpUrl', U(MODULE_NAME.'/index') );
  237. $this->success (L('edit_ok'));
  238. } else {
  239. $this->success (L('edit_error').': '.$this->dao->getDbError());
  240. }
  241. }
  242. public function repair_cache() {
  243. $this->repair();
  244. $this->repair();
  245. savecache('Category');
  246. $this->assign ( 'jumpUrl', U(MODULE_NAME.'/index') );
  247. $this->success(L('do_success'));
  248. }
  249. public function repair() {
  250. @set_time_limit(500);
  251. $this->categorys = $categorys = array();
  252. if(APP_LANG)$langwhere = " and lang = ".LANG_ID;
  253. $categorys = $this->dao->where("parentid=0".$langwhere)->Order('listorder ASC,id ASC')->select();
  254. $this->set_categorys($categorys);
  255. if(is_array($this->categorys)) {
  256. foreach($this->categorys as $id => $cat) {
  257. //if($id == 0 || $cat['type']==1) continue;
  258. $this->categorys[$id]['arrparentid'] = $arrparentid = $this->get_arrparentid($id);
  259. $this->categorys[$id]['arrchildid'] = $arrchildid = $this->get_arrchildid($id);
  260. $this->categorys[$id]['parentdir'] = $cat['parentdir'] = $parentdir = $this->get_parentdir($id);
  261. $child = is_numeric($arrchildid) ? 0 : 1;
  262. if( $cat['type']==1){
  263. $url=$cat['url'];
  264. }else{
  265. $url = geturl($cat,'',$this->Urlrule);
  266. $url = $url[0];
  267. }
  268. $this->dao->save(array('url'=>$url,'parentdir'=>$parentdir,'arrparentid'=>$arrparentid,'arrchildid'=>$arrchildid,'child'=>$child,'id'=>$id));
  269. }
  270. }
  271. }
  272. public function set_categorys($categorys = array()) {
  273. if (is_array($categorys) && !empty($categorys)) {
  274. foreach ($categorys as $id => $c) {
  275. $this->categorys[$c[id]] = $c;
  276. if(APP_LANG)$langwhere = " and lang = ".LANG_ID;
  277. $r = $this->dao->where("parentid = $c[id]".$langwhere)->Order('listorder ASC,id ASC')->select();
  278. $this->set_categorys($r);
  279. }
  280. }
  281. return true;
  282. }
  283. public function get_parentdir($id) {
  284. if($this->categorys[$id]['parentid']==0) return '';
  285. $arrparentid = $this->categorys[$id]['arrparentid'];
  286. unset($r);
  287. if ($arrparentid) {
  288. $arrparentid = explode(',', $arrparentid);
  289. $arrcatdir = array();
  290. foreach($arrparentid as $pid) {
  291. if($pid==0) continue;
  292. $arrcatdir[] = $this->categorys[$pid]['catdir'];
  293. }
  294. return implode('/', $arrcatdir).'/';
  295. }
  296. }
  297. public function get_arrparentid($id, $arrparentid = '') {
  298. if(!is_array($this->categorys) || !isset($this->categorys[$id])) return false;
  299. $parentid = $this->categorys[$id]['parentid'];
  300. $arrparentid = $arrparentid ? $parentid.','.$arrparentid : $parentid;
  301. if($parentid) {
  302. $arrparentid = $this->get_arrparentid($parentid, $arrparentid);
  303. } else {
  304. $this->categorys[$id]['arrparentid'] = $arrparentid;
  305. }
  306. return $arrparentid;
  307. }
  308. public function get_arrchildid($id) {
  309. $arrchildid = $id;
  310. if(is_array($this->categorys)) {
  311. foreach($this->categorys as $catid => $cat) {
  312. if($cat['parentid'] && $id != $catid) {
  313. $arrparentids = explode(',', $cat['arrparentid']);
  314. if(in_array($id, $arrparentids)) $arrchildid .= ','.$catid;
  315. }
  316. }
  317. }
  318. return $arrchildid;
  319. }
  320. public function delete() {
  321. $catid = intval($_GET['id']);
  322. $module = $this->categorys[$catid]['module'];
  323. if($this->categorys[$catid]['type']==1){
  324. if($this->categorys[$catid]['arrchildid']!=$catid)$this->error(L('category_does_not_allow_delete'));
  325. $this->dao->delete($catid);
  326. delattach("catid in($catid)");
  327. }else{
  328. $module = M($module);
  329. $arrchildid = $this->categorys[$catid]['arrchildid'];
  330. $where = "catid in(".$arrchildid.")";
  331. $count = $module->where($where)->count();
  332. if($count) $this->error(L('category_does_not_allow_delete'));
  333. $this->dao->delete($arrchildid);
  334. $moduleid = $this->mod[$module];
  335. delattach("moduleid =$moduleid and catid in($arrchildid)");
  336. $arr=explode(',',$arrchildid);
  337. foreach((array)$arr as $r){
  338. if($this->categorys[$r]['module']=='Page'){
  339. $module=M('Page');
  340. $module->delete($r);
  341. }
  342. }
  343. }
  344. $this->repair();
  345. savecache('Category');
  346. $this->success(L('do_success'));
  347. }
  348. }