PageRenderTime 23ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/extras/installer-ftp/app/Hdinstaller_Renderer_Php.php

http://xoopscube-modules.googlecode.com/
PHP | 475 lines | 264 code | 60 blank | 151 comment | 60 complexity | 459d6d6d8edeb36c3f6fba339790f297 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, AGPL-1.0
  1. <?php
  2. class Hdinstaller_Renderer_Php extends Ethna_Renderer
  3. {
  4. /**
  5. *
  6. */
  7. var $template_vars = array();
  8. ///
  9. /**
  10. * @brief
  11. * @param
  12. * @retval
  13. */
  14. function Hdinstaller_Renderer_Php(&$c)
  15. {
  16. $this->Ethna_Renderer($c);
  17. $template_dir = $this->ctl->getTemplatedir();
  18. $this->setTemplateDir($template_dir);
  19. }
  20. /**
  21. * ????????
  22. *
  23. * @param string $template ???????
  24. * @param bool $capture true ?????????????
  25. *
  26. * @access public
  27. */
  28. function perform($template = null, $capture = false)
  29. {
  30. if ($template === null && $this->template === null) {
  31. return Ethna::raiseWarning('template is not defined');
  32. }
  33. if ($template !== null) {
  34. $this->template = $template;
  35. }
  36. if (!is_absolute_path($this->template)){
  37. $this->template = sprintf('%s%s', $this->template_dir, $this->template);
  38. }
  39. if (is_readable($this->template)){
  40. ob_start();
  41. include $this->template;
  42. $captured = ob_get_contents();
  43. ob_end_clean();
  44. if ($capture === true) {
  45. return $captured;
  46. } else {
  47. echo $captured;
  48. }
  49. } else {
  50. return Ethna::raiseWarning('template not found ' . $this->template);
  51. }
  52. }
  53. /**
  54. * ?????????????
  55. *
  56. * @param string $name ???
  57. *
  58. * @return mixed???
  59. *
  60. * @access public
  61. */
  62. function getProp($name = null)
  63. {
  64. $ret = null;
  65. if (isset($this->template_vars['app'][$name])){
  66. $ret = $this->template_vars['app'][$name];
  67. }
  68. return $ret;
  69. }
  70. /**
  71. * @brief ????????????(echo???)
  72. * @param string
  73. * @retval string
  74. */
  75. function e($name)
  76. {
  77. echo $this->getProp($name);
  78. }
  79. /**
  80. * @brief ????????????(get???)
  81. * @param string
  82. * @retval string
  83. */
  84. function g($name)
  85. {
  86. return $this->getProp($name);
  87. }
  88. ///
  89. /**
  90. * @brief ????????????(???:??)
  91. * @param
  92. * @retval
  93. */
  94. function c($name)
  95. {
  96. echo defined($name) ? constant($name) : $name;
  97. }
  98. ///
  99. /**
  100. * @brief ????????????(gettext)
  101. * @param
  102. * @retval
  103. */
  104. function _($message)
  105. {
  106. $i18n =& $this->ctl->getI18N();
  107. echo $i18n->get($message);
  108. }
  109. /**
  110. * ?????????????
  111. *
  112. * @param name ???
  113. *
  114. * @access public
  115. */
  116. function removeProp($name)
  117. {
  118. if (isset($this->template_vars[$name])){
  119. unset($this->template_vars[$name]);
  120. }
  121. }
  122. /**
  123. * ?????????????????
  124. *
  125. * @param array $array
  126. *
  127. * @access public
  128. */
  129. function setPropArray($array)
  130. {
  131. $this->template_vars = $array;
  132. }
  133. /**
  134. * ??????????????????????
  135. *
  136. * @param array $array
  137. *
  138. * @access public
  139. */
  140. function setPropArrayByRef(&$array)
  141. {
  142. $this->template_vars &= $array;
  143. }
  144. /**
  145. * ??????????????
  146. *
  147. * @param string $name ???
  148. * @param mixed $value ?
  149. *
  150. * @access public
  151. */
  152. function setProp($name, $value)
  153. {
  154. $this->template_vars[$name] = $value;
  155. }
  156. /**
  157. * ?????????????????
  158. *
  159. * @param string $name ???
  160. * @param mixed $value ?
  161. *
  162. * @access public
  163. */
  164. function setPropByRef($name, &$value)
  165. {
  166. $this->template_vars[$name] =& $value;
  167. }
  168. /// ActionError???
  169. /**
  170. * @brief
  171. * @param
  172. * @retval
  173. */
  174. function getErrors()
  175. {
  176. $ae =& $this->ctl->getActionError();
  177. return $ae->getMessageList();
  178. }
  179. /// Form Value???
  180. /**
  181. * @brief
  182. * @param
  183. * @retval
  184. */
  185. function fv($form_name)
  186. {
  187. if (isset($this->template_vars['form'][$form_name])){
  188. echo htmlspecialchars($this->template_vars['form'][$form_name], ENT_QUOTES);
  189. }
  190. }
  191. /// Smarty Plugin?form_name??????
  192. /**
  193. * @brief
  194. * @param
  195. * @retval
  196. */
  197. function fn($name)
  198. {
  199. $af =& $this->ctl->getActionForm();
  200. $ae =& $this->ctl->getActionError();
  201. $def = $af->getDef($name);
  202. if (is_null($def) || isset($def['name']) == false) {
  203. $form_name = $name;
  204. } else {
  205. $form_name = $def['name'];
  206. }
  207. if ($ae->isError($name)) {
  208. // ???????????
  209. // print '<span class="error">' . $form_name . '</span>';
  210. print $form_name ;
  211. } else {
  212. // ??????
  213. // print '<span class="">' . $form_name . '</span>';
  214. print $form_name ;
  215. }
  216. if (isset($def['required']) && $def['required'] == true) {
  217. // ??????
  218. print '<span class="required">*</span>';
  219. }
  220. }
  221. /// Smarty plugin ? form_input??????
  222. /**
  223. * @brief
  224. * @param
  225. * @retval
  226. */
  227. public function f($name, $attr='')
  228. {
  229. echo $this->_f($name, $attr);
  230. }
  231. // ??
  232. private function _f($name, $attr)
  233. {
  234. $c =& $this->ctl;
  235. $af =& $this->ctl->getActionForm();
  236. $def = $af->getDef($name);
  237. if (isset($def['form_type']) == false) {
  238. $def['form_type'] = FORM_TYPE_TEXT;
  239. }
  240. /// params
  241. // $params = func_get_args();
  242. // attributes ???
  243. /* !isset($attr) and $attr = '';
  244. !isset($key_value) and $key_value = '';
  245. !isset($id) and $id = '';
  246. !isset($value) and $value = '';
  247. !isset($delimiter) and $delimiter = '&nbsp;';
  248. !isset($postfix) and $postfix = '&nbsp;'; */
  249. $form_value = $af->get($name);
  250. if (is_array($def['type']) && is_null($form_value)){
  251. $form_value = array();
  252. }
  253. $form_type = to_array($def['form_type']);
  254. $form_type = current($form_type);
  255. switch($form_type){
  256. case FORM_TYPE_RADIO :
  257. /**
  258. * type/form_type ? array????????????
  259. * key_value?
  260. * 1. ???????key_value?key???form_options?value??????????radio????id??????????????????
  261. * 2. ????????form_options?foreach???????radio???id?????
  262. */
  263. if ($key_value){
  264. // ??radio
  265. !$id and $id = $name."_".$key_value;
  266. $attr .= sprintf(' id="%s"', $id);
  267. $label = $def['form_options'] && isset($def['form_options'][$key_value]) ? $def['form_options'][$key_value] : '' ;
  268. if ($label){
  269. $label = sprintf('<label for="%s">%s</label>', $id, $label);
  270. }
  271. $checked = $form_value==$key_value ? "checked" : "";
  272. $input = sprintf('<input type="radio" name="%s" value="%s" %s %s />%s%s%s',
  273. $name, $key_value, $attr, $checked, $prefix, $label, $postfix) ;
  274. } else {
  275. // ??radio
  276. $input = "";
  277. if(isset($def['form_options']) && is_array($def['form_options'])){
  278. foreach($def['form_options'] as $key_value=>$label){
  279. $id = $name."_".$key_value;
  280. $checked = $form_value==$key_value ? "checked" : "";
  281. $input .= sprintf('<input type="radio" name="%s" id="%s" value="%s" %s %s />',
  282. $name, $id, $key_value, $attr, $checked);
  283. $input .= sprintf('%s<label for="%s">%s</label>%s',
  284. $prefix, $id, $label, $postfix);
  285. }
  286. }
  287. }
  288. break;
  289. case FORM_TYPE_CHECKBOX :
  290. /**
  291. * type ? array?????name=form_name[key]????array??????????checkbox???
  292. * form_type ? array?????form_options?foreach????????checkbox????id?????
  293. * ????key_value ????????key_value?key???form_options?value??????????checkbox????id??????????????????
  294. */
  295. if (!is_array($def['type']) || $key_value){
  296. // ??checkbox
  297. if ($key_value){
  298. $name = $name."[".$key_value."]";
  299. !$id and $id = $name."_".$key_value ;
  300. } else {
  301. !$id and $id = substr(md5(mt_rand()), 2,10) ;
  302. }
  303. $attr .= sprintf(' id="%s"', $id);
  304. $checked = $form_value ? 'checked' : "";
  305. $input = sprintf('<input type="checkbox" name="%s" value="1" %s %s />%s<label for="%s">%s</label>%s',
  306. $name, $attr, $checked, $prefix, $id, $def['name'], $postfix);
  307. } else {
  308. // ??checkbox
  309. $input = "";
  310. if(isset($def['form_options']) && is_array($def['form_options'])){
  311. foreach ($def['form_options'] as $key_value => $label){
  312. $id = substr(md5(mt_rand()), 2,10) ;
  313. $checked = in_array($key_value, array_keys($form_value)) ? 'checked' : '';
  314. $input .= sprintf('<input type="checkbox" name="%s[%s]" value="1" id="%s" %s %s />%s<label for="%s">%s</label>%s',
  315. $name, $key_value, $id, $attr, $checked, $prefix, $id, $label, $postfix);
  316. }
  317. }
  318. }
  319. break;
  320. case FORM_TYPE_SELECT :
  321. /**
  322. * type ? array???name?[]????key_value????name=form_name[key_value]
  323. * form_type ? array????????????????????type?array??????(????Ethna?Validate?????type=array??????)
  324. */
  325. // multiple
  326. if (is_array($def['form_type'])){
  327. $multiple = 'multiple="multiple"';
  328. !isset($size) and $size = 3;
  329. if (!is_array($def['type'])){
  330. return sprintf("[ERROR] You must set ActionForm type to array() when you want to use multiple select. %s. %s:%d ", $def['name'], __FILE__, __LINE__);
  331. }
  332. $key_value = false;
  333. } else {
  334. $multiple = "";
  335. $size = 1;
  336. }
  337. // value type
  338. if (is_array($def['type'])){
  339. if ($key_value || $key_value!==""){
  340. $name .= '['.$key_value.']';
  341. } else {
  342. // only for multiple
  343. if (is_array($def['form_type'])){
  344. $name .= '[]';
  345. } else {
  346. // need *NOT* key_value
  347. // return sprintf("[ERROR] no key_value set at form %s. %s:%d ", $def['name'], __FILE__, __LINE__);
  348. $name .= '[]';
  349. }
  350. }
  351. }
  352. // value
  353. $form_value = to_array($form_value);
  354. if (is_array($def['form_type'])){
  355. // multiple select...pass thru
  356. } else {
  357. if (is_array($def['type'])){
  358. // single & form_name[key]
  359. $form_value = $form_value[$key_value];
  360. } else {
  361. $form_value = current($form_value);
  362. }
  363. }
  364. $input = sprintf('<select name="%s" size="%d" %s %s>', $name, $size, $attr, $multiple)."\n" ;
  365. if(isset($def['form_options']) && is_array($def['form_options'])){
  366. foreach($def['form_options'] as $option_value=>$option_label){
  367. if (is_array($form_value)){
  368. $selected = in_array($option_value, $form_value) ? ' selected' : '';
  369. } else {
  370. $selected = $option_value==$form_value ? ' selected' : '';
  371. }
  372. $input .= sprintf('<option value="%s" %s>%s</option>'."\n",
  373. $option_value, $selected ,$option_label) ;
  374. }
  375. }
  376. $input .= "</select>\n";
  377. break;
  378. case FORM_TYPE_FILE:
  379. is_array($def['type']) and $name = $name."[".$key_value."]";
  380. $input = sprintf('<input type="file" name="%s"', $name);
  381. if ($attr) {
  382. $input .= " $attr";
  383. }
  384. $input .= " />";
  385. break;
  386. case FORM_TYPE_TEXTAREA:
  387. is_array($def['type']) and $name = $name."[".$key_value."]";
  388. $input = sprintf('<textarea name="%s"', $name);
  389. if ($attr) {
  390. $input .= " $attr";
  391. }
  392. $input .= sprintf('>%s</textarea>', htmlspecialchars($af->get($name), ENT_QUOTES));
  393. break;
  394. case FORM_TYPE_TEXT:
  395. case FORM_TYPE_PASSWORD:
  396. default:
  397. if (is_array($def['type'])){
  398. $form_value = isset($form_value[$key_value]) ? $form_value[$key_value]: "";
  399. }
  400. is_array($def['type']) and $name = $name."[".$key_value."]";
  401. $input = sprintf('<input type="%s" name="%s" value="%s"',
  402. $form_type==FORM_TYPE_PASSWORD ? 'password' : 'text',
  403. $name, htmlspecialchars($form_value, ENT_QUOTES));
  404. if ($attr) {
  405. $input .= " $attr";
  406. }
  407. if (isset($def['max']) && $def['max']) {
  408. $input .= sprintf(' maxlength="%d"', $def['max']);
  409. }
  410. $input .= " />";
  411. break;
  412. }
  413. return $input ;
  414. }
  415. }