PageRenderTime 33ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/content/content.tree.php

https://github.com/6ui11em/nestedcats
PHP | 322 lines | 224 code | 85 blank | 13 comment | 35 complexity | 19cdb267f441364382877562ad669161 MD5 | raw file
  1. <?php
  2. require_once(TOOLKIT . '/class.administrationpage.php');
  3. define_safe('BASE_URL', URL . '/symphony/extension/nestedcats');
  4. define_safe('EXTENSION', URL . '/extensions/nestedcats');
  5. Class contentExtensionNestedcatsTree extends AdministrationPage{
  6. private $_driver;
  7. private $_page;
  8. private $_id;
  9. private $_flag;
  10. private $aTableHead;
  11. function __construct(&$parent){
  12. parent::__construct($parent);
  13. $this->_driver = $this->_Parent->ExtensionManager->create('nestedcats');
  14. }
  15. function view(){
  16. $this->__switchboard('view');
  17. }
  18. function action(){
  19. $this->__switchboard('action');
  20. }
  21. function __switchboard($type){
  22. $this->_page = isset($this->_context[0]) ? $this->_context[0] : 'view';
  23. $this->_id = (!empty($this->_context[1]) && is_numeric($this->_context[1])) ? $this->_context[1] : 0;
  24. $this->_flag = $this->_context[2];
  25. // Notices
  26. if(isset($this->_flag)){
  27. $result = null;
  28. switch($this->_flag){
  29. case 'edited': $result = __('Category updated at %1$s.'); break;
  30. case 'deleted': $result = __('Category deleted'); break;
  31. case 'created': $result = __('Category created at %1$s. <a href="%2$s">Create another?</a>'); break;
  32. }
  33. if ($result)
  34. $this->pageAlert(__(
  35. $result, array(
  36. DateTimeObj::get(__SYM_TIME_FORMAT__),
  37. BASE_URL . '/tree/new/'.$this->_id,
  38. )
  39. ), Alert::SUCCESS);
  40. }
  41. $function = ($type == 'action' ? '__action' : '__view') . ($this->_page == 'view' ? 'Index' : ucfirst($this->_page));
  42. if(!method_exists($this, $function)) {
  43. if($type == 'action') return;
  44. $this->_Parent->errorPageNotFound();
  45. }
  46. return $this->$function();
  47. }
  48. function __viewIndex(){
  49. $this->addStylesheetToHead(EXTENSION . '/assets/nestedcats.css', 'screen', 120);
  50. $this->addScriptToHead(EXTENSION . '/assets/nestedcats.js', 200);
  51. // $this->addScriptToHead(EXTENSION . '/assets/order.js', 210);
  52. $this->setTitle(__('Symphony &ndash; Categories &ndash; View'));
  53. $this->setPageType('table');
  54. $this->appendSubheading(__('Tree'), Widget::Anchor(__('Create New'), BASE_URL . '/tree/new/' . $this->_id, __('Create New'), 'create button'));
  55. $data = $this->_driver->fetch($this->_id, $includeCurrent=false);
  56. if($this->_id != 0){
  57. $this->aTableHead = array(array(__('Nested categories'), 'col'));
  58. // breadcrumbs
  59. if($path = $this->_driver->getPath($this->_id)){
  60. $ul = new XMLElement('ul');
  61. $ul->setAttribute('class','nc-breadcrumbs');
  62. $li = new XMLElement('li');
  63. $li->appendChild(Widget::Anchor(__('Full Tree &#8594;'), BASE_URL . '/tree/view/', __('To the begining')));
  64. $ul->appendChild($li);
  65. foreach($path as $c){
  66. $li = new XMLElement('li');
  67. if($c['id'] == $this->_id){
  68. $a = Widget::Anchor($c['title'], BASE_URL . '/tree/edit/' . $c['id'], __('Edit'));
  69. } else {
  70. $a = Widget::Anchor($c['title'] . ' &#8594;', BASE_URL . '/tree/view/' . $c['id'], __('Category: ') . $c['title']);
  71. }
  72. $li->appendChild($a);
  73. $ul->appendChild($li);
  74. }
  75. $this->Form->appendChild($ul);
  76. }else{
  77. $this->Form->appendChild(new XMLElement('h2', __('Can\'t find category')));
  78. }
  79. } else {
  80. $this->aTableHead = array(array(__('Title'), 'col'));
  81. }
  82. if($data){
  83. $aTableBody = $this->_driver->buildTreeView($data);
  84. } else {
  85. $aTableBody = array(Widget::TableRow(array(Widget::TableData(__('None found'), 'inactive', NULL, count($this->aTableHead)))));
  86. }
  87. $pid = 'pid' . $this->_id;
  88. $table = Widget::Table(Widget::TableHead($this->aTableHead), NULL, Widget::TableBody($aTableBody), NULL, $pid);
  89. $this->Form->appendChild($table);
  90. // WITH SELECTED
  91. $tableActions = new XMLElement('div');
  92. $tableActions->setAttribute('class', 'actions');
  93. $options = array(
  94. array(NULL, false, __('With Selected...')),
  95. array('delete', false, __('Delete'))
  96. );
  97. $wrapDiv = new XMLElement('div');
  98. $wrapDiv->appendChild(Widget::Select('with-selected', $options, array('id' => 'sel')));
  99. $wrapDiv->appendChild(Widget::Input('action[apply]', __('Apply'), 'submit'));
  100. $tableActions->appendChild($wrapDiv);
  101. $notice = new XMLElement('p', __('All nested Categories will be also deleted'));
  102. $notice->setAttribute('id', 'note');
  103. $notice->setAttribute('class', 'hidden');
  104. $tableActions->appendChild($notice);
  105. $this->Form->appendChild($tableActions);
  106. }
  107. function __viewNew(){
  108. $this->addStylesheetToHead(URL . '/extensions/nestedcats/assets/nestedcats.css', 'screen', 120);
  109. $this->addScriptToHead(URL . '/extensions/nestedcats/assets/nestedcats.js', 200);
  110. // $this->addScriptToHead(URL . '/extensions/nestedcats/assets/order.js', 210);
  111. $this->setTitle(__('Symphony &ndash; New Category'));
  112. $this->setPageType('form');
  113. $this->Form->setAttribute('enctype', 'multipart/form-data');
  114. $this->appendSubheading(__('New Category'));
  115. if($this->_id) $parent = $this->_driver->get($this->_id);
  116. // Category Title
  117. $fieldset = new XMLElement('fieldset');
  118. $fieldset->setAttribute('class', 'primary');
  119. $label = Widget::Label(__('Title'));
  120. $label->appendChild(Widget::Input('fields[title]', $_POST['fields']['title'], 'text'));
  121. if($this->_errors['title']){
  122. $label = Widget::wrapFormElementWithError($label, __('This is a required field.'));
  123. }
  124. $fieldset->appendChild($label);
  125. $fieldset->appendChild(Widget::Input('fields[level]', $parent['level'] ? $parent['level'] : 0, 'hidden'));
  126. $fieldset->appendChild(Widget::Input('fields[rgt]', $parent['rgt'] ? $parent['rgt'] : 0, 'hidden'));
  127. $fieldset->appendChild(Widget::Input('fields[parent]', $parent['id'] ? $parent['id'] : 0, 'hidden'));
  128. $this->Form->appendChild($fieldset);
  129. // Parent Category
  130. $fieldset = new XMLElement('fieldset');
  131. $fieldset->setAttribute('class', 'secondary');
  132. $label = Widget::Label(__('Parent Category'));
  133. $select = $this->_driver->buildSelectAtCatsPage(!empty($this->_id) ? $this->_id : $_POST['fields']['parent']);
  134. $label->appendChild($select);
  135. $fieldset->appendChild($label);
  136. $this->Form->appendChild($fieldset);
  137. // Submit
  138. $div = new XMLElement('div');
  139. $div->setAttribute('class', 'actions');
  140. $div->appendChild(Widget::Input('action[save]', __('Create'), 'submit', array('accesskey' => 's')));
  141. $this->Form->appendChild($div);
  142. }
  143. function __viewEdit(){
  144. if(!$cat = $this->_driver->get($this->_id)) $this->_Parent->errorPageNotFound();
  145. $this->addStylesheetToHead(URL . '/extensions/nestedcats/assets/nestedcats.css', 'screen', 120);
  146. $this->addScriptToHead(URL . '/extensions/nestedcats/assets/nestedcats.js', 200);
  147. // $this->addScriptToHead(URL . '/extensions/nestedcats/assets/order.js', 210);
  148. $this->setTitle(__('Symphony &ndash; Edit Category &ndash; ') . $cat['title']);
  149. $this->setPageType('form');
  150. $this->Form->setAttribute('enctype', 'multipart/form-data');
  151. $this->appendSubheading(__('Edit Category ') . $cat['title']);
  152. // breadcrumbs
  153. if($path = $this->_driver->getPath($this->_id)){
  154. $ul = new XMLElement('ul');
  155. $ul->setAttribute('class','nc-breadcrumbs');
  156. $li = new XMLElement('li');
  157. $li->appendChild(Widget::Anchor(__('Full Tree &#8594;'), BASE_URL . '/tree/view/', __('To the begining')));
  158. $ul->appendChild($li);
  159. foreach($path as $c){
  160. $li = new XMLElement('li');
  161. if($c['id'] == $this->_id){
  162. $a = new XMLElement('span', $c['title']);
  163. } else {
  164. $a = Widget::Anchor($c['title'] . ' &#8594;', BASE_URL . '/tree/view/' . $c['id'], __('Category: ') . $c['title']);
  165. }
  166. $li->appendChild($a);
  167. $ul->appendChild($li);
  168. }
  169. $this->Form->appendChild($ul);
  170. }
  171. // Category Title
  172. $fieldset = new XMLElement('fieldset');
  173. $fieldset->setAttribute('class', 'primary');
  174. $label = Widget::Label(__('Title'));
  175. $label->appendChild(Widget::Input('fields[title]', $cat['title'], 'text'));
  176. if($this->_errors['title']){
  177. $label = Widget::wrapFormElementWithError($label, __('This is a required field'));
  178. }
  179. $fieldset->appendChild($label);
  180. $fieldset->appendChild(Widget::Input('fields[level]', $cat['level'] ? $cat['level'] : 0, 'hidden'));
  181. $fieldset->appendChild(Widget::Input('fields[id]', $cat['id'], 'hidden'));
  182. $fieldset->appendChild(Widget::Input('fields[rgt]', $cat['rgt'], 'hidden'));
  183. $fieldset->appendChild(Widget::Input('fields[lft]', $cat['lft'], 'hidden'));
  184. $this->Form->appendChild($fieldset);
  185. // Parent Category
  186. $fieldset = new XMLElement('fieldset');
  187. $fieldset->setAttribute('class', 'secondary');
  188. $label = Widget::Label(__('Parent Category'));
  189. $select = $this->_driver->buildSelectAtCatsPage($cat['parent'], array('lft' => $cat['lft'], 'rgt' => $cat['rgt']));
  190. $label->appendChild($select);
  191. $fieldset->appendChild($label);
  192. $this->Form->appendChild($fieldset);
  193. // Submit
  194. $div = new XMLElement('div');
  195. $div->setAttribute('class', 'actions');
  196. $div->appendChild(Widget::Input('action[edit]', __('Save Changes'), 'submit', array('accesskey' => 's')));
  197. $button = new XMLElement('button', __('Delete'));
  198. $button->setAttributeArray(array('name' => 'action[delete]', 'class' => 'confirm delete', 'title' => __('Delete this Category')));
  199. $div->appendChild($button);
  200. $this->Form->appendChild($div);
  201. }
  202. function __actionIndex(){
  203. if($_POST['with-selected'] == 'delete' && (!empty($_POST['items']))){
  204. $this->_driver->delete($_POST);
  205. redirect(BASE_URL . '/tree/view/' . $this->_id . '/deleted/');
  206. }
  207. }
  208. function __actionNew(){
  209. if(empty($_POST['fields']['title'])) {
  210. $this->_errors = 'title';
  211. $this->pageAlert(__('Title is a required field'), Alert::ERROR);
  212. return;
  213. }
  214. if($this->_driver->newCat($_POST['fields'])) {
  215. redirect(BASE_URL . '/tree/view/' . $_POST['fields']['parent'] . '/created/');
  216. } else {
  217. define_safe('__SYM_DB_INSERT_FAILED__', true);
  218. $this->pageAlert(NULL, AdministrationPage::PAGE_ALERT_ERROR);
  219. }
  220. }
  221. function __actionEdit(){
  222. if(@array_key_exists('update', $_POST['action']) || @array_key_exists('edit', $_POST['action'])){
  223. if(empty($_POST['fields']['title'])) {
  224. $this->_errors = 'title';
  225. $this->pageAlert(__('Title is a required field'), Alert::ERROR);
  226. return;
  227. }
  228. $this->_driver->edit($_POST);
  229. redirect(BASE_URL . '/tree/edit/' . $this->_id . '/edited/');
  230. }
  231. if(@array_key_exists("delete", $_POST['action'])){
  232. $this->_driver->delete($_POST);
  233. redirect(BASE_URL . '/tree/view/' . $_POST['fields']['parent'] . '/deleted/');
  234. }
  235. }
  236. }
  237. ?>