PageRenderTime 58ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/reference/en/11-Events.markdown

https://github.com/sympal/sympal-docs
Markdown | 430 lines | 337 code | 93 blank | 0 comment | 0 complexity | 3082db5dd73fb51444d32d4c9da84ee0 MD5 | raw file
  1. # Introduction
  2. One of the greatest features of Sympal is that it takes advantage of the
  3. wonderful event system that Symfony provides. Sympal implements dozens of events
  4. that are strategically placed in the core code so that you can connect to them
  5. and manipulate functionality without altering any core code.
  6. In this chapter we will describe all the different events in Sympal and how you
  7. can utilize them.
  8. # Installation
  9. The installation of Sympal offers two events that you can connect to. These are
  10. simple events and allow you to execute your own code before and after the
  11. installation runs. Below you will find examples for both.
  12. ## Pre Install
  13. You can easily connect to the `sympal.pre_install` event and execute some custom
  14. code before Sympal is installed.
  15. [php]
  16. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  17. {
  18. public function initialize()
  19. {
  20. $this->dispatcher->connect('sympal.pre_install',
  21. array($this, 'sympalPreInstall'))
  22. }
  23. public function sympalPreInstall(sfEvent $event)
  24. {
  25. // do some custom code
  26. }
  27. }
  28. ## Post Install
  29. You can also just as easily connect to the `sympal.post_install` event and
  30. execute some custom code after Sympal is installed.
  31. [php]
  32. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  33. {
  34. public function initialize()
  35. {
  36. $this->dispatcher->connect('sympal.post_install',
  37. array($this, 'sympalPostInstall'))
  38. }
  39. public function sympalPostInstall(sfEvent $event)
  40. {
  41. // do some custmo code
  42. }
  43. }
  44. # Models
  45. Sympal implements a few events in Doctrine models so that you can easily
  46. customize models, add methods, etc. from your own code.
  47. ## Setup
  48. When using Sympal you can alter any aspect of the a model during its setup and
  49. initialization. This allows Sympal plugins to add columns to models, remove
  50. columns, add relationships, etc. Below you will find some examples of how to
  51. utilize these events.
  52. Imagine you have a Sympal plugin name `sfSympalUserProfilePlugin` which
  53. introduces a new model named `Profile` and adds a foreign key `profile_id` to
  54. the `User` model and adds the relationship between the two. First we need to
  55. hook in to the set table definition for the `User` model to add the `profile_id`
  56. column.
  57. > **TIP**
  58. > The `model_name` is what to use if the model was named `ModelName`. The event
  59. > names are always lower case and underscored. Even if your model name is
  60. > capitalized.
  61. Now we can add the column and our relationship to the `User` model with the
  62. following code.
  63. [php]
  64. class sfSympalUserProfilePluginConfiguration extends sfPluginConfiguration
  65. {
  66. public function initialize()
  67. {
  68. $this->dispatcher->connect('sympal.user.set_table_definition',
  69. array($this, 'userSetTableDefinition'));
  70. $this->dispatcher->connect('sympal.user.set_up',
  71. array($this, 'userSetUp'));
  72. }
  73. public function userSetTableDefinition(sfEvent $event)
  74. {
  75. $subject = $event->getSubject();
  76. $subject->hasColumn('profile_id', 'integer');
  77. }
  78. public function userSetUp(sfEvent $event)
  79. {
  80. $subject = $event->getSubject();
  81. $subject->hasOne('Profile', array(
  82. 'local' => 'profile_id',
  83. 'foreign' => 'id'
  84. ));
  85. }
  86. }
  87. Now our `User` model has a relationship to the `Profile` object and was all done
  88. without actually modifying any core code.
  89. [php]
  90. $user = new User();
  91. $profile = $user->Profile;
  92. # Content Format
  93. Sympal offers your content automatically in several formats. If you have a new
  94. format you wish to add you can easily do so by connecting to the
  95. `sympal.content_renderer.unknown_format` event.
  96. [php]
  97. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  98. {
  99. public function initialize()
  100. {
  101. $this->dispatcher->connect('sympal.content_renderer.unknown_format',
  102. array($this, 'listenForUnknownFormat'));
  103. }
  104. public function listenForUnknownFormat(sfEvent $event)
  105. {
  106. // Set the extension name of your format
  107. $event['format'] = 'encryptedJson';
  108. // Build the output for your format
  109. $content = $event['content'];
  110. $array = $content->toArray(true);
  111. $json = json_encode($array);
  112. $encrypted = $this->someEncryptFunction($json);
  113. $event->setProcessed();
  114. $event->setReturnValue($encrypted);
  115. return true;
  116. }
  117. public function someEncryptFunction($json)
  118. {
  119. // encrypt the json with some key/salt
  120. // so the retrieving end can decrypt it
  121. return $json;
  122. }
  123. }
  124. You can also take a look at the `sfSympalContentListPlugin` which is a core
  125. Sympal plugin. It adds feeds functionality to your content lists. It can be
  126. found in `sfSympalPlugin/lib/plugins/sfSympalContentListPlugin`.
  127. # Admin Menu
  128. The admin menu in Sympal is a central administrative navigation menu that you can
  129. connect to and add new menus, or alter existing menus. You can add to this admin menu by connecting to the `sympal.load_admin_menu` event.
  130. In this example we'll add a new child to the Administration menu.
  131. [php]
  132. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  133. {
  134. public function initialize()
  135. {
  136. $this->dispatcher->connect('sympal.load_admin_menu',
  137. array($this, 'loadAdminMenu'));
  138. }
  139. public function loadAdminMenu(sfEvent $event)
  140. {
  141. $menu = $event->getSubject();
  142. $menu->getChild('Administration')
  143. ->addChild('New Item', '@new_item_route');
  144. }
  145. }
  146. This is useful if you write a Sympal plugin that offers some new modules for
  147. administering the functionality of your plugin. You can connect to the above
  148. event and add links to these modules.
  149. # Editor Menu
  150. The editor menu is the dropdown menu in the inline edit car that shows when you are editing some content. All the contents of this panel are populated via events. So even the items you see by
  151. default are added via these events.
  152. [asset:127 title=Editor menu]
  153. To add to menu you can simply connect to the `sympal.load_editor`
  154. event.
  155. [php]
  156. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  157. {
  158. public function initialize()
  159. {
  160. $this->dispatcher->connect('sympal.load_editor',
  161. array($this, 'loadEditor'));
  162. }
  163. public function loadEditor(sfEvent $event)
  164. {
  165. $menu = $event->getSubject();
  166. $menu->addChild('New Item', '@new_item_route');
  167. }
  168. }
  169. This is useful if you add some new functionality to content and want to show
  170. controls for the functionality in the editor menu.
  171. # Actions
  172. You can easily add functionality to all your actions class by connecting to the
  173. `component.method_not_found` event. This is a event provided by the Symfony
  174. core.
  175. [php]
  176. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  177. {
  178. public function initialize()
  179. {
  180. $this->dispatcher->connect('component.method_not_found',
  181. array(new myActionsExtension(), 'extend'));
  182. }
  183. }
  184. class myActionsExtension extends sfSympalExtendClass
  185. {
  186. public function goBack()
  187. {
  188. $this->redirect($this->getRequest()->getReferer());
  189. }
  190. }
  191. The above code would make available a `goBack()` method in all your actions
  192. class that goes back to the referer.
  193. > **NOTE**
  194. > Sympal uses this event internally and makes available several new functions to
  195. > your actions class. `goBack()` is in fact one of them. For demonstration
  196. > purposes we used this feature as an example above.
  197. [php]
  198. class my_moduleActions extends sfActions
  199. {
  200. public function executeSave_comment(sfWebRequest $request)
  201. {
  202. // save comment
  203. // go back to where we came from
  204. $this->goBack();
  205. }
  206. }
  207. # Configuration Form
  208. Sympal provides a means to edit all the configurations from a web interface. The
  209. default way to edit configuration values is by manually overriding the `app.yml`
  210. file and changing values. If you want your configurations to be editable in the
  211. browser then you can connect to the `sympal.load_config_form` event and add the
  212. settings to the form.
  213. ## Adding Configuration
  214. Imagine you had a `sfSympalTestPlugin` and you have a `config/app.yml` that
  215. looks like the following:
  216. [yml]
  217. all:
  218. sympal_config:
  219. test:
  220. my_setting: true
  221. Now if you want that setting to be editable from the browser you can do the
  222. following.
  223. [php]
  224. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  225. {
  226. public function initialize()
  227. {
  228. $this->dispatcher->connect('sympal.load_config_form',
  229. array($this, 'loadConfig'));
  230. }
  231. public function loadConfig(sfEvent $event)
  232. {
  233. $form = $event->getSubject();
  234. $form->addSetting('test', 'my_setting',
  235. 'My Setting', 'InputCheckbox', 'Boolean');
  236. }
  237. }
  238. ## Pre Save
  239. You can hook in to the saving process of the configuration form and execute some
  240. custom code before it is saved.
  241. [php]
  242. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  243. {
  244. public function initialize()
  245. {
  246. $this->dispatcher->connect('sympal.config_form.pre_save',
  247. array($this, 'configFormPreSave'));
  248. }
  249. public function configFormPreSave(sfEvent $event)
  250. {
  251. $form = $event->getSubject();
  252. // execute some custom code
  253. }
  254. }
  255. ## Post Save
  256. You can hook in to the saving process of the configuration form and execute some
  257. custom code after it is saved.
  258. [php]
  259. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  260. {
  261. public function initialize()
  262. {
  263. $this->dispatcher->connect('sympal.config_form.post_save',
  264. array($this, 'configFormPostSave'));
  265. }
  266. public function configFormPostSave(sfEvent $event)
  267. {
  268. $form = $event->getSubject();
  269. // execute some custom code
  270. }
  271. }
  272. # Dashboard
  273. The contents of the dashboard you see when you login are populated by events as
  274. well. So if you want to add a new box or change something on the dashboard then
  275. you can do by connecting to some events.
  276. ## Boxes
  277. First if you want to add some new boxes to the dashboard you can do so by
  278. connecting to the `sympal.load_dashboard_boxes` event.
  279. [php]
  280. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  281. {
  282. public function initialize()
  283. {
  284. $this->dispatcher->connect('sympal.load_dashboard_boxes',
  285. array($this, 'loadDashboardBoxes'));
  286. }
  287. public function loadDashboardBoxes(sfEvent $event)
  288. {
  289. $menu = $event->getSubject();
  290. $menu->addChild('New Box', '@new_box_route');
  291. }
  292. }
  293. # Content Rendering
  294. When a Sympal content record is rendered we notify a few events to give you an
  295. opportunity to change something about the rendering.
  296. ## Filter Slot Content
  297. You can filter the rendered slot value with the `sympal.content_renderer.filter_slot_content` event.
  298. [php]
  299. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  300. {
  301. public function initialize()
  302. {
  303. $this->dispatcher->connect('sympal.content_renderer.filter_slot_content',
  304. array($this, 'filterSympalSlotContent'));
  305. }
  306. public function filterSympalContent(sfEvent $event, $content)
  307. {
  308. // Manipulate the $content string
  309. // Add to it
  310. // Change it, etc..
  311. return $content;
  312. }
  313. }
  314. ## Filter Content
  315. After some content has been rendered you can use a filter event to filter the
  316. html of the rendered content. This is a similar concept to the core Symfony
  317. event named `response.filter_content` which allows you to alter the response
  318. HTML before it is sent to the browser.
  319. [php]
  320. class sfSympalTestPluginConfiguration extends sfPluginConfiguration
  321. {
  322. public function initialize()
  323. {
  324. $this->dispatcher->connect('sympal.content_renderer.filter_content',
  325. array($this, 'filterSympalContent'));
  326. }
  327. public function filterSympalContent(sfEvent $event, $content)
  328. {
  329. // Manipulate the $content string
  330. // Add to it
  331. // Change it, etc..
  332. return $content;
  333. }
  334. }
  335. > **TIP**
  336. > The web debug toolbar is added to the HTML automatically by Symfony by
  337. > connecting to the `response.filter_content` event.