summaryrefslogtreecommitdiffstats
path: root/smart_paging.module
blob: 8fba43bbfc417c1f702682c3753ca8b34adfcacb (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
<?php
// $Id: smart_paging.module,v 1.1.2.10 2011/02/22 15:29:51 arpeggio Exp $

/**
 * @file
 * Provides smart paging capability to Drupal contents.
 *
 * @author Roland Michael dela Peña.
 * @version $Id: smart_paging.module,v 1.1.2.10 2011/02/22 15:29:51 arpeggio Exp $
 */

define('SMART_PAGING_NO_METHOD', -1);
define('SMART_PAGING_PLACEHOLDER_METHOD', 0);
define('SMART_PAGING_CHARACTER_LIMIT_METHOD', 1);
define('SMART_PAGING_WORD_LIMIT_METHOD', 2);
define('SMART_PAGING_MAX_CHAR_LIMIT', 3072);
define('SMART_PAGING_MAX_WORD_LIMIT', 512);

/******************************************************************************
 * Drupal Hooks                                                               *
 ******************************************************************************/
/**
 * Implements hook_menu().
 *
 * Called when Drupal is building menus.  Cache parameter lets module know
 * if Drupal intends to cache menu or not - different results may be
 * returned for either case.
 *
 * @return
 *   An array with the menu path, callback, and parameters.
 */ 
function smart_paging_menu() {
  $items = array();
  $items['admin/config/content/smart_paging'] = array(
    'title'            => 'Smart Paging',
    'description'      => 'Configure Smart Paging default settings',
    'access arguments' => array('administer smart_paging'),
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('smart_paging_admin_settings'),
    'file'             => 'includes/smart_paging.admin.inc',
    'weight'           => 2,
  );
  return $items;
}

/**
 * Implements hook_form_alter()
 */
function smart_paging_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'field_ui_display_overview_form') {
    // Modified part copy from field_ui_display_overview_form() at /modules/field_ui/field_ui.admin.inc
    // Base button element for the various formatter settings actions.
    $base_button = array(
      '#submit' => array('field_ui_display_overview_multistep_submit'),
      '#ajax' => array(
        'callback' => 'field_ui_display_overview_multistep_js',
        'wrapper' => 'field-display-overview-wrapper',
        'effect' => 'fade',
      ),
      '#field_name' => 'smart_paging',
    );
    $summary = isset($form_state['formatter_settings']['smart_paging']) ? $form_state['formatter_settings']['smart_paging'] : NULL;
    if ($form_state['formatter_settings_edit'] == 'smart_paging') {
      $form['fields']['smart_paging']['#region_callback'] = 'field_ui_display_overview_row_region';
      $form['fields']['smart_paging']['format']['#cell_attributes'] = array('colspan' => 3);
      $form['fields']['smart_paging']['format']['settings_edit_form'] = array(
        '#type'       => 'container',
        '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
        '#parents'    => array('fields', 'smart_paging', 'settings_edit_form'),
        'label'       => array(
          '#markup' => t('Format settings:') . ' <span class="formatter-name">' . t('Smart Paging') . '</span>',
        ),
        'settings' => smart_paging_field_formatter_settings_form($form, $summary),
        'actions'  => array(
          '#type'  => 'actions',
          'save_settings' => $base_button + array(
            '#type'  => 'submit',
            '#name'  => 'smart_paging_formatter_settings_update',
            '#value' => t('Update'),
            '#op'    => 'update',
          ),
          'cancel_settings' => $base_button + array(
            '#type'  => 'submit',
            '#name'  => 'smart_paging_formatter_settings_cancel',
            '#value' => t('Cancel'),
            '#op'    => 'cancel',
            // Do not check errors for the 'Cancel' button, but make sure we
            // get the value of the 'formatter type' select.
            '#limit_validation_errors' => array(array('fields', 'smart_paging', 'type')),
          ),
        ),
      );
      $form['fields']['smart_paging']['#attributes']['class'][] = 'field-formatter-settings-editing';
    }
    elseif (isset($form['#entity_type']) && $form['#entity_type'] != 'comment') {
      $form['fields']['smart_paging']['#region_callback'] = 'field_ui_display_overview_row_region';
      $form['fields']['smart_paging']['settings_summary'] = array(
        '#markup' => '<div class="field-formatter-summary">' . smart_paging_field_formatter_settings_summary($form, $summary) . '</div>',
        '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
      );
      $form['fields']['smart_paging']['settings_edit'] = $base_button + array(
        '#type' => 'image_button',
        '#name' => 'smart_paging_formatter_settings_edit',
        '#src' => 'misc/configure.png',
        '#attributes' => array('class' => array('field-formatter-settings-edit'), 'alt' => t('Edit')),
        '#op' => 'edit',
        // Do not check errors for the 'Edit' button, but make sure we get
        // the value of the 'formatter type' select.
        '#limit_validation_errors' => array(array('fields', 'smart_paging', 'type')),
        '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
        '#suffix' => '</div>',
      );
    }
    if (!isset($form_state['formatter_settings']['smart_paging']) || (isset($form_state['formatter_settings']['smart_paging']) && empty($form_state['formatter_settings']['smart_paging']))) {
      $bundle_settings = field_bundle_settings($form['#entity_type'], $form['#bundle']);
      if (isset($bundle_settings['extra_fields']['display']['smart_paging'][$form['#view_mode']]['settings'])) {
        $form_state['formatter_settings']['smart_paging'] = $bundle_settings['extra_fields']['display']['smart_paging'][$form['#view_mode']]['settings'];
      }
      else {
        $display_defaults = smart_paging_field_extra_fields(TRUE);
        $form_state['formatter_settings']['smart_paging'] = $display_defaults['extra_fields']['display']['smart_paging']['settings'];
      }
    }
    $form['#submit'][] = '_smart_paging_display_overview_form_submit';
  }
  if (isset($form_state['field']) && isset($form['#entity_type']) && (strpos($form_id, '_node_form') !== FALSE || $form_id == 'user_profile_form' || $form_id == 'taxonomy_form_term')) {
    // Get smart_paging_settings_context at display settings
    $display_stored   = field_bundle_settings($form['#entity_type'], $form['#bundle']);
    $display_defaults = smart_paging_field_extra_fields(TRUE);
    if (isset($display_stored['extra_fields']['display']['smart_paging']['default'])) {
      $display_settings = $display_stored['extra_fields']['display']['smart_paging']['default']['settings'];
    }
    else {
      $display_settings = $display_defaults['extra_fields']['display']['smart_paging']['settings'];
    }
    if ($display_settings['smart_paging_settings_context'] == 'content') {
      foreach ($form_state['field'] as $field_name => $field) {
        if (isset($form[$field_name]['#language'])) {
          $lang = $form[$field_name]['#language'];
          if (isset($field[$lang]['instance']['settings']['text_processing'])) {
            foreach ($form[$field_name][$lang] as $delta => $properties) {
              if (is_numeric($delta)) {
                global $user;
                $formats = filter_formats($user);
                if (isset($properties['#format'])) {
                  if (is_null($properties['#format'])) {
                    $format = $formats[filter_default_format($user)];
                    $filter = _filter_tips($format->format, FALSE);
                    if (isset($filter[$format->name]['smart_paging_filter'])) {
                      $text_processing = TRUE;
                    }
                    break;
                  }
                  else {
                    $format = $formats[$properties['#format']];
                    $filter = _filter_tips($properties['#format'], FALSE);
                    if (isset($filter[$format->name]['smart_paging_filter'])) {
                      $text_processing = TRUE;
                    }
                    break;
                  }
                }
              }
            }
          }
        }
      }
      if (isset($text_processing)) {
        $entity_type = $form['#entity_type'];
        $entity_info = entity_get_info($entity_type);      
        if (isset($form_state['term']->{$entity_info['entity keys']['id']})) {
          $entity_id = $form_state['term']->{$entity_info['entity keys']['id']};
        }
        elseif (isset($form_state["$entity_type"]->{$entity_info['entity keys']['id']})) {
          $entity_id = $form_state["$entity_type"]->{$entity_info['entity keys']['id']};
        }
        if (isset($entity_id)) {
          // Retrieve the stored Smart Paging settings
          $query = db_select('smart_paging', 'sp')
            ->condition('sp.entity_id', $entity_id)
            ->condition('sp.entity_type', $entity_type)
            ->fields('sp', array('configuration'))
            ->execute();
          $smart_paging_config = unserialize($query->fetchField(0));
          $form_state['storage']['smart_paging_entity_id']   = $entity_id;
          $form_state['storage']['smart_paging_entity_type'] = $entity_type;
        }
        $form['smart_paging_settings'] = array(              
          '#type'        => 'fieldset',
          '#title'       => t('Smart Paging settings'),
          '#description' => t('Remember to enable the Smart Paging input filter at !formats to be able to use this feature at your selected text format of filtered text field(s).', array('!formats' => l(t('Text formats settings page'), 'admin/config/content/formats'))),
          '#attributes'  => array('class' => array('smart-paging-settings')),
          '#collapsible' => TRUE,
          '#collapsed'   => TRUE,
          '#weight'      => 100,
        );
        $method_options = _smart_paging_method_list();
        // Only node entity do have Additional settings aesthetics
        if (isset($form_state['node'])) {
          if (variable_get('smart_paging_method', SMART_PAGING_PLACEHOLDER_METHOD) == SMART_PAGING_PLACEHOLDER_METHOD) {
            $value = ' ' . t('(placeholder="!value")', array('!value' => variable_get('smart_paging_pagebreak', '<!--pagebreak-->')));
            $default_method = $method_options[SMART_PAGING_PLACEHOLDER_METHOD] . $value;
          }
          elseif (variable_get('smart_paging_method', SMART_PAGING_PLACEHOLDER_METHOD) == SMART_PAGING_CHARACTER_LIMIT_METHOD) {
            $value = ' ' . t('(!value characters)', array('!value' => variable_get('smart_paging_character_count', SMART_PAGING_MAX_CHAR_LIMIT)));
            $default_method = $method_options[SMART_PAGING_CHARACTER_LIMIT_METHOD] . $value;
          }
          elseif (variable_get('smart_paging_method', SMART_PAGING_PLACEHOLDER_METHOD) == SMART_PAGING_WORD_LIMIT_METHOD) {
            $value = ' ' . t('(!value words)', array('!value' => variable_get('smart_paging_word_count', SMART_PAGING_MAX_WORD_LIMIT)));
            $default_method = $method_options[SMART_PAGING_WORD_LIMIT_METHOD] . $value;
          }
          else {
            $default_method = $method_options[SMART_PAGING_NO_METHOD];
          }
          if (variable_get('smart_paging_title_display_suffix', TRUE)) {
            $default_method .= t(', Title suffix="') . variable_get('smart_paging_title_suffix', t(': Page ')) . '"';
          }
          else {
            $default_method .= t(', No title suffix');
          }
          drupal_add_js(array('smart_paging' => array('default_method' => $default_method)), 'setting');
          $form['smart_paging_settings'] += array(              
            '#attached'    => array(
              'js' => array(drupal_get_path('module', 'smart_paging') . '/js/smart_paging-admin.js'),
            ),
            '#group'      => 'additional_settings',
          );
        }
        $form['smart_paging_settings']['smart_paging_use_default'] = array(
          '#type'  => 'checkbox', 
          '#title' => t('Use default values'),
          '#default_value' => isset($smart_paging_config['use_default']) ? $smart_paging_config['use_default'] : TRUE,
        );
        $form['smart_paging_settings']['smart_paging_method'] = array(
          '#type'          => 'select', 
          '#title'         => t('Page break method'),
          '#default_value' => isset($smart_paging_config['method']) ? $smart_paging_config['method'] : variable_get('smart_paging_method', SMART_PAGING_PLACEHOLDER_METHOD),
          '#options'       => $method_options,
          '#states'        => array(
            'disabled' => array(
              ':input[name="smart_paging_use_default"]' => array('checked' => TRUE),
            ),
          ),
        );
        $form['smart_paging_settings']['smart_paging_pagebreak'] = array(
          '#type'          => 'textfield',
          '#title'         => t('Page break placeholder'),
          '#default_value' => isset($smart_paging_config['pagebreak']) ? $smart_paging_config['pagebreak'] : variable_get('smart_paging_pagebreak', '<!--pagebreak-->'),
          '#description'   => t('HTML comment or valid HTML tag with unique identifier, eg. &lt;hr class="pagebreak" /&gt;.'),
          '#size'          => 50,
          '#states'        => array(
            'visible' => array(
              ':input[name="smart_paging_method"]' => array(
                'value'   => (string) SMART_PAGING_PLACEHOLDER_METHOD, 
                'enabled' => TRUE,
              ),
            ),
          ),
        );
        $form['smart_paging_settings']['smart_paging_character_count'] = array(
          '#type'          => 'textfield',
          '#title'         => t('Character limit'),
          '#description'   => t('Number of characters that will be shown for each page when "Automatic page break by character limit" is selected.'),
          '#default_value' => isset($smart_paging_config['character_count']) ? $smart_paging_config['character_count'] : variable_get('smart_paging_character_count', SMART_PAGING_MAX_CHAR_LIMIT),
          '#size'          => 50,
          '#states'        => array(
            'visible' => array(
              ':input[name="smart_paging_method"]' => array(
                'value'   => (string) SMART_PAGING_CHARACTER_LIMIT_METHOD,
                'enabled' => TRUE,
              ),
            ),
          ),
        );
        $form['smart_paging_settings']['smart_paging_word_count'] = array(
          '#type'          => 'textfield',
          '#title'         => t('Word limit'),
          '#description'   => t('Number of words that will be shown for each page when "Automatic page break by word limit" is selected.'),
          '#default_value' => isset($smart_paging_config['word_count']) ? $smart_paging_config['word_count'] : variable_get('smart_paging_word_count', SMART_PAGING_MAX_WORD_LIMIT),
          '#size'          => 50,
          '#states'        => array(
            'visible' => array(
              ':input[name="smart_paging_method"]' => array(
                'value'   => (string) SMART_PAGING_WORD_LIMIT_METHOD,
                'enabled' => TRUE,
              ),
            ),
          ),
        );
        $form['smart_paging_settings']['smart_paging_title_display_suffix'] = array(
          '#type'          => 'checkbox',
          '#title'         => t('Display content title suffix'),
          '#description'   => t("Unchecking this option will stop display of content title suffix on web pages"),
          '#default_value' => isset($smart_paging_config['title_display_suffix']) ? $smart_paging_config['title_display_suffix'] : variable_get('smart_paging_title_display_suffix', TRUE),
          '#size'          => 50,
          '#states'        => array(
            'invisible' => array(
              ':input[name="smart_paging_method"]' => array(
                'value'   => (string) SMART_PAGING_NO_METHOD,
              ),
            ),
            'disabled' => array(
              ':input[name="smart_paging_use_default"]' => array('checked' => TRUE),
            ),
          ),
        );
        $form['smart_paging_settings']['smart_paging_title_suffix'] = array(
          '#type'          => 'textfield',
          '#title'         => t('Content title suffix'),
          '#description'   => t("Text that will appear next to content's sub pages title (default is %suffix), eg. Title%suffix 2, Title%suffix 3 and so on...", array('%suffix' => t(': Page '))),
          '#default_value' => isset($smart_paging_config['title_suffix']) ? $smart_paging_config['title_suffix'] : variable_get('smart_paging_title_suffix', t(': Page ')),
          '#size'          => 50,
          '#states'        => array(
            'invisible' => array(
              ':input[name="smart_paging_method"]' => array(
                'value'   => (string) SMART_PAGING_NO_METHOD,
              ),
            ),
            'visible' => array(
              ':input[name="smart_paging_title_display_suffix"]' => array('checked' => TRUE),
            ),
            'disabled' => array(
              ':input[name="smart_paging_use_default"]' => array('checked' => TRUE),
            ),
          ),
        );
      }
      $form['#validate'][] = '_smart_paging_settings_validate';
      $form['#submit'][]   = '_smart_paging_settings_submit';
    }
  }
}

