PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/rokcommon/RokCommon/Form/AbstractItem.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 478 lines | 161 code | 75 blank | 242 comment | 30 complexity | e85298456547f77c4e12c576f7714523 MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id: AbstractItem.php 57540 2012-10-14 18:27:59Z btowles $
  4. * @author RocketTheme http://www.rockettheme.com
  5. * @copyright Copyright (C) 2007 - ${copyright_year} RocketTheme, LLC
  6. * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
  7. */
  8. abstract class RokCommon_Form_AbstractItem implements RokCommon_Form_IItem
  9. {
  10. /**
  11. * @var RokCommon_Service_Container
  12. */
  13. protected $container;
  14. /**
  15. * The JForm object of the form attached to the form field.
  16. *
  17. * @var object
  18. * @since 1.6
  19. */
  20. protected $form;
  21. /**
  22. * The form control prefix for field names from the JForm object attached to the form field.
  23. *
  24. * @var string
  25. * @since 1.6
  26. */
  27. protected $formControl;
  28. /**
  29. * The hidden state for the form field.
  30. *
  31. * @var boolean
  32. * @since 1.6
  33. */
  34. protected $hidden = false;
  35. /**
  36. * True to translate the field label string.
  37. * True to translate the field label string.
  38. *
  39. * @var boolean
  40. * @since 1.6
  41. */
  42. protected $translateLabel = true;
  43. /**
  44. * True to translate the field description string.
  45. *
  46. * @var boolean
  47. * @since 1.6
  48. */
  49. protected $translateDescription = true;
  50. /**
  51. * The JXMLElement object of the <field /> XML element that describes the form field.
  52. *
  53. * @var object
  54. * @since 1.6
  55. */
  56. protected $element;
  57. /**
  58. * The document id for the form field.
  59. *
  60. * @var string
  61. * @since 1.6
  62. */
  63. protected $id;
  64. /**
  65. * The input for the form field.
  66. *
  67. * @var string
  68. * @since 1.6
  69. */
  70. protected $input;
  71. /**
  72. * The label for the form field.
  73. *
  74. * @var string
  75. * @since 1.6
  76. */
  77. protected $label;
  78. /**
  79. * The name of the form field.
  80. *
  81. * @var string
  82. * @since 1.6
  83. */
  84. protected $name;
  85. /**
  86. * The name of the field.
  87. *
  88. * @var string
  89. * @since 1.6
  90. */
  91. protected $fieldname;
  92. /**
  93. * The group of the field.
  94. *
  95. * @var string
  96. * @since 1.6
  97. */
  98. protected $group;
  99. /**
  100. * The form field type.
  101. *
  102. * @var string
  103. * @since 1.6
  104. */
  105. protected $type;
  106. protected $basetype;
  107. /**
  108. * The value of the form field.
  109. *
  110. * @var mixed
  111. * @since 1.6
  112. */
  113. protected $value;
  114. /**
  115. * @var string
  116. */
  117. protected $panel_position = 'left';
  118. /**
  119. * @var bool
  120. */
  121. protected $show_label = true;
  122. /**
  123. * @var mixed
  124. */
  125. protected $base_value;
  126. /**
  127. * @var bool
  128. */
  129. protected $customized = false;
  130. /**
  131. * @var bool
  132. */
  133. protected $setinoverride = true;
  134. /**
  135. * @var string
  136. */
  137. protected $class;
  138. /**
  139. * @var bool
  140. */
  141. protected $detached;
  142. /**
  143. * @var mixed
  144. */
  145. protected $default;
  146. protected $assets_content;
  147. /**
  148. * Method to instantiate the form field object.
  149. *
  150. * @param object $form The form to attach to the form field object.
  151. *
  152. * @return void
  153. * @since 1.6
  154. */
  155. public function __construct(RokCommon_Form $form = null)
  156. {
  157. $this->container = RokCommon_Service::getContainer();
  158. // If there is a form passed into the constructor set the form and form control properties.
  159. if ($form instanceof RokCommon_Form) {
  160. $this->form = $form;
  161. $this->formControl = $form->getFormControl();
  162. }
  163. }
  164. /**
  165. * @param $name
  166. * @param $value
  167. *
  168. * @return void
  169. */
  170. public function __set($name, $value)
  171. {
  172. if (property_exists($this, $name)) {
  173. $this->$name = $value;
  174. }
  175. }
  176. /**
  177. * Method to get the field title.
  178. *
  179. * @return string The field title.
  180. * @since 11.1
  181. */
  182. public function getTitle()
  183. {
  184. // Initialise variables.
  185. $title = '';
  186. if ($this->hidden) {
  187. return $title;
  188. }
  189. // Get the label text from the XML element, defaulting to the element name.
  190. $title = $this->element['label'] ? (string)$this->element['label'] : (string)$this->element['name'];
  191. $title = $this->translateLabel ? JText::_($title) : $title;
  192. return $title;
  193. }
  194. /**
  195. * Method to get the field label markup.
  196. *
  197. * @return string The field label markup.
  198. * @since 1.6
  199. */
  200. public function getLabel()
  201. {
  202. // Initialise variables.
  203. $label = '';
  204. if ($this->hidden) {
  205. return $label;
  206. }
  207. // Get the label text from the XML element, defaulting to the element name.
  208. $text = $this->element['label'] ? (string)$this->element['label'] : (string)$this->element['name'];
  209. $text = $this->translateLabel ? JText::_($text) : $text;
  210. // Build the class for the label.
  211. $class = !empty($this->description) ? 'hasTip' : '';
  212. $class = $this->required == true ? $class . ' required' : $class;
  213. // Add the opening label tag and main attributes attributes.
  214. $label .= '<label id="' . $this->id . '-lbl" for="' . $this->id . '" class="' . $class . '"';
  215. // If a description is specified, use it to build a tooltip.
  216. if (!empty($this->description)) {
  217. $label .= ' title="' . htmlspecialchars(trim($text, ':') . '::' . ($this->translateDescription ? JText::_($this->description) : $this->description), ENT_COMPAT, 'UTF-8') . '"';
  218. }
  219. // Add the label text and closing tag.
  220. $label .= '>' . $text . '</label>';
  221. return $label;
  222. }
  223. /**
  224. * Method to attach a JForm object to the field.
  225. *
  226. * @param \RokCommon_Form $form The JForm object to attach to the form field.
  227. *
  228. * @return object The form field object so that the method can be used in a chain.
  229. * @since 1.6
  230. */
  231. public function setForm(RokCommon_Form $form)
  232. {
  233. $this->form = $form;
  234. $this->formControl = $form->getFormControl();
  235. return $this;
  236. }
  237. /**
  238. * Method to get the name used for the field input tag.
  239. *
  240. * @param string $fieldName The field element name.
  241. *
  242. * @return string The name to be used for the field input tag.
  243. *
  244. * @since 11.1
  245. */
  246. public function getName($fieldName)
  247. {
  248. /** @var $namehandler RokCommon_Form_IItemNameHandler */
  249. $namehandler = $this->container->getService('form.namehandler');
  250. return $namehandler->getName($fieldName, $this->group, $this->formControl, false);
  251. }
  252. /**
  253. * Method to get the id used for the field input tag.
  254. *
  255. * @param string $fieldId The field element id.
  256. * @param string $fieldName The field element name.
  257. *
  258. * @return string The id to be used for the field input tag.
  259. * @since 1.6
  260. */
  261. public function getId($fieldId, $fieldName)
  262. {
  263. /** @var $namehandler RokCommon_Form_IItemNameHandler */
  264. $namehandler = $this->container->getService('form.namehandler');
  265. return $namehandler->getId($fieldName, $fieldId, $this->group, $this->formControl, false);
  266. }
  267. /**
  268. * Method to get certain otherwise inaccessible properties from the form field object.
  269. *
  270. * @param string $name The property name for which to the the value.
  271. *
  272. * @return mixed|null The property value or null.
  273. * @since 1.6
  274. */
  275. public function __get($name)
  276. {
  277. switch ($name) {
  278. case 'input':
  279. // If the input hasn't yet been generated, generate it.
  280. if (empty($this->input)) {
  281. $this->input = $this->getInput();
  282. }
  283. return $this->input;
  284. break;
  285. case 'label':
  286. // If the label hasn't yet been generated, generate it.
  287. if (empty($this->label)) {
  288. $this->label = $this->getLabel();
  289. }
  290. return $this->label;
  291. break;
  292. case 'title':
  293. return $this->getTitle();
  294. break;
  295. default :
  296. if (property_exists($this, $name) && isset($this->$name)) {
  297. return $this->$name;
  298. } elseif (method_exists($this, 'get' . ucfirst($name))) {
  299. return call_user_func(array($this, 'get' . ucfirst($name)));
  300. } elseif (property_exists($this, $name)) {
  301. return $this->$name;
  302. } elseif (isset($this->element[$name])) {
  303. return (string)$this->element[$name];
  304. } else {
  305. return null;
  306. }
  307. break;
  308. }
  309. }
  310. /**
  311. * Method to attach a JForm object to the field.
  312. *
  313. * @param object $element The JXMLElement object representing the <field /> tag for the
  314. * form field object.
  315. * @param mixed $value The form field default value for display.
  316. * @param string $group The field name group control value. This acts as as an array
  317. * container for the field. For example if the field has name="foo"
  318. * and the group value is set to "bar" then the full field name
  319. * would end up being "bar[foo]".
  320. *
  321. * @return boolean True on success.
  322. * @since 1.6
  323. */
  324. public function setup(& $element, $value, $group = null)
  325. {
  326. global $gantry;
  327. // Make sure there is a valid JFormField XML element.
  328. if (!($element instanceof RokCommon_XMLElement)) {
  329. return false;
  330. }
  331. // Reset the input and label values.
  332. $this->input = null;
  333. $this->label = null;
  334. // Set the xml element object.
  335. $this->element = $element;
  336. // Get some important attributes from the form field element.
  337. $class = (string)$element['class'];
  338. $id = (string)$element['id'];
  339. $name = (string)$element['name'];
  340. $type = (string)$element['type'];
  341. $panel_position = (string)$element['panel_position'];
  342. $this->show_label = ((string)$element['show_label'] == 'false') ? false : true;
  343. $this->setinoverride = ((string)$element['setinoverride'] == 'false') ? false : true;
  344. $default = (string)$element['default'];
  345. // if (!empty($name)) {
  346. // if (empty($group)) {
  347. // $gantry_name = $name;
  348. // } else {
  349. // $groups = explode('.', $group);
  350. // if (count($groups > 0)) {
  351. // //array_shift($groups);
  352. // $groups[] = $name;
  353. // $gantry_name = implode('-', $groups);
  354. // }
  355. // }
  356. // // TODO set this up to get for Default not for RokCommon param value
  357. // //$this->base_value = $gantry->get($gantry_name, null);
  358. // }
  359. // Set the field description text.
  360. $this->description = (string)$element['description'];
  361. // Set the visibility.
  362. $this->hidden = ((string)$element['type'] == 'hidden' || (string)$element['hidden'] == 'true');
  363. // Determine whether to translate the field label and/or description.
  364. $this->translateLabel = !((string)$this->element['translate_label'] == 'false' || (string)$this->element['translate_label'] == '0');
  365. $this->translateDescription = !((string)$this->element['translate_description'] == 'false' || (string)$this->element['translate_description'] == '0');
  366. // Set the group of the field.
  367. $this->group = $group;
  368. // Set the field name and id.
  369. $this->fieldname = $name;
  370. $this->name = $this->getName($name, $group);
  371. $this->id = $this->getId($id, $name, $group);
  372. $this->type = $type;
  373. $this->class = $class;
  374. if ($panel_position != null) $this->panel_position = $panel_position;
  375. // Set the field default value.
  376. $this->value = $value;
  377. if (null != $this->default && $this->default != $this->value) $this->customized = true;
  378. return true;
  379. }
  380. /**
  381. * @static
  382. * @return void
  383. */
  384. public static function initialize()
  385. {
  386. }
  387. /**
  388. * @static
  389. * @return void
  390. */
  391. public static function finalize()
  392. {
  393. }
  394. /**
  395. * @param $callback
  396. *
  397. * @return mixed
  398. */
  399. public function render($callback)
  400. {
  401. return call_user_func_array($callback, array($this));
  402. }
  403. }