# See more examples

1. <?php
2. /**
3. * Default controller
4. *
5. * @author Mark Nielsen
6. * @version $Id$
7. * @package blocks/helloworld
8. */
10. defined('MOODLE_INTERNAL') or die('Direct access to this script is forbidden.');
12. class block_helloworld_controller_default extends mr_controller_block {
14. /**
15. * Do common setup routines or use to change defaults
16. */
17. public function init() {
18. // Example, change default for tab
19. // $this->tabs->set($this->action);
20. }
22. /**
23. * Require capability for viewing this controller
24. */
25. public function require_capability() {
26. /* Example implementation
27. switch ($this->action) {
28. case 'view':
29. require_capability(...);
30. break;
31. case 'foo':
32. require_capability(...);
33. break;
34. default:
35. require_capability(...);
36. }
37. */
39. // Require admin for our admin action
40. switch ($this->action) {
41. case 'admin':
42. require_capability('moodle/site:config', $this->get_context());
43. break;
44. }
45. }
47. /**
48. * Define tabs for all controllers
49. *
50. * Demo mr_tabs
51. */
52. public static function add_tabs($controller, &$tabs) {
53. // This is the NEW and prefered way to setup tabs.
54. // Only implement add_tabs in the default controller
55. // and then use the new mr_tabs methods toptab() and
56. // subtab() to create the tab structure.
57. $tabs->toptab('default')
58. ->subtab('view')
59. ->subtab('admin', array('action' => 'admin'), has_capability('moodle/site:config', $controller->get_context())) // Visible to admins
60. ->toptab('html', array('action' => 'html'))
61. ->toptab('db', array('controller' => 'db'))
62. ->subtab('view', array('controller' => 'db'))
63. ->subtab('record', array('controller' => 'db', 'action' => 'record'))
64. ->subtab('queue', array('controller' => 'db', 'action' => 'queue'))
65. ->toptab('plugin', array('controller' => 'plugin'))
66. ->toptab('cache', array('controller' => 'cache'))
67. ->toptab('filter', array('controller' => 'filter'))
68. ->toptab('table', array('controller' => 'table'))
69. ->toptab('report', array('controller' => 'report'))
70. ->toptab('server', array('controller' => 'server'))
71. ->toptab('model', array('controller' => 'model'));
72. }
74. /**
75. * Default screen
76. *
77. * Demo of the buffer helper
78. * Demo of mr_html_tag and tag helper
79. */
80. public function view_action() {
81. // Note, you can get an instance of mr_html_tag like so:
82. $tag = $this->helper->tag();
84. // Advanced usage of mr_html_tag, calls are explained below
85. return $this->helper->tag->div()->class('centerpara')->close( // Passing tag contents through close() instead of the opening call: div(). This increases readability. I can also pass as many params as I want.
86. $this->helper->tag->p( // Below, I can pass as many params as I want, all will be added to the contents of this <p> tag
87. $this->helper->world->say_hello(),
88. $this->helper->tag->p('Global config:'), // Don't need to call close(), EG: "$this->helper->tag->p('Global config:')->close()" because the tag will be casted to a string and mr_html_tag implements __toString()
89. $this->helper->buffer('print_object', $this->config)
90. ), // Same here, don't need to call close()
91. $this->output->single_button($this->url->out(false, array('action' => 'saygood')), 'Say good'),
92. $this->output->single_button($this->url->out(false, array('action' => 'saybad')), 'Say bad')
93. );
94. }
96. /**
97. * Example of setting a positive message and then going back to originating screen
98. *
99. * Demo of mr_notify
100. */
101. public function saygood_action() {
102. $this->notify->good('good');
103. redirect($this->url);
104. }
106. /**
107. * Example of setting a negative message and then going back to originating screen
108. *
109. * Demo of mr_notify
110. */
111. public function saybad_action() {
112. $this->notify->bad('bad');
113. redirect($this->url);
114. }
116. /**
117. * Admin only, restricted access by $this->require_capability()
118. */
119. public function admin_action() {
120. return 'admin only';
121. }
123. /**
124. * HTML demo
125. *
126. * Demo of mr_html_tag
127. * Demo of tag helper
128. */
129. public function html_action() {
130. $this->tabs->set('html');
131. $this->print_header();
133. echo $this->output->heading('Demo of mr_html_tag');
134. $this->helper->highlight(__CLASS__, __FUNCTION__);
135. echo $this->output->box_start('generalbox boxaligncenter boxwidthnormal');
137. #### DEMO CODE ####
138. // New instance
139. $tag = new mr_html_tag();
140. // Create html with the mr_html_tag instance
141. $html = $tag->a('Click me!')
142. ->title('This " should be encoded')
143. ->href('http://google.com')
144. ->close();
145. $this->helper->dump($html, 1);
147. // Create html with the mr_html_tag::open()
148. $html = mr_html_tag::open()->a('Click me!')
149. ->title('This " should be encoded')
150. ->href('http://google.com')
151. ->close();
152. $this->helper->dump($html, 2);
154. // Create html with the helper
155. $html = $this->helper->tag->a('Click me!')
156. ->title('This " should be encoded')
157. ->href('http://google.com')
158. ->close();
159. $this->helper->dump($html, 3);
161. // Example of using the other special methods
162. $link = $tag->a('Click me!')
163. ->title('This " should be encoded')
164. ->href('http://google.com')
165. ->class('foo');
167. // Modify attributes (Note, you can do these in bluk
168. // with add_attributes(), append_attributes(), prepend_attributes()
169. // and remove_attributes())
170. $link->prepend_class('bar')
171. ->append_class('baz')
172. ->remove_title();
173. $this->helper->dump($link->get_class(), 4);
175. // When $link is casted to a string, it will automatically render to HTML
176. $this->helper->dump((string) $link, 5);
178. // You can bulk add/modify attributes as well
180. // Example of making a tag with no content
181. $html = $tag->input()
182. ->type('submit')
183. ->value(get_string('savechanges'))
184. ->close();
185. $this->helper->dump($html, 6);
187. // You can pass tag content to either the first call or to close()
188. $html = $tag->div('This is ')
189. ->class('generalbox')
190. ->close('cool!');
191. $this->helper->dump($html, 7);
193. // Advanced usage
194. // Taking advantage of mr_html_tag::__toString to avoid calling close()
195. // Taking advantage of passing multiple tag content strings
196. $html = $tag->div()->class('generalbox')->close(
197. $tag->p(
198. 'Hello all!',
199. $tag->strong('This is cool and stuffs')
200. ),
201. $tag->span('Dingo')->class('centerpara')
202. );
203. $this->helper->dump($html, 8);
204. #### DEMO CODE ####
206. $this->output->box_end();
207. $this->print_footer();
208. }
209. }

Documentation generated on Thu, 28 Jun 2012 16:33:53 -0700 by phpDocumentor 1.4.3