-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathentity_view_count.module
More file actions
297 lines (255 loc) · 6.93 KB
/
entity_view_count.module
File metadata and controls
297 lines (255 loc) · 6.93 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
<?php
/**
* @file
* Code for entity view count.
*/
require_once 'entity_view_count.field.inc';
/**
* Implements hook_menu().
*/
function entity_view_count_menu() {
$items = array();
$items['admin/config/system/entity_view_count'] = array(
'title' => 'Entity view count settings',
'description' => 'Settings for the Entity view count module.',
'page callback' => 'drupal_get_form',
'page arguments' => array('entity_view_count_settings'),
'access arguments' => array('administer entity view count entries'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function entity_view_count_permission() {
return array(
'administer entity view count entries' => array(
'title' => t('Administer entity view count entries'),
'description' => t('Grant permission to manage entity view count entries'),
),
'view entity view count field' => array(
'title' => t('View entity view count field'),
'description' => t('Allow to the user watch the entity view count values.'),
),
);
}
/**
* Implements hook_view_api().
*/
function entity_view_count_views_api() {
return array(
'api' => 3,
'path' => drupal_get_path('module', 'entity_view_count') . '/includes/views',
);
}
/**
* Implement hook_ctools_plugin_directory().
*/
function entity_view_count_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools') {
return 'plugins/' . $plugin;
}
}
/**
* Implement hook_entity_info().
*/
function entity_view_count_entity_info() {
$info = array();
$info['entity_view_count'] = array(
'label' => t('Entity view count'),
'base table' => 'entity_view_count',
'entity class' => 'EntityViewCount',
'controller class' => 'EntityAPIController',
'entity keys' => array(
'id' => 'id',
'bundle' => 'type',
),
'fieldable' => TRUE,
'metadata controller class' => 'EntityDefaultMetadataController',
'views controller class' => 'EntityViewCountViewController',
);
return $info;
}
/**
* Create entity view count.
*
* @param array $params
* Array of with default arrays.
*
* @return EntityViewCount
* Return non-saved object of entity view count.
*/
function entity_view_count_create(array $params) {
global $user;
if (isset($params['entity']) ) {
$wrapper = entity_metadata_wrapper($params['entity_type'], $params['entity']);
$params['entity_id'] = $wrapper->getIdentifier();
// Remove the entity key.
unset($params['entity']);
}
$params += array(
'created' => REQUEST_TIME,
'uid' => $user->uid,
'type' => 'entity_view_count_base',
);
return entity_create('entity_view_count', $params);
}
/**
* Load entity view count.
*
* @param Integer $id
* The entry ID.
*
* @return EntityViewCount
* The object from the DB.
*/
function entity_view_count_load($id) {
return entity_load_single('entity_view_count', $id);
}
/**
* Load multiple object from the db.
*
* @param array $ids
* List of entity IDs.
*
* @return EntityViewCount[]
*/
function entity_view_count_load_multiple(array $ids) {
return entity_load('entity_view_count', $ids);
}
/**
* Delete multiple entity view count entries from the DB.
*
* @param array $ids
* List of ids.
*/
function entity_view_count_delete_multiple($ids) {
entity_delete_multiple('entity_view_count', $ids);
}
/**
* Implements hook_entity_view().
*/
function entity_view_count_entity_view($entity, $type, $view_mode, $langcode) {
$info = variable_get('entity_view_count_track_' . $type);
if (empty($info[$view_mode])) {
return;
}
$params = array(
'entity_type' => $type,
'entity' => $entity,
'view_mode' => $view_mode,
'entity_owner' => !empty($entity->uid) ? $entity->uid : 0,
);
entity_view_count_create($params)->save();
}
/**
* Form callback; Set the settings for the module.
*/
function entity_view_count_settings($form, $form_state) {
$form['track'] = array(
'#type' => 'vertical_tabs',
);
$entities = entity_get_info();
foreach ($entities as $name => $entity) {
if (empty($entity['view modes'])) {
continue;
}
$form['track'][$name] = array(
'#type' => 'fieldset',
'#title' => $entity['label'],
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'visibility',
'#weight' => 0,
);
$selected = variable_get('entity_view_count_track_' . $name, array());
$default_views = array();
foreach ($selected as $view_mode_name => $select) {
if (!$select) {
continue;
}
$default_views[] = $view_mode_name;
}
$form['track'][$name]['entity_view_count_track_' . $name] = array(
'#type' => 'checkboxes',
'#title' => t('Select which view modes will be counted as viewed'),
'#default_value' => $default_views,
);
foreach ($entity['view modes'] as $view_mode_name => $info) {
$form['track'][$name]['entity_view_count_track_' . $name]['#options'][$view_mode_name] = $info['label'];
}
}
return system_settings_form($form);
}
/**
* Get the number of times the entity was viewed.
*
* @param String $entity_type
* The entity type.
* @param Integer $entity_id
* The entity ID.
*
* @return integer
* The number of times the entity was viewed.
*/
function entity_view_count_get_entity_count($entity_type, $entity_id) {
$query = new EntityFieldQuery();
return $query
->entityCondition('entity_type', 'entity_view_count')
->propertyCondition('entity_type', $entity_type)
->propertyCondition('entity_id', $entity_id)
->count()
->execute();
}
/**
* Get the number of times the user view entities.
*
* @param integer $uid
* The user ID.
*
* @return Integer
* The number of times the user viewed entities.
*/
function entity_view_count_user_viewing_entities_count($uid) {
$query = new EntityFieldQuery();
return $query
->entityCondition('entity_type', 'entity_view_count')
->propertyCondition('uid', $uid)
->count()
->execute();
}
/**
* Count how much the users owned entities were viewed.
*
* @param integer $uid
* The user ID.
*
* @return integer
* The amount of times the user owned entities were viewed.
*/
function entity_view_count_user_owned_entity_count($uid) {
$query = new EntityFieldQuery();
return $query
->entityCondition('entity_type', 'entity_view_count')
->propertyCondition('entity_owner', $uid)
->count()
->execute();
}
/**
* Implements hook_entity_delete().
*
* Delete any entries relate to the delete entity.
*/
function entity_view_count_entity_delete($entity, $type) {
$wrapper = entity_metadata_wrapper($type, $entity);
$query = new EntityFieldQuery();
$results = $query
->entityCondition('entity_type', 'entity_view_count')
->propertyCondition('entity_type', $type)
->propertyCondition('entity_id', $wrapper->getIdentifier())
->execute();
if (empty($results['entity_view_count'])) {
return;
}
entity_view_count_delete_multiple(array_keys($results['entity_view_count']));
}