This repository has been archived by the owner on Mar 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwaggle.module
900 lines (812 loc) · 25.5 KB
/
waggle.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
<?php
include_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'waggle') . '/waggle.permissions.inc';
/**
* Implements hook_menu().
* @TODO Create a waggle api permission and make all api calls require it
* @TODO Make the paths more RESTful, including returning the correct data when not POSTed
*/
function waggle_menu() {
$items['waggle/api/staff-autocomplete-list'] = array(
'page callback' => 'waggle_staff_autocomplete_list',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/tags-autocomplete-list'] = array(
'page callback' => 'waggle_tags_autocomplete_list',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/story/add-user/%node'] = array(
'page callback' => 'waggle_add_user',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/story/remove-user/%node'] = array(
'page callback' => 'waggle_remove_user',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/story/add-tag/%node'] = array(
'page callback' => 'waggle_add_tag',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/story/remove-tag/%node'] = array(
'page callback' => 'waggle_remove_tag',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/story/status/%node'] = array(
'page callback' => 'waggle_story_status',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/story/due-date/%node'] = array(
'page callback' => 'waggle_story_duedate',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['waggle/api/remote-add'] = array(
'page callback' => 'waggle_remote_add',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['waggle/api/story/note/%node'] = array(
'page callback' => 'waggle_note_add',
'page arguments' => array(4),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_entity_info_alter().
*/
function waggle_entity_info_alter(&$entity_info) {
$entity_info['user']['view modes']['waggle_association'] = array(
'label' => t('Waggle Association'),
'custom settings' => TRUE,
);
}
/**
* Implements hook_theme_registry_alter().
*/
function waggle_theme_registry_alter(&$theme_registry) {
$mod_path = drupal_get_path('module', 'waggle') . '/templates';
$theme_registry_copy = $theme_registry; // munge on a copy
_theme_process_registry($theme_registry_copy, 'phptemplate', 'theme_engine', 'pow', $mod_path);
$theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
// Additional hooks can be added to this array to add other template suggestions.
$hooks = array('user_profile', 'user_picture', 'field__field_tags__story', 'comment', 'comment_wrapper');
foreach ($hooks as $h) {
if (!isset($theme_registry[$h]['theme paths'])) {
$theme_registry[$h]['theme paths'] = array();
}
_waggle_insert_after_first_element($theme_registry[$h]['theme paths'], $mod_path);
//$theme_registry[$h]['template'] = $mod_path . '/' . str_replace('_','-',$h);
}
}
function _waggle_insert_after_first_element(&$a, $element) {
if(is_array($a)) {
$first_element = array_shift($a);
if ($first_element) {
array_unshift($a, $first_element, $element);
}
else {
array_unshift($a, $element);
}
}
}
/**
* Implements hook_block_info().
*/
function waggle_block_info() {
$blocks['waggle_add_story'] = array(
'info' => 'Waggle Add Story',
'weight' => -1,
'status' => 1,
'region' => 'content',
'visibility' => BLOCK_VISIBILITY_LISTED,
'pages' => 'accordion',
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function waggle_block_view($delta = '') {
switch ($delta) {
case 'waggle_add_story' :
return array(
'subject' => NULL,
'content' => waggle_add_story(),
);
break;
}
}
/**
* Generates the add story form for the accordion view.
* Modled off node_add($type).
*/
function waggle_add_story($form = NULL, &$form_state = array()) {
global $user;
if (!$user->uid) {
return '';
}
module_load_include('inc', 'node', 'node.pages');
$type = 'story';
$node = (object) array(
'uid' => $user->uid,
'name' => (isset($user->name) ? $user->name : ''),
'type' => $type,
'language' => LANGUAGE_NONE,
'title' => NULL
);
$node_form = drupal_get_form($type . '_node_form', $node);
$name = $user->name;
$account = user_load($user->uid);
if (!empty($account->field_name)) {
$name = implode(' ', array_filter($account->field_name['und'][0])) . ' (' . $name . ')';
}
$node_form['story_head']= array(
'#type' => 'markup',
'#markup' => '
<div class="story-head clearfix">
<div class="meta submitted">
' . theme('user_picture', array('account' => $user)) . '
<div class="submitted"><div class="author-name">' . $name . '</div></div>
<div class="time-ago">new story</div>
<div class="details waggle-secondary"><a>edit details</a></div>
</div>
</div>',
'#weight' => 1,
);
$node_form['story_extra_info'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => 'story-extra-info',
'style' => 'display:none;',
),
'#weight' => 2,
);
foreach ($node_form as $label => $item) {
if (is_array($item) && preg_match('/field_/', $label)) {
$node_form['story_extra_info'][$label] = $item;
unset($node_form[$label]);
}
}
$node_form['body']['#weight'] = 3;
$node_form['body']['#suffix'] = '<div class="suggestions-wrapper clearfix"><div class="suggestions"></div></div>';
$node_form['story_foot'] = array(
'#type' => 'container',
'#attributes' => array(
'class' => 'story-info-footer',
),
'#weight' => 10,
);
foreach(array('actions') as $key){
$node_form['story_foot'][$key] = $node_form[$key];
unset($node_form[$key]);
}
return $node_form;
}
/**
* Implements hook_form_FORM_ID_alter().
* Form ID: story_node_form
*/
function waggle_form_story_node_form_alter(&$form, &$form_state) {
$form['#validate'][] = 'waggle_story_node_validate';
$form['actions']['submit']['#submit'][] = 'waggle_story_node_submit';
global $user;
$user = user_load($user->uid);
$form['field_role_visibility']['und']['#default_value'] = array_keys($user->roles);
form_load_include($form_state, 'inc', 'node', 'node.pages');
}
/**
* Used to add IP and Hostname values before saving to the database.
*/
function waggle_story_node_validate(&$form, &$form_state) {
if (isset($_SERVER['REMOTE_ADDR'])) {
$value = $form_state['values'];
$value['field_ip_address']['und'][0]['value'] = $_SERVER['REMOTE_ADDR'];
if (gethostbyname($_SERVER['REMOTE_ADDR'])) {
$value['field_hostname']['und'][0]['value'] = gethostbyname($_SERVER['REMOTE_ADDR']);
}
form_set_value($form, $value, $form_state);
}
}
function waggle_story_node_submit(&$form, &$form_state){
$form_state['redirect'] = '';
drupal_set_message('Your ticket was successfully submitted and will be ready within 60 seconds.');
}
/**
* Implements hook_form_FORM_ID_alter().
* Form ID: Comment Form.
*/
function waggle_form_comment_form_alter(&$form, &$form_state, $form_id) {
//dpm($form);
if ($form['#node']->type == 'story') {
global $user;
$picture = theme('user_picture', array('account' => $user));
$form['comment_body']['und'][0]['#title'] = '';
$form['#prefix'] = '<div class="new-comment-wrapper" id="new-comment-wrapper-' . $form['#node']->nid
. '"><div class="attribution">' . $picture . '</div><div class="comment-text"><div class="comment-arrow"></div>';
$form['#suffix'] = '</div></div>';
}
}
/**
* Implements hook_preprocess_user_profile().
*/
function waggle_preprocess_user_profile(&$vars) {
if ($vars['elements']['#view_mode'] == 'waggle_association') {
$vars['theme_hook_suggestions'][] = 'user_profile__waggle_association';
}
}
/***** API CALLS ********/
/**
* Function to add a given user from a given node.
*/
function waggle_add_user($node){
if(isset($_POST['uid']) && $account = user_load($_POST['uid'])) {
$uid = $account->uid;
}
else {
drupal_json_output(FALSE);
druapl_exit();
}
$found = FALSE;
//Make sure the user isn't already attatched to this story
if (!empty($node->field_associated_users)) {
foreach($node->field_associated_users['und'] as $delta => $data){
if($data['uid'] == $uid){
$found = TRUE;
break;
}
}
}
$json = 0;
if (!$found) {
$node->field_associated_users['und'][] = array('uid' => $uid);
node_save($node);
$account = user_load($uid);
$json = drupal_render(user_view($account, 'waggle_association'));
}
drupal_json_output('<div class="field-item">' . $json . '</div>');
}
/**
* Function to remove a given user from a given node.
*/
function waggle_remove_user($node){
if(isset($_POST['uid']) && $account = user_load($_POST['uid'])) {
$uid = $account->uid;
}
else {
drupal_json_output(FALSE);
druapl_exit();
}
$found = FALSE;
//Make sure the user is attatched to this story
foreach($node->field_associated_users['und'] as $delta => $data){
if($data['uid'] == $uid){
$found = TRUE;
unset($node->field_associated_users['und'][$delta]);
$node->field_associated_users['und'] = array_values($node->field_associated_users['und']);
unset($node->vid);
}
}
if ($found) {
node_save($node);
}
drupal_json_output($found);
}
/**
* Function to add a given tag to a given node.
*/
function waggle_add_tag($node){
if (isset($_POST['tid']) && $term = taxonomy_term_load($_POST['tid'])) {
$tid = $term->tid;
}
else if (isset($_POST['term'])) {
$vocab = taxonomy_vocabulary_machine_name_load('tags');
if (!$vocab) {
drupal_set_message(t('Could not find the tags vocabulary! Skipping hashtag processing.'), 'error');
drupal_json_output(FALSE);
drupal_exit();
}
// Clean the user input and make sure it doesn't already exist (case-insensitive)
$input = $_POST['term'];
// Remove prepended hashes
while (substr($input, 0, 1) == '#') {
$input = substr($input, 1);
}
$term = taxonomy_get_term_by_name($input, 'tags');
if (count($term)) {
// taxonomy_get_term_by_name returns an array of term objects.
$term = reset($term);
}
// If the term didn't exist after all, make it!
else {
$term = new stdClass();
$term->name = $_POST['term'];
$term->vid = $vocab->vid;
taxonomy_term_save($term);
$tid = $term->tid;
}
}
else {
drupal_json_output(FALSE);
drupal_exit();
}
$found = FALSE;
//Make sure the tag isn't already attatched to this story
if (!empty($node->field_tags)) {
foreach($node->field_tags['und'] as $delta => $data){
if($data['tid'] == $tid){
$found = TRUE;
break;
}
}
}
$json = 0;
if (!$found) {
$node->field_tags['und'][] = array('tid' => $tid);
node_save($node);
$json = '<li>' . l($term->name, 'taxonomy/term/' . $tid) . '</li>';
}
drupal_json_output($json);
}
/**
* Function to remove a given tag from a given node.
*/
function waggle_remove_tag($node){
if(isset($_POST['tid']) && $term = taxonomy_term_load($_POST['tid'])) {
$tid = $term->tid;
}
else {
drupal_json_output(FALSE);
drupal_exit();
}
$found = FALSE;
//Make sure the tag is attached to this story
foreach($node->field_tags['und'] as $delta => $data){
if($data['tid'] == $tid){
$found = TRUE;
unset($node->field_tags['und'][$delta]);
$node->field_tags['und'] = array_values($node->field_tags['und']);
unset($node->vid);
}
}
if ($found) {
node_save($node);
}
drupal_json_output($found);
}
/**
* Changes the status of a story
*/
function waggle_story_status($node) {
if ($node->type != 'story') {
drupal_json_output(FALSE);
}
else if (isset($_POST['status'])) {
$vocabs = taxonomy_get_vocabularies();
$vocab = array_filter($vocabs, function($v) { return $v->machine_name == 'story_status'; });
$vocab = array_shift($vocab);
$term = taxonomy_term_load($_POST['status']);
if ($term && $term->vid == $vocab->vid) {
$node->revision = TRUE;
dpm($node);
$node->field_status['und'][0] = array('tid' => $_POST['status']);
node_save($node);
if ($node) {
drupal_json_output(1);
}
else {
drupal_json_output(0);
}
}
}
else {
// If no POST status but correct node, return the current status
$status = 1;
if (!empty($node->field_status)) {
$tid = $node->field_status['und'][0]['tid'];
$status = array($tid => taxonomy_term_load($tid)->name);
}
drupal_json_output($status);
}
}
/**
* Changes the due date of a story
*/
function waggle_story_duedate($node) {
if ($node->type != 'story') {
drupal_json_output(FALSE);
}
else if (isset($_POST['duedate'])) {
$node->field_due_date = array(
'und' => array(
0 => array(
'value' => date('Y-m-d', strtotime($_POST['duedate'])),
),
),
);
node_save($node);
}
}
/**
* Autocomplete function for Add Associated User boxes
*/
function waggle_staff_autocomplete_list(){
$query = db_select('users', 'u');
$query->leftJoin('field_data_field_name', 'f', 'u.uid=f.entity_id');
$results = $query->fields('u', array('uid', 'name', 'picture', 'mail'))
->fields('f', array('field_name_first', 'field_name_middle', 'field_name_last'))
->condition('u.uid', array(0, 1), 'NOT IN')
->condition('u.status', 1)
->execute()
->fetchAllAssoc('uid');
$accounts = array();
foreach($results as $uid => $fields){
$name_parts = array();
foreach(array('field_name_first', 'field_name_middle', 'field_name_last') as $part){
if(!empty($fields->$part)){
$name_parts[] = $fields->$part;
}
}
$accounts[$uid] = array(
'name' => implode(' ', $name_parts),
'last_name' => $fields->field_name_last,
'first_name' => implode(' ', array($fields->field_name_first, $fields->field_name_middle)),
'mail' => $fields->mail,
'linkblue' => $fields->name,
'picture' => '<img src="' . ($fields->picture ? file_create_url(file_load($fields->picture)->uri) : drupal_get_path('module', 'waggle') . '/default_user.png') . '"/>',
);
}
drupal_json_output($accounts);
}
/**
* Autocomplete function for Add Associated User boxes
*/
function waggle_tags_autocomplete_list(){
$results = db_select('taxonomy_term_data', 't')
->fields('t', array('tid', 'name'))
->condition('t.vid', 1)
->orderBy('t.name', 'ASC')
->execute()
->fetchAllAssoc('tid');
$terms = array();
foreach($results as $tid => $fields){
$terms['by_tid'][$tid] = array(
'name' => $fields->name,
);
$terms['by_name'][$fields->name] = array(
'tid' => $tid,
);
}
drupal_json_output($terms);
}
/**
* Implement hook_node_presave().
*/
function waggle_node_presave($node) {
if (isset($node->is_new) && $node->is_new == 1) {
waggle_process_added_text($node, $node->body['und'][0]['value']);
}
}
/**
* Implement hook_comment_presave().
*/
function waggle_comment_presave($comment) {
$node = node_load($comment->nid);
waggle_process_added_text($node, $comment->comment_body['und'][0]['value']);
// TODO: This triggers hook_node_presave again, which we probably don't want.
node_save($node);
}
/**
* Looks for @user and #tag and adds the relevant data to the node.
*/
function waggle_process_added_text($node, $text) {
$usernames = array();
$hashtags = array();
if (preg_match_all('/(?<!\w)[@#][a-zA-Z][&\w\/\-]*\b/', $text, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match_index => $match) {
if ($match[0][0] == '@') {
$usernames[] = substr($match[0],1);
} else if ($match[0][0] == '#') {
$hashtags[] = substr($match[0],1);
}
}
}
$uids = array();
foreach($usernames as $username) {
if ($account = user_load_by_name($username)) {
$uids[] = $account->uid;
}
}
// Probably not the most efficient, but gets unique set of uids into the right array format.
$node->field_associated_users['und'] = array_map(function($i) { return array('uid' => $i); },
array_unique(
array_merge(
array_map(function($i) { return $i['uid']; }, isset($node->field_associated_users['und'])?$node->field_associated_users['und']:array()),
$uids
)
)
);
$vocab = taxonomy_vocabulary_machine_name_load('tags');
if (!$vocab) {
drupal_set_message(t('Could not find the tags vocabulary! Skipping hashtag processing.'), 'error');
return;
}
$vid = $vocab->vid;
$tags = array();
foreach($hashtags as $hashtag) {
if ($matched_tag = taxonomy_get_term_by_name($hashtag, 'tags')) {
foreach ($matched_tag as $tid => $tag) {
$tags[] = $tag;
}
} else {
$term = new stdClass();
$term->name = $hashtag;
$term->vid = $vid;
taxonomy_term_save($term);
$tags[] = $term;
}
}
if (!isset($node->field_tags['und']))
$node->field_tags['und'] = array();
foreach ($tags as $tag) {
$already_tagged = FALSE;
foreach ($node->field_tags['und'] as $existing_tag) {
if ($tag->tid == $existing_tag['tid']) {
$already_tagged = TRUE;
continue;
}
}
if (!$already_tagged) {
$node->field_tags['und'][] = array('tid' => $tag->tid);
}
}
}
/**
* Function to create a new story.
*/
function waggle_start_story(){
$get = $_GET;
global $user;
$new_story = (object) array('type' => 'story');
node_object_prepare($new_story);
// Look for @users and #tags in the body
// @TODO Generalize this bit as its own function to avoid code duplication with waggle_add_note
$body_parts = explode(' ', $get['note']);
$add_users = array();
foreach($body_parts as $delta => $part){
switch(substr($part, 0, 1) == '@'){
case '@':
$linkblue = substr($part, 1);
$account = user_load_by_name($linkblue);
if($account && $account->uid){
if(!empty($account->field_name)){
$name_parts = array();
foreach(array('first', 'middle', 'last') as $name_part){
if(!empty($account->field_name['und'][0][$name_part])){
$name_parts[] = $account->field_name['und'][0][$name_part];
}
}
$full_name = implode(' ', $name_parts);
}
else{
$full_name = $account->name;
}
$body_parts[$delta] = '<span class="tagged-user user-' . $account->uid . '">' . $full_name . '</span>';
$add_users[] = $account->uid;
}
break;
case '#':
break;
default:
break;
}
}
foreach($add_users as $add_uid){
$new_story->field_associated_users['und'][] = array('uid' => $add_uid);
}
$new_story->title = 'story-' . time();
$new_story->body['und'][0]['value'] = implode(' ', $body_parts);
$new_story->body['und'][0]['format'] = 'filtered_html';
node_save($new_story);
if($new_story){
if(isset($_GET['s'])){
waggle_load_stories(_waggle_search($_GET['s']));
return;
}
else{
waggle_load_stories_default();
return;
}
}
drupal_json_output(FALSE);
//return 'yes';
}
function waggle_remote_add() {
//watchdog('waggle', print_r($_POST, 1));
$required_post_fields = array('username', 'email', 'description', 'ip_address');
foreach($required_post_fields as $field) {
if (!isset($_POST[$field])) {
drupal_json_output(FALSE);
return;
}
}
$account = user_load_by_name($_POST['username']);
// If there's a problem with the name, try email
if (!$account) {
$account = user_load_by_mail($_POST['email']);
}
if (!$account) {
drupal_json_output(FALSE);
return;
}
$new_story = (object) array('type' => 'story');
node_object_prepare($new_story);
$new_story->uid = $account->uid;
$new_story->title = 'story-' . time();
$new_story->body['und'][0]['value'] = $_POST['description'];
$new_story->body['und'][0]['format'] = 'filtered_html';
$new_story->field_ip_address['und'][0]['value'] = $_POST['ip_address'];
$new_story->field_submitter_name['und'][0]['value'] = $_POST['submitter_name'];
$new_story->field_submitter_email['und'][0]['value'] = $_POST['email'];
// submitted_by deprecated in favor of on_behalf_of, below
if(isset($_POST['submitted_by'])){
$submitted_by = user_load_by_name($_POST['submitted_by']);
if($submitted_by){
$new_story->field_submitted_by['und'][0]['uid'] = $submitted_by->uid;
}
}
if(isset($_POST['on_behalf_of'])) {
$on_behalf_of = user_load_by_name($_POST['on_behalf_of']);
if($on_behalf_of){
$new_story->field_submitted_by['und'][0]['uid'] = $account->uid;
$new_story->uid = $on_behalf_of->uid;
}
}
if(isset($_POST['location'])){
$new_story->field_location['und'][0]['value'] = $_POST['location'];
}
if(isset($_POST['phone'])){
$new_story->field_phone['und'][0]['value'] = $_POST['phone'];
}
if(isset($_POST['tags'])){
if (!isset($new_story->field_tags['und']))
$new_story->field_tags['und'] = array();
$vocab = taxonomy_vocabulary_machine_name_load('tags');
if (!$vocab) {
drupal_set_message(t('Could not find the tags vocabulary! Skipping hashtag processing.'), 'error');
drupal_json_output(FALSE);
drupal_exit();
}
foreach(preg_split('/(;|\n)/',$_POST['tags']) as $tag) {
$tag = trim($tag);
if ($tag == '') {
continue;
}
$term = taxonomy_get_term_by_name($tag, 'tags');
if (count($term)) {
// taxonomy_get_term_by_name returns an array of term objects.
$term = reset($term);
}
// If the term didn't exist after all, make it!
else {
$term = new stdClass();
$term->name = $tag;
$term->vid = $vocab->vid;
taxonomy_term_save($term);
$tid = $term->tid;
}
$new_story->field_tags['und'][] = array('tid' => $term->tid);
}
}
if(isset($_POST['associate'])) {
if (!isset($new_story->field_associated_users['und']))
$new_story->field_associated_users['und'] = array();
foreach(preg_split('/(;|\n)/',$_POST['associate']) as $username) {
$account = user_load_by_name(trim($username));
if ($account) {
$new_story->field_associated_users['und'][] = array('uid' => $account->uid);
}
}
}
if(isset($_POST['role_visibility'])) {
if (!isset($new_story->field_role_visibility['und'])) {
$new_story->field_role_visibility = array('und' => array());
}
$roles = user_roles();
foreach(preg_split('/(;|\n)/',$_POST['role_visibility']) as $role_name) {
if ($rid = array_search(trim($role_name),$roles)) {
$new_story->field_role_visibility['und'][] = array('value' => $rid);
}
}
}
//watchdog('waggle', "Received remote submission with POST data: <pre>\n" . print_r($_POST,true) . '</pre>');
foreach ($_FILES as $uploaded_filename => $uploaded_file) {
if (!isset($new_story->field_attachments['und']))
$new_story->field_attachments = array('und' => array());
$f = file_unmanaged_copy($uploaded_file['tmp_name'], 'private://attachments/' . $uploaded_file['name']);
if ($f) {
$file = new stdClass();
$file->filename = basename($f);
//$file->filepath = $f;
$file->uri = $f;
$file->filemime = file_get_mimetype($filepath);
$file->uid = $new_story->uid;
$file->status = FILE_STATUS_PERMANENT;
$file = file_save($file);
$new_story->field_attachments['und'][] =
array(
'fid' => $file->fid,
'filename' => $file->filename,
'uri' => $file->uri,
'filemime' => $file->filemime,
'filesize' => $file->filesize,
'uid' => $file->uid,
'status' => $file->status,
'display' => 1,
);
}
}
node_save($new_story);
if($new_story){
drupal_json_output(TRUE);
}
else {
drupal_json_output(FALSE);
}
return;
}
function waggle_note_add($node) {
if ($node->type != 'story') {
drupal_json_output(FALSE);
return;
}
else if (isset($_POST['note'])) {
global $user;
// Make sure the user has proper access to the ticket.
if (!node_access('update', $node, $user)) {
drupal_json_output(FALSE);
return;
}
// Create a new comment from the note.
$comment = (object) array(
'nid' => $node->nid,
'cid' => 0,
'pid' => 0,
'uid' => $user->uid,
'mail' => '',
'is_anonymous' => 0,
'homepage' => '',
'status' => COMMENT_PUBLISHED,
'subject' => '',
'language' => LANGUAGE_NONE,
'comment_body' => array(
LANGUAGE_NONE => array(
0 => array(
'value' => check_plain($_POST['note']),
'format' => 'filtered_html',
),
),
),
);
comment_submit($comment);
comment_save($comment);
$comment_build = comment_view($comment, $node);
// Don't display as a preview as this is being submitted.
unset($comment_build['comment_body']['#object']->in_preview);
// Return the rendered comment.
drupal_json_output(drupal_render($comment_build));
}
}