PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/frontend/controllers/MangaController.php

https://bitbucket.org/haichau59/manga
PHP | 343 lines | 254 code | 36 blank | 53 comment | 40 complexity | a6e058883ad0b2f154101ae1e02e5809 MD5 | raw file
  1. <?php
  2. class MangaController extends Controller
  3. {
  4. /**
  5. * @return array action filters
  6. */
  7. public function filters()
  8. {
  9. return array(
  10. 'accessControl', // perform access control for CRUD operations
  11. 'postOnly + delete', // we only allow deletion via POST request
  12. );
  13. }
  14. /**
  15. * Specifies the access control rules.
  16. * This method is used by the 'accessControl' filter.
  17. * @return array access control rules
  18. */
  19. public function accessRules()
  20. {
  21. return array(
  22. array('allow', // allow all users to perform 'index' and 'view' actions
  23. 'actions'=>array('index','view', 'search', 'browse', 'rateManga'),
  24. 'users'=>array('*'),
  25. ),
  26. array('allow', // allow authenticated user to perform 'create' and 'update' actions
  27. 'actions'=>array('create','update'),
  28. 'users'=>array('@'),
  29. ),
  30. array('allow', // allow admin user to perform 'admin' and 'delete' actions
  31. 'actions'=>array('admin','delete'),
  32. 'users'=>array('admin'),
  33. ),
  34. array('deny', // deny all users
  35. 'users'=>array('*'),
  36. ),
  37. );
  38. }
  39. /**
  40. * Displays a particular model.
  41. * @param integer $id the ID of the model to be displayed
  42. */
  43. public function actionView($id)
  44. {
  45. if (!isset($id))
  46. return $this->redirect(array("manga/browse"));
  47. $model = Manga::model();
  48. /** @var $asset VodAsset */
  49. $asset = $model->findByAttributes(array('id' => $id, 'status' => 1));
  50. if (!$asset)
  51. return $this->redirect(array("manga/browse"));
  52. $encrypted_id = $this->crypt->encrypt($id);
  53. $cover = MangaCover::model()->findByAttributes(array("manga_id" => $id));
  54. $posterUrl = "";
  55. if($cover != NULL) {
  56. $posterUrl = $cover->url;
  57. }
  58. $related = $asset->getRelatedManga("rand()", 0, 6);
  59. $relatedManga = $related['data'];
  60. for ($i = 0; $i < count($relatedManga); $i++) {
  61. $cover = MangaCover::model()->findByAttributes(array("manga_id" => $relatedManga[$i]['id']));
  62. $relatedPosterUrl = "";
  63. if($cover != NULL) {
  64. $relatedManga[$i]['cover'] = $cover->url;
  65. }
  66. }
  67. // $comments = $asset->getComments(0, 1000);
  68. // $comments = $comments['data'];
  69. // xem co dang ky dich vu chua? neu co la dich vu gi?
  70. $cats = $asset->mangaCategoryMappings;
  71. $nameCats = " ";
  72. foreach ($cats as $cat) {
  73. $catModel = MangaCategory::model()->findByPk($cat->manga_category_id);
  74. $nameCats .= $catModel->display_name . ',';
  75. }
  76. $this->page_id = "manga_index_$id";
  77. $this->render('index', array(
  78. 'asset' => $asset,
  79. 'posterUrl' => $posterUrl,
  80. 'catenames' => $nameCats,
  81. 'related' => $relatedManga,
  82. // 'comments' => $comments,
  83. 'page_id' => 'manga_index'));
  84. }
  85. /**
  86. * Creates a new model.
  87. * If creation is successful, the browser will be redirected to the 'view' page.
  88. */
  89. public function actionCreate()
  90. {
  91. $model=new Manga;
  92. // Uncomment the following line if AJAX validation is needed
  93. // $this->performAjaxValidation($model);
  94. if(isset($_POST['Manga']))
  95. {
  96. $model->attributes=$_POST['Manga'];
  97. if($model->save())
  98. $this->redirect(array('view','id'=>$model->id));
  99. }
  100. $this->render('create',array(
  101. 'model'=>$model,
  102. ));
  103. }
  104. /**
  105. * Updates a particular model.
  106. * If update is successful, the browser will be redirected to the 'view' page.
  107. * @param integer $id the ID of the model to be updated
  108. */
  109. public function actionUpdate($id)
  110. {
  111. $model=$this->loadModel($id);
  112. // Uncomment the following line if AJAX validation is needed
  113. // $this->performAjaxValidation($model);
  114. if(isset($_POST['Manga']))
  115. {
  116. $model->attributes=$_POST['Manga'];
  117. if($model->save())
  118. $this->redirect(array('view','id'=>$model->id));
  119. }
  120. $this->render('update',array(
  121. 'model'=>$model,
  122. ));
  123. }
  124. /**
  125. * Deletes a particular model.
  126. * If deletion is successful, the browser will be redirected to the 'admin' page.
  127. * @param integer $id the ID of the model to be deleted
  128. */
  129. public function actionDelete($id)
  130. {
  131. $this->loadModel($id)->delete();
  132. // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
  133. if(!isset($_GET['ajax']))
  134. $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
  135. }
  136. /**
  137. * Lists all models.
  138. */
  139. public function actionIndex()
  140. {
  141. $dataProvider=new CActiveDataProvider('Manga');
  142. $this->render('index',array(
  143. 'dataProvider'=>$dataProvider,
  144. ));
  145. }
  146. /**
  147. * Manages all models.
  148. */
  149. public function actionAdmin()
  150. {
  151. $model=new Manga('search');
  152. $model->unsetAttributes(); // clear any default values
  153. if(isset($_GET['Manga']))
  154. $model->attributes=$_GET['Manga'];
  155. $this->render('admin',array(
  156. 'model'=>$model,
  157. ));
  158. }
  159. /**
  160. * Returns the data model based on the primary key given in the GET variable.
  161. * If the data model is not found, an HTTP exception will be raised.
  162. * @param integer the ID of the model to be loaded
  163. */
  164. public function loadModel($id)
  165. {
  166. $model=Manga::model()->findByPk($id);
  167. if($model===null)
  168. throw new CHttpException(404,'The requested page does not exist.');
  169. return $model;
  170. }
  171. /**
  172. * Performs the AJAX validation.
  173. * @param CModel the model to be validated
  174. */
  175. protected function performAjaxValidation($model)
  176. {
  177. if(isset($_POST['ajax']) && $_POST['ajax']==='manga-form')
  178. {
  179. echo CActiveForm::validate($model);
  180. Yii::app()->end();
  181. }
  182. }
  183. public function actionSearch($q, $page = 1) {
  184. if (!isset($q) || $q == "") {
  185. return $this->redirect(array("manga/browse"));
  186. }
  187. if (!isset($page) || $page < 1)
  188. $page = 1;
  189. $result = Manga::findManga('','', $page - 1, $this->pageSize, $q);
  190. $assets = $result['data'];
  191. for ($i = 0; $i < count($assets); $i++) {
  192. $cover = MangaCover::model()->findByAttributes(array("manga_id" => $assets[$i]['id']));
  193. $posterUrl = "";
  194. if($cover != NULL) {
  195. $posterUrl = $cover->url;
  196. }
  197. $assets[$i]['posterUrl'] = $posterUrl;
  198. }
  199. $pager = array();
  200. foreach (array('total_result', 'page_number', 'page_size', 'total_page') as $e) {
  201. if (array_key_exists($e, $result))
  202. $pager[$e] = $result[$e];
  203. }
  204. $this->page_id = "manga_search";
  205. $this->render('browse', array(
  206. 'type' => 'search',
  207. 'keyword' => $q,
  208. 'category' => "Kết quả tìm kiếm",
  209. 'assets' => $assets,
  210. 'category_id' => "",
  211. 'order_by' => null,
  212. 'pager' => $pager,
  213. 'page_id' => 'manga_search'
  214. ));
  215. }
  216. public function actionBrowse() {
  217. $category_id = isset($_REQUEST['category']) && preg_match("/^\d+$/", $_REQUEST['category']) ?
  218. $_REQUEST['category'] : null;
  219. $order_by = isset($_REQUEST['order']) && preg_match("/(newest|most_viewed)/", $_REQUEST['order']) ?
  220. $_REQUEST['order'] : "";
  221. $page = isset($_REQUEST['page']) && preg_match("/^\d+$/", $_REQUEST['page']) ?
  222. $_REQUEST['page'] : 0;
  223. $pageSize = isset($_REQUEST['page_size']) && preg_match("/^\d+$/", $_REQUEST['page_size']) ?
  224. $_REQUEST['page_size'] : $this->pageSize;
  225. $keyword = null;
  226. $page = $page > 0 ? $page - 1 : 0;
  227. $db_order = "";
  228. $categoryName = "";
  229. switch ($order_by) {
  230. case "newest":
  231. $db_order = 't.create_date DESC';
  232. $categoryName = "Truyện mới nhất";
  233. break;
  234. case "most_viewed":
  235. $db_order = 't.view_count DESC';
  236. $categoryName = "Xem nhiều nhất";
  237. break;
  238. case "top_rated":
  239. $db_order = 't.rating_count DESC';
  240. $categoryName = "Truyện bình chọn nhiều";
  241. break;
  242. case "most_discussed":
  243. $db_order = 't.comment_count DESC';
  244. $categoryName = "Truyện bình luận nhiều";
  245. break;
  246. case "featured"://not support now
  247. // $models = Asset::model()->findAll();
  248. //break;
  249. default: //case "default":
  250. $order = 'default';
  251. $db_order = "t.create_date DESC, t.display_name_ascii";
  252. $categoryName = "Truyện đang hot";
  253. break;
  254. }
  255. if (isset($category_id)) {
  256. $catModel = new MangaCategory();
  257. $category = $catModel->findByPk($category_id);
  258. if (isset($category)) {
  259. $categoryName = $category->display_name;
  260. }
  261. }
  262. $result = Manga::findManga($category_id, $db_order, $page, $pageSize, $keyword);
  263. $assets = $result['data'];
  264. for ($i = 0; $i < count($assets); $i++) {
  265. $cover = MangaCover::model()->findByAttributes(array("manga_id" => $assets[$i]['id']));
  266. $posterUrl = "";
  267. if($cover != NULL) {
  268. $posterUrl = $cover->url;
  269. }
  270. $assets[$i]['posterUrl'] = $posterUrl;
  271. }
  272. $pager = array();
  273. foreach (array('total_result', 'page_number', 'page_size', 'total_page') as $e) {
  274. if (array_key_exists($e, $result))
  275. $pager[$e] = $result[$e];
  276. }
  277. $this->page_id = "manga_browse";
  278. $this->render('browse', array(
  279. 'type' => 'browse',
  280. 'category' => $categoryName,
  281. 'assets' => $assets,
  282. 'category_id' => $category_id,
  283. 'order_by' => $order_by,
  284. 'pager' => $pager,
  285. 'page_id' => 'manga_browse'
  286. ));
  287. }
  288. public function actionRateManga() {
  289. header('Content-Type: application/json; charset="UTF-8"');
  290. $mangaID = isset($_REQUEST['manga']) ? $_REQUEST['manga'] : 0;
  291. $manga = Manga::model()->findByPk($mangaID);
  292. if($manga == NULL) {
  293. echo json_encode(array('message' => 'Lỗi trong quá trình xử lý, xin vui lòng thử lại sau'));
  294. }
  295. $mobileNumber = $this->msisdn;
  296. $stars = isset($_REQUEST['rate']) ? intval($_REQUEST['rate']) : '0';
  297. $manga->rating = ($manga->rating * $manga->rating_count + $stars)/($manga->rating_count + 1);
  298. $manga->rating_count++;
  299. if (!$manga->update()) {
  300. echo json_encode(array('message' => 'Lỗi trong quá trình xử lý, xin vui lòng thử lại sau'));
  301. Yii::app()->end();
  302. } else {
  303. echo json_encode(array('message' => 'Đánh giá của bạn đã được ghi nhận, cám ơn bạn đã đóng góp ý kiến'));
  304. Yii::app()->end();
  305. }
  306. }
  307. }