/**
 * Smart Paging settings validation handler.
 */
function _smart_paging_settings_validate($form, &$form_state) {
  if (isset($form_state['values']['smart_paging_pagebreak']) && (strpos($form_state['values']['smart_paging_pagebreak'], '<') === FALSE || strpos($form_state['values']['smart_paging_pagebreak'], '>') != (drupal_strlen($form_state['values']['smart_paging_pagebreak']) - 1))) {
    form_set_error('smart_paging_pagebreak', t('Page break placeholder is not a valid HTML tag or comment.'));
  }
  if (isset($form_state['values']['smart_paging_character_count']) && !is_numeric($form_state['values']['smart_paging_character_count'])) {
    form_set_error('smart_paging_character_count', t('Character limit should be numeric value.'));
  }
  if (isset($form_state['values']['smart_paging_word_count']) && !is_numeric($form_state['values']['smart_paging_word_count'])) {
    form_set_error('smart_paging_word_count', t('Word limit should be numeric value.'));
  }
}

/**
 * Smart Paging settings submit handler.
 */
function _smart_paging_settings_submit($form, &$form_state) {
  if (isset($form_state['values']['smart_paging_use_default']) && !$form_state['values']['smart_paging_use_default'] && isset($form_state['storage']['smart_paging_entity_id'])) {
    $smart_paging_use_default = $form_state['values']['smart_paging_use_default'];
    $entity_type = $form_state['storage']['smart_paging_entity_type'];
    $entity_id   = $form_state['storage']['smart_paging_entity_id'];
    $config = array(
      'use_default'          => $form_state['values']['smart_paging_use_default'],
      'method'               => $form_state['values']['smart_paging_method'],
      'pagebreak'            => $form_state['values']['smart_paging_pagebreak'],
      'character_count'      => $form_state['values']['smart_paging_character_count'],
      'word_count'           => $form_state['values']['smart_paging_word_count'],
      'title_suffix'         => $form_state['values']['smart_paging_title_suffix'],
      'title_display_suffix' => $form_state['values']['smart_paging_title_display_suffix']
    );
    try {
      db_insert('smart_paging')
        ->fields(array(
          'entity_id',
          'entity_type',
          'configuration',
        ))
        ->values(array(
          'entity_id'     => $entity_id,
          'entity_type'   => $entity_type,
          'configuration' => serialize($config),
        ))
        ->execute();
    }
    catch(Exception $error) {
      db_update('smart_paging')
        ->fields(array(
          'configuration' => serialize($config),
        ))
        ->condition('entity_id', $entity_id)
        ->condition('entity_type', $entity_type)
        ->execute();
    }
  }
  elseif (isset($form_state['values']['smart_paging_use_default']) && $form_state['values']['smart_paging_use_default']) {
    db_delete('smart_paging')
      ->condition('entity_id', $form_state['storage']['smart_paging_entity_id'])
      ->condition('entity_type', $form_state['storage']['smart_paging_entity_type'])
      ->execute();
  }
}

