-
Notifications
You must be signed in to change notification settings - Fork 2
/
chat.module
244 lines (216 loc) · 6.4 KB
/
chat.module
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
/**
* Implements hook_permission().
*/
function chat_permission() {
return array(
'administer sms' => array(
'title' => t('Administer Chat SMS'),
),
);
}
function chat_menu() {
$items['sms/%'] = array(
'title' => 'SMS',
'page callback' => 'smsentity_view_entity',
'page arguments' => array(1),
'access callback' => TRUE,
);
$items['api'] = array(
'title' => 'SMS POST API',
'page callback' => 'sms_api',
'access callback' => TRUE,
);
return $items;
}
function sms_api() {
// dsm($_REQUEST);
$message = urldecode($_REQUEST['text']);
$prefix = substr($message, 0, 4);
if (strtolower($prefix) == 'chat') {
$message = trim(substr($message, 4));
$new_sms = entity_create('sms', array(
'type' => 'sms', //bundle
'mobile' => check_plain($_REQUEST['phone']),
'message' => $message,
))->save();
dsm($new_sms);
}
return "Message Posted";
}
function chat_entity_info() {
$info = array();
// Our custom sms entity needs to be defined here.
// See http://drupal.org/node/1026420 and http://drupal.org/node/878804
// for more documentation.
$info['sms'] = array(
// Human readable label.
'label' => t('SMS'),
// Table for storing entity data, defined in hook_schema().
'base table' => 'sms',
// This helps Entity API know how to query your custom table.
'entity keys' => array(
'id' => 'id',
'label' => 'mobile',
),
// These are the default controllers.
// 'entity class' => 'Entity',
'entity class' => 'SMSEntity',
// This is a uri function provided by the Entity contrib module.
// It's a simple wrapper around the uri method in the controller class.
'uri callback' => 'entity_class_uri',
// 'controller class' => 'DrupalDefaultEntityController',
// Other options provided by the EntityAPI contrib module:
// 'controller class' => 'EntityAPIController ',
// 'controller class' => 'EntityAPIControllerExportable',
'controller class' => 'SMSEntityController',
// The information below is used to extend the EntityDefaultUIController
'admin ui' => array(
'path' => 'admin/sms',
'controller class' => 'SMSEntityUIController',
'menu wildcard' => '%entity_object',
'file' => 'sms.admin.inc',
),
//Inside of the default ui controller, there is some code that requires this
//for creating the appropriate hook_menu equivalents
'module' => 'chat',
// Access callback to determine permisisons.
'access callback' => 'sms_access_callback',
// Tell FieldAPI that fields can be attached to our sms entity
'fieldable' => TRUE,
'bundles' => array(
'sms' => array(
'label' => t('SMS'),
'admin' => array(
'path' => 'admin/sms',
'access arguments' => array('administer sms'),
),
),
),
// Add views support
'module' => 'chat',
'views controller class' => 'SMSEntityViewsController',
);
return $info;
}
/**
* Our custom entity class.
*
* The Entity class we're overriding is in
* sites/all/modules/contrib/entity/includes/entity.inc
*/
class SMSEntity extends Entity {
/**
* Override this in order to implement a custom default URI.
*/
protected function defaultUri() {
return array('path' => 'sms/' . $this->identifier());
}
}
/**
* Callback for /sms/ID page.
*
* Just a place to render a complete sms entity.
*/
function smsentity_view_entity($smsid) {
$sms = entity_load_single('sms', $smsid);
drupal_set_title($sms->mobile);
$sms_entity = entity_view('sms', array($smsid => $sms));
return $sms_entity;
}
class SMSEntityController extends EntityAPIController {
public function buildContent($entity, $view_mode = 'full', $langcode = NULL) {
$build = parent::buildContent($entity, $view_mode, $langcode);
$build['mobile'] = array(
'#type' => 'markup',
'#markup' => $entity->mobile,
);
$build['message'] = array(
'#type' => 'markup',
'#markup' => $entity->message,
);
return $build;
}
/**
* Override save method.
*
* Populate created and updated dates automatically.
*/
public function save($entity, DatabaseTransaction $transaction = NULL) {
if (isset($entity->is_new)) {
$entity->created_at = REQUEST_TIME;
}
$entity->message = trim($entity->message);
return parent::save($entity, $transaction);
}
}
/**
* Check access permissions for sms entities.
*/
function sms_access_callback($op, $sms = NULL, $account = NULL) {
if (user_access('administer sms', $account)) {
return TRUE;
}
return FALSE;
}
/**
* Our custom controller for the admin ui.
*
* The EntityDefaultUIController can be found in
* sites/all/modules/contrib/entity/includes/entity.ui.inc
*/
class SMSEntityUIController extends EntityDefaultUIController {
protected function overviewTableHeaders($conditions, $rows, $additional_header = array()) {
$header = parent::overviewTableHeaders($conditions, $rows, array('Message', 'Received'));
return $header;
}
protected function overviewTableRow($conditions, $id, $entity, $additional_cols = array()) {
$diff = time() - $entity->created_at;
$time_ago = format_interval($diff);
$row = parent::overviewTableRow($conditions, $id, $entity, array($entity->message, $time_ago));
// Add your custom data here
return $row;
}
}
class SMSEntityViewsController extends EntityDefaultViewsController {
/**
* Add extra fields to views_data().
*/
public function views_data() {
$data = parent::views_data();
// Add your custom data here
return $data;
}
}
/**
* Implements hook_entity_property_info().
*/
function chat_entity_property_info() {
$info = array();
$properties = &$info['sms']['properties'];
$properties['id'] = array(
'label' => t('SMS ID'),
'description' => t('The uniquie ID of the sms.'),
'type' => 'integer',
'schema field' => 'id',
);
$properties['mobile'] = array(
'label' => t('Mobile No'),
'description' => t('Mobile No'),
'type' => 'text',
'schema field' => 'mobile',
);
$properties['message'] = array(
'label' => t('Embedcode'),
'description' => t('SMS Message'),
'type' => 'text',
'schema field' => 'message',
);
$properties['created_at'] = array(
'label' => t('Created date'),
'description' => t('Date the SMS was created'),
'type' => 'date',
'schema field' => 'created_at',
);
return $info;
}