-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpinapi.drush.inc
331 lines (300 loc) · 9.67 KB
/
pinapi.drush.inc
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
/**
* @file
* Provides Drush integration for the Pin API module.
*/
/**
* Implements hook_drush_command().
*/
function pinapi_drush_command() {
$items['pinapi-collection-create'] = array(
'description' => 'Create collection.',
'arguments' => array(
'machine_name' => 'Machine name of the collection.',
),
'required-arguments' => TRUE,
'options' => array(
'name' => 'The human friendly name',
'status' => 'The status of the collection',
),
);
$items['pinapi-collection-calculate'] = array(
'description' => 'Calculate collection.',
'arguments' => array(
'cid' => 'Collection ID.',
),
'required-arguments' => TRUE,
);
$items['pinapi-collection-import'] = array(
'description' => 'Imports pins to a collection.',
'arguments' => array(
'cid' => 'Collection ID.',
),
'required-arguments' => TRUE,
'options' => array(
'input' => 'Input directory containing txt files of pins. (Defaults to current working directory)',
'sort' => 'Sort the imported file first to improve importing performance. (Defaults to 1 or TRUE)',
'filter' => 'Filter out a sub string from codes.',
'test' => 'Set pins to be marked as test pins. (Defaults to 0 or FALSE)',
'quantity' => 'The quantity of each pin being imported. (Defaults to 1)',
'status' => 'The status of the pin. (Defaults to 1)',
),
);
$items['pinapi-collection-info'] = array(
'description' => 'Shows collection information.',
'arguments' => array(
'cid' => 'Collection ID.',
),
'required-arguments' => TRUE,
);
$items['pinapi-collection-reset'] = array(
'description' => 'Reset collection.',
'arguments' => array(
'cid' => 'Collection ID.',
),
'required-arguments' => TRUE,
);
$items['pinapi-collection-clear'] = array(
'description' => 'Clear collection.',
'arguments' => array(
'cid' => 'Collection ID.',
),
'required-arguments' => TRUE,
);
$items['pinapi-pin-create'] = array(
'description' => 'Create pin.',
'arguments' => array(
'code' => 'Code',
'cid' => 'Collection ID',
),
'required-arguments' => TRUE,
'options' => array(
'quantity' => 'Quantity',
'status' => 'Status',
),
);
$items['pinapi-txn-calculate'] = array(
'description' => 'Calculate transactions.',
'arguments' => array(
'cid' => 'Collection ID.',
),
'required-arguments' => TRUE,
'options' => array(
'name' => 'A statistic name',
'uid' => 'A uid',
),
);
return $items;
}
/**
* Command Callback: Create Collection.
*/
function drush_pinapi_collection_create($machine_name) {
$name = drush_get_option('name');
$status = drush_get_option('status', 1);
$new_collection = (object) array(
'machine_name' => $machine_name,
'name' => $name ? $name : $machine_name,
'status' => $status,
);
$result = db_query("SELECT cid FROM {pinapi_collection} WHERE machine_name = :machine_name", array(':machine_name' => $machine_name));
if ( drush_db_result($result) === FALSE ) {
if ( !drush_get_context('DRUSH_SIMULATE') ) {
$new_collection_object = pinapi_collection_save($new_collection);
if ( $new_collection_object !== FALSE ) {
drush_log(t('Created @name collection.', array('@name' => $machine_name)), 'success');
return $collection->cid;
}
else {
drush_set_error("Could not create a new collection with the name " . $machine_name . "!");
}
}
}
else {
drush_set_error("There is already a collection with the name " . $machine_name);
}
}
/**
* Command Callback: Imports pins to a collection.
*/
function drush_pinapi_collection_import($cid) {
$collection = pinapi_collection_load($cid);
if ( $collection !== FALSE ) {
$input = drush_get_option('input', drush_cwd());
$filter = drush_get_option('filter', NULL);
$sort = drush_get_option('sort', TRUE);
$quantity = drush_get_option('quantity', 1);
$test = drush_get_option('test', 0);
$status = drush_get_option('status', 1);
$files = file_scan_directory($input, '/.*\.txt$/');
if ( !empty($files) ) {
drush_print(dt('Found @count files in @input', array('@count' => count($files), '@input' => $input)));
foreach ( $files as $file ) {
drush_print($file->uri, 2);
}
if ( drush_confirm(dt('Import these files into @name collection?:', array('@name' => $collection->name))) ) {
$delete_after_import = array();
foreach ( $files as $file ) {
// Filter out characters from file.
if ( !is_null($filter) ) {
$file->uri_filtered = str_replace($file->name, $file->name . '-filtered', $file->uri);
drush_shell_exec("cat %s | sed 's/%s/%s/g' > %s", $file->uri, $filter, '', $file->uri_filtered);
// Update new source file location.
$file->name = $file->name . '-filtered';
$file->uri = $file->uri_filtered;
// Add this file, to list of files to be deleted after importing.
$delete_after_import[] = $file->uri_filtered;
}
// Sort file to improve import performance.
if ( $sort ) {
$file->uri_sorted = str_replace($file->name, $file->name . '-sorted', $file->uri);
drush_shell_exec('sort %s -o %s', $file->uri, $file->uri_sorted);
// Update new source file location.
$file->name = $file->name . '-sorted';
$file->uri = $file->uri_sorted;
// Add this file, to list of files to be deleted after importing.
$delete_after_import[] = $file->uri_sorted;
}
$sql = "LOAD DATA INFILE '" . $file->uri . "' ";
$sql .= "INTO TABLE {pinapi_pin} ";
$sql .= "FIELDS TERMINATED BY ',' ";
$sql .= "ENCLOSED BY '' ";
$sql .= "ESCAPED BY '' ";
$sql .= "LINES TERMINATED BY '" . '\n' . "' ";
$sql .= "(code) ";
$sql .= "SET ";
$sql .= "code = TRIM(code), ";
$sql .= "cid = :cid, ";
$sql .= "quantity_original = :quantity, ";
$sql .= "quantity = :quantity, ";
$sql .= "test = :test, ";
$sql .= "status = :status";
$args = array(
':cid' => $cid,
':quantity' => $quantity,
':test' => $test,
':status' => $status,
);
$query = db_query($sql, $args);
// Delete reformatted files.
foreach ( $delete_after_import as $uri ) {
file_unmanaged_delete($uri);
}
drush_log(dt('Successfully imported @count pins from @input', array('@count' => $query->rowCount(), '@input' => $file->uri)), 'success');
}
}
else {
drush_set_error('Aborting.');
}
}
else {
drush_set_error("Could not find .txt files in folder.");
}
}
else {
drush_set_error("Could not find collection!");
}
}
/**
* Command Callback: Show collection information.
*/
function drush_pinapi_collection_info($cid) {
$collection = pinapi_collection_load($cid);
if ( $collection !== FALSE ) {
$collection_info = array(
'Name' => $collection->name,
'Machine Name' => $collection->machine_name,
'Status' => $collection->status ? 'active' : 'inactive',
);
foreach ( $collection->inventory as $key => $value ) {
$collection_info[$key] = $value;
}
drush_print_table(drush_key_value_to_array_table($collection_info));
}
else {
drush_set_error("Could not find collection!");
}
}
/**
* Command Callback: Calculate collection.
*/
function drush_pinapi_collection_calculate($cid) {
$collection = pinapi_collection_load($cid);
if ( $collection !== FALSE ) {
if ( drush_confirm(t("Calculate @name collection?:", array('@name' => $collection->name))) ) {
pinapi_collection_calculate($cid);
}
else {
drush_set_error("Aborting.");
}
}
else {
drush_set_error("Could not find collection!");
}
}
/**
* Command Callback: Reset collection.
*/
function drush_pinapi_collection_reset($cid) {
$collection = pinapi_collection_load($cid);
if ( $collection !== FALSE ) {
if ( drush_confirm(t("Reset @name collection?:", array('@name' => $collection->name))) ) {
pinapi_collection_reset($cid);
}
else {
drush_set_error("Aborting.");
}
}
else {
drush_set_error("Could not find collection!");
}
}
/**
* Command Callback: Reset collection.
*/
function drush_pinapi_collection_clear($cid) {
$collection = pinapi_collection_load($cid);
if ( $collection !== FALSE ) {
if ( drush_confirm(t("Clear @name collection?:", array('@name' => $collection->name))) ) {
pinapi_collection_clear($cid);
}
else {
drush_set_error("Aborting.");
}
}
else {
drush_set_error("Could not find collection!");
}
}
/**
* Command Callback: Create pin.
*/
function drush_pinapi_pin_create($code, $cid) {
$quantity = drush_get_option('quantity', 1);
$status = drush_get_option('status', 1);
$collection = pinapi_collection_load($cid);
if ( $collection !== FALSE ) {
$pin = (object) array(
'code' => $code,
'cid' => $collection->cid,
'quantity' => $quantity,
'status' => $status,
);
pinapi_pin_save($pin);
drush_log(t('Successfully created pin.'), 'success');
}
else {
drush_set_error("Could not find collection!");
}
}
/**
* Command Callback: Calculate transactions.
*/
function drush_pinapi_txn_calculate($cid) {
$name = drush_get_option('name', 'txn_total');
$uid = drush_get_option('uid');
if ( !drush_get_context('DRUSH_SIMULATE') ) {
pinapi_txn_calculate($name, $cid, $uid);
drush_log(t('Calculated transactions.'), 'success');
}
}