PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins_repo/openXWorkflow/www/admin/plugins/openXWorkflow/library/OX/UI/Page/Entity/Header.php

https://github.com/orchestra-io/sample-openx
PHP | 230 lines | 139 code | 37 blank | 54 comment | 14 complexity | 706541b49c97cf0b4bd0fae945e366d2 MD5 | raw file
  1. <?php
  2. /**
  3. * A header for entity list, edit and add pages. Provides support for generating rich
  4. * title with links to parent entities and breadcrumbs.
  5. */
  6. class OX_UI_Page_Entity_Header extends OX_UI_Page_Header
  7. {
  8. /** Page for adding entities */
  9. const PAGE_TYPE_ADD = 'add';
  10. /** Page for editing entities */
  11. const PAGE_TYPE_EDIT = 'edit';
  12. /** Page with lists of entities */
  13. const PAGE_TYPE_LIST = 'list';
  14. /** Page type */
  15. private $type;
  16. /**
  17. * Information about the main entity the page is handling.
  18. *
  19. * @var OX_UI_Page_EntityInfo
  20. */
  21. private $entityInfo;
  22. /**
  23. * Instances of OX_UI_Page_EntityInfo for the current entity's parents (if any) to
  24. * form breadcrumbs.
  25. *
  26. * @var array
  27. */
  28. private $breadcrumbs;
  29. /**
  30. * Builds the header instance for the provided page type, entity and parent entity.
  31. *
  32. * @param $oEntityOrClassName the actual entity or entity class name if the entity
  33. * does not exist yet (e.g. on entity add screen).
  34. * @param $oParentEntity parent entity or null if the entity does not have a
  35. * displayable parent entity.
  36. * */
  37. public function __construct($type, $oEntityOrClassName, $oParentEntity)
  38. {
  39. $this->type = $type;
  40. $this->entityInfo = OX_UI_Page_EntityInfo::getEntityInfo($oEntityOrClassName, $oParentEntity);
  41. $this->breadcrumbs = self::getPageEntityBreadcrumbs($oEntityOrClassName, $oParentEntity);
  42. }
  43. /**
  44. * Builds page title in a plain text form, suitable for e.g. browser window title.
  45. */
  46. public function getTitleText()
  47. {
  48. $result = '';
  49. switch ($this->type) {
  50. case OX_UI_Page_Entity_Header::PAGE_TYPE_LIST :
  51. $result .= $this->entityInfo->getTypeNamePlural();
  52. if (count($this->breadcrumbs) > 0 && $this->breadcrumbs [0]) {
  53. $parent = $this->breadcrumbs [0];
  54. $result .= ' ' . $parent->getTypeNamePossesive() . ' ' . $parent->getEntity()->getName();
  55. }
  56. return $result;
  57. case OX_UI_Page_Entity_Header::PAGE_TYPE_EDIT :
  58. $result .= $this->entityInfo->getTypeNameSingular() . ' ' . $this->entityInfo->getEntity()->getName();
  59. return $result;
  60. case OX_UI_Page_Entity_Header::PAGE_TYPE_ADD :
  61. $result .= $this->entityInfo->getTypeNameAdd();
  62. return $result;
  63. }
  64. throw new Exception("Unknown page type: " . $this->type);
  65. }
  66. /**
  67. * Builds page title in an XHTML form, suitable for page header.
  68. */
  69. public function getTitleXhtml()
  70. {
  71. $result = '';
  72. switch ($this->type) {
  73. case OX_UI_Page_Entity_Header::PAGE_TYPE_LIST :
  74. $result .= '<span class="label">' . $this->entityInfo->getTypeNamePlural() . '</span>';
  75. if (count($this->breadcrumbs) > 0 && $this->breadcrumbs [0]) {
  76. $parent = $this->breadcrumbs [0];
  77. $result .= ' ' . $parent->getTypeNamePossesive();
  78. $result .= ' <a href="' . $parent->getEntityEditUrl() . '">' . htmlspecialchars($parent->getEntity()->getName()) . "</a>";
  79. }
  80. return $result;
  81. case OX_UI_Page_Entity_Header::PAGE_TYPE_EDIT :
  82. $result .= '<span class="label">' . $this->entityInfo->getTypeNameSingular() . '</span>';
  83. $result .= ' ' . htmlspecialchars($this->entityInfo->getEntity()->getName());
  84. return $result;
  85. case OX_UI_Page_Entity_Header::PAGE_TYPE_ADD :
  86. $result .= '<span class="label">' . $this->entityInfo->getTypeNameAdd() . '</span>';
  87. return $result;
  88. }
  89. throw new Exception("Unknown page type: " . $this->type);
  90. }
  91. /**
  92. * Builds icon CSS class for this page's header, ready to be inserted into the markup.
  93. */
  94. public function getIconCssClass()
  95. {
  96. $type = '';
  97. if ($this->type == self::PAGE_TYPE_ADD) {
  98. $type = 'Add';
  99. }
  100. return 'icon' . $this->entityInfo->getIcon() . $type . 'Large';
  101. }
  102. /**
  103. * Returns true if this header has breadcrumbs.
  104. */
  105. public function hasBreadcrumbs()
  106. {
  107. return count($this->breadcrumbs) > 0;
  108. }
  109. /**
  110. * Returns an array of page page breadcrumbs.
  111. *
  112. * @return array
  113. */
  114. public function getBreadcrumbsXhtml()
  115. {
  116. switch ($this->type) {
  117. case OX_UI_Page_Entity_Header::PAGE_TYPE_LIST :
  118. return $this->getSelectBreadcrumbsXhtml();
  119. case OX_UI_Page_Entity_Header::PAGE_TYPE_ADD :
  120. case OX_UI_Page_Entity_Header::PAGE_TYPE_EDIT :
  121. return $this->getLinkBreadcrumbsXhtml();
  122. }
  123. throw new Exception("Unknown page type: " . $this->type);
  124. }
  125. /**
  126. * Builds link-based breadcrumbs for navigating to the parent entity.
  127. */
  128. private function getLinkBreadcrumbsXhtml()
  129. {
  130. $xhtml = array ();
  131. foreach ($this->breadcrumbs as $bcInfo) {
  132. if ($bcInfo->getEntity()) {
  133. $xhtml [] = $this->getLinkBreadcrumbXhtml($bcInfo);
  134. }
  135. }
  136. return $xhtml;
  137. }
  138. private function getLinkBreadcrumbXhtml($bcInfo)
  139. {
  140. $bc = '<a class="ent inlineIcon icon' . $bcInfo->getIcon() . '" ';
  141. $bc .= 'href="' . $bcInfo->getEntityEditUrl() . '">';
  142. $bc .= $bcInfo->getTypeNameSingular() . ' ' . htmlspecialchars($bcInfo->getEntity()->getName()) . '</a>';
  143. return $bc;
  144. }
  145. /**
  146. * Builds select-based breadcrumbs for navigating between parent entities, useful
  147. * on list pages.
  148. */
  149. private function getSelectBreadcrumbsXhtml()
  150. {
  151. $xhtml = array ();
  152. foreach ($this->breadcrumbs as $bcInfo) {
  153. $xhtml [] = $this->getSelectBreadcrumbXhtml($bcInfo);
  154. }
  155. return $xhtml;
  156. }
  157. private function getSelectBreadcrumbXhtml($bcInfo)
  158. {
  159. if (count($bcInfo->getSiblingMultiOptions()) <= 1)
  160. {
  161. return $this->getLinkBreadcrumbXhtml($bcInfo);
  162. }
  163. $view = Zend_Registry::getInstance()->get('smartyView');
  164. $selectHelper = new Zend_View_Helper_FormSelect();
  165. $selectHelper->setView($view);
  166. $entityInfo = $this->entityInfo;
  167. $actionUrl = OX_UI_View_Helper_ActionUrl::actionUrl($entityInfo->getListAction(), $entityInfo->getController(), $entityInfo->getModule());
  168. $bc = '<form class="nofocus" action="' . $actionUrl . '">';
  169. $bc .= '<label class="inlineIcon icon' . $bcInfo->getIcon() . '">';
  170. $bc .= $bcInfo->getTypeNameSingular();
  171. $bc .= $selectHelper->formSelect($bcInfo->getEntityIdParam(), $bcInfo->getEntityId(), array (
  172. 'class' => 'submit-on-change'), $bcInfo->getSiblingMultiOptions(), '\n');
  173. $bc .= '</label></form>';
  174. return $bc;
  175. }
  176. /**
  177. * Builds breadcrumbs array for the provided entity and parent entity.
  178. */
  179. private static function getPageEntityBreadcrumbs($oEntity, $oParentEntity = null)
  180. {
  181. $entityClass = OX_Common_ClassUtils::getClass($oEntity);
  182. switch ($entityClass) {
  183. case 'AcPlacement' :
  184. if ($oParentEntity) {
  185. return array (
  186. OX_UI_Page_EntityInfo::getEntityInfo($oParentEntity, $oParentEntity->getAccount()));
  187. } else {
  188. return array ();
  189. }
  190. }
  191. return array ();
  192. }
  193. }