PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/concrete/controllers/single_page/dashboard/system/attributes/sets.php

https://bitbucket.org/saltwaterdev/agilyx.com
PHP | 207 lines | 188 code | 13 blank | 6 comment | 46 complexity | 218ef687c94ad6ec22922e26bd504842 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0, BSD-3-Clause
  1. <?php
  2. namespace Concrete\Controller\SinglePage\Dashboard\System\Attributes;
  3. use Concrete\Core\Attribute\SetManagerInterface;
  4. use Concrete\Core\Entity\Attribute\Key\Key;
  5. use Concrete\Core\Page\Controller\DashboardPageController;
  6. use Loader;
  7. use Concrete\Core\Attribute\Key\Category as AttributeKeyCategory;
  8. use AttributeSet;
  9. class Sets extends DashboardPageController
  10. {
  11. public $category;
  12. public function view()
  13. {
  14. $this->set('categories', AttributeKeyCategory::getList());
  15. }
  16. public function category($categoryID = false, $mode = false)
  17. {
  18. //$this->addHeaderItem('<style type="text/css"> .ccm-attribute-sortable-set-list li a:hover { cursor:move;}</style>');
  19. $this->addFooterItem('<script type="text/javascript">
  20. $(function() {
  21. $("ul.ccm-attribute-sortable-set-list").sortable({
  22. opacity: 0.5,
  23. stop: function() {
  24. var ualist = $(this).sortable(\'serialize\');
  25. ualist += \'&categoryID=' . $categoryID . '\';
  26. $.post(\'' . REL_DIR_FILES_TOOLS_REQUIRED . '/dashboard/attribute_set_order_update\', ualist, function(r) {
  27. });
  28. }
  29. });
  30. });
  31. </script>');
  32. if (!intval($categoryID)) {
  33. $this->redirect('/dashboard/system/attributes/sets');
  34. }
  35. $this->category = AttributeKeyCategory::getByID($categoryID);
  36. if (is_object($this->category)) {
  37. $sets = $this->category->getController()->getSetManager()->getAttributeSets();
  38. $this->set('sets', $sets);
  39. } else {
  40. $this->redirect('/dashboard/system/attributes/sets');
  41. }
  42. $this->set('categoryID', $categoryID);
  43. switch ($mode) {
  44. case 'set_added':
  45. $this->set('message', t('Attribute set added.'));
  46. break;
  47. case 'set_deleted':
  48. $this->set('message', t('Attribute set deleted.'));
  49. break;
  50. case 'set_updated':
  51. $this->set('message', t('Attribute set updated.'));
  52. break;
  53. }
  54. }
  55. public function add_set()
  56. {
  57. $this->category($this->post('categoryID'));
  58. if ($this->token->validate('add_set')) {
  59. if (!trim($this->post('asHandle'))) {
  60. $this->error->add(t("Specify a handle for your attribute set."));
  61. } else {
  62. $as = AttributeSet::getByHandle($this->post('asHandle'));
  63. if (is_object($as)) {
  64. $this->error->add(t('That handle is in use.'));
  65. }
  66. }
  67. if (!trim($this->post('asName'))) {
  68. $this->error->add(t("Specify a name for your attribute set."));
  69. } else {
  70. if (preg_match('/[<>;{}?"`]/', trim($this->post('asName')))) {
  71. $this->error->add(t('Name cannot contain the characters: %s',
  72. Loader::helper('text')->entities('<>;{}?`')));
  73. }
  74. }
  75. if (!$this->error->has()) {
  76. /**
  77. * @var $manager SetManagerInterface
  78. */
  79. $manager = $this->category->getController()->getSetManager();
  80. if ($manager->allowAttributeSets()) {
  81. $manager->addSet($this->post('asHandle'), $this->post('asName'), false, 0);
  82. $this->redirect('dashboard/system/attributes/sets', 'category',
  83. $this->category->getAttributeKeyCategoryID(), 'set_added');
  84. }
  85. }
  86. } else {
  87. $this->error->add($this->token->getErrorMessage());
  88. }
  89. }
  90. public function update_set()
  91. {
  92. $this->edit($this->post('asID'));
  93. if ($this->token->validate('update_set')) {
  94. $as = AttributeSet::getByID($this->post('asID'));
  95. if (!is_object($as)) {
  96. $this->error->add(t('Invalid attribute set.'));
  97. } else {
  98. if (!trim($this->post('asHandle')) && (!$as->isAttributeSetLocked())) {
  99. $this->error->add(t("Specify a handle for your attribute set."));
  100. } else {
  101. $asx = AttributeSet::getByHandle($this->post('asHandle'));
  102. if (is_object($asx) && $asx->getAttributeSetID() != $as->getAttributeSetID()) {
  103. $this->error->add(t('That handle is in use.'));
  104. }
  105. }
  106. if (!trim($this->post('asName'))) {
  107. $this->error->add(t("Specify a name for your attribute set."));
  108. }
  109. }
  110. if (!$this->error->has()) {
  111. if (!$as->isAttributeSetLocked()) {
  112. $as->setAttributeSetHandle($this->post('asHandle'));
  113. }
  114. $as->setAttributeSetName($this->post('asName'));
  115. $this->entityManager->persist($as);
  116. $this->entityManager->flush();
  117. $this->redirect('dashboard/system/attributes/sets', 'category', $as->getAttributeSetKeyCategoryID(),
  118. 'set_updated');
  119. }
  120. } else {
  121. $this->error->add($this->token->getErrorMessage());
  122. }
  123. }
  124. public function update_set_attributes()
  125. {
  126. if ($this->token->validate('update_set_attributes')) {
  127. $as = AttributeSet::getByID($this->post('asID'));
  128. if (!is_object($as)) {
  129. $this->error->add(t('Invalid attribute set.'));
  130. }
  131. if (!$this->error->has()) {
  132. // go through and add all the attributes that aren't in another set
  133. foreach ($as->getAttributeKeyCollection() as $setKey) {
  134. $this->entityManager->remove($setKey);
  135. }
  136. $this->entityManager->flush();
  137. $cat = AttributeKeyCategory::getByID($as->getAttributeSetKeyCategoryID());
  138. $category = $cat->getAttributeKeyCategory();
  139. $unassigned = $category->getSetManager()->getUnassignedAttributeKeys();
  140. if (is_array($this->post('akID'))) {
  141. foreach ($unassigned as $ak) {
  142. if (in_array($ak->getAttributeKeyID(), $this->post('akID'))) {
  143. // This is a lame hack but our legacy support is biting us here:
  144. if (!($ak instanceof Key)) {
  145. $ak = \Concrete\Core\Attribute\Key\Key::getByID($ak->getAttributeKeyID());
  146. }
  147. $as->addKey($ak);
  148. }
  149. }
  150. $this->entityManager->persist($as);
  151. $this->entityManager->flush();
  152. }
  153. $this->redirect('dashboard/system/attributes/sets', 'category', $cat->getAttributeKeyCategoryID(),
  154. 'set_updated');
  155. }
  156. } else {
  157. $this->error->add($this->token->getErrorMessage());
  158. }
  159. $this->edit($this->post('asID'));
  160. }
  161. public function delete_set()
  162. {
  163. if ($this->token->validate('delete_set')) {
  164. $as = AttributeSet::getByID($this->post('asID'));
  165. if (!is_object($as)) {
  166. $this->error->add(t('Invalid attribute set.'));
  167. } else {
  168. if ($as->isAttributeSetLocked()) {
  169. $this->error->add(t('This attribute set is locked. That means it was added by a package and cannot be manually removed.'));
  170. $this->edit($as->getAttributeSetID());
  171. }
  172. }
  173. if (!$this->error->has()) {
  174. $categoryID = $as->getAttributeSetKeyCategoryID();
  175. $this->entityManager->remove($as);
  176. $this->entityManager->flush();
  177. $this->redirect('dashboard/system/attributes/sets', 'category', $categoryID, 'set_deleted');
  178. }
  179. } else {
  180. $this->error->add($this->token->getErrorMessage());
  181. }
  182. $this->view();
  183. }
  184. public function edit($asID = false)
  185. {
  186. $as = AttributeSet::getByID($asID);
  187. if (is_object($as)) {
  188. $this->set('set', $as);
  189. } else {
  190. $this->redirect('/dashboard/system/attributes/sets');
  191. }
  192. }
  193. }