/**
 * Implements hook_node_delete().
 */
function smart_paging_node_delete($node) {
  db_delete('smart_paging')
    ->condition('entity_id', $node->nid)
    ->condition('entity_type', 'node')
    ->execute();
}

/**
 * Implements hook_user_delete().
 */
function smart_paging_user_delete($account) {
  db_delete('smart_paging')
    ->condition('entity_id', $account->uid)
    ->condition('entity_type', 'user')
    ->execute();
}

/**
 * Implements hook_taxonomy_term_delete().
 */
function smart_paging_taxonomy_term_delete($term) {
  db_delete('smart_paging')
    ->condition('entity_id', $term->tid)
    ->condition('entity_type', 'taxonomy_term')
    ->execute();
}

/**
 * Implements hook_entity_insert().
 */
function smart_paging_entity_insert($entity, $entity_type) {
  if (isset($entity->smart_paging_use_default)) {
    $entity_info = entity_get_info($entity_type);
    $entity_id = $entity->{$entity_info['entity keys']['id']};
    $config = array(
      'use_default'     => $entity->smart_paging_use_default,
      'method'          => !$entity->smart_paging_use_default ? $entity->smart_paging_method : variable_get('smart_paging_method', SMART_PAGING_PLACEHOLDER_METHOD),
      'pagebreak'       => !$entity->smart_paging_use_default ? $entity->smart_paging_pagebreak : variable_get('smart_paging_pagebreak', '<!--pagebreak-->'),
      'character_count' => !$entity->smart_paging_use_default ? $entity->smart_paging_character_count : variable_get('smart_paging_character_count', SMART_PAGING_MAX_CHAR_LIMIT),
      'word_count'      => !$entity->smart_paging_use_default ? $entity->smart_paging_word_count : variable_get('smart_paging_word_count', SMART_PAGING_MAX_WORD_LIMIT),
      'title_suffix'    => !$entity->smart_paging_use_default ? $entity->smart_paging_title_suffix : variable_get('smart_paging_title_suffix', t(': Page ')),
      'title_display_suffix'    => !$entity->smart_paging_use_default ? $entity->smart_paging_title_display_suffix : variable_get('smart_paging_title_display_suffix', FALSE),
    );
    try {
      db_insert('smart_paging')
        ->fields(array(
          'entity_id',
          'entity_type',
          'configuration',
        ))
        ->values(array(
          'entity_id'     => $entity_id,
          'entity_type'   => $entity_type,
          'configuration' => serialize($config),
        ))
        ->execute();
    }
    catch (Exception $error) {
      db_update('smart_paging')
        ->fields(array(
          'configuration' => serialize($config),
        ))
        ->condition('entity_id', $entity_id)
        ->condition('entity_type', $entity_type)
        ->execute();
    }
  }
}

/**
 * Implements hook_url_inbound_alter().
 */
function smart_paging_url_inbound_alter(&$path, &$original_path, $path_language) {
  if (variable_get('smart_paging_enable_clean_url', TRUE)) {
    $arg_count = count(arg()) - 1;
    $page      = arg($arg_count);
    $sub_page  = arg($arg_count - 1);
    // Check if path has pattern of http://example.com/<path alias>/<pager prefix>/<sub_page>/<page>
    if ($arg_count > 2 && is_numeric($sub_page) && is_numeric($page)) {
      if (arg($arg_count - 2) == variable_get('smart_paging_path_prefix', 'page')) {
        // Set the 'page' value of GET method
        $_GET['page'] = "$sub_page,$page";
        // Extract alias from arg()
        $alias = arg();
        unset($alias[$arg_count - 2], $alias[$arg_count - 1], $alias[$arg_count]);
        // Get the Drupal system URL for a path alias
        if (!($drupal_path = drupal_lookup_path('source', implode('/', $alias) , $path_language)) && function_exists("path_alias_xt_get_normal_path")) {
          $drupal_path = path_alias_xt_get_normal_path(implode('/', $alias) , $path_language);
        }
        if (!empty($drupal_path)) {
          // Alter inbound URL request
          $path = $drupal_path;
        }
        else {
          $path = implode('/', $alias);
        }
      }
    }
  }
}

/**
 * Implements hook_url_outbound_alter().
 *
 * Change pagination query type URL to a clean URL
 */
function smart_paging_url_outbound_alter(&$path, &$options, $original_path) {
  if (isset($options['query']['page']) && variable_get('smart_paging_enable_clean_url', TRUE)) {
    $url_fragment = smart_paging_get_url_fragment();
    $page_query = explode(',', $options['query']['page']);
    if (!(empty($url_fragment)) && is_array($url_fragment)){
      foreach ($url_fragment as $index => $fragment) {
        if ($page_query[1] == $index) {
          $options['fragment'] = $fragment;
        }
      }
    }
    if (!empty($path) && !$options['alias']) {
      $language = isset($options['language']) && isset($options['language']->language) ? $options['language']->language : NULL;
      if (function_exists("path_alias_xt_get_path_alias")) {
        $alias = path_alias_xt_get_path_alias($original_path, $language);
      }
      else {
        $alias = drupal_get_path_alias($original_path, $language);
      } 
      if ($alias != $original_path) {
        $path = $alias;
        $options['alias'] = TRUE;
      }
      else {
        $path_arg = arg();
        $path_arg = array_slice($path_arg, 0, array_search(variable_get('smart_paging_path_prefix', 'page'), $path_arg));
        $path = empty($path_arg) ? $original_path : implode('/', $path_arg);
      }
    }
    $path_prefix = variable_get('smart_paging_path_prefix', 'page');
    if (isset($page_query[1]) && $page_query[1] != 0) {
      $sub_page = $page_query[0];
      $page  = isset($page_query[1]) ? $page_query[1] : '0';
      $path .= "/$path_prefix/$sub_page/$page";
    }
    else {
      $sub_page = $page_query[0];
      if ($sub_page) {
        $path .= "/$path_prefix/$sub_page/0";
      }
    }
    unset($options['query']['page']);
  }
}

/**
 * Implements hook_field_attach_view_alter().
 */
