1 <?php
2 /**
3 * @package FrameworkOnFramework
4 * @subpackage view
5 * @copyright Copyright (C) 2010-2016 Nicholas K. Dionysopoulos / Akeeba Ltd. All rights reserved.
6 * @license GNU General Public License version 2 or later; see LICENSE.txt
7 */
8 // Protect from unauthorized access
9 defined('FOF_INCLUDED') or die;
10
11 /**
12 * FrameworkOnFramework Form class. It preferrably renders an XML view template
13 * instead of a traditional PHP-based view template.
14 *
15 * @package FrameworkOnFramework
16 * @since 2.0
17 */
18 class FOFViewForm extends FOFViewHtml
19 {
20 /** @var FOFForm The form to render */
21 protected $form;
22
23 /**
24 * Displays the view
25 *
26 * @param string $tpl The template to use
27 *
28 * @return boolean|null False if we can't render anything
29 */
30 public function display($tpl = null)
31 {
32 $model = $this->getModel();
33
34 // Get the form
35 $this->form = $model->getForm();
36 $this->form->setModel($model);
37 $this->form->setView($this);
38
39 // Get the task set in the model
40 $task = $model->getState('task', 'browse');
41
42 // Call the relevant method
43 $method_name = 'on' . ucfirst($task);
44
45 if (method_exists($this, $method_name))
46 {
47 $result = $this->$method_name($tpl);
48 }
49 else
50 {
51 $result = $this->onDisplay();
52 }
53
54 // Bail out if we're told not to render anything
55
56 if ($result === false)
57 {
58 return;
59 }
60
61 // Show the view
62 // -- Output HTML before the view template
63 $this->preRender();
64
65 // -- Try to load a view template; if not exists render the form directly
66 $basePath = FOFPlatform::getInstance()->isBackend() ? 'admin:' : 'site:';
67 $basePath .= $this->config['option'] . '/';
68 $basePath .= $this->config['view'] . '/';
69 $path = $basePath . $this->getLayout();
70
71 if ($tpl)
72 {
73 $path .= '_' . $tpl;
74 }
75
76 $viewTemplate = $this->loadAnyTemplate($path);
77
78 // If there was no template file found, display the form
79 if ($viewTemplate instanceof Exception)
80 {
81 $viewTemplate = $this->getRenderedForm();
82 }
83
84 // -- Output the view template
85 echo $viewTemplate;
86
87 // -- Output HTML after the view template
88 $this->postRender();
89 }
90
91 /**
92 * Returns the HTML rendering of the FOFForm attached to this view. Very
93 * useful for customising a form page without having to meticulously hand-
94 * code the entire form.
95 *
96 * @return string The HTML of the rendered form
97 */
98 public function getRenderedForm()
99 {
100 $html = '';
101 $renderer = $this->getRenderer();
102
103 if ($renderer instanceof FOFRenderAbstract)
104 {
105 // Load CSS and Javascript files defined in the form
106 $this->form->loadCSSFiles();
107 $this->form->loadJSFiles();
108
109 // Get the form's HTML
110 $html = $renderer->renderForm($this->form, $this->getModel(), $this->input);
111 }
112
113 return $html;
114 }
115
116 /**
117 * The event which runs when we are displaying the Add page
118 *
119 * @param string $tpl The view sub-template to use
120 *
121 * @return boolean True to allow display of the view
122 */
123 protected function onAdd($tpl = null)
124 {
125 // Hide the main menu
126 JRequest::setVar('hidemainmenu', true);
127
128 // Get the model
129 $model = $this->getModel();
130
131 // Assign the item and form to the view
132 $this->item = $model->getItem();
133
134 return true;
135 }
136 }
137