PageRenderTime 1289ms CodeModel.GetById 34ms RepoModel.GetById 3ms app.codeStats 0ms

/Ip/Internal/InlineManagement/AdminController.php

https://gitlab.com/x33n/ImpressPages
PHP | 510 lines | 389 code | 102 blank | 19 comment | 78 complexity | f961985c19a0d3ac3f9a141a8af965fb MD5 | raw file
  1. <?php
  2. /**
  3. * @package ImpressPages
  4. *
  5. */
  6. namespace Ip\Internal\InlineManagement;
  7. use Ip\Internal\InlineValue\Entity\Scope as Scope;
  8. class AdminController extends \Ip\Controller
  9. {
  10. var $dao;
  11. public function __construct()
  12. {
  13. $this->dao = new Dao();
  14. }
  15. public function getManagementPopupLogo()
  16. {
  17. $cssClass = '';
  18. if (isset($_POST['cssClass'])) {
  19. $cssClass = $_POST['cssClass'];
  20. }
  21. $config = new Config();
  22. $availableFonts = $config->getAvailableFonts();
  23. $popupData = array(
  24. 'availableFonts' => $availableFonts
  25. );
  26. $html = ipView('view/popup/logo.php', $popupData)->render();
  27. $logoStr = $this->dao->getGlobalValue(Dao::PREFIX_LOGO, '');
  28. $logo = new Entity\Logo($logoStr);
  29. $logoData = array(
  30. 'image' => $logo->getImage() ? $logo->getImage() : '',
  31. 'imageOrig' => $logo->getImageOrig() ? $logo->getImageOrig() : '',
  32. 'requiredWidth' => $logo->getRequiredWidth(),
  33. 'requiredHeight' => $logo->getRequiredHeight(),
  34. 'type' => $logo->getType(),
  35. 'x1' => $logo->getX1(),
  36. 'y1' => $logo->getY1(),
  37. 'x2' => $logo->getX2(),
  38. 'y2' => $logo->getY2(),
  39. 'text' => $logo->getText()
  40. );
  41. $service = new Service();
  42. $data = array(
  43. 'status' => 'success',
  44. 'logoData' => $logoData,
  45. 'html' => $html,
  46. 'textPreview' => $service->generateTextLogo($cssClass),
  47. 'imagePreview' => $service->generateImageLogo($cssClass)
  48. );
  49. return new \Ip\Response\Json($data);
  50. }
  51. public function getManagementPopupImage()
  52. {
  53. if (!isset($_POST['key'])) {
  54. throw new \Exception("Required parameter not set");
  55. }
  56. $key = $_POST['key'];
  57. if (!isset($_POST['languageId'])) {
  58. throw new \Exception("Required parameter not set");
  59. }
  60. $languageId = $_POST['languageId'];
  61. if (!isset($_POST['pageId'])) {
  62. throw new \Exception("Required parameter not set");
  63. }
  64. $pageId = $_POST['pageId'];
  65. $imageStr = $this->dao->getValue(Dao::PREFIX_IMAGE, $key, $languageId, $pageId);
  66. $scope = $this->dao->getLastOperationScope();
  67. $types = array();
  68. $scopePageTitle = __('Current page and sub-pages', 'Ip-admin', false);
  69. $scopeParentPageTitle = __('Page "[[page]]" and all sub-pages', 'Ip-admin', false);
  70. $scopeLanguageTitle = __('All [[language]] pages', 'Ip-admin', false);
  71. $scopeAllPagesTitle = __('All pages', 'Ip-admin', false);
  72. $types[Scope::SCOPE_PAGE] = array('title' => $scopePageTitle, 'value' => Scope::SCOPE_PAGE);
  73. if ($scope && $scope->getType() == Scope::SCOPE_PARENT_PAGE) {
  74. $pageName = '';
  75. $scopeParentPageTitle = str_replace('[[page]]', $pageName, $scopeParentPageTitle);
  76. $types[Scope::SCOPE_PARENT_PAGE] = array(
  77. 'title' => $scopeParentPageTitle,
  78. 'value' => Scope::SCOPE_PARENT_PAGE
  79. );
  80. }
  81. $scopeLanguageTitle = str_replace(
  82. '[[language]]',
  83. ipContent()->getLanguage($languageId)->getAbbreviation(),
  84. $scopeLanguageTitle
  85. );
  86. $types[Scope::SCOPE_LANGUAGE] = array('title' => $scopeLanguageTitle, 'value' => Scope::SCOPE_LANGUAGE);
  87. $types[Scope::SCOPE_GLOBAL] = array('title' => $scopeAllPagesTitle, 'value' => Scope::SCOPE_GLOBAL);
  88. if ($scope && isset($types[$scope->getType()])) {
  89. $types[$scope->getType()]['selected'] = true;
  90. } else {
  91. $types[Scope::SCOPE_GLOBAL]['selected'] = true;
  92. }
  93. $popupData = array(
  94. 'types' => $types,
  95. 'showRemoveLink' => $imageStr !== false
  96. );
  97. $html = ipView('view/popup/image.php', $popupData)->render();
  98. $image = new Entity\Logo($imageStr);
  99. $imageData = array(
  100. 'image' => $image->getImage() ? $image->getImage() : '',
  101. 'imageOrig' => $image->getImageOrig() ? $image->getImageOrig() : '',
  102. 'requiredWidth' => $image->getRequiredWidth(),
  103. 'requiredHeight' => $image->getRequiredHeight(),
  104. 'x1' => $image->getX1(),
  105. 'y1' => $image->getY1(),
  106. 'x2' => $image->getX2(),
  107. 'y2' => $image->getY2()
  108. );
  109. $data = array(
  110. "status" => "success",
  111. "imageData" => $imageData,
  112. "html" => $html
  113. );
  114. return new \Ip\Response\Json($data);
  115. }
  116. public function saveLogo()
  117. {
  118. if (!isset($_POST['text']) || !isset($_POST['color']) || !isset($_POST['font']) || !isset($_POST['type'])) {
  119. $this->jsonError("Missing post data");
  120. }
  121. //STORE TEXT LOGO
  122. $logoStr = $this->dao->getGlobalValue(Dao::PREFIX_LOGO, '');
  123. $logo = new Entity\Logo($logoStr);
  124. $logo->setText($_POST['text']);
  125. $logo->setColor($_POST['color']);
  126. $logo->setFont(isset($_POST['font']) ? $_POST['font'] : null);
  127. if ($_POST['type'] == Entity\Logo::TYPE_IMAGE) {
  128. $logo->setType(Entity\Logo::TYPE_IMAGE);
  129. } else {
  130. $logo->setType(Entity\Logo::TYPE_TEXT);
  131. }
  132. //STORE IMAGE LOGO
  133. if (isset($_POST['newImage']) && is_file(ipFile('file/repository/' . $_POST['newImage']))) {
  134. //remove old image
  135. if ($logo->getImageOrig()) {
  136. \Ip\Internal\Repository\Model::unbindFile(
  137. $logo->getImageOrig(),
  138. 'developer/inline_management',
  139. 1
  140. ); //1 means logo
  141. }
  142. \Ip\Internal\Repository\Model::bindFile(
  143. $_POST['newImage'],
  144. 'developer/inline_management',
  145. 1
  146. ); //1 means logo
  147. $logo->setImageOrig($_POST['newImage']);
  148. }
  149. if (isset($_POST['cropX1']) && isset($_POST['cropY1']) && isset($_POST['cropX2']) && isset($_POST['cropY2']) && isset($_POST['windowWidth']) && isset($_POST['windowHeight'])) {
  150. //new small image
  151. $logo->setX1($_POST['cropX1']);
  152. $logo->setY1($_POST['cropY1']);
  153. $logo->setX2($_POST['cropX2']);
  154. $logo->setY2($_POST['cropY2']);
  155. $logo->setRequiredWidth($_POST['windowWidth']);
  156. $logo->setRequiredHeight($_POST['windowHeight']);
  157. }
  158. $this->dao->setGlobalValue(Dao::PREFIX_LOGO, '', $logo->getValueStr());
  159. $inlineManagementService = new Service();
  160. $cssClass = null;
  161. if (isset($_POST['cssClass'])) {
  162. $cssClass = $_POST['cssClass'];
  163. }
  164. $data = array(
  165. "status" => "success",
  166. "logoHtml" => $inlineManagementService->generateManagedLogo($cssClass)
  167. );
  168. return new \Ip\Response\Json($data);
  169. }
  170. public function saveText()
  171. {
  172. $inlineManagementService = new Service();
  173. if (!isset($_POST['key']) || !isset($_POST['cssClass']) || !isset($_POST['htmlTag']) || !isset($_POST['value']) || !isset($_POST['languageId'])) {
  174. throw new \Exception("Required parameters missing");
  175. }
  176. $key = $_POST['key'];
  177. $tag = $_POST['htmlTag'];
  178. $cssClass = $_POST['cssClass'];
  179. $value = $_POST['value'];
  180. $languageId = $_POST['languageId'];
  181. $this->dao->setLanguageValue(Dao::PREFIX_TEXT, $key, $languageId, $value);
  182. $data = array(
  183. "status" => "success",
  184. "stringHtml" => $inlineManagementService->generateManagedText($key, $tag, null, $cssClass)
  185. );
  186. return new \Ip\Response\Json($data);
  187. }
  188. public function saveImage()
  189. {
  190. if (!isset($_POST['key'])) {
  191. throw new \Exception("Required parameter not set");
  192. }
  193. $key = $_POST['key'];
  194. if (!isset($_POST['type'])) {
  195. throw new \Exception("Required parameter not set");
  196. }
  197. $type = $_POST['type'];
  198. if (!isset($_POST['cssClass'])) {
  199. throw new \Exception("Required parameter not set");
  200. }
  201. $cssClass = $_POST['cssClass'];
  202. if (!isset($_POST['defaultValue'])) {
  203. throw new \Exception("Required parameter not set");
  204. }
  205. $defaultValue = $_POST['defaultValue'];
  206. if (!isset($_POST['options'])) {
  207. $options = array();
  208. } else {
  209. $options = $_POST['options'];
  210. }
  211. if (!isset($_POST['languageId'])) {
  212. throw new \Exception("Required parameter not set");
  213. }
  214. $languageId = $_POST['languageId'];
  215. if (!isset($_POST['pageId'])) {
  216. throw new \Exception("Required parameter not set");
  217. }
  218. $pageId = $_POST['pageId'];
  219. $imageStr = $this->dao->getValue(Dao::PREFIX_IMAGE, $key, $languageId, $pageId);
  220. $image = new Entity\Image($imageStr);
  221. $scope = $this->dao->getLastOperationScope();
  222. $sameScope = $scope && $scope->getType() == $type;
  223. //STORE IMAGE
  224. if (isset($_POST['newImage']) && is_file(ipFile('file/repository/' . $_POST['newImage']))) {
  225. //remove old image
  226. if ($image->getImageOrig() && is_file(ipFile($image->getImageOrig()))) {
  227. if ($sameScope) { //otherwise we need to leave image for original scope
  228. \Ip\Internal\Repository\Model::unbindFile(
  229. $image->getImageOrig(),
  230. 'developer/inline_management',
  231. $image->getId()
  232. );
  233. }
  234. }
  235. \Ip\Internal\Repository\Model::bindFile(
  236. $_POST['newImage'],
  237. 'developer/inline_management',
  238. $image->getId()
  239. ); //1 means logo
  240. $image->setImageOrig($_POST['newImage']);
  241. } else {
  242. if (!$sameScope) { //duplicate original image if we are resaving it in different scope
  243. if ($image->getImageOrig() && is_file(ipFile($image->getImageOrig()))) {
  244. \Ip\Internal\Repository\Model::bindFile(
  245. $image->getImageOrig(),
  246. 'developer/inline_management',
  247. $image->getId()
  248. );
  249. $image->setImageOrig($image->getImageOrig());
  250. }
  251. }
  252. }
  253. if (isset($_POST['cropX1']) && isset($_POST['cropY1']) && isset($_POST['cropX2']) && isset($_POST['cropY2']) && isset($_POST['windowWidth']) && isset($_POST['windowHeight'])) {
  254. //new small image
  255. $image->setX1($_POST['cropX1']);
  256. $image->setY1($_POST['cropY1']);
  257. $image->setX2($_POST['cropX2']);
  258. $image->setY2($_POST['cropY2']);
  259. $image->setRequiredWidth($_POST['windowWidth']);
  260. $image->setRequiredHeight($_POST['windowHeight']);
  261. } else {
  262. if (!$sameScope) {
  263. //in this place cropped image should be duplicated. But after implementation of reflection service it is not used
  264. }
  265. }
  266. if (!$sameScope) {
  267. //we are trying to save into different scope. We need to delete any images that could exist there
  268. switch ($type) {
  269. case Scope::SCOPE_PAGE:
  270. //this always should return false. But just in case JS part would change, we implement it.
  271. $oldImageStr = $this->dao->getPageValue(Dao::PREFIX_IMAGE, $key, $languageId, $pageId);
  272. break;
  273. case Scope::SCOPE_PARENT_PAGE:
  274. trigger_error(
  275. "developer/inline_management",
  276. "Unexpected situation"
  277. ); //there is no option to save to parent if $sameScope is true.
  278. break;
  279. case Scope::SCOPE_LANGUAGE:
  280. $oldImageStr = $this->dao->getLanguageValue(Dao::PREFIX_IMAGE, $key, $languageId);
  281. break;
  282. case Scope::SCOPE_GLOBAL:
  283. $oldImageStr = $this->dao->getGlobalValue(Dao::PREFIX_IMAGE, $key);
  284. break;
  285. }
  286. if ($oldImageStr) {
  287. $oldScope = $this->dao->getLastOperationScope();
  288. if ($oldScope->getType() == $type
  289. ) { //if really have old image in this scope. If $oldScope != $type, we got global image - not from the scope we are saving in
  290. $oldImage = new Entity\Image($oldImageStr);
  291. $this->removeImageRecord($oldImage, $key, $oldScope);
  292. }
  293. }
  294. }
  295. switch ($type) {
  296. case Scope::SCOPE_PAGE:
  297. $this->dao->setPageValue(Dao::PREFIX_IMAGE, $key, $languageId, $pageId, $image->getValueStr());
  298. break;
  299. case Scope::SCOPE_PARENT_PAGE:
  300. $this->dao->setPageValue(
  301. Dao::PREFIX_IMAGE,
  302. $key,
  303. $scope->getLanguageId(),
  304. $scope->getPageId(),
  305. $image->getValueStr()
  306. );
  307. break;
  308. case Scope::SCOPE_LANGUAGE:
  309. $this->dao->setLanguageValue(Dao::PREFIX_IMAGE, $key, $languageId, $image->getValueStr());
  310. break;
  311. case Scope::SCOPE_GLOBAL:
  312. default:
  313. $this->dao->setGlobalValue(Dao::PREFIX_IMAGE, $key, $image->getValueStr());
  314. break;
  315. }
  316. $inlineManagementService = new Service();
  317. $options['languageId'] = $languageId;
  318. $options['pageId'] = $pageId;
  319. $newHtml = $inlineManagementService->generateManagedImage($key, $defaultValue, $options, $cssClass);
  320. $data = array(
  321. "status" => "success",
  322. "newHtml" => $newHtml
  323. );
  324. return new \Ip\Response\Json($data);
  325. }
  326. public function removeImage()
  327. {
  328. if (!isset($_POST['key'])) {
  329. throw new \Exception("Required parameter not set");
  330. }
  331. $key = $_POST['key'];
  332. if (!isset($_POST['cssClass'])) {
  333. throw new \Exception("Required parameter not set");
  334. }
  335. $cssClass = $_POST['cssClass'];
  336. if (!isset($_POST['defaultValue'])) {
  337. throw new \Exception("Required parameter not set");
  338. }
  339. $defaultValue = $_POST['defaultValue'];
  340. if (!isset($_POST['options'])) {
  341. $options = array();
  342. } else {
  343. $options = $_POST['options'];
  344. }
  345. if (!isset($_POST['languageId'])) {
  346. throw new \Exception("Required parameter not set");
  347. }
  348. $languageId = $_POST['languageId'];
  349. if (!isset($_POST['pageId'])) {
  350. throw new \Exception("Required parameter not set");
  351. }
  352. $pageId = $_POST['pageId'];
  353. $imageStr = $this->dao->getValue(Dao::PREFIX_IMAGE, $key, $languageId, $pageId);
  354. if ($imageStr) {
  355. $image = new Entity\Image($imageStr);
  356. $scope = $this->dao->getLastOperationScope();
  357. $this->removeImageRecord($image, $key, $scope);
  358. }
  359. $inlineManagementService = new Service();
  360. $options['languageId'] = $languageId;
  361. $options['pageId'] = $pageId;
  362. $newHtml = $inlineManagementService->generateManagedImage($key, $defaultValue, $options, $cssClass);
  363. $data = array(
  364. "status" => "success",
  365. "newHtml" => $newHtml
  366. );
  367. return new \Ip\Response\Json($data);
  368. }
  369. /**
  370. * @param Entity\Image $image
  371. * @param string $key
  372. * @param \Ip\Internal\InlineValue\Entity\Scope $scope
  373. */
  374. private function removeImageRecord($image, $key, $scope)
  375. {
  376. if ($scope) {
  377. switch ($scope->getType()) {
  378. case Scope::SCOPE_PAGE:
  379. case Scope::SCOPE_PARENT_PAGE:
  380. $this->dao->deletePageValue(Dao::PREFIX_IMAGE, $key, $scope->getPageId());
  381. break;
  382. case Scope::SCOPE_LANGUAGE:
  383. $this->dao->deleteLanguageValue(Dao::PREFIX_IMAGE, $key, $scope->getLanguageId());
  384. break;
  385. case Scope::SCOPE_GLOBAL:
  386. $this->dao->deleteGlobalValue(Dao::PREFIX_IMAGE, $key);
  387. break;
  388. }
  389. if ($image) {
  390. if ($image->getImageOrig() && is_file(ipFile($image->getImageOrig()))) {
  391. \Ip\Internal\Repository\Model::unbindFile(
  392. $image->getImageOrig(),
  393. 'developer/inline_management',
  394. $image->getId()
  395. );
  396. }
  397. }
  398. }
  399. }
  400. private function jsonError($errorMessage)
  401. {
  402. $data = array(
  403. "status" => "error",
  404. "error" => $errorMessage
  405. );
  406. return new \Ip\Response\Json($data);
  407. }
  408. }