function smart_paging_field_attach_view_alter(&$build, $context) {
  // Teaser view and comment entity type (it has its own pagination) are not included
  if ($context['entity_type'] != 'comment' && $context['view_mode'] == 'full') {
    global $pager_page_array, $pager_total;
    $suffix;
    $display_suffix;
    $total_page   = 0;
    $output_index = 0;
    $current_page = 0;
    // Element index of our pager in multiple pagers on one page.
    $pager_element = 1;
    // Determine the user's current viewing page number
    if (isset($_GET['page'])) {
      $pager_page_array = explode(',', $_GET['page']);
      if (count($pager_page_array) == 1) {
        $pager_page_array = array($pager_page_array[0], 0);
        $current_page     = 0;
      }
      else {
        $current_page = $pager_page_array[$pager_element];
      }
      $output_index = $current_page;
    }
    // Get display settings
    $display_stored   = field_bundle_settings($build['#entity_type'], $build['#bundle']);
    $display_defaults = smart_paging_field_extra_fields(TRUE);
    if (isset($display_stored['extra_fields']['display']['smart_paging'][$context['display']])) {
      $display_settings = $display_stored['extra_fields']['display']['smart_paging'][$context['display']];
    }
    elseif (isset($display_stored['extra_fields']['display']['smart_paging']['default'])) {
      $display_settings = $display_stored['extra_fields']['display']['smart_paging']['default'];
    }
    else {
      $display_settings = $display_defaults['extra_fields']['display']['smart_paging'];
    }
    // Sort array fields according to weight
    $fields = array();
    foreach (element_children($build) as $field_name) {
      if (isset($build[$field_name]['#weight'])) {
        $fields[$field_name] = $build[$field_name]['#weight'];
      }
    }
    asort($fields);
    foreach (array_keys($fields) as $field_name) {
      foreach ($build[$field_name] as $delta => $field_content) {
        if (is_numeric($delta) && isset($field_content['#markup'])) {
          if (strpos($field_content['#markup'], '<!--smart_paging_autop_filter-->') !== FALSE) {
            $use_autop = TRUE;
            $field_content['#markup'] = str_replace('<!--smart_paging_autop_filter-->', '', $field_content['#markup']); 
          }
          $is_smart_paging_filter_found = strpos($field_content['#markup'], '<!--smart_paging_filter-->');
          $is_smart_paging_filter_done_found = strpos($field_content['#markup'], '<!--smart_paging_filter_done-->');
          if ($is_smart_paging_filter_found !== FALSE || $is_smart_paging_filter_done_found !== FALSE) {
            $entity_type = $context['entity_type'];
            $entity_info = entity_get_info($entity_type);            
            $entity_id   = $context['entity']->{$entity_info['entity keys']['id']};
            $language    = isset($context['entity']->language) ? $context['entity']->language : 'und';
            if ($display_settings['settings']['smart_paging_settings_context'] == 'content_type') {
              // Use Smart Paging settings stored at Manage Display
              $smart_paging_method = $display_settings['settings']['smart_paging_method'];
              $placeholder = $display_settings['settings']['smart_paging_pagebreak'];
              $display_suffix = $display_settings['settings']['smart_paging_title_display_suffix'];
              $suffix    = $display_settings['settings']['smart_paging_title_suffix'];
              $max_char  = $display_settings['settings']['smart_paging_character_count'];
              $max_words = $display_settings['settings']['smart_paging_word_count'];
            }
            else {
              // Retrieve the stored Smart Paging settings for this content
              $smart_paging_config = smart_paging_get_content_configuration($entity_type, $entity_id);
              $smart_paging_use_default = isset($smart_paging_config['use_default']) ? $smart_paging_config['use_default'] : TRUE;
              $smart_paging_method = $smart_paging_use_default ? variable_get('smart_paging_method', SMART_PAGING_PLACEHOLDER_METHOD) : $smart_paging_config['method'];
              $placeholder = $smart_paging_use_default ? variable_get('smart_paging_pagebreak', '<!--pagebreak-->') : $smart_paging_config['pagebreak'];
              $display_suffix = $smart_paging_use_default ? variable_get('smart_paging_title_display_suffix', TRUE) : $smart_paging_config['title_display_suffix'];
              $suffix    = $smart_paging_use_default ? variable_get('smart_paging_title_suffix', t(': Page ')) : $smart_paging_config['title_suffix'];
              $max_char  = $smart_paging_use_default ? variable_get('smart_paging_character_count', SMART_PAGING_MAX_CHAR_LIMIT) : $smart_paging_config['character_count'];
              $max_words = $smart_paging_use_default ? variable_get('smart_paging_word_count', SMART_PAGING_MAX_WORD_LIMIT) : $smart_paging_config['word_count'];
            }
            $markup_content = $field_content['#markup'];
          }
          // Check first if Smart Paging is allowed as input filter
          if ($is_smart_paging_filter_found !== FALSE) {
            // Remove the Smart Paging input filter marker
            $markup_content = str_replace('<!--smart_paging_filter-->', '', $markup_content);
            if ($smart_paging_method == SMART_PAGING_NO_METHOD) {
              // Do nothing, no paging is desired
              $cached_field = cache_get("field:$entity_type:$entity_id", 'cache_field');
              $cached_field->data[$field_name][$language][$delta]['safe_value'] = '<!--smart_paging_filter_done-->' . $markup_content;
              cache_set("field:$entity_type:$entity_id", $cached_field->data, 'cache_field');
              return;
            }
            if ($smart_paging_method != SMART_PAGING_PLACEHOLDER_METHOD) {
              // Check if breaking the page is based on number of characters
              if ($smart_paging_method == SMART_PAGING_CHARACTER_LIMIT_METHOD) {
                static $char_count = 0;
              }
              // Check if breaking the page is based on number of words
              elseif ($smart_paging_method == SMART_PAGING_WORD_LIMIT_METHOD) {
                static $word_count = 0;
              }
              // Remove user defined Smart Paging placeholder, we don't need it here
              $markup_content = str_replace($placeholder, '', $markup_content);
              $split_tags = preg_split('/<(!--.*?--|[^>]+?)>/s', $markup_content, -1, PREG_SPLIT_DELIM_CAPTURE);
              $markup_content = '';
              foreach ($split_tags as $split_tags_key => $split_tags_value) {
                if ($split_tags_key & 1) {
                  $markup_content .= '<' . $split_tags_value . '>';
                }
                else {
                  if ($smart_paging_method == SMART_PAGING_CHARACTER_LIMIT_METHOD) {
                    $split_htmlcodes = preg_split('/(&[a-zA-Z]+;)/s', $split_tags_value, -1, PREG_SPLIT_DELIM_CAPTURE);
                    foreach ($split_htmlcodes as $split_htmlcodes_key => $split_htmlcodes_value) {
                      if ($split_htmlcodes_key & 1) {
                        // Count html charater as 1
                        $char_count++;
                        $markup_content .= $split_htmlcodes_value;
                      }
                      else {
                        $split_text = explode(' ', $split_htmlcodes_value);
                        $split_text_size = count($split_text);
                        foreach ($split_text as $text_value) {
                          $char_count += drupal_strlen($text_value);
                          $markup_content .= $text_value;
                          if ($split_text_size > 1) {
                            // Count the space
                            $char_count++;
                            $markup_content .= ' ';
                          }
                          if ($char_count >= $max_char) {
                            $markup_content .= $placeholder;
                            $char_count = 0;
                          }
                        }
                      }
                    }
                  }
                  else {
                    $split_text = explode(' ', $split_tags_value);
                    $split_text_size = count($split_text);
                    foreach ($split_text as $text_value) {
                      // Exclude whitespace
                      if (preg_match('/\S/s', $text_value)) {
                        ++$word_count;
                      }
                      $markup_content .= $text_value;
                      if ($split_text_size > 1) {
                        $markup_content .= ' ';
                      }
                      if ($word_count >= $max_words) {
                        $markup_content .= $placeholder;
                        $word_count = 0;
                      }
                    }
                  }
                }
              }
            }
            // Remove all line break/new line near the closing tag. Eg. Change from <[html tag]/>\n\r<[html tag]> to <[html tag]/><[html tag]>
            preg_match_all('#<[A-Za-z/ ]+>#', $markup_content, $matched_tags);
            foreach ($matched_tags[0] as $matched_tag) {
              $matched_tag_regex = preg_quote($matched_tag, '#');
              $markup_content = preg_replace('#' . $matched_tag_regex . '[\n\r]*<#', "$matched_tag<", $markup_content);
            }
            do {
              // Look for consecutive placeholder and closing tag. Eg. <!--pagebreak--></p>
              $placeholder_regex = preg_quote($placeholder, '#');
              preg_match_all("#($placeholder_regex)([ \t]*)(</[a-zA-Z]+>)#", $markup_content, $match);
              if (isset($match[1][0]) && isset($match[3][0])) {
                // Move the placeholder to the end of closing tag. From the above example </p><!--pagebreak-->
                $match_regex = preg_quote($match[1][0] . (isset($match[2][0]) ? $match[2][0] : '') . $match[3][0], '#');
                $markup_content = preg_replace("#$match_regex#", (isset($match[2][0]) ? $match[2][0] : '') . $match[3][0] . $match[1][0], $markup_content);
              }
            } while (!empty($match[0]) && !empty($match[1]) && !empty($match[2]) && !empty($match[3]));
            // Break HTML content properly and insert placeholder
            $markup_content = smart_page_break_insert_placeholder($markup_content);
            // Check if last page is an empty tag
            $pagebreak = array_filter(explode($placeholder, $markup_content));
            $pagebreak_end = count($pagebreak) - 1;
            $last_page = '';
            if (isset($pagebreak[$pagebreak_end])) {
              $last_page = strip_tags($pagebreak[$pagebreak_end]);
            }
            if (empty($last_page)) {
              // Remove the last page with only an empty tag content
              unset($pagebreak[$pagebreak_end]);
            }
            if (isset($use_autop)) {
              // Use "Convert line breaks into HTML" filter
              foreach ($pagebreak as $index => $content_value) {
                $pagebreak[$index] = _filter_autop($content_value);
              }
            }
           
            $markup_content = implode($placeholder, $pagebreak);
            // Don't save to cache when node is in Preview mode
            if (isset($context['entity']->op) && $context['entity']->op != 'Preview') {
              // Save the processed page break content to cache
              $cached_field = cache_get("field:$entity_type:$entity_id", 'cache_field');
              $cached_field->data[$field_name][$language][$delta]['safe_value'] = '<!--smart_paging_filter_done-->' . $markup_content;
              cache_set("field:$entity_type:$entity_id", $cached_field->data, 'cache_field');
            }
            // Set the field content output
            $build[$field_name][$delta]['#markup'] = $markup_content;
          }
          if ($is_smart_paging_filter_found !== FALSE || $is_smart_paging_filter_done_found !== FALSE) {
            // Remove the Smart Paging input filter done marker
            $markup_content = str_replace('<!--smart_paging_filter_done-->', '', $markup_content);
            if (empty($pagebreak)) {
              // Break the content based on user's included page break placeholders
              $pagebreak = array_filter(explode($placeholder, $markup_content));
            }
            $pagebreak_count = count($pagebreak);
            if ($pagebreak_count > 1) {
              if ($total_page) {
                // At this point, we are in next succeeding fields. This field
                // content will be shown at the last page of the previous field
                $output_index = $current_page - $total_page;
              }
              $output = '';
              if (isset($pagebreak[$output_index])) {
                $output = $pagebreak[$output_index];
              }
              // Record total number of pages
              $total_page += count($pagebreak);
              // Final field content output
              $build[$field_name][$delta]['#markup'] = $output;
            }
          }
        }
        // Smart Paging as better SEO friendly targeting meta tags and URL (sub pages):
        // Sample: <!--smartpagingmeta {"link_rel_canonical_href":"","meta_name_description_content":"Tips for breakfast facts and nutrition information","meta_name_keywords_content":"breakfast, breakfast nutrition, healthy, healthy eating habits"}-->
        // Sample: <!--smartpagingurl "Breakfast-Facts-for-Nutrition"-->
        if (is_array($build[$field_name][$delta]) && isset($build[$field_name][$delta]['#markup']) && isset($placeholder)) {
          $content_break = explode($placeholder, $field_content['#markup']);
          $custom_url_page = array();
          foreach ($content_break as $page_num => $sub_page_content) {
            preg_match_all('#<!--smartpagingurl [[:punct:][:alnum:][:space:]]*-->#', $sub_page_content, $matched_tags);
            if (!empty($matched_tags[0])) {
              $custom_url_page[$page_num] = preg_replace('#<!--smartpagingurl |-->|"|\'#', '', $matched_tags[0][0]);
            }
          }
          smart_paging_get_url_fragment($custom_url_page);
          $sub_content = $build[$field_name][$delta]['#markup'];
          preg_match_all('#<!--smartpagingmeta [[:punct:][:alnum:][:space:]]*-->#', $sub_content, $matched_tags);
          if (!empty($matched_tags[0])) {
            $smart_paging_seo = preg_replace('#<!--smartpagingmeta |<!--smartpagingurl [[:punct:][:alnum:][:space:]]*-->|-->#', '', $matched_tags[0][0]);
            drupal_add_html_head(array(
              '#attributes' => array(
                'name'		=> 'Smart_Paging',
                'content' => $smart_paging_seo,
              ),
              '#tag' => 'meta'
            ), 'smart_paging_seo');
            drupal_get_html_head();
          }
        }
      }
    }
    
    if (!empty($total_page)) {
      global $base_url;
      $current_url = function_exists("path_alias_xt_get_path_alias") ? path_alias_xt_get_path_alias($_GET['q']) : drupal_get_path_alias($_GET['q']);
      $current_url = preg_replace('#(^/)|(/$)#', '', $current_url);
      $clean_url   = variable_get('clean_url', 0);
      $current_url = $clean_url ? $current_url : "?q=$current_url";
      $pager_details = array(
        'first_element' => empty($pager_page_array[0]) ? 0 : $pager_page_array[0],
        'current_page'  => $current_page,
        'base_url'      => $base_url,
        'current_url'   => $current_url,
        'path_prefix'   => variable_get('smart_paging_path_prefix', 'page'),
        'custom_url_page' => $custom_url_page,
      );
      $enable_clean_url = variable_get('smart_paging_enable_clean_url', TRUE);
      if (variable_get('smart_paging_use_js_pager', TRUE) && $enable_clean_url) {
        drupal_add_js(drupal_get_path('module', 'smart_paging') . '/js/smart_paging-pager.js');
        drupal_add_js(array('smart_paging' => $pager_details), 'setting');
      }

      if (variable_get('smart_paging_use_link_rel', TRUE)) {
        // Pagination with rel="next" and rel="prev"
        // Ref: http://googlewebmastercentral.blogspot.co.uk/2011/09/pagination-with-relnext-and-relprev.html
        $total_page_offset = $total_page - 1;
        if ($enable_clean_url) {
          $pager_prefix = $pager_details['path_prefix'] . '/' . $pager_details['first_element'];
        }
        else {
          $pager_prefix = 'page=' . $pager_details['first_element'];
        }
        if ($current_page == 0) {
          // We are on first page
          $link_rel_next_page = "$pager_prefix/1";
        }
        else if ($current_page == $total_page_offset) {
          // We are on last page
          $link_rel_prev_page = $total_page_offset - 1;
          if ($link_rel_prev_page) {
            $link_rel_prev_page = "$pager_prefix/$link_rel_prev_page";
          }
        }
        else {
          // We are on middle page
          $link_rel_next_page = $current_page + 1;
          if ($link_rel_next_page) {
            $link_rel_next_page = "$pager_prefix/$link_rel_next_page";
          }
          $link_rel_prev_page = $current_page - 1;
          if ($link_rel_prev_page) {
            $link_rel_prev_page = "$pager_prefix/$link_rel_prev_page";
          }
        }
        if (isset($link_rel_prev_page)) {
          if ($enable_clean_url) {
            $href = $link_rel_prev_page ? "$base_url/$current_url/$link_rel_prev_page" : "$base_url/$current_url";
          }
          else {
            $href = $link_rel_prev_page ? "$base_url/$current_url?$link_rel_prev_page" : "$base_url/$current_url";
          }
          drupal_add_html_head(array(
            '#attributes' => array(
              'rel'  => 'prev',
              'href' => $href,
            ),
            '#tag' => 'link'
          ), 'smart_paging_link_rel_prev');
        }
        if (isset($link_rel_next_page)) {
          if ($enable_clean_url) {
            $href = $link_rel_next_page ? "$base_url/$current_url/$link_rel_next_page" : "$base_url/$current_url";
          }
          else {
            $href = $link_rel_next_page ? "$base_url/$current_url?$link_rel_next_page" : "$base_url/$current_url";
          }
          drupal_add_html_head(array(
            '#attributes' => array(
              'rel'  => 'next',
              'href' => $href,
            ),
            '#tag' => 'link'
          ), 'smart_paging_link_rel_next');
        }
      }

      $pager_total[$pager_element] = $total_page;
      if ($display_suffix && $current_page > 0) {
        $title_suffix = theme('smart_paging_page_title_suffix', array('page' => array(
          'suffix'       => $suffix,
          'current_page' => $current_page,
          'total_page'   => $total_page,
        )));
      }
      $variables['tags']     = array();
      $variables['element']  = $pager_element;
      $variables['quantity'] = 10;
      $build['smart_paging']['#markup'] = '<div class="smart-paging-pager">' . theme('pager', $variables) . '</div>';
      $build['smart_paging']['#weight'] = isset($display_settings['weight']) ? $display_settings['weight'] : 0;
      $build['smart_paging']['#access'] = isset($display_settings['visible']) ? $display_settings['visible'] : TRUE;
    }
  }
  // Comment entity
  else {
    foreach (element_children($build) as $field_name) {
      foreach ($build[$field_name] as $delta => $field_content) {
        if (is_numeric($delta)) {
          // Check first if there's a Smart Paging filter placeholder
          if (isset($field_content['#markup']) && strpos($field_content['#markup'], '<!--smart_paging_filter-->') !== FALSE) {
            $markup_content = $field_content['#markup'];
            // Remove the Smart Paging input filter marker and placeholder for comment entity
            $markup_content = str_replace('<!--smart_paging_filter-->', '', $markup_content);            
            $markup_content = str_replace(variable_get('smart_paging_pagebreak', '<!--pagebreak-->'), '', $markup_content);
            $build[$field_name][$delta]['#markup'] = $markup_content;
          }
          if (isset($field_content['#markup']) && strpos($field_content['#markup'], '<!--smart_paging_autop_filter-->') !== FALSE) {
            $markup_content = $build[$field_name][$delta]['#markup'];
            $markup_content = str_replace('<!--smart_paging_autop_filter-->', '', $markup_content); 
            $build[$field_name][$delta]['#markup'] = _filter_autop($markup_content);
          }
        }
      }
    }
  }
}

