PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/www/applications/gallery/models/model.gallery.php

https://github.com/triartdesign/tudestino
PHP | 429 lines | 426 code | 0 blank | 3 comment | 1 complexity | 91c191bb535254c65f9e26b6899f028e 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 Gallery_Model extends ZP_Model {
  9. private $route;
  10. private $table;
  11. private $primaryKey;
  12. public function __construct() {
  13. $this->Db = $this->db();
  14. $this->helper(array("time", "alerts", "router"));
  15. $this->table = "gallery";
  16. $this->primaryKey = $this->Db->table($this->table);
  17. $this->language = whichLanguage();
  18. }
  19. public function cpanel($action, $limit = NULL, $order = "ID_Image DESC", $search = NULL, $field = NULL, $trash = FALSE) {
  20. $this->Db->table($this->table);
  21. if($action === "edit" or $action === "save") {
  22. $validation = $this->editOrSave($action);
  23. if($validation) {
  24. return $validation;
  25. }
  26. }
  27. if($action === "all") {
  28. return $this->all($trash, $order, $limit);
  29. } elseif($action === "edit") {
  30. return $this->edit();
  31. } elseif($action === "save") {
  32. return $this->save();
  33. } elseif($action === "search") {
  34. return $this->search($search, $field);
  35. }
  36. }
  37. private function all($trash, $order, $limit) {
  38. if($trash === FALSE) {
  39. if(SESSION("ZanUserPrivilege") === _super) {
  40. $data = $this->Db->findBySQL("State != 'Deleted'", NULL, $order, $limit);
  41. } else {
  42. $data = $this->Db->findBySQL("ID_User = '".$_SESSION["ZanAdminID"]."' AND State != 'Deleted'", NULL, $order, $limit);
  43. }
  44. } else {
  45. if(SESSION("ZanUserPrivilege") === _super) {
  46. $data = $this->Db->findBy("State", "Deleted", NULL, $order, $limit);
  47. } else {
  48. $data = $this->Db->findBySQL("ID_User = '". SESSION("ZanAdminID") ."' AND State = 'Deleted'", NULL, $order, $limit);
  49. }
  50. }
  51. return $data;
  52. }
  53. private function editOrSave($action) {
  54. $helpers = array("alerts", "time", "validations");
  55. $this->helper($helpers);
  56. if(POST("title") === NULL) {
  57. return getAlert("You need to write a title");
  58. }
  59. if(POST("category") === NULL and POST("ID_Category") === "0") {
  60. $this->category = 0;
  61. } else {
  62. if(POST("category") !== NULL) {
  63. $this->category = POST("category");
  64. $categorynice = nice($this->category);
  65. $data = $this->Db->call("setCategory('$this->category', '$categorynice', '". getXMLang(_webLang, TRUE) . "', 'Active')");
  66. $this->category = $data[0]["ID_Category"];
  67. } else {
  68. $this->category = POST("ID_Category");
  69. }
  70. }
  71. if($action === "edit") {
  72. if(FILES("file", "name") !== "") {
  73. $this->Files = $this->core("Files");
  74. $this->Files->filename = FILES("file", "name");
  75. $this->Files->fileType = FILES("file", "type");
  76. $this->Files->fileSize = FILES("file", "size");
  77. $this->Files->fileError = FILES("file", "error");
  78. $this->Files->fileTmp = FILES("file", "tmp_name");
  79. if($this->category === NULL or $this->category === 0) {
  80. $dir = _lib . _sh . _files . _sh . _images . _sh . "gallery" . _sh . "unknown" . _sh;
  81. } else {
  82. $this->Db->table("categories", "Nice");
  83. $data = $this->Db->find($this->category);
  84. $dir = _lib . _sh . _files . _sh . _images . _sh . "gallery" . _sh . $data[0]["Nice"] . _sh;
  85. }
  86. if(!file_exists($dir)) {
  87. mkdir($dir, 0777);
  88. }
  89. $upload = $this->Files->upload($dir);
  90. if($upload["upload"] === TRUE) {
  91. $this->Images = $this->core("Images");
  92. $this->original = $this->Images->getResize("original", $dir, $upload["filename"], _minOriginal, _maxOriginal);
  93. $this->medium = $this->Images->getResize("medium", $dir, $upload["filename"], _minOriginal, _maxOriginal);
  94. $this->small = $this->Images->getResize("small", $dir, $upload["filename"], _minOriginal, _maxOriginal);
  95. } else {
  96. return getAlert($upload["message"]);
  97. }
  98. } else {
  99. if($action === "edit") {
  100. $this->original = "";
  101. $this->medium = "";
  102. $this->small = "";
  103. } else {
  104. return getAlert("Selected Image");
  105. }
  106. }
  107. }
  108. $this->ID = POST("ID_Image");
  109. $this->title = POST("title", "decode", "escape");
  110. $this->nice = nice($this->title);
  111. $this->description = POST("description");
  112. $this->state = POST("state");
  113. $this->date1 = now(4);
  114. $this->date2 = now(2);
  115. }
  116. private function save() {
  117. if(is_array(FILES("files", "name"))) {
  118. $filecount = count(FILES("files", "name"));
  119. $this->Files = $this->core("Files");
  120. $i = 0;
  121. $noimage = 0;
  122. foreach($_FILES["files"]["name"] as $file) {
  123. if(FILES("files", "name", $i) !== "") {
  124. $this->Files->filename = FILES("files", "name", $i);
  125. $this->Files->fileType = FILES("files", "type", $i);
  126. $this->Files->fileSize = FILES("files", "size", $i);
  127. $this->Files->fileError = FILES("files", "error", $i);
  128. $this->Files->fileTmp = FILES("files", "tmp_name", $i);
  129. if($this->category === NULL or $this->category === 0) {
  130. $dir = _lib . _sh . _files . _sh . _images . _sh . "gallery" . _sh . "unknown" . _sh;
  131. } else {
  132. $this->Db->table("categories", "Nice");
  133. $data = $this->Db->find($this->category);
  134. $dir = _lib . _sh . _files . _sh . _images . _sh . "gallery" . _sh . $data[0]["Nice"] . _sh;
  135. }
  136. if(!file_exists($dir)) {
  137. mkdir($dir, 0777);
  138. }
  139. $upload = $this->Files->upload($dir);
  140. if($upload["upload"] === TRUE) {
  141. $this->Images = $this->core("Images");
  142. $this->original = $this->Images->getResize("original", $dir, $upload["filename"], _minOriginal, _maxOriginal);
  143. $this->medium = $this->Images->getResize("medium", $dir, $upload["filename"], _minOriginal, _maxOriginal);
  144. $this->small = $this->Images->getResize("small", $dir, $upload["filename"], _minOriginal, _maxOriginal);
  145. } else {
  146. return getAlert($upload["message"]);
  147. }
  148. $query ="setImage(". SESSION("ZanUserID") .", $this->category, '$this->title', '$this->nice', '$this->description', '$this->small', '$this->medium', '$this->original', '$this->date1', '$this->date2', '$this->state')";
  149. $data = $this->Db->call($query);
  150. } else {
  151. $noimage++;
  152. }
  153. $i++;
  154. }
  155. }
  156. if($noimage === $filecount) {
  157. return getAlert("Selected Image");
  158. } else {
  159. return getAlert("The image has been saved correctly", "success");
  160. }
  161. }
  162. private function edit() {
  163. $query = "updateImage($this->ID, $this->category, '$this->title', '$this->nice', '$this->description', '$this->small', '$this->medium', '$this->original', '$this->state')";
  164. $data = $this->Db->call($query);
  165. if(isset($data[0]["Image_Not_Exists"])) {
  166. return getAlert("This image not exists");
  167. }
  168. return getAlert("The image has been edit correctly", "success");
  169. }
  170. public function getByID($ID, $mode = FALSE) {
  171. if($mode === FALSE) {
  172. $data = $this->Db->call("getImage('$ID')");
  173. if(!isset($data[0]["ID_Category"])) {
  174. $data[0]["ID_Category"] = 0;
  175. }
  176. return $data;
  177. } else {
  178. $this->Db->table($this->table);
  179. $this->Db->encode(TRUE);
  180. $record = $this->Db->find($ID);
  181. if($record) {
  182. $data["ID"] = $record[0]["ID_Image"];
  183. $data["Title"] = $record[0]["Title"];
  184. $data["Nice"] = $record[0]["Nice"];
  185. $data["Album"] = $record[0]["Album"];
  186. $data["Album_Nice"] = $record[0]["Album_Nice"];
  187. $data["Description"] = $record[0]["Description"];
  188. $data["Original"] = _webURL . _sh . $record[0]["Original"];
  189. $data["prev"] = _webBase . _sh . _webLang. _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."prev" . _sh . "#Image";
  190. $data["next"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."next" . _sh . "#Image";
  191. $data["home"] = _webBase . _sh . _webLang . _sh . _gallery . _sh;
  192. $data["back"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . "album" . _sh . $data["Album_Nice"];
  193. return $data;
  194. } else {
  195. return false;
  196. }
  197. }
  198. }
  199. public function getCategories() {
  200. $data = $this->Db->call("getCategoriesByApplication('gallery', '". whichLanguage() ."')");
  201. return $data;
  202. }
  203. public function getCount($album = NULL) {
  204. $this->Db->table($this->table);
  205. if(!$album) {
  206. return $this->Db->countBySQL("State = 'Active'");
  207. } else {
  208. return $this->Db->countBySQL("State = 'Active' AND Album_Nice = '$album'");
  209. }
  210. }
  211. public function getByAlbum($album = NULL, $limit) {
  212. $this->Db->table($this->table);
  213. $this->Db->encode(TRUE);
  214. if(!$album) {
  215. $records = $this->Db->findBySQL("State = 'Active'", NULL, "ID_Image Desc", $limit);
  216. if($records) {
  217. $i = 0;
  218. foreach($records as $record) {
  219. $data[$i]["ID_Image"] = $record["ID_Image"];
  220. $data[$i]["Title"] = $record["Title"];
  221. $data[$i]["Nice"] = $record["Nice"];
  222. $data[$i]["Description"] = $record["Description"];
  223. $data[$i]["Small"] = $record["Small"];
  224. $data[$i]["Album"] = $record["Album"];
  225. $data[$i]["Album_Nice"] = $record["Album_Nice"];
  226. $data[$i]["Start_Date"] = $record["Start_Date"];
  227. $data[$i]["Text_Date"] = $record["Text_Date"];
  228. $i++;
  229. }
  230. }
  231. } else {
  232. $records = $this->Db->findBySQL("Album_Nice = '$album' AND State = 'Active'", NULL, "ID_Image Desc", $limit);
  233. if($records) {
  234. $i = 0;
  235. foreach($records as $record) {
  236. $data[$i]["ID_Image"] = $record["ID_Image"];
  237. $data[$i]["Title"] = $record["Title"];
  238. $data[$i]["Nice"] = $record["Nice"];
  239. $data[$i]["Description"] = $record["Description"];
  240. $data[$i]["Small"] = $record["Small"];
  241. $data[$i]["Album"] = $record["Album"];
  242. $data[$i]["Album_Nice"] = $record["Album_Nice"];
  243. $data[$i]["Start_Date"] = $record["Start_Date"];
  244. $data[$i]["Text_Date"] = $record["Text_Date"];
  245. $i++;
  246. }
  247. }
  248. }
  249. if(isset($data)) {
  250. return $data;
  251. } else {
  252. return FALSE;
  253. }
  254. }
  255. public function getNext($ID, $album = "none") {
  256. $this->Db->table($this->table);
  257. $this->Db->encode(TRUE);
  258. $record = $this->Db->findBySQL("ID_Image > '$ID' AND Album_Nice = '$album' AND State = 'Active' LIMIT 1");
  259. if($record) {
  260. $data["ID"] = $record[0]["ID_Image"];
  261. $data["Title"] = $record[0]["Title"];
  262. $data["Nice"] = $record[0]["Nice"];
  263. $data["Album"] = $record[0]["Album"];
  264. $data["Album_Nice"] = $record[0]["Album_Nice"];
  265. $data["Description"] = $record[0]["Description"];
  266. $data["Original"] = _webURL . _sh . $record[0]["Original"];
  267. $data["prev"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."prev" . _sh . "#Image";
  268. $data["next"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."next" . _sh . "#Image";
  269. $data["home"] = _webBase . _sh . _webLang . _sh . _gallery . _sh;
  270. $data["back"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . "album" . _sh . $data["Album_Nice"];
  271. return $data;
  272. } else {
  273. return false;
  274. }
  275. }
  276. public function getPrev($ID, $album = "none") {
  277. $this->Db->table($this->table);
  278. $this->Db->encode(TRUE);
  279. $record = $this->Db->findBySQL("ID_Image < '$ID' AND Album_Nice = '$album' AND State = 'Active' ORDER BY ID_Image Desc LIMIT 1");
  280. #____($record);
  281. if($record) {
  282. $data["ID"] = $record[0]["ID_Image"];
  283. $data["Title"] = $record[0]["Title"];
  284. $data["Nice"] = $record[0]["Nice"];
  285. $data["Album"] = $record[0]["Album"];
  286. $data["Album_Nice"] = $record[0]["Album_Nice"];
  287. $data["Description"] = $record[0]["Description"];
  288. $data["Original"] = _webURL . _sh . $record[0]["Original"];
  289. $data["prev"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."prev" . _sh . "#Image";
  290. $data["next"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."next" . _sh . "#Image";
  291. $data["home"] = _webBase . _sh . _webLang . _sh . _gallery . _sh;
  292. $data["back"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . "album" . _sh . $data["Album_Nice"];
  293. return $data;
  294. } else {
  295. return false;
  296. }
  297. }
  298. public function getLast($album = "none") {
  299. $this->Db->table($this->table);
  300. $this->Db->encode(TRUE);
  301. $record = $this->Db->findBySQL("State = 'Active' AND Album_Nice = '$album' ORDER BY ID_Image DESC LIMIT 1");
  302. if($record) {
  303. $data["ID"] = $record[0]["ID_Image"];
  304. $data["Title"] = $record[0]["Title"];
  305. $data["Nice"] = $record[0]["Nice"];
  306. $data["Album"] = $record[0]["Album"];
  307. $data["Album_Nice"] = $record[0]["Album_Nice"];
  308. $data["Description"] = $record[0]["Description"];
  309. $data["Original"] = _webURL . _sh . $record[0]["Original"];
  310. $data["prev"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."prev" . _sh . "#Image";
  311. $data["next"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."next" . _sh . "#Image";
  312. $data["home"] = _webBase . _sh . _webLang . _sh . _gallery . _sh;
  313. $data["back"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . "album" . _sh . $data["Album_Nice"];
  314. return $data;
  315. } else {
  316. return false;
  317. }
  318. }
  319. public function getFirst($album = "none") {
  320. $this->Db->table($this->table);
  321. $this->Db->encode(TRUE);
  322. $record = $this->Db->findBySQL("State = 'Active' AND Album_Nice = '$album' ORDER BY ID_Image ASC LIMIT 1");
  323. if($record) {
  324. $data["ID"] = $record[0]["ID_Image"];
  325. $data["Title"] = $record[0]["Title"];
  326. $data["Nice"] = $record[0]["Nice"];
  327. $data["Album"] = $record[0]["Album"];
  328. $data["Album_Nice"] = $record[0]["Album_Nice"];
  329. $data["Description"] = $record[0]["Description"];
  330. $data["Original"] = _webURL . _sh . $record[0]["Original"];
  331. $data["prev"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."prev" . _sh . "#Image";
  332. $data["next"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . _image . _sh . $data["ID"] . _sh . $data["Album_Nice"] . _sh ."next" . _sh . "#Image";
  333. $data["home"] = _webBase . _sh . _webLang . _sh . _gallery . _sh;
  334. $data["back"] = _webBase . _sh . _webLang . _sh . _gallery . _sh . "album" . _sh . $data["Album_Nice"];
  335. return $data;
  336. } else {
  337. return false;
  338. }
  339. }
  340. public function getAlbums() {
  341. $this->Db->table($this->table);
  342. $this->Db->encode(TRUE);
  343. $data = $this->Db->findBySQL("State = 'Active' AND Album != 'None' GROUP BY Album", NULL, NULL, NULL);
  344. if($data) {
  345. return $data;
  346. } else {
  347. return false;
  348. }
  349. }
  350. }