-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExtMessageComponent.php
executable file
·129 lines (108 loc) · 4.57 KB
/
ExtMessageComponent.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/**
* This is the Message-Component
* You can retrieve and save messages using this component
*
* @author dispy <[email protected]>
*/
class ExtMessageComponent extends CApplicationComponent{
public function init(){
//Publish the bootstrap-js and the css-file
$cs = Yii::app()->getAssetManager();
$assetPath = $cs->publish(Yii::getPathOfAlias('ext.message.assets'));
Yii::app()->clientScript->registerScriptFile(
$assetPath.'/js/bootstrap-modal.js');
Yii::app()->clientScript->registerCssFile($assetPath.'/css/messages.css');
//Set Import paths
Yii::import('ext.message.models.*');
Yii::import('ext.message.widgets.*');
}
/**
* Post a message into the database (this is the most convenient variant)
* @param string $type supported types: '' (is a warning), 'error', success, 'info'
* @param string $msg The message you want to display in plain text
* @param array $messageOptions Options: see ExtMessage::$defaults
* @param array $triggerOptions Options: see ExtTrigger::getDefaults
*/
public function postMessage($type, $msg, $messageOptions = array(), $triggerOptions = array()){
//create Message and set attributes
$message = new ExtMessage();
$message->type = $type;
$message->text = $msg;
$message->params= $messageOptions;
//delegate to lower level
$this->postExtMessage($message, $triggerOptions);
}
/**
* This is the direct variant, you won't use generally
* @param string $message the message to post
* @param array $options the trigger options (see ExtTrigger::getDefaults)
* @return boolean true/false
* @throws RuntimeException
*/
public function postExtMessage($message, $options){
//Assure that the message is saved
if(!$message->save()){
$errors = $message->getErrors();
throw new RuntimeException(
Yii::t('message',
'Unable to save message:{err}',
array('{err]' => $errors[0]))
);
}
//Merge the options for the trigger
$options = array_merge(ExtTrigger::getDefaults(), $options);
//Create Trigger itself
$trigger = new ExtTrigger();
$trigger->message_id = $message->id;
$trigger->setAttributes($options, false);
//Set the user or the session this trigger is assigned to
if(!Yii::app()->user->isGuest){
$trigger->user_id = Yii::app()->user->id;
}
$trigger->session_identifier = $this->getSessionIdentifier();
return $trigger->save();
}
/**
* Gets all the messages for the current user
* if the user isn't logged in, it will use the cookie
* @see getSessionIdentifier
* @return array array of Message-Instances
*/
public function getMessages(){
$criteria = new CDbCriteria();
//If there's no filter set, assume that the filtering is done otherwise
// e.g.: ACL
$filterDisabled = 'user_id = NULL AND session_identifier = NULL';
$conditions = array($filterDisabled);
//If the user is logged in we search for his User-ID
if(!Yii::app()->user->isGuest){
$conditions[] = 'user_id = '.Yii::app()->user->id;
}
//Non-exclusive: he may have gotten a message before he logged in
$conditions[] = 'session_identifier = :sessId';
$criteria->params[':sessId'] = $this->getSessionIdentifier();
$criteria->addCondition($conditions, 'OR');
//We want only Messages for which at least one trigger for the current user
//matched
$relatedModels = array(
'triggers' => array(
'joinType'=>'INNER JOIN',
)
);
$messages = ExtMessage::model()->with($relatedModels)->findAll($criteria);
return $messages;
}
/**
* Returns a unique session identifier
* This assumes that you actually use the PHPSESSID-cookie. If you don't,
* adjust this line
* @return string the unique session-identifier
*/
protected function getSessionIdentifier(){
//If there's no such cookie, the user cannot be identified and therefore
//cannot get _any_ messages
return @$_COOKIE['PHPSESSID'];
}
}
?>