-
Notifications
You must be signed in to change notification settings - Fork 31
/
modx-snippets.php
246 lines (188 loc) · 7.17 KB
/
modx-snippets.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
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
Snippet: [[SnippetName]]
Chunk: [[$ChunkName]]
System Setting: [[++SettingName]]
TV: [[*fieldName/TvName]]
Link tag: [[~PageId? ¶mName=`value`]]
Placeholder: [[+PlaceholderName]]
<?php
/* ***************************************** */
/* GLOBALS AND SETTINGS */
/* ***************************************** */
/* always available objects */
$modx->resource; // the current document
$modx->user; // the current user
/* get a system setting */
$modx->getOption('SettingName'); // eg site_start
/* ***************************************** */
/* LOGGING */
/* ***************************************** */
/* log an error */
$elementName = '[SnippetName]';
$modx->log(modX::LOG_LEVEL_ERROR, $elementName.' Could not do something.');
// also: LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARN
/* change target of logging */
$modx->setLogTarget('FILE'); // or ECHO
$target = array(
'target' => 'FILE',
'options' => array(
'filename' => 'path_to_file'),
);
$modx->log(xPDO::LOG_LEVEL_ERROR, 'Error Message', $target);
/* change error level */
$modx->setDebug(E_ALL & ~E_NOTICE); // sets error_reporting to everything except NOTICE remarks
$modx->setLogLevel(modX::LOG_LEVEL_DEBUG);
// To report errors to the MODx error log:
trigger_error('...', E_USER_ERROR);
/* ***************************************** */
/* CHUNKS */
/* ***************************************** */
/* get simple chunk */
$modx->getChunk('ChunkName');
/* parse a chunk many times */
foreach ($docs as $doc) {
$output .= $modx->getChunk('teaserTpl', $doc->toArray() );
}
return $output;
/* ***************************************** */
/* SNIPPETS */
/* ***************************************** */
// class boilerplate for creating a class for use in a snippet
// provides access to $modx object by reference and snippet properties
class MyClass {
function __construct($modx, $scriptProperties) {
$this->modx =& $modx;
$this->props =& $scriptProperties;
}
public function init() {
$this->size = $this->props['size'];
$this->color = $this->props['color'];
}
}
/* ***************************************** */
/* RESOURCES */
/* ***************************************** */
$id = $modx->resource->get('id');
/* Display a particular document */
$resource = $modx->getObject('modResource', array('pagetitle'=>'MyDocument')); // with properties
$resource = $modx->getObject('modResource', $resourceID); // with a document id
$output = '<p>Document:' . $resource->get('longtitle') . '</p>';
$output .= '<p>Content:' . $resource->get('content') . '</p>';
/* change a resource property */
$resource = $modx->getObject('modResource', $resourceID); // with a document id
$resource->set('template', 7);
$resource->save();
/* get all published resources that have not been deleted */
$resources = $modx->getCollection('modResource',
array('published'=>'1','deleted'=>'0'));
foreach($resources as $resource) {
$output .= '<div class="resource"><p>Document:' .
$resource->get('longtitle') . '</p>';
$output .= '<p>Content:' . $resource->get('content') . '</p></div>';
}
/* Get the parent object of the current resource */
$parent = $modx->resource->getOne('Parent');
return $parent->get('pagetitle');
/* Get the child objects of the current resource */
$children = $modx->resource->getMany('Children');
foreach ($children as $child) {
$output .= '<p>' . $child->get('pagetitle') . '</p>';
}
/* ***************************************** */
/* TVs */
/* ***************************************** */
/* To set a TV value for the current resource */
$tv = $modx->getObject('modTemplateVar',array('name'=>'tvName')); // get the required TV object by name (or id)
$tv->setValue($modx->resource->get('id'), $newValue); // set the new value and save it
$tv->save();
/* to get a TV value for the current resource */
/* retrieve a number of TVs for resource */
$results = array();
$tvs = $modx->getCollection(
'modTemplateVar',
array("`name` IN (
'image',
'imageClass',
'imageAlt',
'imageCaption'
)")
);
foreach ($tvs as $tv) {
$results[$tv->get('name')] = (empty($processTVs) ? $tv->getValue($resourceId) : $tv->renderOutput($resourceId));
}
// render to a chunk of goodness
return $modx->getChunk('Picture.tpl',$results);
/* get all tvs for another resource */
$obj = $modx->getObject('modResource', array('name'=>'MyDoc'));
$id = $obj->get('id');
$tvs = $obj->getMany('TemplateVars');
foreach ($tvs as $tv) {
$rawValue = $tv->getValue($id);
$processedValue = $tv->renderOutput($id);
}
/* ***************************************** */
/* USERS */
/* ***************************************** */
/* Get all active users */
$users = $modx->getCollection('modUser',
array('active'=>'1'));
foreach ($users as $user) {
$output .= '<p>Username: ' . $user->get('username') . '</p>';
}
return $output;
/* Get the user object for the creator of the current document */
$user = $modx->resource->getOne('CreatedBy');
return $user->get('username');
/* Publish a set of resources from a list of resource IDs */
$resourceIds = array('12, 23, 32, 45');
foreach ($resourceIds as $id) {
$resource = $modx->getObject('modResource',$id);
$resource->set('published', '1');
$resource->save();
}
/* ***************************************** */
/* XPDO */
/* ***************************************** */
/* getObject - get one object/table row */
/* getCollection - get many objects/table rows */
/* getOne - get one related object */
/* getMany - get many related objects */
/* getCollectionGraph - get many objects and their related objects */
/* other xpdo object methods */
$object->get('fieldName');
$object->toArray();
/* change the template of many documents with the same parent */
foreach ($modx->getIterator('modResource', array('parent' =>15)) as $res) { $res->set('template',5); $res->save(); }
/* general xpdo query */
$sql = 'SELECT * FROM `'.$modx->getOption(xPDO::OPT_TABLE_PREFIX).'site_htmlsnippets`';
$result = $modx->query($sql);
if (!is_object($result)) {
return 'No result!';
}
else {
while ($row = $q_chunks->fetch(PDO::FETCH_ASSOC))
// do something: $row['name'];
}
}
/* ***************************************** */
/* MODX SERVICES - EMAIL */
/* ***************************************** */
/* send an email */
// first, get the email with the placeholders in it replaced by the snippet call.
// (Note the properties in the snippet call are in the array $scriptProperties.
// http://api.modx.com/revolution/2.1/_model_modx_mail_modmail.class.html
$message = $modx->getChunk('myEmail',$scriptProperties);
/* now load modMail, and setup options */
$modx->getService('mail', 'mail.modPHPMailer');
$modx->mail->set(modMail::MAIL_BODY,$message);
$modx->mail->set(modMail::MAIL_FROM,$scriptProperties['fromEmail']);
$modx->mail->set(modMail::MAIL_FROM_NAME,$scriptProperties['fromName']);
$modx->mail->set(modMail::MAIL_SENDER,$scriptProperties['fromName']);
$modx->mail->set(modMail::MAIL_SUBJECT,$scriptProperties['subject']);
$modx->mail->address('reply-to',$scriptProperties['fromEmail']);
$modx->mail->setHTML(true);
/* specify the recipient */
$modx->mail->address('to',$scriptProperties['toEmail']);
/* send! */
$modx->mail->send();
$modx->mail->reset();
?>