/**
 * Implements hook_html_head_alter().
 * Smart Paging as better SEO friendly targeting meta tags
 */
function smart_paging_html_head_alter(&$head_elements) {
  if (isset($head_elements['smart_paging_seo'])) {
    $smart_paging_seo = json_decode($head_elements['smart_paging_seo']['#attributes']['content']);
    $tags = array();
    if (!empty($smart_paging_seo)) {
      foreach ($smart_paging_seo as $tag => $tag_content) {
        $target_tag = explode('_', $tag);
        if (isset($target_tag[0]) && isset($target_tag[1]) && isset($target_tag[2]) && isset($target_tag[3])) {
          $tags[$target_tag[2]] = array(
            'attr_name' => $target_tag[1],
            'tag' => $target_tag[0], 
            'content_attr' => $target_tag[3],
            'content' => $tag_content,
          );
        }
      }
    }
    if (!empty($tags)) {
      foreach ($head_elements as $head_element_key => $head_element) {
        foreach ($tags as $target_name => $attr) {
          if ($head_element['#tag'] == $attr['tag'] && (isset($head_element['#attributes'][$attr['attr_name']]) && $head_element['#attributes'][$attr['attr_name']] == $target_name)) {
            if (!empty($attr['content'])) {
              $head_elements[$head_element_key]['#attributes'][$attr['content_attr']] = $attr['content'];
            }
            else {
              unset($head_elements[$head_element_key]);
            }
            unset($tags[$target_name]);
          }
        }
      }
    }
    unset($head_elements['smart_paging_seo']);
    if (!empty($tags)) {
      foreach ($tags as $target_name => $attr) {
        $head_elements["smart_paging_$target_name"] = array(	
          '#type' => 'html_tag',
          '#tag'  => $attr['tag'],
          '#attributes'	=> array(
            $attr['attr_name']    => $target_name,
            $attr['content_attr'] => $attr['content'],			
          ),
        );
        unset($tags[$target_name]);
      }
    }
  }
  if (isset($head_elements['smart_paging_link_rel_prev']) || isset($head_elements['smart_paging_link_rel_next'])) {
    // Remove rel="canonical" link
    foreach ($head_elements as $head_element_key => $head_element) {
      if (isset($head_element['#tag']) && $head_element['#tag'] == 'link' && $head_element['#attributes']['rel'] == 'canonical') {
        unset($head_elements[$head_element_key]);
      }
    }
  }
}

