PageRenderTime 39ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/simplify/code/SimplifyAction.php

https://bitbucket.org/xananax/projects_zena_assi
PHP | 229 lines | 131 code | 31 blank | 67 comment | 26 complexity | c857d8abe768882c32e33fa32bfb8bd2 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT, LGPL-2.1, GPL-2.0
  1. <?php
  2. /**
  3. * SimplifyAction
  4. * Provides url actions to draw the Simplify Field Tree, and toggle Permissions on or off
  5. *
  6. * @package simplify
  7. */
  8. class SimplifyAction extends Controller {
  9. static $url_segment = 'simplify';
  10. static $url_rule = '/$Action/$ID';
  11. static $group = null;
  12. /**
  13. * url action function that calls PageTree with the given group
  14. * action: simplify/drawTree/ID action where ID is the group Code
  15. * eg; simplify/drawTree/administrators
  16. *
  17. * @return string A fully formatted tree as a HTML list
  18. */
  19. public static function drawTree() {
  20. //ID = Group Code
  21. $groupCode = Director::urlParam("ID");
  22. $group = DataObject::get_one("Group", "Code = '{$groupCode}'");
  23. if ($group) {
  24. return self::PageTree($group);
  25. }
  26. }
  27. /**
  28. * url action function that toggles a given field permission on or off
  29. * action: simplify/toggleFieldPermission/ID
  30. * where ID = Page|Name|Type|GroupID
  31. * eg; simplify/toggleFieldPermission/Page|Behaviour|1
  32. * TODO: the pipe delimiting is rather eh. refactor.
  33. *
  34. */
  35. public static function toggleFieldPermission() {
  36. //ID = Permission "Page|Name|Type|GroupID"
  37. $code = explode("|", Director::urlParam("ID"));
  38. $page = $code[0];
  39. $field = $code[1];
  40. $type = $code[2];
  41. $groupID = $code[3];
  42. //Does perm exist - delete if so
  43. $perm = SimplifyPermission::checkField($page, $field, $type, $groupID);
  44. if ($perm) {
  45. $perm->delete();
  46. } else {
  47. //it doesn't exist, create it
  48. $perm = new SimplifyPermission;
  49. $perm->HidePage = $page;
  50. $perm->HideName = $field;
  51. $perm->HideType = $type;
  52. $perm->GroupID = $groupID;
  53. $perm->write();
  54. }
  55. }
  56. /**
  57. * url action function that toggles a given standard permission on or off
  58. * action: simplify/togglePermission/ID
  59. * where ID = Code|GroupID
  60. * eg; simplify/toggleFieldPermission/SIMPLIFY_HIDE_HELP|1
  61. * TODO: the pipe delimiting is rather eh. refactor.
  62. *
  63. */
  64. public static function togglePermission() {
  65. $id = explode("|", Director::urlParam("ID"));
  66. $code = $id[0];
  67. $groupID = $id[1];
  68. //Does perm exist - deny if so
  69. $perm = DataObject::get_one("Permission", "Code='{$code}' AND GroupID={$groupID}");
  70. if ($perm) {
  71. $perm->delete();
  72. } else {
  73. Permission::grant($groupID, $code);
  74. }
  75. }
  76. /**
  77. * Display a tree of all page types and their children, with perms
  78. *
  79. * @param Group group show permissions related to this group
  80. * @return string A fully formatted tree as a HTML list
  81. */
  82. public static function PageTree($group = null) {
  83. self::$group = $group;
  84. //Get all the page types
  85. $classes = SiteTree::page_type_classes();
  86. $pageTreeList = array();
  87. //We don't want to remove the fields from this list, so disable the remove
  88. SimplifyPermissionProvider::setRemoveEnabled(false);
  89. //Get an instance of each page type, add to an array
  90. //TODO: This works, but only returns the initial field state of the objects
  91. // Might be a better way to scrape ALL the possible fields?
  92. foreach($classes as $class) {
  93. $instance = singleton($class);
  94. if($instance instanceof HiddenClass) continue;
  95. if(!$instance->canCreate()) continue;
  96. $pageTreeList[] = $instance;
  97. }
  98. //Get the children of each page type and return it as an UL
  99. $pageTree = self::getChildrenAsUL($pageTreeList, 0, " id='perm-tree' class='tree' ", "SiteTree");
  100. //Re-enable the remove
  101. SimplifyPermissionProvider::setRemoveEnabled(true);
  102. return $pageTree;
  103. }
  104. /**
  105. *
  106. * Custom getChildrenAsUL - specific for Pages/Tabsets/Tabs/Fields
  107. * TODO this is very slow - improve it!
  108. * TODO could load branches via AJAX instead
  109. */
  110. public function getChildrenAsUL($fields, $level = 0, $ulExtraAttributes = null, $parentPage, &$itemCount = 0) {
  111. $output = "";
  112. $hasNextLevel = false;
  113. //Set to true to remove any node from being displayed. Its children still will be.
  114. $removeNode = false;
  115. //Remove Root, as its not really needed and confuses this tree
  116. if (is_a($fields, "FieldSet") && is_a($fields->First(), "TabSet")) {
  117. $firstField = $fields->First();
  118. $firstField = method_exists($firstField, "Name") ? $firstField->Name() : "";
  119. if ($firstField == "Root"){
  120. $removeNode = true;
  121. }
  122. }
  123. if (!$removeNode) $output = "<ul {$ulExtraAttributes}>\n";
  124. $ulExtraAttributes = null;
  125. foreach($fields as $field) {
  126. $css = '';
  127. $display = '';
  128. $recurse = false;
  129. $name = '';
  130. $type = '';
  131. //Handle Page classes and children (getCMSFields)
  132. if (is_a($field, "Page")) {
  133. $css .= "tree-page ";
  134. $recurse = true;
  135. $name = $field->class;
  136. $display = $field->class;
  137. $parentPage = $field->class;
  138. $children = $field->getCMSFields(null);
  139. } else
  140. //Handle TabSet classes and children (Tabs)
  141. if (is_a($field, "TabSet")) {
  142. $css .= "tree-tabset ";
  143. $recurse = true;
  144. $display = method_exists($field, "Name") ? $field->Name() : $field->class;
  145. $name = $display;
  146. $children = $field->Tabs();
  147. } else
  148. //Handle Tab classes and children (Fields)
  149. if(is_a($field, "Tab")) {
  150. $css .= "tree-tab ";
  151. $recurse = true;
  152. $display = method_exists($field, "Name") ? $field->Name() : $field->class;
  153. $name = $display;
  154. $children = $field->Fields();
  155. } else
  156. //Handle all FormField subclasses - excluding LiteralField
  157. //If the class doesn't have a Title, display the class instead
  158. //If the class has a Name, display that in brackets afterwards (maybe, comm for now)
  159. if(is_subclass_of($field, "FormField") and !is_a($field, "LiteralField")) {
  160. $title = method_exists($field, "Title") ? $field->Title() : $field->class;
  161. $name = method_exists($field, "Name") ? $field->Name() : $field->class;
  162. if (!$title) {
  163. $title = $field->class;
  164. }
  165. $css .= "tree-field ";
  166. $display = $title."(".$field->class.")";
  167. } else
  168. //Handle LiteralField classes - the content is HTML, so convert to raw first
  169. if(is_a($field, "LiteralField")) {
  170. $css .= "tree-literal ";
  171. $name = method_exists($field, "Name") ? $field->Name() : $field->class;
  172. $display = Convert::xml2raw($field->getContent());
  173. } else {
  174. //If the item isn't any of the above classes, we don't know what it is...
  175. $css .= "tree-unknown ";
  176. $name = method_exists($field, "Name") ? $field->Name() : $field->class;
  177. $display = $field->class." is an unknown type...";
  178. }
  179. //Find out if this field has a SimplifyPermission entry for the given group
  180. if (SimplifyPermission::checkField($parentPage, $name, $field->class, self::$group)) {
  181. $css .= 'selected ';
  182. }
  183. //Build the page|field|type|group key
  184. $code = $parentPage . "|" . $name . "|" . $field->class . "|" . self::$group->ID;
  185. //Build the node
  186. if (!$removeNode) $output .= "<li class='{$css}'><a href='#' rel='{$code}'>{$display}</a>\n";
  187. //Do the recursive call
  188. if ($recurse) {
  189. $output .= self::getChildrenAsUL($children, $level+1, $ulExtraAttributes, $parentPage);
  190. }
  191. if (!$removeNode) $output .= "</li>\n";
  192. $itemCount++;
  193. }
  194. if (!$removeNode) $output .= "</ul>\n";
  195. return $output;
  196. }
  197. }
  198. ?>