PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libraries/default/base/template/helper/form.php

https://github.com/bhar1red/anahita
PHP | 119 lines | 57 code | 18 blank | 44 comment | 6 complexity | e693f431fab46a381a8768321af61e3e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * LICENSE: ##LICENSE##
  4. *
  5. * @category Anahita
  6. * @package Lib_Base
  7. * @subpackage Template_Helper
  8. * @author Arash Sanieyan <ash@anahitapolis.com>
  9. * @author Rastin Mehr <rastin@anahitapolis.com>
  10. * @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
  11. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  12. * @version SVN: $Id$
  13. * @link http://www.anahitapolis.com
  14. */
  15. /**
  16. * Form Helper
  17. *
  18. * @category Anahita
  19. * @package Lib_Base
  20. * @subpackage Template_Helper
  21. * @author Arash Sanieyan <ash@anahitapolis.com>
  22. * @author Rastin Mehr <rastin@anahitapolis.com>
  23. * @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
  24. * @link http://www.anahitapolis.com
  25. */
  26. class LibBaseTemplateHelperForm extends KTemplateHelperAbstract
  27. {
  28. /**
  29. * Renders a form using an xml path
  30. *
  31. * @param array $config
  32. * @return void
  33. */
  34. public function render($config=array())
  35. {
  36. $config = new KConfig($config);
  37. $config->append(array(
  38. 'group' => '_default',
  39. 'name' => 'params'
  40. ));
  41. $parameter = $this->getParameters($config);
  42. return $this->_render($parameter, $config);
  43. }
  44. /**
  45. * Renders a form using an xml path
  46. *
  47. * @param array $config
  48. * @return void
  49. */
  50. public function getParameters($config=array())
  51. {
  52. $config = new KConfig($config);
  53. $config->append(array(
  54. 'data' => array(),
  55. 'element_paths' => array(dirname(__FILE__).'/forms')
  56. ));
  57. $content = file_exists($config->path) ? file_get_contents($config->path) : '';
  58. $paths = array();
  59. //replace all the addpath="{KServiceIdentifier}" with real path
  60. if ( preg_match_all('/addpath="([^"]+)"/', $content, $paths) )
  61. {
  62. $replaces = array();
  63. foreach($paths[1] as $path)
  64. {
  65. if ( strpos($path,'.') ) {
  66. $replaces[] = str_replace(JPATH_ROOT.'/', '', dirname(KLoader::path($path.'.dummy')));
  67. } else
  68. $replaces[] = $path;
  69. }
  70. $content = str_replace($paths[1], $replaces, $content);
  71. }
  72. $xml = & JFactory::getXMLParser('Simple');
  73. $parameter = new JParameter('');
  74. $data = KConfig::unbox($config->data);
  75. if ( $data instanceof JParameter )
  76. $data = $data->toArray();
  77. if ( is_array($data) )
  78. $parameter->loadArray($data);
  79. else
  80. $parameter->loadINI($data);
  81. $parameter->template_data = $config->template_data;
  82. foreach($config->element_paths as $path )
  83. $parameter->addElementPath($path);
  84. if ($xml->loadString($content))
  85. if ($params = & $xml->document->params)
  86. foreach ($params as $param)
  87. $parameter->setXML( $param );
  88. return $parameter;
  89. }
  90. /**
  91. * Render a parameter
  92. *
  93. * @param JParameter $parameter
  94. * @param KConfig $config
  95. * @return string
  96. */
  97. protected function _render($parameter, $config)
  98. {
  99. return $parameter->render($config->name, $config->group);
  100. }
  101. }