/applications/blog/controllers/controller.blog.php

https://github.com/MilkZoft/www · PHP · 445 lines · 442 code · 0 blank · 3 comment · 1 complexity · 24ac58ce6f8bd24d7a09128ad8c7ca8e MD5 · raw file

  1. <?php
  2. /**
  3. * Access from index.php:
  4. */
  5. if(!defined("_access")) {
  6. die("Error: You don't have permission to access here...");
  7. }
  8. class Blog_Controller extends ZP_Controller {
  9. private $pagination = NULL;
  10. public function __construct() {
  11. $this->application = $this->app("blog");
  12. $this->config($this->application);
  13. $this->config("applications");
  14. $this->Templates = $this->core("Templates");
  15. $this->Pagination = $this->core("Pagination");
  16. $this->Blog_Model = $this->model("Blog_Model");
  17. $this->Tags_Model = $this->model("Tags_Model");
  18. $helpers = array("router", "time");
  19. $this->helper($helpers);
  20. $this->journal = TRUE;
  21. $this->Templates->theme(_webTheme);
  22. }
  23. public function index() {
  24. if(segment(2) === _category and segment(3) !== _page) {
  25. $this->category();
  26. } elseif(segment(2) === _tag and segment(3)) {
  27. $this->tag();
  28. } elseif(isYear(segment(2)) and isMonth(segment(3)) and isDay(segment(4)) and segment(5) and segment(5) !== _page) {
  29. $this->slug();
  30. } elseif(isYear(segment(2)) and isMonth(segment(3)) and isDay(segment(4))) {
  31. $this->day();
  32. } elseif(isYear(segment(2)) and isMonth(segment(3))) {
  33. $this->month();
  34. } elseif(isYear(segment(2))) {
  35. $this->year();
  36. } else {
  37. if($this->journal and !segment(1)) {
  38. $this->journal();
  39. } else {
  40. $this->last();
  41. }
  42. }
  43. }
  44. public function archive() {
  45. $this->CSS("archive", TRUE);
  46. $date = $this->Blog_Model->getArchive();
  47. if($date) {
  48. $vars["css"] = "";
  49. $vars["date"] = $date;
  50. $this->view("archive", $vars, $this->application);
  51. }
  52. return FALSE;
  53. }
  54. public function mural($limit = 10) {
  55. $this->CSS("mural", $this->application, TRUE);
  56. $this->CSS("slides", NULL, TRUE);
  57. $data = $this->Blog_Model->getMural($limit);
  58. if($data) {
  59. $vars["mural"] = $data;
  60. $this->view("mural", $vars, $this->application);
  61. } else {
  62. return FALSE;
  63. }
  64. }
  65. private function journal() {
  66. $this->title("Journal");
  67. $this->CSS("posts", $this->application);
  68. $this->CSS("journal", $this->application);
  69. $this->CSS("videos", "videos");
  70. $this->CSS("prettyPhoto", "videos");
  71. $this->CSS("pagination");
  72. $this->Videos_Model = $this->model("Videos_Model");
  73. $videos = $this->Videos_Model->getVideos(12);
  74. $data = $this->Blog_Model->getPosts(9, FALSE);
  75. if($data) {
  76. $vars["vblock1"] = "Canal 11";
  77. $vars["vblock2"] = "Videos";
  78. $vars["vblock3"] = "Radio en Línea";
  79. $vars["videos"] = $videos;
  80. $vars["posts"] = $data;
  81. $vars["view"][0] = $this->view("journal", TRUE);
  82. $vars["view"][1] = $this->view("journal", TRUE, "videos");
  83. $this->template("content", $vars);
  84. }
  85. $this->render();
  86. }
  87. private function day() {
  88. $this->CSS("posts", $this->application);
  89. $this->CSS("pagination");
  90. $limit = $this->limit("day");
  91. $data = $this->Blog_Model->getByDate($limit, segment(2), segment(3), segment(4));
  92. if($data) {
  93. $this->title("Blog - ". segment(2) ."/". segment(3) ."/". segment(4));
  94. $vars["posts"] = $data;
  95. $vars["pagination"] = $this->pagination;
  96. $vars["view"] = $this->view("posts", TRUE);
  97. $this->template("content", $vars);
  98. } else {
  99. $this->template("error404");
  100. }
  101. $this->render();
  102. }
  103. private function month() {
  104. $this->CSS("posts", $this->application);
  105. $this->CSS("pagination");
  106. $limit = $this->limit("month");
  107. $data = $this->Blog_Model->getByDate($limit, segment(2), segment(3));
  108. if($data) {
  109. $this->title("Blog - ". segment(2) ."/". segment(3));
  110. $vars["posts"] = $data;
  111. $vars["pagination"] = $this->pagination;
  112. $vars["view"] = $this->view("posts", TRUE);
  113. $this->template("content", $vars);
  114. } else {
  115. $vars["error"] = __("There is not a publications in this month");
  116. $vars["view"] = $this->view("error404", TRUE);
  117. $this->template("content", $vars);
  118. }
  119. $this->render();
  120. }
  121. private function year() {
  122. $this->CSS("posts", $this->application);
  123. $this->CSS("pagination");
  124. $limit = $this->limit("year");
  125. $data = $this->Blog_Model->getByDate($limit, segment(2));
  126. if($data) {
  127. $this->title("Blog - ". segment(2));
  128. $vars["posts"] = $data;
  129. $vars["pagination"] = $this->pagination;
  130. $vars["view"] = $this->view("posts", TRUE);
  131. $this->template("content", $vars);
  132. } else {
  133. $this->template("error404");
  134. }
  135. $this->render();
  136. }
  137. public function getLastPostByCategory($category) {
  138. $data = $this->Blog_Model->getByCategory($category, 1);
  139. if($data) {
  140. $vars["post"] = $data[0];
  141. $this->view("one", $vars, "blog");
  142. }
  143. }
  144. public function category() {
  145. $this->title(ucfirst(segment(3)));
  146. $this->CSS("posts", $this->application);
  147. $this->CSS("pagination");
  148. if(isLang()) {
  149. $category = segment(3);
  150. } else {
  151. $category = segment(2);
  152. }
  153. $limit = $this->limit("categories");
  154. $data = $this->Blog_Model->getByCategory(segment(3), $limit);
  155. if($data) {
  156. $vars["posts"] = $data;
  157. $vars["pagination"] = $this->pagination;
  158. if($category === "politica") {
  159. $vars["view"] = $this->view("politica", TRUE);
  160. } else {
  161. $vars["view"] = $this->view("posts", TRUE);
  162. }
  163. $this->template("content", $vars);
  164. } else {
  165. $this->template("error404");
  166. }
  167. $this->render();
  168. }
  169. public function tag($tag) {
  170. $this->CSS("posts", $this->application);
  171. $this->CSS("pagination");
  172. $limit = $this->limit("tag");
  173. $data = $this->Blog_Model->getByTag($tag, $limit[0], $limit[1]);
  174. if($data) {
  175. $vars["posts"] = $data;
  176. $vars["pagination"] = $this->pagination;
  177. $vars["view"] = $this->view("posts", TRUE);
  178. $this->template("content", $vars);
  179. } else {
  180. $this->template("error404");
  181. }
  182. $this->render();
  183. }
  184. private function slug() {
  185. $this->Comments_Model = $this->model("Comments_Model");
  186. $this->CSS("posts", $this->application);
  187. $this->CSS("comments", $this->application);
  188. if(POST("post-comment")) {
  189. $alert = $this->Comments_Model->saveComments();
  190. } else {
  191. $alert = FALSE;
  192. }
  193. $data = $this->Blog_Model->getPost(segment(5), segment(2), segment(3), segment(4));
  194. $year = (isYear(segment(2))) ? segment(2) : NULL;
  195. $month = (isMonth(segment(3))) ? segment(3) : NULL;
  196. $day = (isDay(segment(4))) ? segment(4) : NULL;
  197. $URL = _webBase . _sh . _webLang . _sh . _blog . _sh . $year . _sh . $month . _sh . $day . _sh . segment(5);
  198. $vars["alert"] = $alert;
  199. $vars["ID_Post"] = $data[0]["post"][0]["ID_Post"];
  200. $vars["dataTags"] = $data[0]["tags"];
  201. $vars["post"] = $data[0]["post"][0];
  202. $vars["dataCategories"] = $data[0]["categories"];
  203. $vars["dataComments"] = $data[0]["comments"];
  204. $vars["URL"] = $URL;
  205. if($data) {
  206. $this->title(decode($data[0]["post"][0]["Title"]));
  207. if($data[0]["post"][0]["Pwd"] === "") {
  208. $vars["view"][0] = $this->view("post", TRUE);
  209. if($data[0]["post"][0]["Enable_Comments"]) {
  210. $vars["view"][1] = $this->view("comments", TRUE);
  211. }
  212. } elseif(POST("access")) {
  213. if(POST("password", "encrypt") === POST("pwd")) {
  214. if(!SESSION("access-id")) {
  215. SESSION("access-id", $data[0]["post"][0]["ID_Post"]);
  216. } else {
  217. SESSION("access-id", $data[0]["post"][0]["ID_Post"]);
  218. }
  219. redirect($URL);
  220. } else {
  221. showAlert("Incorrect password", _webBase . _sh . _webLang . _sh . _blog);
  222. }
  223. } elseif(!SESSION("access-id") and strlen($data[0]["post"][0]["Pwd"]) === 40 and !POST("access")) {
  224. $vars["password"] = $data[0]["post"][0]["Pwd"];
  225. $vars["view"] = $this->view("access", TRUE);
  226. } elseif(SESSION("access-id") === $data[0]["post"][0]["ID_Post"]) {
  227. $vars["view"][0] = $this->view("post", TRUE);
  228. if($data[0]["post"][0]["Enable_Comments"] === "Yes") {
  229. $vars["view"][1] = $this->view("comments", TRUE);
  230. }
  231. } elseif(SESSION("access-id") and SESSION("access-id") !== $data[0]["post"][0]["ID_Post"]) {
  232. $vars["password"] = $data[0]["post"][0]["Pwd"];
  233. $vars["view"] = $this->view("access", TRUE);
  234. }
  235. $this->template("content", $vars);
  236. } else {
  237. $this->template("error404");
  238. }
  239. $this->render();
  240. }
  241. private function last() {
  242. $this->title("Blog");
  243. $this->CSS("posts", $this->application);
  244. $this->CSS("pagination");
  245. $limit = $this->limit();
  246. $data = $this->Blog_Model->getPosts($limit);
  247. if($data) {
  248. $vars["posts"] = $data;
  249. $vars["pagination"] = $this->pagination;
  250. $vars["view"] = $this->view("posts", TRUE);
  251. $this->template("content", $vars);
  252. } else {
  253. $post = __("Welcome to") . " ";
  254. $post .= a(_webName, _webBase) . " ";
  255. $post .= __("this is your first post, going to your") . " ";
  256. $post .= a(__("Control Panel"), _webBase . _sh . _webLang . _sh . _cpanel) . " ";
  257. $post .= __("and when you add a new post this post will be disappear automatically, enjoy it!");
  258. $vars["hello"] = __("Hello World");
  259. $vars["date"] = now(1);
  260. $vars["post"] = $post;
  261. $vars["comments"] = __("No Comments");
  262. $vars["view"] = $this->view("zero", TRUE);
  263. $this->template("content", $vars);
  264. }
  265. $this->render();
  266. }
  267. private function limit($type = "posts") {
  268. $start = 0;
  269. if($type === "posts") {
  270. if(isLang()) {
  271. if(segment(2) === _page and segment(3) > 0) {
  272. $start = (segment(3) * _maxLimit) - _maxLimit;
  273. }
  274. } else {
  275. if(segment(1) === _page and segment(2) > 0) {
  276. $start = (segment(2) * _maxLimit) - _maxLimit;
  277. }
  278. }
  279. $limit = $start .", ". _maxLimit;
  280. $count = $this->Blog_Model->count();
  281. $URL = _webBase . _sh . _webLang . _sh . _blog . _sh . _page . _sh;
  282. } elseif($type === "categories") {
  283. if(isLang()) {
  284. if(segment(2) === _category and segment(3) !== _page and segment(4) === _page and segment(5) > 0) {
  285. $start = (segment(5) * _maxLimit) - _maxLimit;
  286. }
  287. } else {
  288. if(segment(1) === _category and segment(2) !== _page and segment(3) === _page and segment(4) > 0) {
  289. $start = (segment(4) * _maxLimit) - _maxLimit;
  290. }
  291. }
  292. $limit = $start .", ". _maxLimit;
  293. $URL = _webBase . _sh . _webLang . _sh . _blog . _sh . _category . _sh . segment(3) . _sh . _page . _sh;
  294. $count = $this->Blog_Model->count("categories");
  295. } elseif($type === "day") {
  296. if(isLang()) {
  297. if(isYear(segment(2)) and isMonth(segment(3)) and isDay(segment(4)) and segment(5) === _page and segment(6) > 0) {
  298. $start = (segment(6) * _maxLimit) - _maxLimit;
  299. }
  300. } else {
  301. if(isYear(segment(1)) and isMonth(segment(2)) and isDay(segment(3)) and segment(4) === _page and segment(5) > 0) {
  302. $start = (segment(5) * _maxLimit) - _maxLimit;
  303. }
  304. }
  305. $limit = $start .", ". _maxLimit;
  306. $count = $this->Blog_Model->count("posts");
  307. $URL = _webBase . _sh . _webLang . _sh . _blog . _sh . segment(2) . _sh . segment(3) . _sh . segment(4) . _sh . _page . _sh;
  308. } elseif($type === "month") {
  309. if(isLang()) {
  310. if(isYear(segment(2)) and isMonth(segment(3)) and segment(4) === _page and segment(5) > 0) {
  311. $start = (segment(5) * _maxLimit) - _maxLimit;
  312. }
  313. } else {
  314. if(isYear(segment(1)) and isMonth(segment(2)) and segment(3) === _page and segment(4) > 0) {
  315. $start = (segment(4) * _maxLimit) - _maxLimit;
  316. }
  317. }
  318. $limit = $start .", ". _maxLimit;
  319. $count = $this->Blog_Model->count("posts");
  320. $URL = _webBase . _sh . _webLang . _sh . _blog . _sh . segment(2) . _sh . segment(3) . _sh . _page . _sh;
  321. } elseif($type === "year") {
  322. if(isLang()) {
  323. if(isYear(segment(2)) and segment(3) === _page and segment(4) > 0) {
  324. $start = (segment(4) * _maxLimit) - _maxLimit;
  325. }
  326. } else {
  327. if(isYear(segment(1)) and segment(2) === _page and segment(3) > 0) {
  328. $start = (segment(3) * _maxLimit) - _maxLimit;
  329. }
  330. }
  331. $limit = $start .", ". _maxLimit;
  332. $count = $this->Blog_Model->count("posts");
  333. $URL = _webBase . _sh . _webLang . _sh . _blog . _sh . segment(2) . _sh . _page . _sh;
  334. } elseif($type === "tag") {
  335. if(isLang()) {
  336. if(segment(2) === _tag and segment(3) and segment(4) === _page and segment(5) > 0) {
  337. $start = (segment(5) * _maxLimit) - _maxLimit;
  338. }
  339. } else {
  340. if(segment(1) === _tag and segment(2) and segment(3) === _page and segment(4) > 0) {
  341. $start = (segment(4) * _maxLimit) - _maxLimit;
  342. }
  343. }
  344. $limit = $start .", ". _maxLimit;
  345. $count = $this->Blog_Model->count("tag");
  346. $URL = _webBase . _sh . _webLang . _sh . _blog . _sh . _tag . _sh . segment(3) . _sh . _page . _sh;
  347. }
  348. if($count > _maxLimit) {
  349. $this->pagination = $this->Pagination->paginate($count, _maxLimit, $start, $URL);
  350. } else {
  351. $this->pagination = NULL;
  352. }
  353. return $limit;
  354. }
  355. }