PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/src/testSource/phpcms/content/application/controllers/articlelist.php

https://gitlab.com/loda.sun.suryani/qijiatuku
PHP | 387 lines | 381 code | 4 blank | 2 comment | 12 complexity | 1d658451fff02e504617f530003a3b04 MD5 | raw file
  1. <?php
  2. if (! defined ( 'BASEPATH' ))
  3. exit ( 'No direct script access allowed' );
  4. class ArticleList extends MY_Controller {
  5. function __construct() {
  6. parent::__construct ();
  7. $this->load->library ( 'form_validation' ); //表单验证类
  8. $this->load->library ( 'datagrid' ); //文本控件
  9. $this->load->helper ( 'url' );
  10. $this->load->helper ( 'html' );
  11. $this->load->library ( 'editors' );
  12. $this->load->library ( 'session' ); //session类
  13. $this->load->helper ( 'pagenav' ); //分页类
  14. $this->load->helper ( 'toolkit' );
  15. $this->load->helper ( 'security' );
  16. }
  17. function index() {
  18. $view_data = array ();
  19. $view_data ['pages_nav'] = '';
  20. $view_data ['main_grid'] = '';
  21. //=========列表=====================
  22. $page_size = 10;
  23. $total_num = 0;
  24. $page_num = $this->input->post ( 'page_num' );
  25. if ($page_num < 1) {
  26. $page_num = 1;
  27. }
  28. $sql_where = "WHERE is_temp<1";
  29. if ($this->input->post ( 'article_title' )) {
  30. $sql_where = sprintf ( "$sql_where AND article_title like '%s%s%s' ", '%',
  31. $this->input->post ( 'article_title' ), '%' );
  32. }
  33. $sql_count = "SELECT count(*) as tot FROM news_article $sql_where"; //取总数,用于分页
  34. $row = $this->db->get_record_by_sql ( $sql_count, 'num' );
  35. $total_num = $row [0]; //取得总数
  36. if ($page_num > ceil ( $total_num / $page_size ) && $total_num != 0) {
  37. $page_num = ceil ( $total_num / $page_size );
  38. }
  39. $pages_obj = new PageNav ( $page_size, $total_num, $page_num, 10, 2 );
  40. $view_data ['pages_nav'] = $pages_obj->show_pages ();
  41. $select_limit_start = intval ( ($page_num - 1) * $page_size );
  42. $sql = "SELECT * FROM news_article $sql_where ORDER BY article_id DESC";
  43. $sql = "$sql LIMIT {$select_limit_start},{$page_size}";
  44. $data = $this->db->get_rows_by_sql ( $sql );
  45. $field_list = trim ( "article_title,create_time,article_tags" ); //这些字段才会出现在表格中
  46. $field_arr = null;
  47. if ($field_list) {
  48. $field_arr = explode ( ',', $field_list );
  49. $field_arr = array_flip ( $field_arr );
  50. //my_debug($field_arr);
  51. }
  52. if (count ( $data )) {
  53. foreach ( $data as $k => $row ) {
  54. if ($field_arr) {
  55. $data [$k] = array_intersect_key ( $row, $field_arr );
  56. }
  57. $article_id = $row ['article_id'];
  58. $tmp = date ( "Y-m-d", $row ['create_time'] );
  59. $tmp .= "<font color=\"red\">";
  60. $tmp .= date ( " H:i:s", $row ['create_time'] );
  61. $tmp .= "</font>";
  62. $data [$k] ['create_time'] = $tmp;
  63. $tag_name = '';
  64. $img_tags = str_replace ( "c", "", $row ['article_tags'] );
  65. if (! empty ( $img_tags )) {
  66. $tag = "SELECT tag_name FROM news_tag WHERE tag_id IN($img_tags)"; //取总数,用于分页
  67. $name = $this->db->get_rows_by_sql ( $tag );
  68. if (count ( $name )) {
  69. foreach ( $name as $n ) {
  70. $tag_name .= $n ['tag_name'] . ",";
  71. }
  72. }
  73. $data [$k] ['article_tags'] = substr ( $tag_name, 0,
  74. strlen ( $tag_name ) - 1 );
  75. } else {
  76. $data [$k] ['article_tags'] = null;
  77. }
  78. $data [$k] ['编辑'] = "<a href = \"javascript void(0)\" onclick=\"addpermi({$row['article_id']});return false;\">编辑</a>";
  79. $data [$k] ['delete'] = "<a href = \"javascript void(0)\" onclick=\"if(!confirm('确定要删除?')){return false;}masterdel({$row['article_id']});return false;\"\">刪除</a>";
  80. }
  81. $this->datagrid->reset ();
  82. $view_data ['main_grid'] = $this->datagrid->build ( 'datagrid', $data, TRUE );
  83. }
  84. //=========列表=====================
  85. $this->load->view ( 'article/articlelist_view', $view_data );
  86. }
  87. function add() {
  88. //创建一个空的记录,进入编辑
  89. $UID = $this->session->userdata ( 'UID' );
  90. $TG_user_name = $this->session->userdata ( 'TG_user_name' );
  91. $TG_checkKey = $this->session->userdata ( 'TG_checkKey' );
  92. $record_id = intval ( $this->input->get ( "id" ) );
  93. if (! $record_id) {
  94. $db_ret = $this->db->insert ( "news_article",
  95. array ('is_temp' => 1, 'create_time' => time () ) );
  96. if ($db_ret) {
  97. $insert_id = $this->db->insert_id ();
  98. redirect (
  99. modify_build_url ( array ('c' => 'articlelist', 'id' => $insert_id ) ) );
  100. }
  101. }
  102. //=============================删除过期的临时数据,begin{{==========================
  103. $time = time () - 3600 * 2;
  104. //删除2小时前的临时
  105. $thumb_sql = "select article_thumb FROM news_article WHERE length(article_thumb)>0 AND is_temp = 1 AND create_time < $time";
  106. $thumb = $this->db->get_rows_by_sql ( $thumb_sql );
  107. foreach ( $thumb as $k => $v ) {
  108. if (file_exists ( FCPATH . $v ['article_thumb'] ) === true) {
  109. unlink ( FCPATH . $v ['article_thumb'] );
  110. }
  111. }
  112. $this->db->query (
  113. sprintf ( "DELETE FROM news_article WHERE is_temp = 1 AND create_time<%s", $time ) );
  114. //=============================删除过期的临时数据,}}end============================
  115. //从数据库中取出该记录
  116. $view_data = array ();
  117. $persist_record = $this->db->get_record_by_field ( "news_article", 'article_id',
  118. $record_id );
  119. $view_data ['row'] = $persist_record;
  120. $view_data ['message'] = null;
  121. //表单验证规则
  122. $this->form_validation->set_rules ( 'article_title', '文章标题', "required" );
  123. $this->form_validation->set_rules ( 'article_description', '文章简介', "required" );
  124. if ($this->input->post ( 'submit' )) {
  125. if ($this->form_validation->run ()) {
  126. $this->db->where ( 'article_id', $record_id );
  127. $this->db->update ( 'news_article',
  128. array (
  129. 'article_title' => strip_tags (
  130. trim ( $this->input->post ( 'article_title' ) ) ),
  131. 'article_author_name' => strip_tags (
  132. trim ( $this->input->post ( 'article_author_name' ) ) ),
  133. 'article_source' => trim (
  134. $this->input->post ( 'article_source' ) ),
  135. 'article_editor' => strip_tags (
  136. trim ( $this->input->post ( 'article_editor' ) ) ),
  137. 'article_description' => trim (
  138. $this->input->post ( 'article_description' ) ),
  139. 'article_content' => trim (
  140. $this->input->post ( 'article_content' ) ),
  141. 'article_tips' => trim ( $this->input->post ( 'article_tips' ) ),
  142. 'publish_time' => time (),
  143. 'modify_time' => time (),
  144. 'article_author_id' => $UID,
  145. 'article_tags' => trim ( $this->input->post ( 'article_tags' ) ),
  146. 'is_temp' => '0' ) );
  147. if ($this->db->affected_rows ()) {
  148. $view_data ['message'] = ("已经写入数据库." . time ());
  149. } else {
  150. $view_data ['message'] = ("没有更新任何内容," . microtime ());
  151. }
  152. //redirect ( site_url ( "c=formlist" ) );
  153. //关闭界面
  154. echo "<script>if(parent.window.close_dialog){parent.window.close_dialog();}</script>";
  155. }
  156. }
  157. //---选择器=-控制分类-
  158. $ab_tags_array = explode ( ",",
  159. str_replace ( "c", "", $persist_record ['article_tags'] ) );
  160. $this->load->model ( 'tag_model' );
  161. $data = null;
  162. $data .= $this->tag_model->build_tag_select ( 'tagselector', array (16 ),
  163. ! empty ( $ab_tags_array ) ? $ab_tags_array : null, 'article_tags' );
  164. $data .= "<style>.selected {border-bottom:1px solid #ff0000;color:red;}
  165. .tag_dimen{color:blue;}
  166. #tagselector {border:1px solid #f0f0f0;padding:6px;}
  167. #div{border:1px solid #f0f0f0;margin:2px;padding:1px;}
  168. #span{border:1px solid #909090;margin:2px;padding:1px;}
  169. </style>";
  170. $view_data ['data_tag'] = $data;
  171. //上传控件
  172. $article_pic = modify_build_url (
  173. array (
  174. 'c' => 'articlelist',
  175. 'm' => 'upload',
  176. 'article_id' => $record_id,
  177. 'TG_loginuserid' => $UID,
  178. 'TG_loginuser' => base64_encode ( $TG_user_name ),
  179. 'TG_checkKey' => $TG_checkKey,
  180. 'input' => 'article_pic' ) );
  181. $pic = array (
  182. 'element_id' => 'article_pic',
  183. 'queue_id' => 'custom_queue',
  184. 'script' => $article_pic,
  185. 'lable' => "false" );
  186. $view_data ['article_picture'] = $this->editors->get_upload ( $pic );
  187. $this->load->view ( 'article/editarticle_view', $view_data );
  188. }
  189. function masterdel() {
  190. $record_id = $this->input->get ( "id" );
  191. $page_num = $this->input->get ( "page_num" );
  192. $record_id = intval ( $record_id );
  193. $thumb = $this->db->get_record_by_sql (
  194. "select*from news_article where length(article_thumb)>0 AND article_id = $record_id" );
  195. if ($thumb ['article_thumb']) {
  196. if (file_exists ( FCPATH . $thumb ['article_thumb'] )) {
  197. unlink ( FCPATH . $thumb ['article_thumb'] );
  198. }
  199. }
  200. $this->db->where ( 'article_id', $record_id );
  201. $success = $this->db->delete ( 'news_article' );
  202. /*if ($success) {
  203. msg ( "",
  204. modify_build_url (
  205. array ('c' => 'articlelist', 'm' => 'index', 'page_num' => $page_num ) ) );
  206. }*/
  207. return;
  208. }
  209. function dao() {
  210. $UID = $this->session->userdata ( 'UID' );
  211. $news = $this->load->database ( 'news', TRUE );
  212. $yi = $news->get_rows_by_sql ( "select * from cms_tagcategory" );
  213. if (count ( $yi )) {
  214. foreach ( $yi as $k => $vm ) {
  215. $category_id = $vm ['tagcategory_id'];
  216. $article_tags = $vm ['id'];
  217. $persist = $news->get_rows_by_sql (
  218. "select * from cms_news_info_content where category_id=$category_id" );
  219. foreach ( $persist as $k => $v ) {
  220. $content_id = $v ['content_id'];
  221. $p = $news->get_record_by_field ( "cms_news_info_content_expand",
  222. 'content_id', $content_id );
  223. $info_content = $p ['info_content'];
  224. $info_summary = $p ['info_summary'];
  225. $data = array (
  226. 'article_title' => $v ['info_title'],
  227. 'info_summary' => $info_summary,
  228. 'article_content' => $info_content,
  229. 'article_author_id' => $UID,
  230. 'article_tags' => "c" . $article_tags . "c",
  231. 'publish_time' => time (),
  232. 'create_time' => time (),
  233. 'is_temp' => 0 );
  234. if ($info_content) {
  235. $ret = $this->db->insert ( "news_article", $data );
  236. //my_debug ( $data );
  237. }
  238. }
  239. }
  240. }
  241. }
  242. function upload() {
  243. if (count ( $_FILES ) < 1) {
  244. my_debug ( "没有发现上传文件" );
  245. return;
  246. }
  247. if (! array_key_exists ( 'Filedata', $_FILES )) {
  248. my_debug ( "非法的上传操作" );
  249. return;
  250. }
  251. $file_name = $_FILES ['Filedata'] ['name'];
  252. $file_name_parts = explode ( '.', $file_name );
  253. if (count ( $file_name_parts ) < 2) {
  254. my_debug ( "非法的文件名" );
  255. return;
  256. }
  257. $len = count ( $file_name_parts );
  258. $file_ext_name = $file_name_parts [$len - 1];
  259. $file_ext_name = strtolower ( $file_ext_name );
  260. if (! $file_ext_name) {
  261. my_debug ( "非法的文件扩展名" );
  262. return;
  263. }
  264. if (! in_array ( $file_ext_name, array ('jpg', 'jpeg', 'png' ) )) {
  265. my_debug ( "只支持这些扩展名:jpg,jpeg,png" );
  266. return;
  267. }
  268. $article_id = $this->input->get ( 'article_id' );
  269. $file_name = $_FILES ['Filedata'] ['name'];
  270. $temp_name = $_FILES ['Filedata'] ['tmp_name'];
  271. $type = $this->input->get ( 'input' );
  272. //确定保存的文件路径
  273. if ($this->input->get ( 'input' )) {
  274. //磁盘上的物理path
  275. $path = sprintf ( "%spublic/resource/%s/%s/%s/", FCPATH, $type,
  276. $this->uid, date ( "Y/m", time () ) );
  277. //数据库中保存的path
  278. $path2 = sprintf ( "public/resource/%s/%s/%s/", $type, $this->uid,
  279. date ( "Y/m", time () ) );
  280. }
  281. create_dir ( $path ); //创建文件保存路径
  282. //如有旧文件,删除
  283. if ($article_id) {
  284. $record = $this->db->get_record_by_field ( 'news_article', 'article_id', $article_id );
  285. $old_path = '';
  286. if ($record ['article_thumb']) {
  287. $old_path = (FCPATH . $record ['article_thumb']);
  288. }
  289. if (file_exists ( $old_path ) === true) {
  290. unlink ( $old_path );
  291. }
  292. }
  293. $save_name = ($article_id . "_" . time () . ".$file_ext_name");
  294. $targetfile = $path . $save_name;
  295. $targetfile1 = $path2 . $save_name;
  296. $path_thumb = $path2 . ($article_id . "_" . time () . "_000.jpg");
  297. //my_debug($path_thumb);
  298. $is_copied = move_uploaded_file ( $temp_name, $targetfile );
  299. if ($is_copied) {
  300. if (in_array ( $file_ext_name, array ('jpg', 'png', 'jpeg' ) )) {
  301. //生成缩略图
  302. # Constants
  303. define ( "THUMB_MAX_WIDTH", 150 );
  304. define ( "THUMB_MAX_HEIGHT", 150 );
  305. # Get image location
  306. $image_path = ($path . $save_name);
  307. # Load image
  308. $img = null;
  309. $ext = strtolower ( end ( explode ( '.', $image_path ) ) );
  310. $img_type = exif_imagetype ( $image_path );
  311. if ($img_type == IMAGETYPE_JPEG) {
  312. $img = imagecreatefromjpeg ( $image_path );
  313. } else if ($img_type == IMAGETYPE_PNG) {
  314. $img = imagecreatefrompng ( $image_path );
  315. # Only if your version of GD includes GIF support
  316. } else if ($img_type == IMAGETYPE_GIF) {
  317. $img = imagecreatefromgif ( $image_path );
  318. }
  319. # If an image was successfully loaded, test the image for size
  320. if ($img) {
  321. # Get image size and scale ratio
  322. $width = imagesx ( $img );
  323. $height = imagesy ( $img );
  324. $scale = min ( THUMB_MAX_WIDTH / $width, THUMB_MAX_HEIGHT / $height );
  325. # If the image is larger than the max shrink it
  326. if ($scale < 1) {
  327. $new_width = floor ( $scale * $width );
  328. $new_height = floor ( $scale * $height );
  329. # Create a new temporary image
  330. $tmp_img = imagecreatetruecolor (
  331. $new_width, $new_height );
  332. # Copy and resize old image into new image
  333. imagecopyresized ( $tmp_img, $img, 0, 0,
  334. 0, 0, $new_width, $new_height, $width, $height );
  335. imagedestroy ( $img );
  336. $img = $tmp_img;
  337. //my_debug ( $path . $path_thumb );
  338. if ($img) {
  339. touch ( FCPATH . $path_thumb );
  340. //imageFilter ( $img, IMG_FILTER_GRAYSCALE );//变为类度图像
  341. imagejpeg ( $img,
  342. FCPATH . $path_thumb, 50 );
  343. }
  344. imagedestroy ( $img );
  345. }
  346. }
  347. } else {
  348. $path_thumb = null;
  349. }
  350. if ($path_thumb) {
  351. if (file_exists ( FCPATH . $path_thumb ) === true) {
  352. } else {
  353. rename ( FCPATH . $targetfile1, FCPATH . $path_thumb );
  354. $path_thumb = $path_thumb;
  355. }
  356. } else {
  357. $path_thumb = null;
  358. }
  359. if (file_exists ( FCPATH . $targetfile1 ) === true) {
  360. unlink ( FCPATH . $targetfile1 );
  361. }
  362. $this->db->where ( 'article_id', $article_id );
  363. $this->db->update ( 'news_article', array ('article_thumb' => $path_thumb ) );
  364. echo "<input id=$type name=$type type=hidden value=$path_thumb>";
  365. echo "<img src=$path_thumb border=0/>";
  366. }
  367. }
  368. }