/controller/admin/window.php

https://bitbucket.org/lxa478/qcrt · PHP · 327 lines · 267 code · 49 blank · 11 comment · 19 complexity · 1313937813f068a4ee53afc57875ac64 MD5 · raw file

  1. <?php
  2. class admin_window extends admin{
  3. //*** windows ***
  4. public function view(){
  5. F3::set('menu','windows');
  6. F3::set('header','html/admin/header.html');
  7. F3::set('content','html/admin/windows.html');
  8. F3::set('footer','html/admin/footer.html');
  9. F3::set('js','js/admin/windows.js');
  10. F3::set('html_title','Admin - Windows');
  11. if(F3::get('PARAMS["id"]')){
  12. $store = new Axon('store');
  13. $store->load('id="'.F3::get('PARAMS["id"]').'" AND site_id="'.F3::get('SESSION.site').'"');
  14. if($store->dry()){
  15. F3::reroute('/admin/windows');
  16. }else{
  17. F3::set('store_title', function($id){
  18. $store = new Axon('store');
  19. $store->load('id='.$id.' AND site_id="'.F3::get('SESSION.site').'"');
  20. return $store->name.' - #'.$store->number;
  21. });
  22. $window = new Axon('window');
  23. F3::set('windows', $window->afind('store_id="'.F3::get('PARAMS["id"]').'" AND site_id="'.F3::get('SESSION.site').'"'));
  24. }
  25. }else{
  26. $stores = new Axon('store');
  27. $astores = $stores->afind('site_id="'.F3::get('SESSION.site').'" AND site_id="'.F3::get('SESSION.site').'"', 'number ASC');
  28. F3::set('stores', $astores);
  29. }
  30. //Get Thumbnail Function
  31. F3::set('img_thumb', function($key){
  32. $path_parts = pathinfo($key);
  33. return $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension'];
  34. });
  35. echo Template::serve('html/admin/layout.html');
  36. }
  37. public function create(){
  38. if($_POST){
  39. $window = new Axon('window');
  40. $window->store_id = $_POST['store_id'];
  41. $window->name = $_POST['name']=='' ? $_POST['window_name'] : $_POST['name'];
  42. $window->type = $_POST['name']=='' ? 'window' : 'view';
  43. $window->animation = $_POST['animation'];
  44. $window->plinth = $_POST['plinth'];
  45. $window->track_rod = $_POST['track_rod'];
  46. $window->tinted = $_POST['tinted'];
  47. $window->outlets = $_POST['outlets'];
  48. $window->outlet_number = $_POST['outlet_number'];
  49. $window->outlet_location = $_POST['outlet_location'];
  50. $window->pane_1_w = $_POST['pane_1_w'];
  51. $window->pane_1_h = $_POST['pane_1_h'];
  52. $window->pane_2_w = $_POST['pane_2_w'];
  53. $window->pane_2_h = $_POST['pane_2_h'];
  54. $window->pane_3_w = $_POST['pane_3_w'];
  55. $window->pane_3_h = $_POST['pane_3_h'];
  56. $window->pane_4_w = $_POST['pane_4_w'];
  57. $window->pane_4_h = $_POST['pane_4_h'];
  58. $window->pane_5_w = $_POST['pane_5_w'];
  59. $window->pane_5_h = $_POST['pane_5_h'];
  60. $window->pane_6_w = $_POST['pane_6_w'];
  61. $window->pane_6_h = $_POST['pane_6_h'];
  62. $window->image = $_POST['image'];
  63. $window->notes = $_POST['notes'];
  64. $window->active = 1;
  65. $window->admin_id = F3::get('SESSION.admin');
  66. $window->site_id = F3::get('SESSION.site');
  67. $window->save();
  68. F3::reroute('/admin/windows/'.$_POST['store_id']);
  69. return;
  70. }else{
  71. if(F3::get('PARAMS["id"]')){
  72. $store = new Axon('store');
  73. $store->load('id="'.F3::get('PARAMS["id"]').'" AND site_id="'.F3::get('SESSION.site').'"');
  74. if($store->dry()){
  75. F3::reroute('/admin/windows');
  76. }else{
  77. F3::set('store_title', function($id){
  78. $store = new Axon('store');
  79. $store->load('id='.$id.' AND site_id="'.F3::get('SESSION.site').'"');
  80. return $store->name.' - #'.$store->number;
  81. });
  82. }
  83. }else{
  84. F3::reroute('/admin/windows');
  85. }
  86. $date = new DateTime(null, new DateTimeZone('GMT'));
  87. $date->modify('+24 hour');
  88. $expires = $date->format("Y-m-d\TH:i:s\Z");
  89. $aws_callback = 'http://'.F3::get('SESSION.subdomain').'.qcrt.com/admin/windows/uploadCallback/1';
  90. $MAX_FILE_SIZE = 50 * 1048576;
  91. $folder = F3::get('SESSION.subdomain').'/store_'.F3::get('PARAMS["id"]');
  92. $YOUR_POLICY_DOCUMENT_BASE64_ENCODED = base64_encode('{"expiration": "'.$expires.'","conditions":[{"bucket": "qcrt"},["starts-with", "$key", "'.$folder.'/"],{"acl": "public-read"},{"success_action_redirect": "'.$aws_callback.'"},["content-length-range", 0, '. $MAX_FILE_SIZE .'],["starts-with", "$Content-Type", "image/"]]}');
  93. $YOUR_CALCULATED_SIGNATURE = base64_encode(hash_hmac('sha1', $YOUR_POLICY_DOCUMENT_BASE64_ENCODED, 'B7ey5lcqOvEHsTJzMxDewyqvLUyZ/eVgARBx7Hxg', true));
  94. F3::set('aws_key', CFCredentials::get()->key);
  95. F3::set('aws_policy', $YOUR_POLICY_DOCUMENT_BASE64_ENCODED);
  96. F3::set('aws_signature', $YOUR_CALCULATED_SIGNATURE);
  97. F3::set('aws_user_bucket', 'qcrt');
  98. F3::set('aws_folder', $folder);
  99. F3::set('aws_callback', $aws_callback);
  100. F3::set('menu','windows');
  101. F3::set('header','html/admin/header.html');
  102. F3::set('content','html/admin/windows/new.html');
  103. F3::set('footer','html/admin/footer.html');
  104. F3::set('js','js/admin/windows/new.js');
  105. F3::set('html_title','New Window Page');
  106. echo Template::serve('html/admin/layout.html');
  107. }
  108. }
  109. public function uploadCallback(){
  110. if(isset($_GET['key']) && $_GET['key']!=''){
  111. $file_id = uniqid();
  112. $path_parts = pathinfo($_GET['key']);
  113. $ext = $path_parts['extension'];
  114. $folder = $path_parts['dirname'];
  115. $new_key = $folder.'/'.$file_id.'.'.$ext;
  116. $thumb_key = $folder.'/'.$file_id.'_thumb.'.$ext;
  117. //Copy the object
  118. $s3 = new AmazonS3();
  119. $bucket = $_GET['bucket'];
  120. $response = $s3->copy_object(
  121. array( // Source
  122. 'bucket' => $bucket,
  123. 'filename' => $_GET['key']
  124. ),
  125. array( // Destination
  126. 'bucket' => $bucket,
  127. 'filename' => $new_key
  128. ),
  129. array( // Optional parameters
  130. 'acl' => AmazonS3::ACL_PUBLIC
  131. )
  132. );
  133. //Get Image
  134. $s3 = new AmazonS3();
  135. $url = $s3->get_object_url($bucket, $_GET['key']);
  136. $ch = curl_init();
  137. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  138. curl_setopt($ch, CURLOPT_URL, $url);
  139. $data = curl_exec($ch);
  140. curl_close($ch);
  141. $size = 150; // new image width
  142. $image_source = imagecreatefromstring($data);
  143. $width = imagesx($image_source);
  144. $height = imagesy($image_source);
  145. $aspect_ratio = $height/$width;
  146. if ($width <= $size) {
  147. $new_w = $width;
  148. $new_h = $height;
  149. } else {
  150. $new_w = $size;
  151. $new_h = abs($new_w * $aspect_ratio);
  152. }
  153. $thumb = imagecreatetruecolor($new_w, $new_h);
  154. // Copy resampled makes a smooth thumbnail
  155. imagecopyresampled($thumb, $image_source, 0, 0, 0, 0, $new_w, $new_h, $width, $height);
  156. imagedestroy($image_source);
  157. ob_start();
  158. imagejpeg($thumb);
  159. $image_contents = ob_get_clean();
  160. // Upload an object from a resource (requires size):
  161. $s3->create_object($bucket, $thumb_key, array(
  162. 'body' => $image_contents,
  163. 'acl' => AmazonS3::ACL_PUBLIC
  164. ));
  165. //Delete old object
  166. $response = $s3->delete_object($bucket, $_GET['key']);
  167. //Return File Name
  168. echo $new_key;
  169. }else{}
  170. }
  171. public function delete(){
  172. if(isset($_POST['id']) && $_POST['id']!=''){
  173. $window = new Axon('window');
  174. $window->load('id="'.$_POST['id'].'" AND site_id="'.F3::get('SESSION.site').'"');
  175. if($window->image){
  176. $s3 = new AmazonS3();
  177. $s3->delete_object('qcrt', $window->image);
  178. $path_parts = pathinfo($window->image);
  179. $thumb_key = $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension'];
  180. $s3->delete_object('qcrt', $thumb_key);
  181. }
  182. $window->erase();
  183. echo $_POST['id'];
  184. }
  185. }
  186. public function update(){
  187. if($_POST){
  188. $window = new Axon('window');
  189. $window->load('id="'.$_POST['window_id'].'" AND site_id="'.F3::get('SESSION.site').'"');
  190. //Delete Old Image if Different
  191. if($window->image != $_POST['image'] && $window->image!=''){
  192. $s3 = new AmazonS3();
  193. $s3->delete_object('qcrt', $window->image);
  194. $path_parts = pathinfo($window->image);
  195. $thumb_key = $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension'];
  196. $s3->delete_object('qcrt', $thumb_key);
  197. }
  198. $window->store_id = $_POST['store_id'];
  199. $window->name = $_POST['name']=='' ? $_POST['window_name'] : $_POST['name'];
  200. $window->type = $_POST['name']=='' ? 'window' : 'view';
  201. $window->animation = $_POST['animation'];
  202. $window->plinth = $_POST['plinth'];
  203. $window->track_rod = $_POST['track_rod'];
  204. $window->tinted = $_POST['tinted'];
  205. $window->outlets = $_POST['outlets'];
  206. $window->outlet_number = $_POST['outlet_number'];
  207. $window->outlet_location = $_POST['outlet_location'];
  208. $window->pane_1_w = $_POST['pane_1_w'];
  209. $window->pane_1_h = $_POST['pane_1_h'];
  210. $window->pane_2_w = $_POST['pane_2_w'];
  211. $window->pane_2_h = $_POST['pane_2_h'];
  212. $window->pane_3_w = $_POST['pane_3_w'];
  213. $window->pane_3_h = $_POST['pane_3_h'];
  214. $window->pane_4_w = $_POST['pane_4_w'];
  215. $window->pane_4_h = $_POST['pane_4_h'];
  216. $window->pane_5_w = $_POST['pane_5_w'];
  217. $window->pane_5_h = $_POST['pane_5_h'];
  218. $window->pane_6_w = $_POST['pane_6_w'];
  219. $window->pane_6_h = $_POST['pane_6_h'];
  220. $window->image = $_POST['image'];
  221. $window->notes = $_POST['notes'];
  222. $window->active = isset($_POST['status']) ? 1 : 0;
  223. $window->save();
  224. F3::reroute('/admin/windows/'.$_POST['store_id']);
  225. return;
  226. }else{
  227. $window = new Axon('window');
  228. $window->load('id="'.F3::get('PARAMS["id"]').'" AND site_id="'.F3::get('SESSION.site').'"');
  229. if($window->dry()){
  230. F3::reroute('/admin/windows');
  231. }else{
  232. $date = new DateTime(null, new DateTimeZone('GMT'));
  233. $date->modify('+24 hour');
  234. $expires = $date->format("Y-m-d\TH:i:s\Z");
  235. $aws_callback = 'http://'.F3::get('SESSION.subdomain').'.qcrt.com/admin/windows/uploadCallback/1';
  236. $MAX_FILE_SIZE = 50 * 1048576;
  237. $folder = F3::get('SESSION.subdomain').'/store_'.$window->store_id;
  238. $YOUR_POLICY_DOCUMENT_BASE64_ENCODED = base64_encode('{"expiration": "'.$expires.'","conditions":[{"bucket": "qcrt"},["starts-with", "$key", "'.$folder.'/"],{"acl": "public-read"},{"success_action_redirect": "'.$aws_callback.'"},["content-length-range", 0, '. $MAX_FILE_SIZE .'],["starts-with", "$Content-Type", "image/"]]}');
  239. $YOUR_CALCULATED_SIGNATURE = base64_encode(hash_hmac('sha1', $YOUR_POLICY_DOCUMENT_BASE64_ENCODED, 'B7ey5lcqOvEHsTJzMxDewyqvLUyZ/eVgARBx7Hxg', true));
  240. F3::set('aws_key', CFCredentials::get()->key);
  241. F3::set('aws_policy', $YOUR_POLICY_DOCUMENT_BASE64_ENCODED);
  242. F3::set('aws_signature', $YOUR_CALCULATED_SIGNATURE);
  243. F3::set('aws_user_bucket', 'qcrt');
  244. F3::set('aws_folder', $folder);
  245. F3::set('aws_callback', $aws_callback);
  246. F3::set('store_title', function($id){
  247. $store = new Axon('store');
  248. $store->load("id=$id");
  249. return $store->name.' - #'.$store->number;
  250. });
  251. //Get Thumbnail Function
  252. F3::set('img_thumb', function($key){
  253. $path_parts = pathinfo($key);
  254. return $path_parts['dirname'].'/'.$path_parts['filename'].'_thumb.'.$path_parts['extension'];
  255. });
  256. F3::set('window', $window);
  257. F3::set('menu','windows');
  258. F3::set('header','html/admin/header.html');
  259. F3::set('content','html/admin/windows/edit.html');
  260. F3::set('footer','html/admin/footer.html');
  261. F3::set('js','js/admin/windows/edit.js');
  262. F3::set('html_title','Edit Window Page');
  263. echo Template::serve('html/admin/layout.html');
  264. }
  265. }
  266. }
  267. public function status(){
  268. if(isset($_POST['id']) && $_POST['id']!=''){
  269. $window = new Axon('window');
  270. $window->load('id="'.$_POST['id'].'" AND site_id="'.F3::get('SESSION.site').'"');
  271. $window->active = $_POST['status'];
  272. $window->save();
  273. }
  274. return;
  275. }
  276. //*** END WINDOWS ***
  277. }
  278. ?>