| 17 | | symfony plugin-install http://plugins.symfony-project.com/sfPropelNotificationPlugin |
|---|
| | 17 | #!sh |
|---|
| | 18 | symfony plugin-install http://plugins.symfony-project.com/sfPropelNotificationPlugin |
|---|
| | 19 | }}} |
|---|
| | 20 | |
|---|
| | 21 | === Set up relationship with your model's user table === |
|---|
| | 22 | |
|---|
| | 23 | * Update plugin's `schema.xml` : replace `@FIXME@` occurences in the `watch` table with correct values. Eg. : |
|---|
| | 24 | |
|---|
| | 25 | {{{ |
|---|
| | 26 | #!xml |
|---|
| | 27 | <?xml version="1.0" encoding="UTF-8"?> |
|---|
| | 28 | <table name="watch"> |
|---|
| | 29 | <column name="id" type="INTEGER" required="true" primaryKey="true" autoIncrement="true" /> |
|---|
| | 30 | <column name="user_id" type="INTEGER" required="true" /> |
|---|
| | 31 | <column name="watch_type" type="VARCHAR" size="255" required="true" /> |
|---|
| | 32 | <index name="watch_FKIndex2"> |
|---|
| | 33 | <index-column name="user_id" /> |
|---|
| | 34 | </index> |
|---|
| | 35 | <foreign-key foreignTable="User" name="Rel_02" onDelete=""> |
|---|
| | 36 | <reference local="user_id" foreign="id" /> |
|---|
| | 37 | </foreign-key> |
|---|
| | 38 | </table> |
|---|
| | 39 | }}} |
|---|
| | 40 | |
|---|
| | 41 | * Register connected user peer class and unique id in session, under the `sfPropelNotificationPlugin` namespace : |
|---|
| | 42 | |
|---|
| | 43 | For instance, when logging user in : |
|---|
| | 44 | |
|---|
| | 45 | {{{ |
|---|
| | 46 | #!php |
|---|
| | 47 | <?php |
|---|
| | 48 | $this->getUser()->setAttribute('uid', $id, 'sfPropelNotificationPlugin'); |
|---|
| | 49 | $this->getUser()->setAttribute('peer_class', 'UserPeer', 'sfPropelNotificationPlugin'); |
|---|
| | 54 | === Defining notification types === |
|---|
| | 55 | |
|---|
| | 56 | This plugin enable users to subscribe to notification "types". Let's see how to define a notification type which will trigger notification |
|---|
| | 57 | when an (imaginary) article is reported being offensive by five users or more : |
|---|
| | 58 | |
|---|
| | 59 | * Create a `sfPropelNotificationPlugin` directory in your application's `config` directory |
|---|
| | 60 | * Create a `types.yml` file in this directory |
|---|
| | 61 | * Specify a new notification type : |
|---|
| | 62 | {{{ |
|---|
| | 63 | offensive_article: |
|---|
| | 64 | enabled: on |
|---|
| | 65 | logic_class: myLogicProvider |
|---|
| | 66 | }}} |
|---|
| | 67 | |
|---|
| | 68 | The `myLogicProvider` class implements the logic that will tell if notification logic should be triggered : |
|---|
| | 69 | {{{ |
|---|
| | 70 | #!php |
|---|
| | 71 | <?php |
|---|
| | 72 | class myLogicProvider |
|---|
| | 73 | { |
|---|
| | 74 | public static function OffensiveArticle(Article $article) |
|---|
| | 75 | { |
|---|
| | 76 | return $article->countOffensiveReports() >= 5; |
|---|
| | 77 | } |
|---|
| | 78 | } |
|---|
| | 79 | }}} |
|---|
| | 80 | |
|---|
| | 81 | * Register notification behavior with your `Article` class : |
|---|
| | 82 | |
|---|
| | 83 | {{{ |
|---|
| | 84 | #!php |
|---|
| | 85 | <?php |
|---|
| | 86 | // lib/model/Article.php |
|---|
| | 87 | class Article extends BaseArticle |
|---|
| | 88 | { |
|---|
| | 89 | // ... |
|---|
| | 90 | } |
|---|
| | 91 | |
|---|
| | 92 | sfPropelBehavior::add('Article', array('sfPropelNotificationPlugin' => array('types' => array('offensive_article'))); |
|---|
| | 93 | }}} |
|---|
| | 94 | |
|---|
| | 95 | And that's it. Every time an article is modified, it will be checked against every notification type it's registered with. |
|---|
| | 96 | If type's associated logic method returns true, notification will be triggered for every user who subscribed to it, using whatever notification |
|---|
| | 97 | backend(s) they chose. |
|---|
| | 98 | |
|---|
| | 115 | The plugin comes with a default email notification backend (based on symfony's `sfMail` class). |
|---|
| | 116 | |
|---|
| | 117 | You first need to configure it for your needs : |
|---|
| | 118 | |
|---|
| | 119 | * Copy plugin's `config/sfPropelNotificationPlugin/backends.yml` file in yout application's `config/sfPropelNotificationPlugin` directory. |
|---|
| | 120 | * Configure it accordingly to your needs : |
|---|
| | 121 | |
|---|
| | 122 | {{{ |
|---|
| | 123 | email: |
|---|
| | 124 | enabled: on |
|---|
| | 125 | class: sfPropelNotificationPluginSfMailBackend |
|---|
| | 126 | params: |
|---|
| | 127 | mailer: sendmail |
|---|
| | 128 | charset: utf-8 |
|---|
| | 129 | sender_name: Notification bot |
|---|
| | 130 | sender_address: noreply@example.com |
|---|
| | 131 | subject_prefix: "[notification]" |
|---|
| | 132 | }}} |
|---|
| | 133 | |
|---|
| | 134 | You must then implement the notification emails template. Email body is provided by plugin's `sfPropelNotificationPluginBackends::SfMail` action. |
|---|
| | 135 | Enable it in your application's `settings.yml` : |
|---|
| | 136 | |
|---|
| | 137 | {{{ |
|---|
| | 138 | all: |
|---|
| | 139 | .settings: |
|---|
| | 140 | enabled_modules: [default, sfPropelNotificationPluginNotifications, sfPropelNotificationPluginBackends] |
|---|
| | 141 | }}} |
|---|
| | 142 | |
|---|
| | 143 | You must create one template per notification type : |
|---|
| | 144 | |
|---|
| | 145 | * Create a module directory tree in your application : |
|---|
| | 146 | |
|---|
| | 147 | {{{ |
|---|
| | 148 | modules |
|---|
| | 149 | |-- sfPropelNotificationPluginBackends |
|---|
| | 150 | | -- templates |
|---|
| | 151 | }}} |
|---|
| | 152 | |
|---|
| | 153 | * Implement needed templates : |
|---|
| | 154 | |
|---|
| | 155 | Templates names correspond to notification type name (eg. `OffensiveArticleSuccess.php`) |
|---|
| | 156 | The following variables are made available to template : |
|---|
| | 157 | |
|---|
| | 158 | * `$object` : the object which triggered notification |
|---|
| | 159 | * `$watch` : the watch from database. It grants access database user through its `getUser` method. |
|---|
| | 160 | |
|---|
| | 161 | Here is an example template : |
|---|
| | 162 | |
|---|
| | 163 | {{{ |
|---|
| | 164 | #!php |
|---|
| | 165 | // modules/sfPropelNotificationPluginBackends/templates/OffensiveArticleSuccess.php |
|---|
| | 166 | |
|---|
| | 167 | Hello <?php echo $watch->getUser()->getFirstName() ?>, |
|---|
| | 168 | |
|---|
| | 169 | You receive this notification email because the article <?php echo $object->getTitle() ?> (<?php echo url_to('@article_view?id='.$object->getId()) ?>) |
|---|
| | 170 | has been reported offensive several times. |
|---|
| | 171 | |
|---|
| | 172 | You can : |
|---|
| | 173 | |
|---|
| | 174 | * Delete it : <?php echo url_to('@article_delete?id='.$object->getId()) ?> |
|---|
| | 175 | |
|---|
| | 176 | or |
|---|
| | 177 | |
|---|
| | 178 | * Reset its offensiveness status : <?php echo url_to('@article_reset?id='.$object->getId()) ?> |
|---|
| | 179 | }}} |
|---|
| | 180 | |
|---|