/**
 * Implements hook_permission().
 */
function smart_paging_permission() {
  return array(
    'administer smart_paging' => array(
      'title' => t('Administer Smart Paging'),
    ),
  );
}

/**
 * Implements hook_filter_info().
 */
function smart_paging_filter_info() {
  $filters['smart_paging_filter'] = array(
    'title'            => t('Smart Paging'),
    'description'      => t('Please disable the Drupal native "Convert line breaks into HTML" filter and use Smart Paging line breaks convertion into HTML.'),
    'process callback' => '_filter_smart_paging_process',
    'tips callback'    => '_filter_smart_paging_tips',
  );
  $filters['smart_paging_filter_autop'] = array(
    'title'            => t('Convert line breaks into HTML (i.e. !br_html and !p_html) for Smart Paging compatibility', array('!br_html' => '&lt;br&gt;', '!p_html' => '&lt;p&gt;')),
    'description'      => t('Makes Drupal "Convert line breaks into HTML" filter compatible with Smart Paging. Be sure to disable the native Drupal "Convert line breaks into HTML" filter.'),
    'process callback' => '_filter_smart_paging_autop_process',
    'tips callback'    => '_filter_smart_paging_autop_tips',
  );
  return $filters;
}

/**
 * Filter process callback
 *
 * Add a marker that indicates Smart Paging is included in allowed
 * list of content input filtering.
 */
function _filter_smart_paging_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  return '<!--smart_paging_filter-->' . $text;
}

/**
 * Filter tips callback
 *
 * The returned string will be shown in the content add/edit form.
 */
function _filter_smart_paging_tips($filter, $format, $long = FALSE) {
  if (!$long) {
    return t('Allows breaking the content into pages by manually inserting %pagebreak placeholder or automatic page break by character or word limit, it depends on your settings below. Note: this will work only for CCK fields except for %comment entity CCK fields.', array('%pagebreak' => variable_get('smart_paging_pagebreak', '<!--pagebreak-->'), '%comment' => t('comment')));
  }
}

/**
 * Filter process callback
 *
 * Add a marker that indicates Smart Paging is included in allowed
 * list of content input filtering.
 */
function _filter_smart_paging_autop_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  return '<!--smart_paging_autop_filter-->' . $text;
}

/**
 * Filter tips callback
 *
 * The returned string will be shown in the content add/edit form.
 */
function _filter_smart_paging_autop_tips($filter, $format, $long = FALSE) {
  if (!$long) {
    return t('Lines and paragraphs break automatically.');
  }
}

/**
 * Implements hook_field_extra_fields().
 */
function smart_paging_field_extra_fields($get_defaults = FALSE) {
  $extra = array();
  $defaults = array(
    'display' => array(
      'smart_paging' => array(
        'label'       => t('Smart Paging'),
        'description' => t('Pagination for long contents'),
        'weight'      => 10,
        'settings'    => array(
          'smart_paging_settings_context'     => 'content_type',
          'smart_paging_method'               => variable_get('smart_paging_method', SMART_PAGING_PLACEHOLDER_METHOD),
          'smart_paging_pagebreak'            => variable_get('smart_paging_pagebreak', '<!--pagebreak-->'),
          'smart_paging_character_count'      => variable_get('smart_paging_character_count', SMART_PAGING_MAX_CHAR_LIMIT),
          'smart_paging_word_count'           => variable_get('smart_paging_word_count', SMART_PAGING_MAX_WORD_LIMIT),
          'smart_paging_title_display_suffix' => variable_get('smart_paging_title_display_suffix', TRUE),
          'smart_paging_title_suffix'         => variable_get('smart_paging_title_suffix', ': Page '),
        ),
      ),
    ),
  );
  // Node
  $node_types = node_type_get_types();
  foreach ($node_types as $type) {
    $extra['node'][$type->type] = $defaults;
  }
  // User
  $extra['user']['user'] = $defaults;
  // Taxonomy term
  if (module_exists('taxonomy')) {
    $vocabularies = taxonomy_get_vocabularies();
    foreach ($vocabularies as $vocabulary) {
      $extra['taxonomy_term'][$vocabulary->machine_name] = $defaults;
    }
  }
  
  if (!$get_defaults) {
    return $extra;
  }
  else {
    return array('extra_fields' => $defaults);
  }
}

/**
 * Pseudo hook_field_formatter_settings_form().
 */
