PageRenderTime 59ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/module/LabBase/LabBase/Controller/BrandSettingsController.php

https://gitlab.com/hyandi.work/lovebrand_test_work
PHP | 319 lines | 223 code | 57 blank | 39 comment | 50 complexity | 0e1e36c4381e46ca59b1036091374382 MD5 | raw file
  1. <?php
  2. /**
  3. * Author: Sebi
  4. * Date: 2015-06-14
  5. * Time: 18:53
  6. */
  7. namespace LabBase\Controller;
  8. use LabBase\Model\Brand;
  9. use LabBase\Model\BrandTable;
  10. use LabBase\Model\Category;
  11. use LabBase\Model\CategoryTable;
  12. use LabBase\Model\ImageSaver;
  13. use LabBase\Model\Slim;
  14. use LabBase\Model\UserTable;
  15. use Zend\Mvc\Controller\AbstractActionController;
  16. use Zend\View\Model\JsonModel;
  17. class BrandSettingsController extends AbstractActionController {
  18. /** @var UserTable $userTable */
  19. private $userTable;
  20. /** @var BrandTable $brandTable */
  21. private $brandTable;
  22. /** @var int Maximum avatar size in pixels */
  23. const BRAND_BANNER_MAX_WIDTH = 1920;
  24. const BRAND_AVATAR_MAX_WIDTH = 256;
  25. /** @var Brand $brand */
  26. private $brand = null;
  27. private $uploaded = false;
  28. public function indexAction() {
  29. return [];
  30. }
  31. /**
  32. * Post parameter 'imageString' with base64 of the image or 'image' file required
  33. * @param $imagePrefix string Image will be saved with id of $prefix-$brandId
  34. * @param $maxWidth int
  35. * @param $maxHeight int
  36. * @return JsonModel|bool or redirect (depends on whether 'image' file is present or 'imageString' post value is present)
  37. */
  38. private function uploadImage($imagePrefix, $maxWidth, $maxHeight) {
  39. /** @var \Zend\Http\Request $req */
  40. $req = $this->getRequest();
  41. $brandUrl = $this->params('url', false);
  42. if(!$req->isPost()) {
  43. return new JsonModel(['status' => 'Only POST is supported']);
  44. }
  45. $isAsyncRequest = false;
  46. // Try getting imageString from POST imageString
  47. $imageString = $req->getPost('imageString', '');
  48. if(!empty($imageString)) {
  49. $isAsyncRequest = true;
  50. $imageString = base64_decode($imageString);
  51. }
  52. // Try getting imageString from FILES image
  53. if(empty($imageString)) {
  54. $imageString = $req->getFiles('image');
  55. }
  56. // Try getting imageString from Slim
  57. if(empty($imageString)) {
  58. $images = Slim::getImages();
  59. if(empty(!$images)) {
  60. $img = $images[0]['output'];
  61. $imageString = $img['data'];
  62. }
  63. }
  64. if(empty($imageString) || empty($brandUrl)) {
  65. return new JsonModel(['status' => 'Invalid image or brand URL.']);
  66. }
  67. $sl = $this->getServiceLocator();
  68. $this->userTable = $sl->get('UserTable');
  69. $this->brandTable = $sl->get('BrandTable');
  70. $brand = $this->brandTable->getBrandByUrl($brandUrl);
  71. if(!$brand) {
  72. return new JsonModel(['status' => 'An error has occurred. Brand URL points to nonexistent brand.']);
  73. }
  74. $this->brand = $brand;
  75. $user = $this->userTable->getLoggedInUser();
  76. if(!$user) {
  77. return new JsonModel(['status' => 'An error has occurred. You are not logged in.']);
  78. }
  79. if(!$this->brandTable->isBrandManagedByUser($brand->id, $user->id)) {
  80. return new JsonModel(['status' => 'An error has occurred. You do not have permission to edit this brand.']);
  81. }
  82. /** @var $imageSaver ImageSaver */
  83. $imageSaver = $sl->get('ImageSaver');
  84. // todo: either check the return value or wrap in proper try/catch
  85. $imageSaver->uploadImageSimple($imageString, $imagePrefix, $brand->id, $user->id, $brand->id, null, $maxWidth, $maxHeight, false, true);
  86. //$imageSaver->uploadImage($imageString, $imagePrefix.'-'.$brand->id, $maxWidth, $maxHeight, $user->id, $brand->id);
  87. $this->uploaded = true;
  88. //error_log(var_export($img, true));
  89. if($isAsyncRequest) {
  90. return new JsonModel(['status' => 'ok']);
  91. }else{
  92. $this->redirect()->toRoute("brand", ['url' => $brandUrl]);
  93. return FALSE;
  94. }
  95. }
  96. public function uploadBannerAction() {
  97. $ret = $this->uploadImage('brand-banner', BrandSettingsController::BRAND_BANNER_MAX_WIDTH, BrandSettingsController::BRAND_BANNER_MAX_WIDTH);
  98. if($this->uploaded && !$this->brand->hasBanner) {
  99. $this->brand->hasBanner = true;
  100. $this->brandTable->saveBrand($this->brand);
  101. }
  102. return $ret;
  103. }
  104. public function uploadAvatarAction() {
  105. return $this->uploadImage('brand-avatar', BrandSettingsController::BRAND_AVATAR_MAX_WIDTH, BrandSettingsController::BRAND_AVATAR_MAX_WIDTH);
  106. }
  107. /**
  108. * @param $categories Category[]
  109. * @return array
  110. */
  111. public static function categoryArrayToJson($categories) {
  112. $ret = [];
  113. foreach($categories as $cat) {
  114. $ret[] = [
  115. 'id' => $cat->id,
  116. 'label' => $cat->categoryName,
  117. 'url' => $cat->url,
  118. 'weight' => $cat->position,
  119. 'children' => BrandSettingsController::categoryArrayToJson($cat->children)
  120. ];
  121. }
  122. return $ret;
  123. }
  124. public function editCategoriesAction() {
  125. /** @var BrandTable $brandTable */
  126. $brandTable = $this->getServiceLocator()->get('LabBase\Model\BrandTable');
  127. /** @var CategoryTable $categoryTable */
  128. $categoryTable = $this->getServiceLocator()->get('LabBase\Model\CategoryTable');
  129. /** @var UserTable $userTable */
  130. $userTable = $this->getServiceLocator()->get('LabBase\Model\UserTable');
  131. $user = $userTable->getLoggedInUser();
  132. $brand = $brandTable->getBrandByUrl($this->params()->fromRoute('brandUrl'));
  133. if(!$user || !$brand || !$brandTable->isBrandManagedByUser($brand->id, $user->id)) {
  134. return $this->notFoundAction();
  135. }
  136. $rawCats = $categoryTable->getCategoriesByBrand($brand->id);
  137. return [
  138. 'categoryList' => BrandSettingsController::categoryArrayToJson($rawCats),
  139. 'thisBrand' => $brand
  140. ];
  141. }
  142. public function getCategoriesAction() {
  143. /** @var BrandTable $brandTable */
  144. $brandTable = $this->getServiceLocator()->get('LabBase\Model\BrandTable');
  145. /** @var CategoryTable $categoryTable */
  146. $categoryTable = $this->getServiceLocator()->get('LabBase\Model\CategoryTable');
  147. /** @var UserTable $userTable */
  148. $userTable = $this->getServiceLocator()->get('LabBase\Model\UserTable');
  149. $user = $userTable->getLoggedInUser();
  150. $brand = $brandTable->getBrandByUrl($this->params()->fromRoute('brandUrl'));
  151. if(!$user || !$brand || !$brandTable->isBrandManagedByUser($brand->id, $user->id)) {
  152. return $this->notFoundAction();
  153. }
  154. $rawCats = $categoryTable->getCategoriesByBrand($brand->id);
  155. return new JsonModel([
  156. 'categoryList' => BrandSettingsController::categoryArrayToJson($rawCats),
  157. 'status' => 'ok'
  158. ]);
  159. }
  160. public function saveCategoriesAction() {
  161. /** @var \Zend\Http\Request $req */
  162. $req = $this->getRequest();
  163. if(!$req->isPost() || !$req->getPost('categories')) {
  164. return new JsonModel(['status' => 'Expecting POST. Please try again or contact the site administrator']);
  165. }
  166. /** @var BrandTable $brandTable */
  167. $brandTable = $this->getServiceLocator()->get('LabBase\Model\BrandTable');
  168. /** @var CategoryTable $categoryTable */
  169. $categoryTable = $this->getServiceLocator()->get('LabBase\Model\CategoryTable');
  170. /** @var UserTable $userTable */
  171. $userTable = $this->getServiceLocator()->get('LabBase\Model\UserTable');
  172. $user = $userTable->getLoggedInUser();
  173. $brand = $brandTable->getBrandByUrl($this->params()->fromRoute('brandUrl'));
  174. if(!$user || !$brand || !$brandTable->isBrandManagedByUser($brand->id, $user->id)) {
  175. return new JsonModel(['status' => 'Something went wrong. Please try again or contact the site administrator']);
  176. }
  177. $oldCategories = $categoryTable->getCategoriesByBrand($brand->id);
  178. $postCategories = $req->getPost('categories');
  179. if(count($postCategories) < 1) {
  180. return new JsonModel(['status' => 'You cannot delete all categories.']);
  181. }
  182. // flatten the arrays
  183. foreach($oldCategories as $oldCatFlat) {
  184. foreach($oldCatFlat->children as $child) {
  185. $oldCategories[] = $child;
  186. }
  187. $oldCatFlat->children = [];
  188. }
  189. foreach($postCategories as &$postCatFlat) {
  190. if(isset($postCatFlat['children'])) {
  191. foreach ($postCatFlat['children'] as $child) {
  192. $child['parentId'] = $postCatFlat['id'];
  193. $postCategories[] = $child;
  194. }
  195. }
  196. $postCatFlat['children'] = [];
  197. }
  198. foreach($postCategories as $postCreateNew) {
  199. $found = false;
  200. foreach($oldCategories as $oldCatCreateNew) {
  201. if($postCreateNew['id'] == $oldCatCreateNew->id) {
  202. $catObj = $categoryTable->getCategoryById($postCreateNew['id']);
  203. if($catObj) {
  204. $found = true;
  205. }
  206. break;
  207. }
  208. }
  209. if(!$found) {
  210. $newCat = new Category($postCreateNew['label'],
  211. $categoryTable->generateCategoryUrl($postCreateNew['label'], $brand->id), $brand->id, $postCreateNew['weight'],
  212. true, isset($postCreateNew['parentId']) ? $postCreateNew['parentId'] : null);
  213. $id = $categoryTable->saveCategory($newCat);
  214. foreach ($postCategories as &$postCreateNewLink) {
  215. if(isset($postCreateNewLink['parentId']) && $postCreateNewLink['parentId'] == $postCreateNew['id']) {
  216. $postCreateNewLink['parentId'] = $id;
  217. }
  218. }
  219. }
  220. }
  221. foreach($postCategories as $postUpdate) {
  222. foreach($oldCategories as $oldCatUpdate) {
  223. if($postUpdate['id'] == $oldCatUpdate->id) {
  224. $catObj = $categoryTable->getCategoryById($postUpdate['id']);
  225. if($catObj) {
  226. if($catObj->brandId != $brand->id) {
  227. break;
  228. }
  229. if( $catObj->position != (int) $postUpdate['weight'] ||
  230. $catObj->categoryName != htmlspecialchars($postUpdate['label']) ||
  231. (isset($postUpdate['parentId']) && (int)$catObj->parentId != (int)$postUpdate['parentId']) ||
  232. (!isset($postUpdate['parentId']) && (int)$catObj->parentId != 0))
  233. {
  234. $catObj->position = (int)$postUpdate['weight'];
  235. $catObj->categoryName = htmlspecialchars($postUpdate['label']);
  236. if(isset($postUpdate['parentId']) && $postUpdate['parentId'] > 0) {
  237. $catObj->parentId = (int)$postUpdate['parentId'];
  238. }else{
  239. $catObj->parentId = null;
  240. }
  241. $categoryTable->saveCategory($catObj);
  242. }
  243. }
  244. break;
  245. }
  246. }
  247. }
  248. foreach($oldCategories as $postCatDelete) {
  249. $found = false;
  250. foreach($postCategories as $post) {
  251. if($post['id'] == $postCatDelete->id) {
  252. $found = true;
  253. break;
  254. }
  255. }
  256. if(!$found) {
  257. $categoryTable->deleteCategory($postCatDelete->id);
  258. }
  259. }
  260. return new JsonModel(['status' => 'ok']);
  261. }
  262. }