-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro.module
More file actions
executable file
·2345 lines (2153 loc) · 73.8 KB
/
micro.module
File metadata and controls
executable file
·2345 lines (2153 loc) · 73.8 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
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// $Id$
/**
* @file
* Micro entity that attaches to other entities (or site)
*/
/**
* Modules should return this value from hook_micro_access() to allow access to a micro.
*/
define('MICRO_ACCESS_ALLOW', 'allow');
/**
* Modules should return this value from hook_micro_access() to deny access to a micro.
*/
define('MICRO_ACCESS_DENY', 'deny');
/**
* Modules should return this value from hook_micro_access() to not affect micro access.
*/
define('MICRO_ACCESS_IGNORE', NULL);
/**
* Defining the admin path.
*/
define('MICRO_ADMIN_PATH', 'admin/structure/micro');
//=============
//DRUPAL HOOKS.
//=============
/**
* Implements hook_init().
*/
function micro_init() {
$path = drupal_get_path('module', 'micro');
include_once $path .'/includes/micro.token.inc';
if (module_exists('trigger')) {
include_once $path .'/includes/micro.actions.inc';
}
if (module_exists('og')) {
include_once $path .'/includes/micro.og.inc';
}
}
/**
* Implementation of hook_help().
*/
function micro_help($path, $arg) {
if ($path == 'admin/help#micro') {
return t('Provides a new micro entity that can be attached to other entities.');
}
}
/**
* Implements hook_menu().
*/
function micro_menu() {
$items['admin/content/micro'] = array(
'title' => 'Micro',
'description' => 'List and edit micro items.',
'page callback' => 'micro_admin',
'access arguments' => array('administer micro'),
'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
'file' => 'micro.admin.inc',
);
$items['admin/structure/micro'] = array(
'title' => 'Micro types',
'description' => 'Manage content types, including default status, front page promotion, comment settings, etc.',
'page callback' => 'micro_overview_types',
'access arguments' => array('administer content types'),
'file' => 'micro.admin.inc',
);
$items['admin/structure/micro/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/structure/micro/add'] = array(
'title' => 'Add micro type',
'page callback' => 'drupal_get_form',
'page arguments' => array('micro_type_form'),
'access arguments' => array('administer micro types'),
'type' => MENU_LOCAL_ACTION,
'file' => 'micro.admin.inc',
);
$items['admin/structure/micro/manage/%micro_type'] = array(
'title' => 'Edit micro type',
'title callback' => 'micro_type_page_title',
'title arguments' => array(4),
'page callback' => 'drupal_get_form',
'page arguments' => array('micro_type_form', 4),
'access arguments' => array('administer micro types'),
'file' => 'micro.admin.inc',
);
$items['admin/structure/micro/manage/%micro_type/edit'] = array(
'title' => 'Edit micro type',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items['admin/structure/micro/manage/%micro_type/delete'] = array(
'title' => 'Delete micro type',
'page arguments' => array('micro_type_delete_confirm', 4),
'access arguments' => array('administer micro types'),
'file' => 'micro.admin.inc',
);
$items['micro'] = array(
'page callback' => 'micro_page_default',
'access arguments' => array('access micro items'),
// Required to make 'micro/add' appear on the top-level of 'navigation' menu.
'menu_name' => '',
'type' => MENU_CALLBACK,
);
$items['micro/add'] = array(
'title' => 'Add micro item',
'page callback' => 'micro_add_page',
'access callback' => '_micro_add_access',
'menu_name' => 'navigation',
'theme callback' => '_micro_custom_theme',
'file' => 'micro.pages.inc',
);
/*
*$items['micro-rss.xml'] = array(
* 'title' => 'RSS feed',
* 'page callback' => 'micro_feed',
* 'access arguments' => array('access content'),
* 'type' => MENU_CALLBACK,
*);
*/
drupal_static_reset('_micro_types_build');
foreach (micro_type_get_types() as $type) {
$type_url_str = str_replace('_', '-', $type->machine_name);
$items['micro/add/' . $type_url_str] = array(
'title' => $type->name,
'title callback' => 'check_plain',
'page callback' => 'micro_add',
'page arguments' => array($type->machine_name, 3),
'access callback' => 'micro_access',
'access arguments' => array('create', $type->machine_name),
'description' => $type->description,
'file' => 'micro.pages.inc',
);
}
$items['micro/%micro'] = array(
'title callback' => 'micro_page_title',
'title arguments' => array(1),
// The page callback also invokes drupal_set_title() in case
// the menu router's title is overridden by a menu link.
'page callback' => 'micro_page_view',
'page arguments' => array(1),
'access callback' => 'micro_access',
'access arguments' => array('view', 1),
);
$items['micro/%micro/view'] = array(
'title' => 'View',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['micro/%micro/edit'] = array(
'title' => 'Edit',
'page callback' => 'micro_page_edit',
'page arguments' => array(1),
'access callback' => 'micro_access',
'access arguments' => array('update', 1),
'theme callback' => '_micro_custom_theme',
'weight' => 0,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
'file' => 'micro.pages.inc',
);
$items['micro/%micro/delete'] = array(
'title' => 'Delete',
'page callback' => 'drupal_get_form',
'page arguments' => array('micro_delete_confirm', 1),
'access callback' => 'micro_access',
'access arguments' => array('delete', 1),
'theme callback' => '_micro_custom_theme',
'weight' => 1,
'type' => MENU_LOCAL_TASK,
'context' => MENU_CONTEXT_INLINE,
'file' => 'micro.pages.inc',
);
$items['micro/ajax/add'] = array(
'page callback' => 'micro_ajax_input',
'type' => MENU_CALLBACK,
);
$items['micro/ajax/edit/%/%micro'] = array(
'page callback' => 'micro_ajax_edit',
'page arguments' => array(3, 4),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['micro/ajax/add/%/%micro_type/%/%'] = array(
'page callback' => 'micro_ajax_input',
'page arguments' => array(3, 4, 5, 6),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['micro/ajax/flag/%/%micro_type/%/%'] = array(
'page callback' => 'micro_ajax_flag',
'page arguments' => array(3, 4, 5, 6, 7),
'access callback' => 'user_access',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
/*
*$items['micro/ajax/add/%/%micro_type/%'] = array(
* 'page callback' => 'micro_ajax_add',
* 'page arguments' => array(3,4,5),
* 'access callback' => 'micro_access',
* 'access arguments' => array('access content'),
* 'type' => MENU_CALLBACK,
*);
*/
/*
*$items['micro/ajax/add/ajax/%micro_type/%'] = array(
* 'page callback' => 'micro_ajax_add',
* 'page arguments' => array(TRUE, 4, 5),
* 'access callback' => 'micro_access',
* 'access arguments' => array('access content'),
* 'type' => MENU_CALLBACK,
*);
*/
return $items;
}
/**
* Implements hook_theme().
*/
function micro_theme() {
return array(
'micro' => array(
'template' => 'micro',
'render element' => 'elements',
),
'micro_admin_type' => array(
'variables' => array('name' => NULL, 'type' => NULL),
),
'micro_admin_overview' => array(
'variables' => array('name' => NULL, 'type' => NULL),
),
'micro_wrapper' => array(
'template' => 'micro-wrapper',
'render element' => 'content',
),
'micro_linked_page_input' => array(
'render element' => 'elements',
),
'micro_ajax_form_input' => array(
'render element' => 'elements',
),
'micro_inline_form_input' => array(
'render element' => 'elements',
),
'micro_ajax_flag' => array(
'render element' => 'elements',
),
'micro_ajax_edit' => array(
'variables' => array('mid' => NULL),
),
);
}
/**
* Implements hook_permission().
*/
function micro_permission() {
$permissions = array(
'bypass micro access' => array(
'title' => t('Bypass micro access'),
'description' => t('Bypass all access permissions for micro items.'),
),
'access micro items' => array(
'title' => t('Access micro items'),
'description' => t('Access micro items.'),
),
'administer micro types' => array(
'title' => t('Administer micro types'),
'description' => t('Create and delete fields for micro types.'),
),
'administer micro items' => array(
'title' => t('Administer micro items'),
'description' => t('Edit and view all micro items.'),
),
);
// Generate standard micro permissions for all applicable micro types.
foreach (micro_permissions_get_configured_types() as $type) {
$permissions += micro_list_permissions($type);
}
return $permissions;
}
/**
* Implements hook_node_view().
*/
function micro_node_view($node, $view_mode) {
_micro_entity_view($node, $node->nid, $view_mode);
}
/**
* Implements hook_comment_view()
*/
function micro_comment_view($comment, $view_mode, $langcode) {
_micro_entity_view($comment, $comment->cid, $view_mode);
}
/**
* Implements hook_user_view()
*/
function micro_user_view($account, $view_mode, $langcode) {
_micro_entity_view($account, $account->uid, $view_mode);
}
/**
* Implements hook_node_delete().
*/
function micro_node_delete($node) {
_micro_entity_delete('node', $node->type, $node->nid);
}
/**
* Implements hook_comment_delete()
*/
function micro_comment_delete($comment) {
_micro_entity_delete('comment', $comment->type, $node->cid);
}
/**
* Implements hook_user_cancel().
*/
function micro_user_cancel(&$edit, $account, $method) {
// Delete all micro items of the user in any case.
foreach (micro_load_by_user($account) as $micro) {
micro_delete($micro);
}
}
/**
* Implements hook_forms().
* All micro forms share the same form handler.
*/
function micro_forms() {
$forms = array();
if ($types = micro_type_get_types()) {
foreach (array_keys($types) as $type) {
$forms[$type . '_micro_form']['callback'] = 'micro_form';
}
}
return $forms;
}
/**
* Implements hook_entity_info()
*/
function micro_entity_info() {
$return = array(
'micro' => array(
'label' => t('Micro'),
'entity class' => 'Micro',
'controller class' => 'MicroController',
'base table' => 'micro',
'uri callback' => 'micro_uri',
'fieldable' => TRUE,
'view modes' => array(
'small' => array(
'label' => t('Small view'),
),
'full' => array(
'label' => t('Page view'),
),
),
'entity keys' => array(
'id' => 'mid',
'bundle' => 'type',
'label' => 'mid',
),
'bundles' => array(),
'bundle keys' => array(
'bundle' => 'machine_name',
),
),
);
$return['micro_type'] = array(
'label' => t('Micro type'),
'entity class' => 'MicroType',
'controller class' => 'EntityAPIController',
'base table' => 'micro_type',
'fieldable' => FALSE,
'bundle of' => 'micro',
'exportable' => TRUE,
'entity keys' => array(
'id' => 'mtid',
'name' => 'machine_name',
'label' => 'name',
),
);
if (module_exists('search')) {
$return['micro']['view modes'] += array(
'search_index' => array(
'label' => t('Search index'),
'custom settings' => FALSE,
),
'search_result' => array(
'label' => t('Search result'),
'custom settings' => FALSE,
),
);
}
return $return;
}
/**
* Implements hook_entity_info_alter().
*/
function micro_entity_info_alter(&$entity_info) {
foreach (micro_type_get_types(TRUE) as $type => $info) {
$entity_info['micro']['bundles'][$type] = array(
'label' => $info->name,
'admin' => array(
'path' => 'admin/structure/micro/manage/%micro_type',
'real path' => 'admin/structure/micro/manage/' . $type,
'bundle argument' => 4,
'access arguments' => array('administer micro'),
),
);
}
}
/**
* Controller class for micro items.
*
* This extends the EntityAPIController class.
*/
class MicroController extends EntityAPIController {
protected function buildQuery($ids, $conditions = array(), $revision_id = FALSE) {
$query = parent::buildQuery($ids, $conditions, $revision_id);
// Add the machine name field from the {micro_type} table.
$query->innerJoin('micro_type', 'mt', 'base.mtid = mt.mtid');
$query->addField('mt', 'machine_name', 'type');
return $query;
}
}
/**
* The class used for micro entities.
*/
class Micro extends EntityDBExtendable {
public function __construct($values = array()) {
parent::__construct($values, 'micro');
}
}
/**
* Use a separate class for micro types so we can specify some defaults
* modules may alter.
*/
class MicroType extends EntityDBExtendable {
public $name;
public function __construct($values = array()) {
parent::__construct($values, 'micro_type');
}
/**
* Returns whether the micro type is locked, thus may not be deleted or renamed.
*
* Micro types provided in code are automatically treated as locked, as well
* as any fixed micro type.
*/
public function isLocked() {
return isset($this->disabled) && (($this->disabled & ENTITY_IN_CODE) || ($this->disabled & ENTITY_FIXED));
}
}
/**
* Implements hook_field_display_ENTITY_TYPE_alter().
*/
function micro_field_display_micro_alter(&$display, $context) {
// Hide field labels in search index.
if ($context['view_mode'] == 'search_index') {
$display['label'] = 'hidden';
}
}
/**
* Gather the rankings from the the hook_ranking implementations.
*
* @param $query
* A query object that has been extended with the Search DB Extender.
*/
function _micro_rankings(SelectQueryExtender $query) {
if ($ranking = module_invoke_all('ranking')) {
$tables = &$query->getTables();
foreach ($ranking as $rank => $values) {
if ($micro_rank = variable_get('micro_rank_' . $rank, 0)) {
// If the table defined in the ranking isn't already joined, then add it.
if (isset($values['join']) && !isset($tables[$values['join']['alias']])) {
$query->addJoin($values['join']['type'], $values['join']['table'], $values['join']['alias'], $values['join']['on']);
}
$arguments = isset($values['arguments']) ? $values['arguments'] : array();
$query->addScore($values['score'], $arguments, $micro_rank);
}
}
}
}
/**
* Implements hook_search_info().
*/
function micro_search_info() {
return array(
'title' => 'Content',
'path' => 'micro',
);
}
/**
* Implements hook_search_access().
*/
function micro_search_access() {
return user_access('access micro items');
}
/**
* Implements hook_search_reset().
*/
function micro_search_reset() {
db_update('search_dataset')
->fields(array('reindex' => REQUEST_TIME))
->condition('type', 'micro')
->execute();
}
/**
* Implements hook_search_status().
*/
function micro_search_status() {
$total = db_query('SELECT COUNT(*) FROM {micro}')->fetchField();
$remaining = db_query("SELECT COUNT(*) FROM {micro} m LEFT JOIN {search_dataset} d ON d.type = 'micro' AND d.sid = m.mid WHERE d.sid IS NULL OR d.reindex <> 0")->fetchField();
return array('remaining' => $remaining, 'total' => $total);
}
/**
* Implements hook_search_admin().
*/
function micro_search_admin() {
// Output form for defining rank factor weights.
$form['content_ranking'] = array(
'#type' => 'fieldset',
'#title' => t('Content ranking'),
);
$form['content_ranking']['#theme'] = 'micro_search_admin';
$form['content_ranking']['info'] = array(
'#value' => '<em>' . t('The following numbers control which properties the content search should favor when ordering the results. Higher numbers mean more influence, zero means the property is ignored. Changing these numbers does not require the search index to be rebuilt. Changes take effect immediately.') . '</em>'
);
// Note: reversed to reflect that higher number = higher ranking.
$options = drupal_map_assoc(range(0, 10));
foreach (module_invoke_all('ranking') as $var => $values) {
$form['content_ranking']['factors']['micro_rank_' . $var] = array(
'#title' => $values['title'],
'#type' => 'select',
'#options' => $options,
'#default_value' => variable_get('micro_rank_' . $var, 0),
);
}
return $form;
}
/**
* Implements hook_search_execute().
*/
function micro_search_execute($keys = NULL, $conditions = NULL) {
// Build matching conditions
$query = db_select('search_index', 'i', array('target' => 'slave'))->extend('SearchQuery')->extend('PagerDefault');
$query->join('micro', 'm', 'm.mid = i.sid');
$query
->addTag('micro_access')
->searchExpression($keys, 'micro');
// Insert special keywords.
$query->setOption('type', 'm.mtid');
/*
*if ($query->setOption('term', 'ti.tid')) {
* $query->join('taxonomy_index', 'ti', 'm.mid = ti.mid');
*}
*/
// Only continue if the first pass query matches.
if (!$query->executeFirstPass()) {
return array();
}
// Add the ranking expressions.
_micro_rankings($query);
// Load results.
$find = $query
->limit(10)
->execute();
$results = array();
foreach ($find as $item) {
// Render the micro.
$micro = micro_load($item->sid);
$build = micro_view($micro, 'search_result');
unset($build['#theme']);
$micro->rendered = drupal_render($build);
// Fetch comments for snippet.
$micro->rendered .= ' ' . module_invoke('comment', 'micro_update_index', $micro);
$extra = module_invoke_all('micro_search_result', $micro);
$uri = entity_uri('micro', $micro);
$results[] = array(
'link' => url($uri['path'], array_merge($uri['options'], array('absolute' => TRUE))),
'type' => check_plain(micro_type_get_name($micro)),
'title' => $micro->title,
'user' => theme('username', array('account' => $micro)),
'date' => $micro->changed,
'micro' => $micro,
'extra' => $extra,
'score' => $item->calculated_score,
'snippet' => search_excerpt($keys, $micro->rendered),
);
}
return $results;
}
/**
* Implements hook_ranking().
*/
function micro_ranking() {
// Create the ranking array and add the basic ranking options.
$ranking = array(
'relevance' => array(
'title' => t('Keyword relevance'),
// Average relevance values hover around 0.15
'score' => 'i.relevance',
),
);
// Add relevance based on creation or changed date.
if ($micro_cron_last = variable_get('micro_cron_last', 0)) {
$ranking['recent'] = array(
'title' => t('Recently posted'),
// Exponential decay with half-life of 6 months, starting at last indexed micro
'score' => 'POW(2.0, (GREATEST(n.created, n.changed) - :micro_cron_last) * 6.43e-8)',
'arguments' => array(':micro_cron_last' => $micro_cron_last),
);
}
return $ranking;
}
/**
* Implements hook_update_index().
*/
function micro_update_index() {
$limit = (int)variable_get('search_cron_limit', 100);
$result = db_query_range("SELECT m.mid FROM {micro} m LEFT JOIN {search_dataset} d ON d.type = 'micro' AND d.sid = m.mid WHERE d.sid IS NULL OR d.reindex <> 0 ORDER BY d.reindex ASC, m.mid ASC", 0, $limit, array(), array('target' => 'slave'));
foreach ($result as $micro) {
_micro_index_micro($micro);
}
}
/**
* Index a single micro.
*
* @param $micro
* The micro to index.
*/
function _micro_index_micro($micro) {
$micro = micro_load($micro->mid);
// Save the created time of the most recent indexed micro, for the search
// results half-life calculation.
variable_set('micro_cron_last', $micro->created);
// Render the micro.
$build = micro_view($micro, 'search_index');
unset($build['#theme']);
$micro->rendered = drupal_render($build);
$text = '<h1>' . check_plain($micro->mid) . '</h1>' . $micro->rendered;
// Fetch extra data normally not visible
$extra = module_invoke_all('micro_update_index', $micro);
foreach ($extra as $t) {
$text .= $t;
}
// Update index
search_index($micro->mid, 'micro', $text);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function micro_form_search_form_alter(&$form, $form_state) {
if (isset($form['module']) && $form['module']['#value'] == 'micro' && user_access('use advanced search')) {
// Keyword boxes:
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced search'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array('class' => array('search-advanced')),
);
$form['advanced']['keywords'] = array(
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
);
$form['advanced']['keywords']['or'] = array(
'#type' => 'textfield',
'#title' => t('Containing any of the words'),
'#size' => 30,
'#maxlength' => 255,
);
$form['advanced']['keywords']['phrase'] = array(
'#type' => 'textfield',
'#title' => t('Containing the phrase'),
'#size' => 30,
'#maxlength' => 255,
);
$form['advanced']['keywords']['negative'] = array(
'#type' => 'textfield',
'#title' => t('Containing none of the words'),
'#size' => 30,
'#maxlength' => 255,
);
// micro types:
$types = array_map('check_plain', micro_type_get_names());
$form['advanced']['type'] = array(
'#type' => 'checkboxes',
'#title' => t('Only of the type(s)'),
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
'#options' => $types,
);
$form['advanced']['submit'] = array(
'#type' => 'submit',
'#value' => t('Advanced search'),
'#prefix' => '<div class="action">',
'#suffix' => '</div>',
'#weight' => 100,
);
// Languages:
$language_options = array();
foreach (language_list('language') as $key => $entity) {
$language_options[$key] = $entity->name;
}
if (count($language_options) > 1) {
$form['advanced']['language'] = array(
'#type' => 'checkboxes',
'#title' => t('Languages'),
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
'#options' => $language_options,
);
}
$form['#validate'][] = 'micro_search_validate';
}
}
/**
* Form API callback for the search form. Registered in micro_form_alter().
*/
function micro_search_validate($form, &$form_state) {
// Initialize using any existing basic search keywords.
$keys = $form_state['values']['processed_keys'];
// Insert extra restrictions into the search keywords string.
if (isset($form_state['values']['type']) && is_array($form_state['values']['type'])) {
// Retrieve selected types - Forms API sets the value of unselected
// checkboxes to 0.
$form_state['values']['type'] = array_filter($form_state['values']['type']);
if (count($form_state['values']['type'])) {
$keys = search_expression_insert($keys, 'type', implode(',', array_keys($form_state['values']['type'])));
}
}
if (isset($form_state['values']['term']) && is_array($form_state['values']['term']) && count($form_state['values']['term'])) {
$keys = search_expression_insert($keys, 'term', implode(',', $form_state['values']['term']));
}
if (isset($form_state['values']['language']) && is_array($form_state['values']['language']) && count($form_state['values']['language'])) {
$keys = search_expression_insert($keys, 'language', implode(',', array_filter($form_state['values']['language'])));
}
if ($form_state['values']['or'] != '') {
if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state['values']['or'], $matches)) {
$keys .= ' ' . implode(' OR ', $matches[1]);
}
}
if ($form_state['values']['negative'] != '') {
if (preg_match_all('/ ("[^"]+"|[^" ]+)/i', ' ' . $form_state['values']['negative'], $matches)) {
$keys .= ' -' . implode(' -', $matches[1]);
}
}
if ($form_state['values']['phrase'] != '') {
$keys .= ' "' . str_replace('"', ' ', $form_state['values']['phrase']) . '"';
}
if (!empty($keys)) {
form_set_value($form['basic']['processed_keys'], trim($keys), $form_state);
}
}
//==========
//MICRO API.
//==========
/**
* Determine whether the current user may perform the given operation on the
* specified micro.
*
* @param $op
* The operation to be performed on the micro. Possible values are:
* - "view"
* - "update"
* - "delete"
* - "create"
* @param $micro
* The micro object on which the operation is to be performed, or micro type
* for "create" operation.
* @param $account
* Optional, a user object representing the user for whom the operation is to
* be performed. Determines access for a user other than the current user.
* @return
* TRUE if the operation may be performed, FALSE otherwise.
*/
function micro_access($op, $micro, $account = NULL) {
global $user;
$rights = &drupal_static(__FUNCTION__, array());
if (!$micro || !in_array($op, array('view', 'update', 'delete', 'create'), TRUE)) {
// If there was no micro to check against, or the $op was not one of the
// supported ones, we return access denied.
return FALSE;
}
// If no user object is supplied, the access check is for the current user.
if (empty($account)) {
$account = $user;
}
// $micro may be either an object or a micro type. Since micro types cannot be
// an integer, use either mid or type as the static cache id.
$cid = is_object($micro) ? $micro->mid : $micro;
// If we've already checked access for this micro, user and op, return from
// cache.
if (isset($rights[$account->uid][$cid][$op])) {
return $rights[$account->uid][$cid][$op];
}
if (user_access('bypass micro access', $account)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
if (!user_access('access micro items', $account)) {
$rights[$account->uid][$cid][$op] = FALSE;
return FALSE;
}
// We grant access to the micro if both of the following conditions are met:
// - No modules say to deny access.
// - At least one module says to grant access.
// If no module specified either allow or deny, we fall back to the defaults.
$access = module_invoke_all('micro_access', $micro, $op, $account);
if (in_array(MICRO_ACCESS_DENY, $access, TRUE)) {
$rights[$account->uid][$cid][$op] = FALSE;
return FALSE;
}
elseif (in_array(MICRO_ACCESS_ALLOW, $access, TRUE)) {
$rights[$account->uid][$cid][$op] = TRUE;
return TRUE;
}
return FALSE;
}
/**
* Create a new micro object.
*
* @param $values
* An associative array with default values for the micro object.
* @return
* A micro object.
*/
function micro_create(array $values) {
return new Micro($values);
}
/**
* Saves a micro to the database.
*
* @param $micro
* The micro object.
*/
function micro_save(Micro $micro) {
if (!isset($micro->type)) {
$micro_type = micro_type_get_type($micro);
$micro->type = $micro_type->machine_name;
}
return $micro->save();
}
/**
* Load a micro object from the database.
*
* @param $mid
* The micro ID.
* @param $vid
* The revision ID.
* @param $reset
* Whether to reset the micro_load_multiple cache.
* @return
* A fully-populated micro object.
*/
function micro_load($mid = NULL, $reset = FALSE) {
$mids = (isset($mid) ? array($mid) : array());
$micro = micro_load_multiple($mids, $reset);
return $micro ? reset($micro) : FALSE;
}
/**
* Load micro entities from the database.
*
* This function should be used whenever you need to load more than one micro
* from the database. Micro items are loaded into memory and will not require
* database access if loaded again during the same page request.
*
* @see entity_load()
*
* @param $mids
* An array of micro IDs.
* @param $conditions
* An array of conditions on the {micro} table in the form 'field' => $value.
* @param $reset
* Whether to reset the internal micro_load cache.
* @return
* An array of micro objects indexed by mid.
*/
function micro_load_multiple($mids = array(), $conditions = array(), $reset = FALSE) {
return entity_load('micro', $mids, $conditions, $reset);
}
/**
* Fetch micro items by account.
*
* @param $account
* The user account to load micro items for, or its uid.
* @param $type
* To load a single micro, pass the type of the micro to load.
* @return
* Either a single micro or an array of micro items keyed by micro type.
*
* @see micro_load_multiple()
*/
function micro_load_by_user($account, $type = NULL) {
// Use a separate query to determine all micro ids per user and cache them.
// That way we can look up micro items by id and benefit from the static cache
// of the entity loader.
$cache = drupal_static(__FUNCTION__, array());
$uid = is_object($account) ? $account->uid : $account;
if (!isset($cache[$uid])) {
if (empty($type)) {
$micro_items = micro_load_multiple(array(), array('uid' => $uid));
// Cache ids for further lookups.
$cache[$uid] = array();
foreach ($micro_items as $mid => $micro) {
$cache[$uid][$micro->mtid] = $mid;
}
return array_combine(array_keys($cache[$uid]), $micro_items);
}
$cache[$uid] = db_select('micro', 'm')
->fields('m', array('mtid', 'mid'))
->condition('uid', $uid)
->execute()
->fetchAllKeyed();
}