-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflag_note.module
754 lines (688 loc) · 23.5 KB
/
flag_note.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
<?php
/**
* @file
*
* Flag note module uses a new flag_link_type 'Flag note form' to allow users
* to attached a note to flags defined to use this link_type.
*/
/**
* Implementation of hook_perm().
*
* We assume that 'create flag note' is handled by the 'create flag' permission
* 'view any note' goes through views permissions.
*/
function flag_note_perm() {
return array(
'delete own flag note',
'delete any flag note',
'edit own flag note',
'edit any flag note',
);
}
/**
* Implementation of hook_preprocess_flag().
*/
function flag_note_preprocess_flag(&$vars) {
if ($vars['flag']->link_type == 'flag_note_form') {
$vars['flag_classes'] .= ' flag-note';
if ($vars['action'] == 'flag' && module_exists('modalframe')) {
drupal_add_js(drupal_get_path('module', 'flag_note') .'/flagnote_modalframe.js');
modalframe_parent_js();
$vars['flag_classes'] .= ' modalframe-flagnote';
}
elseif ($vars['action'] == 'flag' && module_exists('popups')) {
popups_add_popups();
$vars['flag_classes'] .= ' popups-form';
}
}
}
/**
* Implementation of hook_views_api().
*/
function flag_note_views_api() {
return array(
'api' => 2.0,
'path' => drupal_get_path('module', 'flag_note') .'/includes',
);
}
/**
* Implementation of hook_menu().
*/
function flag_note_menu() {
$items = array();
$items['flagnote/action'] = array(
'title' => 'Flag Note',
'page callback' => 'flag_note_page',
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['flagnote/%/delete'] = array(
'title' => 'Flag Note delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('flag_note_delete_confirm', 1),
'access callback' => 'flag_note_has_access',
'access arguments' => array(2, 1),
'type' => MENU_CALLBACK,
);
$items['flagnote/%/edit'] = array(
'title' => 'Flag Note edit',
'page callback' => 'drupal_get_form',
'page arguments' => array('flag_note_edit_form', 1),
'access callback' => 'flag_note_has_access',
'access arguments' => array(2, 1),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Global flags (uid = 0) can only be edited/deleted by users that have the
* 'edit/delete any flag note' right.
*
* @param type 'edit' or 'delete'
*/
function flag_note_has_access($type, $fnid, $uid = NULL) {
if (empty($fnid)) {
return FALSE;
}
if (user_access($type .' any flag note')) {
return TRUE;
}
if (!user_access($type .' own flag note')) {
return FALSE;
}
if (empty($uid)) {
$note = flag_note_get_notes(array('fnid' => $fnid));
if (empty($note)) {
return FALSE;
}
$uid = $note[0]['uid'];
}
return ($uid === $GLOBALS['user']->uid);
}
/**
* Form for confirming deletion of a note.
*/
function flag_note_delete_confirm(&$form_state, $fnid) {
$form['fnid'] = array(
'#type' => 'value',
'#value' => $fnid,
);
return confirm_form($form,
t('Are you sure you want to delete this note ?'),
isset($_GET['destination']) ? $_GET['destination'] : $_REQUEST['q']
);
}
/**
* Submit handler for confirming deletion of a note.
*/
function flag_note_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$flag_note = current(flag_note_get_notes(array('fnid' => $form_state['values']['fnid'])));
$flag = flag_get_flag(NULL, $flag_note['fid']);
if ($flag->flag_note_delete_unflags) {
$flag->flag('unflag', $flag_note['content_id'], user_load($flag_note['uid']));
}
if (flag_note_remove_notes(array('fnid' => $form_state['values']['fnid']))) {
drupal_set_message(t('Your note was successfully deleted.'));
}
else {
drupal_set_message(t('Your note cannot be deleted.'), 'error');
}
}
}
/**
* Form for editing a note.
*/
function flag_note_edit_form(&$form_state, $fnid) {
$flag_note = current(flag_note_get_notes(array('fnid' => $fnid)));
if (empty($flag_note)) {
return FALSE;
}
$flag = flag_get_flag(NULL, $flag_note['fid']);
$form = _flag_note_form($form_state, NULL, $flag->name, NULL, $flag_note);
$form['flag_note']['fnid'] = array('#type' => 'value', '#value' => $flag_note['fnid']);
$form['flag_note']['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
$form['flag_note']['cancel'] = array('#value' => l(t('Cancel'), $_GET['destination']));
return $form;
}
/**
* Submit handler for editing a note.
*/
function flag_note_edit_form_submit(&$form, $form_state) {
$flag_note = current(flag_note_get_notes(array('fnid' => $form_state['values']['fnid'])));
if (empty($flag_note)) {
return FALSE;
}
$record = array_merge($flag_note, array(
'note' => $form_state['values']['note'], 'tid' => $form_state['values']['tid']));
if (flag_note_update_note($record)) {
drupal_set_message(t('Changes were succesfully saved.'));
}
}
/**
* Add options to the flag form.
*/
function flag_note_form_flag_form_alter(&$form, $form_state) {
drupal_add_js(drupal_get_path('module', 'flag_note') .'/flag_note.js');
// Set default options
$link_types = flag_note_flag_link_types();
foreach ($link_types['flag_note_form']['options'] as $option => $value) {
if (!isset($form['#flag']->$option)) {
$form['#flag']->$option = $value;
}
}
$form['display']['link_options_flag_note_form'] = array(
'#type' => 'fieldset',
'#name' => t('Flag note options'),
'#weight' => 100,
'#attributes' => array('id' => 'link-options-flag_note_form'),
);
$form['display']['link_options_flag_note_form']['flag_note_history_disabled'] = array(
'#type' => 'checkbox',
'#default_value' => $form['#flag']->flag_note_history_disabled,
'#title' => t('Disable flag history'),
'#description' => t('If checked, user\'s former notes are deleted when unflagging so '.
'only the last one will be stored.<br />Beware: if checked and in case of a global flag, '.
'notes added by <em>other</em> users get deleted when unflagging.'),
);
$form['display']['link_options_flag_note_form']['flag_note_delete_unflags'] = array(
'#type' => 'checkbox',
'#default_value' => $form['#flag']->flag_note_delete_unflags,
'#title' => t('Unflag item when deleting note'),
'#description' => t('If checked, items are unflagged when deleting flag note.'.
'<br />Beware: if checked and in case of a global flag, '.
'items flagged by <em>other</em> users get unflagged as well.'),
);
$form['display']['link_options_flag_note_form']['flag_note_title'] = array(
'#type' => 'textfield',
'#default_value' => $form['#flag']->flag_note_title,
'#title' => t('Flag note form title'),
);
$form['display']['link_options_flag_note_form']['flag_note_fieldset'] = array(
'#type' => 'textfield',
'#default_value' => $form['#flag']->flag_note_fieldset,
'#title' => t('Flag note fieldset legend'),
);
$form['display']['link_options_flag_note_form']['flag_note_label'] = array(
'#type' => 'textfield',
'#default_value' => $form['#flag']->flag_note_label,
'#title' => t('Flag note textarea label'),
);
$form['display']['link_options_flag_note_form']['flag_note_help'] = array(
'#type' => 'textfield',
'#default_value' => $form['#flag']->flag_note_help,
'#title' => t('Flag note help text (behind textarea)'),
);
if (module_exists('taxonomy')) {
$form['display']['link_options_flag_note_form']['flag_note_vocab'] = array(
'#type' => 'select',
'#title' => t('Taxonomy vocabulary to use for predefined notes'),
'#default_value' => $form['#flag']->flag_note_vocab,
'#options' => _flag_note_get_vocabs(),
'#description' =>
t('Flaggers will be able to select one of the predefined notes (terms) along with the textarea.')
);
$form['display']['link_options_flag_note_form']['flag_note_vocab_label'] = array(
'#type' => 'textfield',
'#default_value' => $form['#flag']->flag_note_vocab_label,
'#title' => t('Label for predefined notes'),
);
$form['display']['link_options_flag_note_form']['flag_note_vocab_help'] = array(
'#type' => 'textfield',
'#title' => t('Help text for predefined notes'),
'#default_value' => $form['#flag']->flag_note_vocab_help,
);
$form['display']['link_options_flag_note_form']['flag_note_hide_text'] = array(
'#type' => 'checkbox',
'#default_value' => $form['#flag']->flag_note_hide_text,
'#title' => t('use only predefined terms and hide text input'),
);
}
$form['display']['link_options_flag_note_form']['flag_note_default_text'] = array(
'#type' => 'textarea',
'#default_value' => $form['#flag']->flag_note_default_text,
'#title' => t('Note default text'),
);
if (module_exists('token')) {
$form['display']['link_options_flag_note_form']['flag_note_token_help'] = array(
'#title' => t('Replacement patterns'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['display']['link_options_flag_note_form']['flag_note_token_help']['help'] = array(
'#value' => theme('token_help', array('global', $form['#flag']->content_type)),
);
}
$form['display']['link_options_flag_note_form']['flag_note_maxlength'] = array(
'#type' => 'textfield',
'#default_value' => $form['#flag']->flag_note_maxlength,
'#title' => t('Flag note max length'),
'#description' => t('0 for unlimited'),
'#length' => 8,
);
$form['#validate'][] = 'flag_note_flag_form_validate';
}
function flag_note_flag_form_validate($form, &$form_state) {
if (empty($form_state['values']['flag_note_vocab'])
and !empty($form_state['values']['flag_note_hide_text'])) {
form_set_error('flag_note_vocab', t('To hide the flag note textarea you must select a vocabulary for predefined text'));
}
}
/**
* Implementation of hook_flag().
*
* Remove notes when content gets unflagged.
*/
function flag_note_flag($action, $flag, $content_id, $account) {
if ($action == 'unflag' and $flag->link_type == 'flag_note_form') {
if ($flag->flag_note_history_disabled) {
$search = array('fid' => $flag->fid, 'content_type' => $flag->content_type, 'content_id' => $content_id);
if (!$flag->global) {
$search['uid'] = $account->uid;
}
flag_note_remove_notes($search);
}
}
}
/**
* Implementation of hook_nodeapi().
*
* Deletes notes once a node is deleted.
*/
function flag_note_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
switch ($op) {
case 'delete':
flag_note_remove_notes(array('content_type' => 'node', 'content_id' => $node->nid));
break;
case 'insert':
case 'update':
// Handles inline flag / note editing in node form
if (is_array($node->flag)) {
_flag_note_node_form_submit($node);
}
break;
}
}
/**
* Implementation of hook_user().
*
* Deletes notes once an user is deleted.
*/
function flag_note_user($op, &$edit, &$account, $category = NULL) {
switch ($op) {
case 'delete':
flag_note_remove_notes(array('content_type' => 'user', 'content_id' => $account->uid));
break;
}
}
/**
* Implementation of hook_comment().
*
* Deletes notes once a comment is deleted.
*/
function flag_note_comment(&$a1, $op) {
switch ($op) {
case 'delete':
flag_note_remove_notes(array('content_type' => 'comment', 'content_id' => $a1->cid));
break;
}
}
/**
* Implementation of hook_form_alter().
*
* Deletes notes once a flag is deleted.
*/
function flag_note_form_flag_delete_confirm_alter(&$form, $form_state) {
$form['#submit'][] = 'flag_note_flag_delete_confirm_submit';
}
/**
* Additional submit handler added to the flag delete confirmation form.
*/
function flag_note_flag_delete_confirm_submit($form, &$form_state) {
flag_note_remove_notes(array('fid' => $form_state['values']['fid']));
}
/**
* Called from the flag's link which is set up in hook_flag_link.
*/
function flag_note_page($action, $flag_name, $content_id) {
$flag = flag_get_flag($flag_name);
switch ($action) {
case 'flag';
return drupal_get_form('flag_note_form', $action, $flag_name, $content_id);
break;
case 'unflag':
default:
return flag_page($action, $flag, $content_id);
break;
}
}
/**
* Helper function shared between flag note edit and create form.
*/
function _flag_note_form(&$form_state, $action, $flag_name, $content_id,
$flag_note = NULL, $nodeform = FALSE) {
$form = array();
$flag = flag_get_flag($flag_name);
if (!$nodeform) {
drupal_set_title($flag->flag_note_title);
}
$form['flag_note'] = array(
'#type' => 'fieldset',
'#tree' => FALSE,
'#title' => filter_xss($flag->flag_note_fieldset),
);
if (empty($flag_note)) {
$form['flag_note']['action'] = array(
'#type' => 'value',
'#value' => 'flag',
);
$form['flag_note']['flag_name'] = array(
'#type' => 'value',
'#value' => $flag_name,
);
$form['flag_note']['content_id'] = array(
'#type' => 'value',
'#value' => $content_id,
);
}
$vid = $flag->flag_note_vocab;
if (module_exists('taxonomy') and $vid) {
$vocabulary = taxonomy_vocabulary_load($vid);
$form['flag_note']['tid'] = array(
'#title' => filter_xss($flag->flag_note_vocab_label),
'#description' => filter_xss($flag->flag_note_vocab_help),
'#required' => $vocabulary->required,
'#weight' => -2,
'#default_value' => $flag_note['tid'],
);
if (module_exists('hs_taxonomy')
and variable_get('taxonomy_hierarchical_select_'. $vid, FALSE)) {
module_load_include('inc', 'hierarchical_select', 'includes/common');
$form['flag_note']['tid'] += array(
'#type' => 'hierarchical_select',
'#size' => 1,
'#config' => array(
'module' => 'hs_taxonomy',
'params' => array('vid' => $vid),
) + hierarchical_select_common_config_get('taxonomy-'. $vid));
}
else {
$form['flag_note']['tid'] += taxonomy_form($vid);
}
}
$default_value = $flag->flag_note_default_text;
if (!empty($flag_note)) {
$default_value = $flag_note['note'];
}
elseif (is_numeric($content_id) and module_exists('token')) {
$default_value = $flag->replace_tokens($default_value,
array_flip($flag->get_labels_token_types()), $content_id);
}
if (!$flag->flag_note_hide_text) {
$form['flag_note']['note'] = array(
'#type' => 'textarea',
'#title' => filter_xss($flag->flag_note_label),
'#cols' => 60,
'#rows' => 5,
'#description' => filter_xss($flag->flag_note_help),
'#weight' => -1,
'#required' => !$nodeform,
'#default_value' => $default_value,
);
$maxlen = $flag->flag_note_maxlength;
if ($maxlen > 0) {
$form['flag_note']['note']['#maxlength'] = $maxlen;
}
}
if (is_numeric($content_id) and !$nodeform) {
$form['flag_note']['submit'] = array(
'#type' => 'submit',
'#value' => $flag->get_label('flag_short', $content_id),
);
}
return $form;
}
/**
* Form for entering a new note.
*/
function flag_note_form(&$form_state, $action, $flag_name, $content_id) {
if (module_exists('modalframe')) {
modalframe_child_js();
}
return _flag_note_form($form_state, $action, $flag_name, $content_id);
}
/**
* Submit handler for adding a new note.
*/
function flag_note_form_submit(&$form, &$form_state) {
$flag = flag_get_flag($form_state['values']['flag_name']);
// Add our info to the flag object to use in tokens when flagging.
if (module_exists('token')) {
$flag->flag_note['text'] = $form_state['values']['note'];
if (module_exists('taxonomy') and $form_state['values']['tid']) {
$term = taxonomy_get_term($form_state['values']['tid']);
if ($term) {
$flag->flag_note['term'] = $term->name;
}
}
}
$result = $flag->flag($form_state['values']['action'], $form_state['values']['content_id']);
if (empty($result)) {
drupal_set_message(t('You are not allowed to flag, or unflag, this content.'));
}
else {
$userflag = flag_get_user_flags($flag->content_type, $form_state['values']['content_id'], $GLOBALS['user']->uid);
if ($userflag) {
$record = $userflag[$flag->name];
// Now uid = 0 if is a global flag. Anyway we want to store the note author
// so we change it.
$record->uid = $GLOBALS['user']->uid;
$record->note = $form_state['values']['note'];
$record->tid = ($form_state['values']['tid'] > 0 ? $form_state['values']['tid'] : NULL);
flag_note_insert_note($record);
drupal_set_message($flag->get_label($form_state['values']['action'] .'_message', $form_state['values']['content_id']));
}
}
if (module_exists('modalframe')) {
$status_messages = array();
foreach (drupal_get_messages($display, FALSE) as $type => $messages) {
$status_messages = array_merge($status_messages, $messages);
}
modalframe_close_dialog(array(
'newLink' => $flag->theme('unflag', $form_state['values']['content_id'], TRUE),
'status' => (count($status_messages) > 1 ? '- ' : '') . implode("\n- ", $status_messages),
));
}
}
/**
* Implementation of hook_flag_link_types().
*
* Defines a link type that the admin can select when editting a flag.
*/
function flag_note_flag_link_types() {
return array('flag_note_form' => array(
'title' => t('Flag note form'),
'description' => t('The user will be able to relate a note to the flagging operation.'),
'options' => array(
'flag_note_title' => t('Flag note'),
'flag_note_fieldset' => t('Flag note'),
'flag_note_label' => t('Flag note'),
'flag_note_help' => t('A descriptive reason.'),
'flag_note_history_disabled' => TRUE,
'flag_note_delete_unflags' => TRUE,
'flag_note_default_text' => '',
'flag_note_maxlength' => 0,
'flag_note_vocab' => FALSE,
'flag_note_vocab_label' => t('Reason'),
'flag_note_vocab_help' => t('Please select a reason.'),
'flag_note_hide_text' => NULL,
)));
}
/**
* Implementation of hook_flag_link().
*
* Build the callback link for flags.
*/
function flag_note_flag_link($flag, $action, $content_id) {
global $user;
$token = flag_get_token($content_id);
$href = "flagnote/action/$action/$flag->name/$content_id";
if (module_exists('flag_abuse') and user_access('reset abuse flags', $user)
and in_array($flag->name, array('abuse_node', 'abuse_comment', 'abuse_user')))
{
$href = "flag/$action/$flag->name/$content_id";
}
return array(
'href' => $href,
'query' => drupal_get_destination() .'&token='. $token,
);
}
/**
* @defgroup Database interaction.
* @{
*/
function flag_note_get_schema() {
static $schema = NULL;
if (empty($schema)) {
$schema = drupal_get_schema('flag_note');
}
return $schema;
}
function flag_note_insert_note($record) {
$record = (array) $record;
$record['note'] = filter_xss($record['note']);
return drupal_write_record('flag_note', $record);
}
function flag_note_update_note($options) {
$options['note'] = filter_xss($options['note']);
return drupal_write_record('flag_note', $options, 'fnid');
}
function flag_note_remove_notes($options) {
$schema = flag_note_get_schema();
foreach ($options as $field => $value) {
$where[] = $field .' = '. db_type_placeholder($schema['fields'][$field]['type']);
}
return db_query('DELETE FROM {flag_note} WHERE '. implode(' AND ', $where), $options);
}
function flag_note_get_notes($options) {
$notes = array();
$schema = flag_note_get_schema();
foreach ($options as $field => $value) {
$where[] = $field .' = '. db_type_placeholder($schema['fields'][$field]['type']);
}
$result = db_query('SELECT * FROM {flag_note} WHERE '. implode(' AND ', $where), $options);
while ($note = db_fetch_array($result)) {
$notes[] = $note;
}
return $notes;
}
/**
* @}
*/
/**
* Implementation of hook_token_list().
*/
function flag_note_token_list($type = 'all') {
$tokens = array();
if ($type == 'flag') {
$tokens['flag note']['flag-note-text'] = t('The note associated with the flagging action.');
$tokens['flag note']['flag-note-term'] = t('The term associated with the flagging action.');
}
return $tokens;
}
/**
* Implementation of hook_token_values().
*/
function flag_note_token_values($type, $object = NULL, $options = array()) {
$values = array();
if ($type == 'flag' and !empty($object->flag_note)) {
$values['flag-note'] = // Keep this for backward compatibility
$values['flag-note-text'] = check_plain($object->flag_note['text']);
$values['flag-note-term'] = check_plain($object->flag_note['term']);
}
return $values;
}
/**
* Get list of vocabs.
*/
function _flag_note_get_vocabs() {
$vocabs = taxonomy_get_vocabularies();
$options[0] = t("- none -");
foreach ($vocabs as $vid => $vocab) {
$options[$vid] = t($vocab->name);
}
return $options;
}
/**
* Implementation of hook_form_alter().
*
* Merge flag note fields into node edit form
*/
function flag_note_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) and isset($form['#node']) and is_array($form['flag'])
and ($form_id == $form['type']['#value'] .'_node_form')) {
$weight = 0;
foreach ($form['flag'] as $name => $value) {
if ($form['flag'][$name]['#type'] == 'checkbox') {
$flag = flag_get_flag($name);
if ($flag->link_type != 'flag_note_form') {
continue ;
}
$form['flag'][$name]['#weight'] = $weight++;
// Gets current (last) flag note if exists
if ($form['#node']->nid) {
$userflag = (array) flag_get_user_flags($flag->content_type, $form['#node']->nid);
if ($userflag[$name]) {
$record = (array) $userflag[$name];
// If it's a global flag avoid searching by uid
if (empty($userflag[$name]->uid)) {
unset ($record['uid']);
}
$note = array_pop(flag_note_get_notes($record));
}
}
$form['flag']['flag_note_'. $name] = current(
_flag_note_form($form_state, 'flag', $name, $form['#node']->nid, $note, TRUE));
$form['flag']['flag_note_'. $name]['#weight'] = $weight++;
$form['flag']['flag_note_'. $name]['#tree'] = TRUE;
}
}
}
}
/**
* Insert new notes or update new ones once the node form is submitted
* (old notes are automatically removed when some content is unflagged).
*/
function _flag_note_node_form_submit($node) {
foreach (array_filter($node->flag, 'is_int') as $name => $state) {
if ($state and isset($node->flag['flag_note_'. $name])) {
$userflag = (array) flag_get_user_flags('node', $node->nid);
if ($userflag[$name]) {
$record = (array) $userflag[$name];
$record['uid'] = $GLOBALS['user']->uid;
$search = $record;
// If it's a global flag just overwrites old note
if (empty($userflag[$name]->uid)) {
unset ($search['uid']);
}
$note = array_pop(flag_note_get_notes($search));
$record['note'] = $node->flag['flag_note_'. $name]['note'];
if (!empty($node->nid) and module_exists('token')) {
$flag = flag_get_flag($name);
$record['note'] = $flag->replace_tokens($record['note'],
array_flip($flag->get_labels_token_types()), $node->nid);
}
$record['tid'] = $node->flag['flag_note_'. $name]['tid'];
if ($note) {
$record['fnid'] = $note['fnid'];
flag_note_update_note($record);
}
else {
flag_note_insert_note($record);
}
}
}
}
}