function smart_paging_field_formatter_settings_form($build, $summary = NULL) {
  $display_stored   = field_bundle_settings($build['#entity_type'], $build['#bundle']);
  $display_defaults = smart_paging_field_extra_fields(TRUE);    
  if (!empty($summary)) {
    $display['settings'] = $summary;
  }
  elseif (isset($display_stored['extra_fields']['display']['smart_paging'][$build['#view_mode']])) {
    $display = $display_stored['extra_fields']['display']['smart_paging'][$build['#view_mode']];
  }
  else {
    $display = $display_defaults['extra_fields']['display']['smart_paging'];
  }
  $form['smart_paging_settings_context'] = array(
    '#type'          => 'radios',
    '#default_value' => $display['settings']['smart_paging_settings_context'],
    '#options'       => array(
      'content'      => t('Show Smart Paging settings during content editing of this content type'),
      'content_type' => t('Use the settings below for every content of this content type'),
    ),
  );
  $states = array(
    'invisible' => array(
      ':input[name="fields[smart_paging][settings_edit_form][settings][smart_paging_settings_context]"]' => array('value' => 'content'),
    ),
  );
  $form['smart_paging_method'] = array(
    '#type'          => 'select', 
    '#title'         => t('Page break method'),
    '#default_value' => $display['settings']['smart_paging_method'],
    '#options'       => _smart_paging_method_list(),
    '#states'        => $states,
  );
  $form['smart_paging_pagebreak'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Page break placeholder'),
    '#default_value' => $display['settings']['smart_paging_pagebreak'],
    '#description'   => t('HTML comment or valid HTML tag with unique identifier, eg. &lt;hr class="pagebreak" /&gt; or &lt;!--pagebreak--&gt.'),
    '#size'          => 30,
    '#states'        => $states,
  );
  $form['smart_paging_character_count'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Character limit'),
    '#description'   => t('Number of characters that will be shown for each page when "Automatic page break by character limit" is selected.'),
    '#default_value' => $display['settings']['smart_paging_character_count'],
    '#size'          => 30,
    '#states'        => $states,
  );
  $form['smart_paging_word_count'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Word limit'),
    '#description'   => t('Number of words that will be shown for each page when "Automatic page break by word limit" is selected.'),
    '#default_value' => $display['settings']['smart_paging_word_count'],
    '#size'          => 30,
    '#states'        => $states,
  );
  $form['smart_paging_title_display_suffix'] = array(
    '#type'          => 'checkbox',
    '#title'         => t('Display content title suffix'),
    '#description'   => t("Unchecking this option will stop display of content title suffix on web pages"),
    '#default_value' => $display['settings']['smart_paging_title_display_suffix'],
    '#states'        => $states,
  );
  $states_title_suffix = $states;
  $states_title_suffix['visible'] = array(
    ':input[name="fields[smart_paging][settings_edit_form][settings][smart_paging_title_display_suffix]"]' => array('checked' => TRUE),
  );
  $form['smart_paging_title_suffix'] = array(
    '#type'          => 'textfield',
    '#title'         => t('Content title suffix'),
    '#description'   => t("Text that will appear next to content's sub pages title (default is %suffix), eg. Title%suffix 2, Title%suffix 3 and so on...", array('%suffix' => t(': Page '))),
    '#default_value' => $display['settings']['smart_paging_title_suffix'],
    '#size'          => 30,
    '#states'        => $states_title_suffix,
  );
  return $form;
}

/**
 * Smart Paging field_ui_display_overview_form submit handler.
 */
function _smart_paging_display_overview_form_submit($form, &$form_state) {
  // Get current bundle settings.
  $bundle_settings = field_bundle_settings($form['#entity_type'], $form['#bundle']);
  $bundle_settings['extra_fields']['display']['smart_paging'][$form['#view_mode']] = array(
    'settings' => $form_state['formatter_settings']['smart_paging'],
    'weight'   => $form_state['values']['fields']['smart_paging']['weight'],
    'visible'  => $form_state['values']['fields']['smart_paging']['type'] == 'visible',
  );
  foreach ($bundle_settings['extra_fields']['display']['smart_paging'] as $view_mode => $view) {
    $bundle_settings['extra_fields']['display']['smart_paging'][$view_mode]['settings']['smart_paging_settings_context'] = $form_state['formatter_settings']['smart_paging']['smart_paging_settings_context'];
  }
  // Save updated bundle settings.
  field_bundle_settings($form['#entity_type'], $form['#bundle'], $bundle_settings);
}

/**
 * Pseudo hook_field_formatter_settings_summary().
 */
function smart_paging_field_formatter_settings_summary($build, $summary = NULL) {
  $display_stored   = field_bundle_settings($build['#entity_type'], $build['#bundle']);
  $display_defaults = smart_paging_field_extra_fields(TRUE);  
  if (!empty($summary)) {
    $display['settings'] = $summary;
  }
  elseif (isset($display_stored['extra_fields']['display']['smart_paging'][$build['#view_mode']])) {
    $display = $display_stored['extra_fields']['display']['smart_paging'][$build['#view_mode']];
  }
  else {
    $display = $display_defaults['extra_fields']['display']['smart_paging'];
  }
  $summary = array();
  $methods = _smart_paging_method_list();
  if (isset($display['settings']['smart_paging_settings_context']) && $display['settings']['smart_paging_settings_context'] == 'content_type') {
    $summary[] = $methods[$display['settings']['smart_paging_method']];
    $summary[] = t('Page break placeholder') . ': <em>' . check_plain($display['settings']['smart_paging_pagebreak']) . '</em>';
    $summary[] = t('Character limit') . ': <em>' . check_plain($display['settings']['smart_paging_character_count']) . '</em>';
    $summary[] = t('Word limit') . ': <em>' . check_plain($display['settings']['smart_paging_word_count']) . '</em>';
    if ($display['settings']['smart_paging_title_display_suffix']) {
      $summary[] = t('Display title suffix.');
      $summary[] = t('Content title suffix: ') . ': <em>' . check_plain($display['settings']['smart_paging_title_suffix']) . '</em>';
    }
    else {
      $summary[] = t('Do not display title suffix.');
    }
  }
  else {
    $summary[] = t('Using Smart Paging settings shown during content editing');
  }
  return implode('<br />', $summary);
}

/**
 * Implements hook_theme().
 */
function smart_paging_theme($existing, $type, $theme, $path) {
  return array(
    'smart_paging_page_title_suffix' => array(
      'render element' => 'page',
    ),
  );
}

/**
 * Default theme for smart_paging_page_title_suffix.
 */
function theme_smart_paging_page_title_suffix($variable) {
  static $title_suffix;
  static $set_title_done;
  if (isset($variable['page'])) {
    $page = $variable['page'];
    if (isset($page['suffix']) && isset($page['current_page']) && isset($page['total_page'])) {
      $title_suffix = $page['suffix'] . ($page['current_page'] + 1) . t(' of ') . $page['total_page'];
      // $variable['bypass_drupal_set_title'] can be set at:
      // THEME_preprocess_smart_paging_page_title_suffix()
      // MODULE_preprocess_smart_paging_page_title_suffix()
      if (!$set_title_done && !isset($variable['bypass_drupal_set_title'])) {
        // Target head title (<title> tag)
        drupal_set_title(drupal_get_title() . check_plain ($title_suffix), PASS_THROUGH);
        $set_title_done = TRUE;
      }
    }
  }
  return $title_suffix;
}

/**
 * Implements MODULE_preprocess_page()
 *
 * Set the page title for the default theme only
 * Variable $smart_paging_page_title_suffix will be available at page.tpl.php
 */
function smart_paging_preprocess_page(&$variables) {
  global $theme;
  if (variable_get('theme_default', '') == $theme) {
    $variables['title'] = drupal_get_title();
    $variables['smart_paging_page_title_suffix'] = theme('smart_paging_page_title_suffix');
  }
}

/**
 * Implements hook_help().
 */
