PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Adapto/Handler/View.php

http://github.com/egeniq/adapto
PHP | 314 lines | 173 code | 49 blank | 92 comment | 46 complexity | 86942729a1ca0388bf42da1e5037bef4 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Adapto Toolkit.
  4. * Detailed copyright and licensing information can be found
  5. * in the doc/COPYRIGHT and doc/LICENSE files which should be
  6. * included in the distribution.
  7. *
  8. * @package adapto
  9. * @subpackage handlers
  10. *
  11. * @copyright (c)2000-2004 Ibuildings.nl BV
  12. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  13. *
  14. */
  15. /**
  16. * Handler class for a readonly view action. Similar to the edit handler,
  17. * but all fields are displayed readonly.
  18. *
  19. * @author ijansch
  20. * @package adapto
  21. * @subpackage handlers
  22. *
  23. */
  24. class Adapto_Handler_View extends Adapto_ViewEditBase
  25. {
  26. public $m_buttonsource = null; // defaulted to public
  27. /**
  28. * The action handler method.
  29. *
  30. * @param Bool $renderbox Render this action in a renderbox or just output the HTML
  31. */
  32. function action_view($renderbox = true)
  33. {
  34. if (!empty($this->m_partial)) {
  35. $this->partial($this->m_partial);
  36. return;
  37. }
  38. $record = $this->getRecord();
  39. // allowed to view record?
  40. if (!$this->allowed($record)) {
  41. $this->renderAccessDeniedPage();
  42. return;
  43. }
  44. $page = &$this->getPage();
  45. $this->notify("view", $record);
  46. $page->addContent($this->m_entity->renderActionPage("admin", $this->invoke("viewPage", $record, $this->m_entity, $renderbox)));
  47. }
  48. /**
  49. * Returns the view record.
  50. */
  51. function getRecordFromDb()
  52. {
  53. list($record) = $this->m_entity
  54. ->selectDb($this->m_postvars['atkselector'], $this->getEntity()->getColumnConfig()->getOrderByStatement(), "", $this->m_entity->m_viewExcludes, "",
  55. "view");
  56. return $record;
  57. }
  58. /**
  59. * Get the start of the form.
  60. *
  61. * @return String HTML The forms' start
  62. */
  63. public function getFormStart($record = null)
  64. {
  65. return '<form name="entryform" id="entryform" action="' . getDispatchFile() . '" method="get">' . session_form(SESSION_NESTED)
  66. . '<input type="hidden" name="atkselector" value="' . $this->getEntity()->primaryKey($record) . '">';
  67. }
  68. /**
  69. * Returns an htmlpage displaying all displayable attributes.
  70. * @param array $record The record to display.
  71. * @param atkEntity $entity The entity for which a viewPage is displayed.
  72. * @param Bool $renderbox Render this action in a renderbox or just output the HTML
  73. * @return String The html page with a reaonly view of relevant fields.
  74. */
  75. function viewPage($record, $entity, $renderbox = true)
  76. {
  77. $ui = &$this->getUi();
  78. $entity->addStyle("style.css");
  79. if (is_object($ui)) {
  80. $params = $entity->getDefaultActionParams();
  81. $tab = $entity->getActiveTab();
  82. $innerform = $this->viewForm($record, "view");
  83. $params["activeTab"] = $tab;
  84. $params["header"] = $this->invoke("viewHeader", $record);
  85. $params['title'] = $entity->actionTitle($this->m_action, $record);
  86. $params["content"] = $entity->tabulate("view", $innerform);
  87. $params["formstart"] = $this->getFormStart($record);
  88. $params["buttons"] = $this->getFormButtons($record);
  89. $params["formend"] = '</form>';
  90. $output = $ui->renderAction("view", $params);
  91. if (!$renderbox) {
  92. return $output;
  93. }
  94. $this->getPage()->setTitle(atktext('app_shorttitle') . " - " . $entity->actionTitle($this->m_action, $record));
  95. $vars = array("title" => $entity->actionTitle($this->m_action, $record), "content" => $output);
  96. if ($this->getRenderMode() == "dialog") {
  97. $total = $ui->renderDialog($vars);
  98. } else {
  99. $total = $ui->renderBox($vars, $this->m_boxTemplate);
  100. }
  101. return $total;
  102. } else {
  103. throw new Adapto_Exception("ui object error");
  104. }
  105. }
  106. /**
  107. * Get the buttons for the current action form.
  108. *
  109. * @param array $record
  110. * @return array Array with buttons
  111. */
  112. public function getFormButtons($record = null)
  113. {
  114. // If no custom button source is given, get the default atkController.
  115. if ($this->m_buttonsource === null) {
  116. $this->m_buttonsource = &$this->m_entity;
  117. }
  118. return $this->m_buttonsource->getFormButtons("view", $record);
  119. }
  120. /**
  121. * Overrideable function to create a header for view mode.
  122. * Similar to the admin header functionality.
  123. */
  124. function viewHeader()
  125. {
  126. return "";
  127. }
  128. /**
  129. * Get the view page
  130. *
  131. * @param Array $record The record
  132. * @param String $mode The mode we're in (defaults to "view")
  133. * @param String $template The template to use for the view form
  134. * @return String HTML code of the page
  135. */
  136. function viewForm($record, $mode = "view", $template = "")
  137. {
  138. $entity = &$this->m_entity;
  139. // get data, transform into form, return
  140. $data = $entity->viewArray($mode, $record);
  141. // get active tab
  142. $tab = $entity->getActiveTab();
  143. // get all tabs of current mode
  144. $tabs = $entity->getTabs($mode);
  145. $fields = array();
  146. $attributes = array();
  147. // For all attributes we use the display() function to display the
  148. // attributes current value. This may be overridden by supplying
  149. // an <attributename>_display function in the derived classes.
  150. for ($i = 0, $_i = count($data["fields"]); $i < $_i; $i++) {
  151. $field = &$data["fields"][$i];
  152. $tplfield = array();
  153. $classes = array();
  154. if ($field["sections"] == "*") {
  155. $classes[] = "alltabs";
  156. } else if ($field["html"] == "section") {
  157. // section should only have the tab section classes
  158. foreach ($field["tabs"] as $section)
  159. $classes[] = "section_" . str_replace('.', '_', $section);
  160. } else if (is_array($field["sections"])) {
  161. foreach ($field["sections"] as $section)
  162. $classes[] = "section_" . str_replace('.', '_', $section);
  163. }
  164. $tplfield["class"] = implode(" ", $classes);
  165. $tplfield["tab"] = $tplfield["class"]; // for backwards compatibility
  166. // visible sections, both the active sections and the tab names (attribute that are
  167. // part of the anonymous section of the tab)
  168. $visibleSections = array_merge($this->m_entity->getActiveSections($tab, $mode), $tabs);
  169. // Todo fixme: initial_on_tab kan er uit, als er gewoon bij het opstarten al 1 keer showTab aangeroepen wordt (is netter dan aparte initial_on_tab check)
  170. // maar, let op, die showTab kan pas worden aangeroepen aan het begin.
  171. $tplfield["initial_on_tab"] = ($field["tabs"] == "*" || in_array($tab, $field["tabs"]))
  172. && (!is_array($field["sections"]) || count(array_intersect($field['sections'], $visibleSections)) > 0);
  173. // Give the row an id if it doesn't have one yet
  174. if (!isset($field["id"]) || empty($field["id"]))
  175. $field['id'] = getUniqueID("anonymousattribrows");
  176. // ar_ stands voor 'attribrow'.
  177. $tplfield["rowid"] = "ar_" . $field['id']; // The id of the containing row
  178. /* check for separator */
  179. if ($field["html"] == "-" && $i > 0 && $data["fields"][$i - 1]["html"] != "-") {
  180. $tplfield["line"] = "<hr>";
  181. } /* double separator, ignore */
  182. elseif ($field["html"] == "-") {
  183. } /* sections */
  184. elseif ($field["html"] == "section") {
  185. $tplfield["line"] = $this->getSectionControl($field, $mode);
  186. } /* only full HTML */
  187. elseif (isset($field["line"])) {
  188. $tplfield["line"] = $field["line"];
  189. } /* edit field */
  190. else {
  191. if ($field["attribute"]->m_ownerInstance->getNumbering()) {
  192. $this->_addNumbering($field, $tplfield, $i);
  193. }
  194. /* does the field have a label? */
  195. if ((isset($field["label"]) && $field["label"] !== "AF_NO_LABEL") || !isset($field["label"])) {
  196. if ($field["label"] == "") {
  197. $tplfield["label"] = "";
  198. } else {
  199. $tplfield["label"] = $field["label"];
  200. }
  201. } else {
  202. $tplfield["label"] = "AF_NO_LABEL";
  203. }
  204. // Make the attribute and entity names available in the template.
  205. $tplfield['attribute'] = $field["attribute"]->fieldName();
  206. $tplfield['entity'] = $field["attribute"]->m_ownerInstance->atkEntityType();
  207. /* html source */
  208. $tplfield["widget"] = $field["html"];
  209. $editsrc = $field["html"];
  210. /* tooltip */
  211. $tooltip = $field["attribute"]->getToolTip();
  212. if ($tooltip) {
  213. $tplfield["tooltip"] = $tooltip;
  214. $editsrc .= $tooltip . "&nbsp;";
  215. }
  216. $tplfield['id'] = str_replace('.', '_', $entity->atkentitytype() . '_' . $field["id"]);
  217. $tplfield["full"] = $editsrc;
  218. $column = $field['attribute']->getColumn();
  219. $tplfield["column"] = $column;
  220. }
  221. $fields[] = $tplfield; // make field available in numeric array
  222. $params[$field["name"]] = $tplfield; // make field available in associative array
  223. $attributes[$field["name"]] = $tplfield; // make field available in associative array
  224. }
  225. $ui = &$this->getUi();
  226. $tabTpl = $this->_getTabTpl($entity, $tabs, $mode, $record);
  227. if ($template) {
  228. $innerform = $ui->render($template, array("fields" => $fields, 'attributes' => $attributes));
  229. } else {
  230. if (count(array_unique($tabTpl)) > 1) {
  231. $tabForm = $this->_renderTabs($fields, $tabTpl);
  232. $innerform = implode(null, $tabForm);
  233. } else {
  234. $innerform = $ui->render($entity->getTemplate("view", $record, $tab), array("fields" => $fields, 'attributes' => $attributes));
  235. }
  236. }
  237. return $innerform;
  238. }
  239. /**
  240. * The dialog partial
  241. *
  242. * @return String HTML code for the view dialog
  243. */
  244. function partial_dialog()
  245. {
  246. return $this->renderViewDialog();
  247. }
  248. /**
  249. * Render view dialog.
  250. *
  251. * @param array $record
  252. * @return string html
  253. */
  254. function renderViewDialog($record = null)
  255. {
  256. if ($record == null) {
  257. $record = $this->getRecord();
  258. }
  259. $this->setRenderMode('dialog');
  260. $result = $this->m_entity->renderActionPage("view", $this->invoke("viewPage", $record, $this->m_entity));
  261. return $result;
  262. }
  263. }
  264. ?>