PageRenderTime 58ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Adapto/Handler/ViewEditBase.php

http://github.com/egeniq/adapto
PHP | 324 lines | 160 code | 39 blank | 125 comment | 34 complexity | 82db0ad3d05bf1c56bfd8e06e09844e2 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. * @copyright (c)2000-2004 Ivo Jansch
  13. * @license http://www.achievo.org/atk/licensing ATK Open Source License
  14. *
  15. */
  16. /**
  17. * Handler class for the edit action of an entity. The handler draws a
  18. * generic edit form for the given entity.
  19. *
  20. * @author ijansch
  21. * @author petercv
  22. * @package adapto
  23. * @subpackage handlers
  24. *
  25. */
  26. class Adapto_Handler_ViewEditBase extends Adapto_ActionHandler
  27. {
  28. /**
  29. * Holds the record for the entity. It is cached as long as the instance
  30. * exists, unless we force a reload.
  31. *
  32. * @var array
  33. */
  34. private $m_record = null;
  35. /**
  36. * Get the record to view/edit. It is cached as long as the instance
  37. * exists, unless we force a reload.
  38. *
  39. * @param bool $force Whether or not to force the fetching of the record
  40. * @return Array The record for viewing/editting
  41. */
  42. public function getRecord($force = false)
  43. {
  44. // if we are not forcing a fetch and we already have a cached record, return it
  45. if ($force === false && $this->m_record !== null) {
  46. return $this->m_record;
  47. }
  48. $record = $this->getRejectInfo(); // Check reject info first
  49. if ($record == null) // If reject info not set - do select
  50. {
  51. $atkstoretype = "";
  52. $sessionmanager = atkGetSessionManager();
  53. if ($sessionmanager)
  54. $atkstoretype = $sessionmanager->stackVar('atkstore');
  55. switch ($atkstoretype) {
  56. case 'session':
  57. $record = $this->getRecordFromSession();
  58. break;
  59. default:
  60. $record = $this->getRecordFromDb();
  61. break;
  62. }
  63. }
  64. // cache the record
  65. $this->m_record = $record;
  66. return $record;
  67. }
  68. /**
  69. * Get the record for the database with the current selector
  70. *
  71. * @return array
  72. */
  73. protected function getRecordFromDb()
  74. {
  75. $selector = atkArrayNvl($this->m_entity->m_postvars, 'atkselector', "");
  76. if ($this->getEntity()->hasFlag(EF_ML)) {
  77. list($record) = $this->m_entity->selectDb($selector, "", "", "", "", "edit");
  78. } else {
  79. $record = $this->m_entity->select($selector)->mode('edit')->getFirstRow();
  80. }
  81. return $record;
  82. }
  83. /**
  84. * Get the current record from the database with the current selector
  85. *
  86. * @return array
  87. */
  88. protected function getRecordFromSession()
  89. {
  90. $selector = atkArrayNvl($this->m_entity->m_postvars, 'atkselector', '');
  91. return Adapto_ClassLoader::getInstance('atk.session.atksessionstore')->getDataRowForSelector($selector);
  92. }
  93. /**
  94. * Get section label.
  95. *
  96. * @param atkEntity $entity
  97. * @param string $rawName
  98. *
  99. * @return string label
  100. *
  101. * @static
  102. */
  103. function getSectionLabel($entity, $rawName)
  104. {
  105. list($tab, $section) = explode('.', $rawName);
  106. $strings = array("section_{$tab}_{$section}", "{$tab}_{$section}", "section_{$section}", $section);
  107. return $entity->text($strings);
  108. }
  109. /**
  110. * Get tab label.
  111. *
  112. * @param atkEntity $entity
  113. * @param string $tab
  114. *
  115. * @return string label
  116. *
  117. * @static
  118. */
  119. function getTabLabel($entity, $tab)
  120. {
  121. $strings = array("tab_{$tab}", $tab);
  122. return $entity->text($strings);
  123. }
  124. /**
  125. * Create the clickable label for the section.
  126. *
  127. * @param array $field
  128. * @param string $mode
  129. * @return string Html
  130. */
  131. function getSectionControl($field, $mode)
  132. {
  133. // label
  134. $label = Adapto_Handler_ViewEditBase::getSectionLabel($this->m_entity, $field['name']);
  135. // our name
  136. list($tab, $section) = explode('.', $field["name"]);
  137. $name = "section_{$tab}_{$section}";
  138. $url = partial_url($this->m_entity->atkentitytype(), $mode, "sectionstate", array("atksectionname" => $name));
  139. // create onclick statement.
  140. $onClick = " onClick=\"javascript:handleSectionToggle(this,null,'{$url}'); return false;\"";
  141. $initClass = "openedSection";
  142. //if the section is not active, we close it on load.
  143. $default = in_array($field["name"], $this->m_entity->getActiveSections($tab, $mode)) ? 'opened' : 'closed';
  144. $sectionstate = atkState::get(array("entitytype" => $this->m_entity->atkentitytype(), "section" => $name), $default);
  145. if ($sectionstate == 'closed') {
  146. $initClass = "closedSection";
  147. $page = &$this->getPage();
  148. $page->register_scriptcode("addClosedSection('$name');");
  149. }
  150. // create the clickable link
  151. return '<span class="atksectionwr"><a href="javascript:void(0)" id="' . $name . '" class="atksection ' . $initClass . '"' . $onClick . '>' . $label
  152. . '</a></span>';
  153. }
  154. /**
  155. * Based on the attributes that are part of this section we
  156. * check if this section should initially be shown or not.
  157. *
  158. * @param string $section section name
  159. * @param array $fields edit fields
  160. * @return boolean
  161. */
  162. function isSectionInitialHidden($section, $fields)
  163. {
  164. foreach ($fields as $field) {
  165. if (is_array($field["sections"]) && in_array($section, $field['sections']) && (!isset($field['initial_hidden']) || !$field['initial_hidden'])) {
  166. return false;
  167. }
  168. }
  169. return true;
  170. }
  171. /**
  172. * Adds numbering to the label of a field
  173. * @access private
  174. * @param array $field the currently handled attribute
  175. * @param array $tplfield the template data for the current attribute
  176. * @param int $i the counter being used to loop the entity for each attribute
  177. */
  178. function _addNumbering(&$field, &$tplfield, &$i)
  179. {
  180. static $number, $subnumber;
  181. if (!$number && !$subnumber)
  182. $number = $field["attribute"]->m_ownerInstance->getNumbering();
  183. if (!$subnumber) {
  184. if (strlen($number) == 1 || (floor($number) <= 9 && floor($number) >= -9 && floor($number) == $number)) {
  185. $subnumber = $number;
  186. $number = null;
  187. } else {
  188. $subnumber = substr($number, strrpos($number, ".") + 1);
  189. $number = substr($number, 0, strrpos($number, "."));
  190. }
  191. }
  192. if ($field["label"]) {
  193. if ($number)
  194. $tplfield["label"] = "$number.$subnumber. ";
  195. else
  196. $tplfield["label"] = "$subnumber. ";
  197. $subnumber++;
  198. }
  199. }
  200. /**
  201. * Section state handler.
  202. */
  203. function partial_sectionstate()
  204. {
  205. atkState::set(array("entitytype" => $this->m_entity->atkentitytype(), "section" => $this->m_postvars['atksectionname']), $this->m_postvars['atksectionstate']);
  206. die;
  207. }
  208. /**
  209. * Get array with tab name as key and tab template as value
  210. *
  211. * @param object $entity
  212. * @param array $tabs
  213. * @param string $mode
  214. * @param array $record
  215. * @return array with tab=>template pear
  216. */
  217. function _getTabTpl($entity, $tabs, $mode, $record)
  218. {
  219. $tabTpl = array();
  220. foreach ($tabs as $t) {
  221. $tabTpl['section_' . $t] = $entity->getTemplate($mode, $record, $t);
  222. }
  223. return $tabTpl;
  224. }
  225. /**
  226. * Render tabs using templates
  227. *
  228. * @todo this method seems broken by design, read comments for more info!
  229. *
  230. * @param array $fields
  231. * @param array $tabTpl
  232. * @return array with already rendering tabs
  233. */
  234. function _renderTabs($fields, $tabTpl)
  235. {
  236. $ui = &$this->getUi();
  237. $tabs = array();
  238. $perTpl = array();//per template array
  239. for ($i = 0, $_i = count($fields); $i < $_i; $i++) {
  240. $allTabs = explode(' ', $fields[$i]["tab"]); // should not use "tab" here, because it actually contains the CSS class names and not only the tab names
  241. $allMatchingTabs = array_values(array_intersect($allTabs, array_keys($tabTpl))); // because of the CSS thingee above we search for the first matching tab
  242. if (count($allMatchingTabs) == 0)
  243. $allMatchingTabs = array_keys($tabTpl);
  244. // again a workaround for this horribly broken method
  245. $tab = $allMatchingTabs[0]; // attributes can be part of one, more than one or all tabs, at the moment it seems only one or all are supported
  246. $perTpl[$tabTpl[$tab]]['fields'][] = $fields[$i];//make field available in numeric array
  247. $perTpl[$tabTpl[$tab]][$fields[$i]["attribute"]] = $fields[$i];//make field available in associative array
  248. $perTpl[$tabTpl[$tab]]['attributes'][$fields[$i]["attribute"]] = $fields[$i];//make field available in associative array
  249. }
  250. // Add 'alltab' fields to all templates
  251. foreach ($fields as $field) {
  252. if (in_array('alltabs', explode(' ', $field["tab"]))) {
  253. $templates = array_keys($perTpl);
  254. foreach ($templates as $tpl) {
  255. if (!$perTpl[$tpl][$field['attribute']]) {
  256. $perTpl[$tpl]['fields'][] = $field;
  257. $perTpl[$tpl][$field['attribute']] = $field;
  258. }
  259. }
  260. }
  261. }
  262. $tpls = array_unique(array_values($tabTpl));
  263. foreach ($tpls as $tpl) {
  264. $tabs[] = $ui->render($tpl, $perTpl[$tpl]);
  265. }
  266. return $tabs;
  267. }
  268. /**
  269. * Attribute handler.
  270. *
  271. * @param string $partial full partial
  272. */
  273. public function partial_attribute($partial)
  274. {
  275. list(, $attribute, $partial) = explode('.', $partial);
  276. $attr = $this->m_entity->getAttribute($attribute);
  277. if ($attr == NULL) {
  278. throw new Adapto_Exception("Unknown / invalid attribute '$attribute' for entity '" . $this->m_entity->atkEntityType() . "'");
  279. return '';
  280. }
  281. return $attr->partial($partial, $this->m_action);
  282. }
  283. }
  284. ?>