function smart_paging_help($path, $arg) {
  switch ($path) {
    case 'admin/help#smart_paging':
      return '<p>' . 
      t('Provides splitting up long Drupal content into sub pages by number of characters/words or by 
      a placeholder HTML tag. Smart Paging feature can be applied only to CCK fields (with text format) 
      of node, user and taxonomy term entities (Note: "Description" field of taxonomy term is not a CCK 
      field). URL for sub pages made more SEO friendly and works even for aliased URL path. The user has 
      more control over Smart Paging configuration. An option is provided at "Manage Display" page of an 
      entity type whether to control Smart Paging display behavior during content editing or at "Manage 
      Display" page itself. Weight and visibility of Smart Paging can also be controlled at "Manage 
      Display" page of an entity type.') . '</p><p>' . 
      t('It splits complex HTML markup content more accurately:') . 
      '</p><div class="messages"><pre>
&lt;div class="text-important"&gt;
  &lt;p class="info"&gt;
  Long content paragraph... 
&lt;!--pagebreak--&gt;
  Continuation of long content paragraph.
  &lt;/p&gt;
  &lt;ul class="text-note"&gt;
    &lt;li&gt;List #1&lt;/li&gt;
    &lt;li&gt;Long list #2...
 &lt;!--pagebreak--&gt;
    Continuation of long list #2&lt;/li&gt;
    &lt;li&gt;List #3&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</pre></div>
      <p>into this...</p>
      <p>' . t(' - Page 1 - ') . '<br />
      http://www.example.com/node/1</p><div class="messages"><pre>
&lt;div class="text-important"&gt;
  &lt;p class="info"&gt;
  Long content paragraph... 
  &lt;/p&gt;
&lt;/div&gt;</pre></div>
      <p>' . t(' - Page 2 - ') . '<br />
      http://www.example.com/node/1/page/0/1</p><div class="messages"><pre>
&lt;div class="text-important"&gt;
  &lt;p class="info"&gt;
  Continuation of long content paragraph.
  &lt;/p&gt;
  &lt;ul class="text-note"&gt;
    &lt;li&gt;List #1&lt;/li&gt;
    &lt;li&gt;Long list #2...&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</pre></div> 
      <p>' . t(' - Page 3 - ') . '<br />
      http://www.example.com/node/1/page/0/2</p><div class="messages"><pre>
&lt;div class="text-important"&gt;
  &lt;ul class="text-note"&gt;
    &lt;li&gt;Continuation of long list #2&lt;/li&gt;
    &lt;li&gt;List #3&lt;/li&gt;
  &lt;/ul&gt;
&lt;/div&gt;</pre></div>';
      break;
  }
}

/******************************************************************************
 * Helper functions                                                           *
 ******************************************************************************/

/**
 * Break HTML content and insert placeholder
 *
 * Helper function to properly break HTML content and insert placeholder.
 *
 * @param $text
 *   HTML text content.
 * @param $placeholder
 *   Placeholder string to be inserted between broken page.
 * @return
 *   HTML output of properly break HTML content and insert placeholder.
 */
function smart_page_break_insert_placeholder($text, $placeholder = NULL) {
  if (empty($placeholder)) {
    $placeholder = variable_get('smart_paging_pagebreak', '<!--pagebreak-->');
  }
  // Perform HTML correction
  $text = _filter_htmlcorrector($text);
  $break_map = array();
  $text   = str_replace($placeholder, '<smart_paging_placeholder>', $text) . '<smart_paging_placeholder>';
  $struct = smart_page_pair_tags($text);
  $split  = $struct['split'];
  $tag_pairs_map    = $struct['tag_pairs_map'];
  $break_positions  = array_keys($tag_pairs_map['smart_paging_placeholder']);
  unset($tag_pairs_map['smart_paging_placeholder']);
  foreach ($break_positions as $break_index => $break_end) {
    if ($break_index) {
      $break_start = $break_positions[$break_index - 1];
      $break_map[$break_end] = array_slice($split, $break_start, ($break_end - $break_start), TRUE);
      // Remove the placeholder from the structure 
      unset($break_map[$break_end][$break_start]);
    }
    else {
      $break_start = $break_index;
      $break_map[$break_end] = array_slice($split, $break_start, ($break_end - $break_start), TRUE);
    }
    foreach ($tag_pairs_map as $tag_name => $map) {
      foreach ($map as $open_tag_pos => $close_tag_pos) {
        if (($open_tag_pos < $break_end && $close_tag_pos > $break_end) || ($open_tag_pos < $break_start && $close_tag_pos > $break_start) || ($open_tag_pos > $break_start && $close_tag_pos < $break_end && $close_tag_pos !== NULL) || ($open_tag_pos > $break_start && $open_tag_pos < $break_end && $close_tag_pos === NULL)) {
          if (empty($close_tag_pos)) {
            $break_map[$break_end][$open_tag_pos] = '<' . $split[$open_tag_pos] . '>';
          }
          else {
            $break_map[$break_end][$open_tag_pos]  = '<' . $split[$open_tag_pos] . '>';
            $break_map[$break_end][$close_tag_pos] = '<' . $split[$close_tag_pos] . '>';
          }
        }
      }
    }
    ksort($break_map[$break_end]);
    if (isset($output)) {
      $output .= $placeholder . implode('', $break_map[$break_end]);
    }
    else {
      $output = implode('', $break_map[$break_end]);
    }
  }
  return $output;
}

/**
 * Create page content HTML paired tags structure
 *
 * Helper function to create a structured HTML paired tags mappings.
 *
 * @param $text
 *   HTML text content.
 * @return
 *   An associative array containing:
 *   - split: array of split HTML tag(s) from text.
 *   - tag_pairs_map: Array of structured HTML paired tags mapping:
 *   $array[<tag name>][<open tag position>] = <close tag position>
 */
function smart_page_pair_tags($text) {
  $tag_pairs_map = array();
  // Properly entify angles.
  $text = preg_replace('@<(?=[^a-zA-Z!/]|$)@', '&lt;', $text);
  // Split tags from text.
  $split = preg_split('/<(!--.*?--|[^>]+?)>/s', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
  // Note: PHP ensures the array consists of alternating delimiters and literals
  // and begins and ends with a literal (inserting null as required).
  foreach ($split as $position => $value) {
    // Tags are in array's odd number index
    if ($position & 1) {
      list($tagname) = explode(' ', $value);
      if ($tagname[0] == '/') {
        $tagname = strtolower(substr($tagname, 1));
        end($tag_pairs_map[$tagname]);
        // Its open tag pair is the last item in the array with empty value
        while ($tag_value = current($tag_pairs_map[$tagname])) {
          prev($tag_pairs_map[$tagname]);
        }
        $pair_pos = key($tag_pairs_map[$tagname]);
        // Save position of closing tag to its pair open tag
        $tag_pairs_map[$tagname][$pair_pos] = $position;
      }
      else {
        $tagname = strtolower($tagname);
        // Save position of open tag. For now, closing tag is still unidentified
        $tag_pairs_map[$tagname][$position] = NULL;
      }
    }
  }
  return array(
    'split' => $split,
    'tag_pairs_map' => $tag_pairs_map,
  );
}

/**
 * Helper function for grabbing Smart Paging method list.
 */
function _smart_paging_method_list() {
  $methods = &drupal_static(__FUNCTION__);
  
  if (!isset($methods)) {
    $methods = array(
      SMART_PAGING_NO_METHOD              => t('Bypass Smart Paging input filter'),
      SMART_PAGING_PLACEHOLDER_METHOD     => t('Manual placement of page break placeholder'), 
      SMART_PAGING_CHARACTER_LIMIT_METHOD => t('Automatic page break by character limit'), 
      SMART_PAGING_WORD_LIMIT_METHOD      => t('Automatic page break by word limit'),
    );
  }
  return $methods;
}

/**
 * Helper function for grabbing Smart Paging content configuration.
 */
function smart_paging_get_content_configuration($entity_type, $entity_id) {
  $config = &drupal_static(__FUNCTION__);
  if (!isset($config)) {
    if (!empty($entity_type) && !empty($entity_id)) {
      // Retrieve the stored Smart Paging settings
      $query = db_select('smart_paging', 'sp')
        ->condition('sp.entity_id', $entity_id)
        ->condition('sp.entity_type', $entity_type)
        ->fields('sp', array('configuration'))
        ->execute();
      $config = unserialize($query->fetchField(0));
    }
  }
  return $config;
}

/**
 * Helper function for grabbing Smart Paging fragment url for more SEO friendly.
 */
function smart_paging_get_url_fragment($url = NULL) {
  $smart_paging_url = &drupal_static(__FUNCTION__);
  if (!isset($smart_paging_url) || !empty($url)) {
    $smart_paging_url = $url;
  }
  return $smart_paging_url;
}

/**
 * Implements hook_paging_wysiwyg_include_directory().
 */
function smart_paging_wysiwyg_include_directory($type) {
  switch ($type) {
    case 'plugins':
      // You can just return $type, if you place your Wysiwyg plugins into a
      // sub-directory named 'plugins'.
      return 'plugins/wysiwyg';
  }
}