forked from kongondo/Blog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessBlog.module
More file actions
3028 lines (2302 loc) · 112 KB
/
ProcessBlog.module
File metadata and controls
3028 lines (2302 loc) · 112 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
/**
* Process Blog Module for ProcessWire.
*
* This module enables you to create and manage a Blog in a unified interface
*
* @author Francis Otieno (Kongondo) <[email protected]>
* @author Ryan Cramer (some original code from Ryan Cramer's Blog Profile)
*
* https://github.com/kongondo/Blog
* Created February 2014
*
* ProcessWire 2.x
* Copyright (C) 2011 by Ryan Cramer
* Licensed under GNU/GPL v2, see LICENSE.TXT
*
* http://www.processwire.com
*
*/
class ProcessBlog extends Process implements Module, ConfigurableModule {
/**
* Return information about this module (required).
*
* @access public
* @return array module info
*
*/
public static function getModuleInfo() {
// @ User role needs 'blog' permission
// @ $permission = 'blog';
// @ Installs MarkupBlog - for frontend rendering of the Blog
// @ Installs BlogPublishDate - for auto-saving Blog Post published date
return array(
'title' => 'Blog',
'summary' => 'Blog Manager module inspired by the Blog Profile by Ryan Cramer',
'author' => 'Francis Otieno (Kongondo)',
'version' => 239,
'href' => 'https://processwire.com/talk/topic/7403-module-blog/',
'singular' => true,
'autoload' => false,
'permission' => 'blog',
'installs' => array('MarkupBlog','BlogPublishDate')
);
}
/**
* The name that will be used for the page this module creates.
*
*/
const PAGE_NAME = 'blog';//
/**
* string name of the cookie used to save limit of posts to show per page in posts dashboard.
*
*/
private $cookieName;
/**
* int value of number of posts/categories/tags to show per page respective dashboard page.
*
*/
private $showLimit;
// Get the module config data
protected $data;
// the main blog pages and their important children
private $blog;
private $posts;
private $categories;
private $tags;
private $comments;
private $widgets;
private $authors;
private $archives;
private $settings;
private $asc;
private $dnc;
private $dc;
private $rposts;
private $rcomments;
/*private $broll;*/
private $tweets;
/*private $pauthor;*/
// other important properties
private $commentsUse;
private $schedulePages;
private $quickPostEditor;
/**
* Default module configuration values.
*
* The values here will saved to the database on install.
*
* blogStyle:
* - style 1: /mysite/blog/posts/example-post/
* - style 2: /mysite/blog/example-post/
* - style 3: /mysite/posts/example-post/
* - style 4: /mysite/example-post/
*
* @access public
*
*/
public static function configDefaults () {
return array(
'blogFullyInstalled' => 0, // boolean 0=false {new install}; 1=true {blog fully installed - i.e. including template files and pages}
'blogStyle' => '', // 1='see example 1'
'schedulePages' => 0,// boolean 0=false; 1=true: Whether to use auto-publish/unpublish feature {needs SchedulePages module}
'commentsUse' => 1, // boolean 0=false; 1=true
'templateFilesInstall' => 1,// int 1=blank template files; 2=demo template files; 3=no template files
/*'prefixTemplatesFields' => 'blog',*///string default='blog'. Will be used to prefix templates, fields and template files. E.g. blog-post, blog_date, blog-post.php. Blank = no prefix
'tagTemplatesFields' => '-blog',// string default='-blog'. Will be used to tag templates and fields in the admin. Blank = no tag
'quickPostEditor' => 0,// boolean 0=false; 1=true: Whether to use a Rich Text Editor (CKEditor) in Quick Post
/* BLOG PARENT PAGES IDs */
// changed to below for convenience in coding - to use array index as both an identifier and identifier storage (id) AS well as related template name
'blog' => '',// ID of the page 'Blog' - if used
'blog-posts' => '',// ID of the page 'Posts' - if used
'blog-categories' => '',// ID of the page 'Categories'
'blog-tags' => '',// ID of the page 'Tags'
'blog-comments' => '',// ID of the page 'Comments' - if used
'blog-widgets' => '',// ID of the page 'Widgets'
'blog-authors' => '',// ID of the page 'Authors'
'blog-archives' => '',// ID of the page 'Archives'
'blog-settings' => '', // ID of the page 'Settings'
'blog-asc' => '',// ID of the page 'Always Show Comments'
'blog-dnc' => '',// ID of the page 'Disable New Comments'
'blog-dc' => '',// ID of the page 'Disable Comments'
'blog-rposts' => '',// ID of the page 'Recent Posts' - widget
'blog-rcomments' => '',// ID of the page 'Recent Comments' - widget
'blog-broll' => '',// ID of the page 'Blogroll' - widget
'blog-tweets' => '',// ID of the page 'Recent Tweets' - widget
'blog-pauthor' => '',// ID of the page 'Post Author' - widget
);
}
/**
* Stores and sets some default module properties.
*
* Default config values that are not yet stored in the database are set by you in the constructor.
*
* @access public
*
*/
public function __construct() {
foreach(self::configDefaults() as $key => $value) {$this->$key = $value;}
}
/**
* Initialise the module. This is an optional initialisation method called before any execute methods.
*
* Initialises various class properties ready for use throughout the class.
*
* @access public
*
*/
public function init() {
if ($this->permissions->get('blog')->id && !$this->user->hasPermission('blog')) throw new WirePermissionException("You have no permission to use this module");
parent::init();
$this->modules->get('JqueryFancybox');
// cookie per user and per relevant blog dashboard (posts, categories or tags) context
$this->cookieName = $this->user->id . '-processBlog-' . $this->wire('input')->urlSegment1;
// default number of posts/categories/tags to show per respective dashboard if no custom limit set (via post/session cookie).
$this->showLimit = 10;
$blogConfigs = $this->wire('modules')->getModuleConfigData($this);
// intialise some properties we'll use throught the class. These contain objects made up of the main blog pages and the main children
$this->blog = $this->wire('pages')->get($blogConfigs['blog']);
$this->posts = $this->wire('pages')->get($blogConfigs['blog-posts']);
$this->categories = $this->wire('pages')->get($blogConfigs['blog-categories']);
$this->tags = $this->wire('pages')->get($blogConfigs['blog-tags']);
$this->comments = $this->wire('pages')->get($blogConfigs['blog-comments']);
$this->widgets = $this->wire('pages')->get($blogConfigs['blog-widgets']);
$this->authors = $this->wire('pages')->get($blogConfigs['blog-authors']);
$this->archives = $this->wire('pages')->get($blogConfigs['blog-archives']);
$this->settings = $this->wire('pages')->get($blogConfigs['blog-settings']);
$this->asc = $this->wire('pages')->get($blogConfigs['blog-asc']);
$this->dnc = $this->wire('pages')->get($blogConfigs['blog-dnc']);
$this->dc = $this->wire('pages')->get($blogConfigs['blog-dc']);
$this->rposts = $this->wire('pages')->get($blogConfigs['blog-rposts']);
$this->rcomments = $this->wire('pages')->get($blogConfigs['blog-rcomments']);
/*$this->broll = $this->wire('pages')->get($blogConfigs['blog-broll']);*/
$this->tweets = $this->wire('pages')->get($blogConfigs['blog-tweets']);
/*$this->pauthor = $this->wire('pages')->get($blogConfigs['blog-pauthor']);*/
$this->blogStyle = $blogConfigs['blogStyle'];
$this->commentsUse = $blogConfigs['commentsUse'];
$this->schedulePages = $blogConfigs['schedulePages'];
$this->quickPostEditor = isset($blogConfigs['quickPostEditor']) ? $blogConfigs['quickPostEditor'] : 0;// primarily for upgrades
$this->templateFilesInstall = $blogConfigs['templateFilesInstall'];
}
/**
* Module configuration interface.
*
* Displayed in module configuration screen. This is a ProcessWire method
* User configurable values stored in the database.
*
* @access public
* @return mixed $form rendered form
*
*/
public static function getModuleConfigInputfields(array $data) {
$dir = wire('config')->urls->ProcessBlog;// our path
wire('config')->styles->add($dir . 'config.css');
foreach(self::configDefaults() as $key => $value) {
if(!isset($data[$key])) $data[$key] = $value;
}
// create the config screen
$form = new InputfieldWrapper();
// checkbox: use RTE (CKEditor) in Quick Post? @note: we define here since setting will be visible pre- and post-install of Blog
$rte = new InputfieldCheckbox();
$rte->attr('id+name', 'quickPostEditor');
$rte->label = __('Use RTE in Quick Post');
$rte->label2 = __('Check to enable use of RTE in Quick Post (rather than a plain textarea)');
$description = __('The module CKEditor is installed. This feature is ready to use if you wish.');
if(!wire('modules')->isInstalled('InputfieldCKEditor')) {
$description = __('To use this feature you must first install the module CKEditor. Currently, you do not have the module installed so this setting will not work.');
$rte->attr('class', 'hidden');
}
$rte->description = $description;
$rte->value = $data['quickPostEditor'];
$checked = $data['quickPostEditor'] ? 'checked' : '';
$rte->attr('checked', $checked);
// do not show configuration settings if blog is already fully installed but show them their settings
if(isset($data['blogFullyInstalled']) && $data['blogFullyInstalled'] == 1) {
$m = new InputfieldMarkup;
$m->label = __('Blog fully installed');
$m->description = __('You have already fully installed Blog with the following settings. These settings can no longer be changed unless you first uninstall the Blog Components (pages, fields and templates) using the Cleanup tab in the Blog Dashboard when logged in as a Superuser.');
// create and render a table showing final blog settings as selected and installed by user
$t = self::renderFinalSettingsTable($data);
$cleanupURL = '<a target="_blank" href="' . wire('config')->urls->admin . 'blog/cleanup/">' . __('Cleanup utility') . '</a>';
$warning = '<h3 class="warning">' . __('Do not uninstall Blog until AFTER you have run the ') . $cleanupURL .
__(' unless you only want to keep your Blog Pages without using the module.') . '</h3>';
$m->attr('value', $t->render() . $warning);
$form->add($m);
$form->add($rte);
return $form;
}
// configuration instructions when blog is not yet fully installed
$m = new InputfieldMarkup;
$m->label = __('Important');
$m->description = __('Please select at least a Blog Style below and save before proceeding to the Blog Dashboard. You can also change the other settings as desired. It is important to note the following:');
$m->skipLabel = Inputfield::skipLabelHeader;// we don't want a label displayed here
$m->notes = __("When you first launch the Blog Dashboard you will need to finalise the installation before running the module. Once that installation is complete, you will no longer be able to see and change the settings below (except for the Quick Post setting) unless you first uninstall the Blog Components (pages, fields and templates). You can do that using the Cleanup tab in the Blog Dashboard. You would have to be logged in as a SuperUser. In case you left this Blog module configuration settings page open then went ahead and finalised installing Blog, please DO NOT change any settings here and press the submit button again! Doing so may lead to Blog not working properly. Please carefully read the note below about pre- and post-finalising Blog install.");
$form->add($m);
// radios: blog styles selection
$r = new InputfieldRadios();
$r->attr('id+name', 'blogStyle');
$r->label = __('Select a Blog URL style');
$r->description = __("How your Blog pages would render relative to your site's structure/tree and an example paths are shown below each option respectively. This setting must be filled before you can proceed.");
$radioOptions = array (
1 => __('Style 1'),
2 => __('Style 2'),
3 => __('Style 3'),
4 => __('Style 4'),
);
$r->addOptions($radioOptions);
$r->value = $data['blogStyle'];
$form->add($r);
// blog styles tree and paths visual
$m = new InputfieldMarkup;
$m->label = __('Blog URL styles');
$m->skipLabel = Inputfield::skipLabelHeader;// we don't want a label displayed here
$b = self::blogStylesTree();
$m->attr('value', $b);
$m->notes = __("When you first launch the Blog Dashboard, you will have an opportunity to automatically RENAME any of the following 9 Blog PARENT pages before they are created: Blog, Posts, Categories, Tags, Comments, Widgets, Authors, Archives and Settings. For instance, if you wish, you could rename 'Posts' to 'Items', or 'Authors' to 'Writers', etc. After that, you could also manually RENAME or MOVE these pages and their CHILDREN in your page tree. With the exception of the pages 'Example Post', 'Example Category' and 'Example Tag', do not DELETE any of the pages created on install OR change their IDs. Depending on your settings here, not all of these pages will be created. For instance, the page Comments and its children will not be created if you disable the commenting feature below.");
$form->add($m);
// checkbox: use schedule pages?
$c = new InputfieldCheckbox();
$c->attr('id+name', 'schedulePages');
$c->label = __('Scheduled auto-publish/unpublish');
$c->label2 = __('Check to enable auto-publish/unpublish at set times');
$description = __('The module SchedulePages is installed. This feature is ready to use if you wish.');
if(!wire('modules')->isInstalled('SchedulePages')) {
$description = __('To use this feature you must first install the module SchedulePages. This must be done BEFORE you access the Blog Dashboard for the first time. Currently, you do not have the module installed so this setting will not work.');
$c->attr('class', 'hidden');
}
$c->description = $description;
$c->value = $data['schedulePages'];
$checked = $data['schedulePages'] ? 'checked' : '';
$c->attr('checked', $checked);
$form->add($c);
// checkbox: use comments? - checked by default
$c = new InputfieldCheckbox();
$c->attr('id+name', 'commentsUse');
$c->label = __('Install commenting feature');
$c->label2 = __('Check to enable install of comments fields for your blog');
$c->description = __('Enabling this feature will allow users to be able to comment on your posts. You will still have granular control on commenting.');
$c->value = $data['commentsUse'];
$checked = isset($data['commentsUse']) && $data['commentsUse'] == '' ? '' : 'checked';
$c->attr('checked', $checked);
$form->add($c);
// radios: install template files?
$r = new InputfieldRadios();
$r->attr('id+name', 'templateFilesInstall');
$r->label = __('Install template files');
$r->description = __('Optionally let the module install related template files for your templates. This will run only once when you first access the Blog Dashboard (ProcessBlog).');
$radioOptions = array (
1 => __('Blank template files'),
2 => __('Template files with a demo blog'),
3 => __("Don't install template files"),
);
$r->addOptions($radioOptions);
$r->value = $data['templateFilesInstall'];
$form->add($r);
/* @todo - @note: removed this for now. It could potentially create problems down the line and decrease code readability
// Blog Template and Fields prefix
$t = new InputfieldText;
$t->attr('id+name', 'prefixTemplatesFields');
$t->label = __('Prefix for blog templates, template files and fields');
$t->attr('value', $data['prefixTemplatesFields']);
$t->description = __("Specify a prefix that will be used to name your templates (e.g. blog-post), template files (blog-post.php) and fields (blog_date). The default is 'blog'. This setting cannot be blank.");
$form->add($t);*/
// text: Blog Template and Fields Admin Tag (for grouping in /admin/setup/fields/ or /admin/setup/templates/)
$t = new InputfieldText;
$t->attr('id+name', 'tagTemplatesFields');
$t->label = __('Tag for blog templates and fields');
$t->attr('value', $data['tagTemplatesFields']);
$t->description = __("Optionally specify a tag that will be used to group your templates and fields in their respective admin pages. The default is 'blog'. Leave this blank if you do not want a tag.");
$form->add($t);
// radios: RTE in Quick Post selection
$form->add($rte);
return $form;
}
/**
* Return an array of this module's saved configuration settings: key=>value pairs.
*
* @access protected
* @return array $settings module's saved configuration settings
*
*/
protected function getDefaultSettings() {
$settings = $this->wire('modules')->getModuleConfigData($this);
return $settings;
}
/**
* Render example blog styles trees and paths
*
* @access public
* @return string $out markup
*
*/
public static function blogStylesTree() {
$domain = $_SERVER['SERVER_NAME'];
$out = '';
$out .= '<div id="blog-styles">';
$blogPages = array(
'blog'=>'Blog',
'posts'=>'Posts',
'post'=>'Example Post',
'categories'=>'Categories',
'category'=>'Example category',
'tags'=>'Tags',
'tag'=>'Example Tag',
'comments'=>'Comments',
'asc'=>'Always Show Comments',
'dnc'=>'Disable New Comments',
'dc'=>'Disable Comments',
'widgets'=>'Widgets',
'rposts'=>'Recent Posts',
'rcomments'=>'Recent Comments',
'broll'=>'Blogroll',
'tweets'=>'Recent Tweets',
'pauthor'=>'Post Author',
'authors'=>'Authors',
'archives'=>'Archives',
'settings'=>'Settings',
);
$blogParents = array('Blog', 'Posts', 'Categories', 'Tags', 'Comments', 'Widgets', 'Authors', 'Archives', 'Settings');
// blog style #1
$b1 = '';
$b1 .= '<!-- Blog style 1-->
<div class="blog-style">
<span class="example-path">' . $domain . __('/blog/posts/example-post/') . '</span><br>
<span class="example-path">' . $domain . __('/blog/categories/example-category/') . '</span><br><br>';
foreach ($blogPages as $key => $value) {
if ($key == 'blog') {$b1 .= $value . '<br>';}
elseif (in_array($value, $blogParents )) $b1 .= '<span>' . $value . '</span><br>';
else $b1.= '<span class="child-item">' . $value . '</span><br>';
}
$b1 .= '</div>';
$out .= $b1;
// blog style #2
$b2 = '';
$b2 .= '<!-- Blog style 2-->
<div class="blog-style">
<span class="example-path">' . $domain . __('/blog/example-post/') . '</span><br>
<span class="example-path">' . $domain . __('/blog/categories/example-category/') . '</span><br><br>';
foreach ($blogPages as $key => $value) {
if($key == 'posts') continue;
if($key == 'blog') {$b2 .= $value . '<br>';}
elseif (in_array($value, $blogParents ) || $key == 'post') {$b2 .= '<span>' . $value . '</span><br>';}
else {$b2 .= '<span class="child-item">' . $value . '</span><br>';}
}
$b2 .= '</div>';
$out .= $b2;
// blog style #3
$b3 = '';
$b3 .= '<!-- Blog style 3-->
<div class="blog-style">
<span class="example-path">' . $domain . __('/posts/example-post/') . '</span><br>
<span class="example-path">' . $domain . __('/categories/example-category/') . '</span><br><br>';
foreach ($blogPages as $key => $value) {
if($key == 'blog') continue;
if (in_array($value, $blogParents )) $b3 .= $value . '<br>';
else $b3 .= '<span>' . $value . '</span><br>';
}
$b3 .= '</div>';
$out .= $b3;
// blog style #4
$b4 = '';
$b4 .= '<!-- Blog style 4-->
<div class="blog-style">
<span class="example-path">' . $domain . __('/example-post/') . '</span><br>
<span class="example-path">' . $domain . __('/categories/example-category/') . '</span><br><br>';
foreach ($blogPages as $key => $value) {
if($key=='blog' || $key == 'posts') continue;
if (in_array($value, $blogParents) || $key =='post') $b4 .= $value . '<br>';
else $b4 .= '<span>' . $value . '</span><br>';
}
$b4 .= '</div>';
$out .= $b4;
$out .= '</div>';
return $out;
}
/**
* Returns a MarkupAdminDataTable object for rendering later.
*
* Shows values of final selected blog settings.
* Used by both firstAccess() and getModuleConfigInputfields().
*
* @access public
* @return object $t table inputField
*
*/
public static function renderFinalSettingsTable(array $data) {
$domain = $_SERVER['SERVER_NAME'];
// create a mew table. We use wire because we are in a static method
$t = wire('modules')->get('MarkupAdminDataTable');
$t->setEncodeEntities(false);
$t->setClass('settingsTable');
// set header rows
$t->headerRow(array(
'Property',
'Setting',
));
/*table rows data - for final Blog settings */
if ($data['blogStyle']==1) $blogStyle = 'Style 1: ' . $domain . '/blog/posts/example-post/';
elseif ($data['blogStyle']==2) $blogStyle = 'Style 2: ' . $domain . '/blog/example-post/';
elseif ($data['blogStyle']==3) $blogStyle = 'Style 3: ' . $domain . '/posts/example-post/';
else $blogStyle = 'Style 4: ' . $domain . '/example-post/';
$schedulePages = $data['schedulePages'] ? 'Scheduled auto-publish/unpublish enabled' : 'Scheduled auto-publish/unpublish not enabled';
$commentsUse = $data['commentsUse'] ? 'Commenting feature enabled' : 'Commenting feature not enabled';
if ($data['templateFilesInstall']==1) $templateFilesInstall = 'Blank template files';
elseif ($data['templateFilesInstall']==2) $templateFilesInstall = 'Template files with demo blog content';
else $templateFilesInstall = 'No template files';
$tagTemplatesFields = $data['tagTemplatesFields'] ? $data['tagTemplatesFields'] : 'No tag specified';
$quickPostEditor = $data['quickPostEditor'] ? 'RTE in Quick Post enabled' : 'RTE in Quick Post not enabled';
$settings = array(
'Blog Style' => $blogStyle,
'Auto-publish/unpublish' => $schedulePages,
'Commenting feature' => $commentsUse,
'Install template files' => $templateFilesInstall,
/*'Templates and fields prefix' => $data['prefixTemplatesFields'], not using this currently*/
'Tag for blog templates and fields' => $tagTemplatesFields,
'Use RTE in Quick Post' => $quickPostEditor,
);
foreach ($settings as $key => $value) {
$t->row(array(
$key,
$value,
));
}
return $t;
}
/**
* Interface to second step of Blog install.
*
* Displayed when Blog is first accessed and it is not yet fully installed.
* Sends install form to installWizard().
*
* @access public
* @return mixed $form rendered form
*
*/
public function firstAccess($data) {
$domain = $_SERVER['SERVER_NAME'];
// url to this module's settings page
$blogURL = $this->config->urls->admin . 'module/edit?name=' . $this;
$fistInstallWelcome = '<h2>' . $this->_('Finalise your Blog installation') . '</h2>
<p>' . $this->_("Please finalise Blog installation by reviewing your settings below and making any necessary changes either here or in the module's <a href={$blogURL}>config screen</a> . Below you can customise the names of your Blog PARENT pages. This is an important final step to customise your Blog. Once the install wizard has run, you will not be able to make changes except renaming your Blog pages. This wizard will install blog pages, templates, template files, fields and an author role according to your settings. This means some of these components may not be installed. When you are happy with the settings, click the button to proceed with the installation. Once installed, do not DELETE any of the PARENT pages, change their IDs or rename the templates or fields.") . '</p>';
// CREATE A NEW FORM
$form = new InputfieldForm;
$form->attr('id', 'blog');
$form->action = './';
$form->method = 'post';
// CREATE A NEW WRAPPER
$w = new InputfieldWrapper;
// CREATE THE FIRST FIELDSET
$fs1 = $this->modules->get("InputfieldFieldset");
$fs1->label = $this->_('Settings');
// CREATE AN INPUTFIELD MARKUP
$m = new InputfieldMarkup;
$m->label = $this->_('Important');
$m->skipLabel = Inputfield::skipLabelHeader;// we don't want a label displayed here
$m->description = $this->_("In case you wish to make any changes to these settings you must do so in Blog's module's settings page before before finalising Blog install below. Click <a href={$blogURL}>here</a> if you want to go back and make changes.");
/*table rows data - for initial setup Blog settings */
$t = self::renderFinalSettingsTable($data);
$m->attr('value', $t->render());
$fs1->add($m);
$w->add($fs1);// first fieldset added to wrapper
// CREATE THE SECOND FIELDSET
$fs2 = new InputfieldFieldset;
$fs2->label = $this->_('Parent Pages Titles');
// $fs2->removeAttr('id');// not necessary
// Here I set my own ID since I will want to target this <li>; otherwise, PW will set its own. @note, there is no method setAttr!
$fs2->setAttribute('id', 'widgets_edit');
$fs2->addClass('edit_fieldsets');
// CREATE AN INPUTFIELD MARKUP
$m = $this->modules->get('InputfieldMarkup');
$m->description = $this->_("Changes here are optional. You can also manually change these titles later when editing the pages. However, depending on the Blog Style you selected, you want to make sure that these titles do not clash with your existing pages.");
$m->notes = $this->_('The wizard will create pages, templates, fields, template files and one role. If there are any existing items on your site with similar names, the whole installation will stop. None of your existing items will be overwritten. You would have to make changes and run the wizard again.');
// CREATE A NEW TABLE - Blog Settings Table
$t = new MarkupAdminDataTable;
$t->setEncodeEntities(false);
$t->setClass('settingsTable');
// set header rows
$t->headerRow(array(
$this->_('Default Title'),
$this->_('Custom Title'),
));
// prepare variables for page titles
$blog = '';
$posts = '';
$categories = '';
$tags = '';
$comments = '';
$widgets = '';
$authors = '';
$archives = '';
$settings = '';
$parentCustomTitles = array();
$textFields = array(
// 0=value; 1=name; 2=new InputfieldText
array('Blog', 'blog', $blog),
array('Posts', 'posts', $posts),
array('Categories', 'categories', $categories),
array('Tags', 'tags', $tags),
array('Comments', 'comments', $comments),
array('Widgets', 'widgets', $widgets),
array('Authors', 'authors', $authors),
array('Archives', 'archives', $archives),
array('Settings', 'settings', $settings),
);
// remove non-relevant pages according to selected blog style
foreach ($textFields as $v) {
if ($data['blogStyle'] == 2 && $v[0] == 'Posts') continue;
if ($data['blogStyle'] == 3 && $v[0] == 'Blog') continue;
if (($data['blogStyle'] == 4 && $v[0] == 'Blog') || ($data['blogStyle'] == 4 && $v[0] == 'Posts')) continue;
if (!$data['commentsUse'] && $v[0] == 'Comments') continue;
$v[2] = new InputfieldText;
$v[2]->attr('name', $v[1]);
$v[2]->attr('value', $v[0]);
$parentCustomTitles[$v[0]] = $v[2];
}
// table rows
foreach ($parentCustomTitles as $key => $value) {
$t->row(array(
$key,
$value->render(),
));
}
$s = $this->modules->get('InputfieldSubmit');
// $s->class .= ' head_button_clone';
$s->attr('id+name', 'install_wizard_btn');
$s->class .= " final_settings_save";// add a custom class to this submit button
$s->attr('value', $this->_('Run install wizard'));
$m->attr('value', $t->render() . $s->render());
$fs2->add($m);
$w->add($fs2);// second fieldset added to wrapper
$form->add($w);
$post = $this->input->post;
// send input->post values to the Method blogInstallerWizard();
if($post->install_wizard_btn) $this->installWizard($form);
return $fistInstallWelcome . $form->render();
}
/**
* Display Blog Dashboard (summary of Posts informaton) and Blod Archives.
*
* This function is executed when a page with this Process (Blog) assigned is accessed.
* Renders different manager views depending on whether Blog is fully installed or not.
*
* @access public
* @return mixed
*
*/
public function execute() {
// Get the module config data
$data = $this->getDefaultSettings();
// if blogStyle has not been specified we throw an error
if(!isset($data['blogStyle']) || $data['blogStyle'] == '') {
// $this->error($this->_('You must first select and save a Blog Style in the module config!'));
$moduleURL = '<a target="_blank" href="' . $this->config->urls->admin . 'module/edit?name=' . $this . '">' . $this->_('module config!') . '</a>';
$this->error($this->_('You must first select and save a Blog Style in the ') . $moduleURL, Notice::allowMarkup);
return;
}
// if blog style url has been set but this is a new install, i.e. blogFullyInstalled == 0, we show first welcome
elseif(isset($data['blogFullyInstalled']) && $data['blogFullyInstalled'] == 0) {
// if scheduled auto-publish/unpublish is selected but the module SchedulePages is not yet installed
if ($data['schedulePages'] == 1 && !$this->wire('modules')->isInstalled('SchedulePages')) {
$this->error($this->_('To install the scheduled auto-publish/unpublish feature you must first install the module SchedulePages!'));
return;
}
return $this->firstAccess($data);
}
// if blog is already fully installed, let's proceed as normal and show the blog dashboard
else {
return $this->blogDashboard();
}
}
/**
* Function to create the Blog Manager menu.
*
* @access private
* @return string $menu
*
*/
private function blogMenu() {
$menu = "<ul class='blog_menu'>";
$on = !$this->wire('input')->urlSegment1 ? 'blog_menu_item onblog' : 'blog_menu_item';
/*
NEED ABSOLUTE URLS TO DEAL WITH ISSUE OF TRAILING SLASH.
- http:// processwire.com/talk/topic/3777-post-empty-on-post-from-different-page/
*/
// $menu .= "<li><a class='$on' href='./'>Dashboard</a></li>";
// had to revert to below [absolute url]; see URL segment + trailing slash issue
$menu .= "<li><a class='$on' href='" . $this->wire('page')->url . "'>" . $this->_('Dashboard') . "</a></li>";
$menuItemsOther = array(
// @todo - tried to make these match their page titles but messing up layout!
'posts' => $this->_('Posts'),
'categories' => $this->_('Categories'),
'tags' => $this->_('Tags'),
'authors' => $this->_('Authors'),
'settings' => $this->_('Settings'),
'cleanup' => $this->_('Cleanup'),
);
// we do not want non-superusers to view the cleanup (blog components uninstaller) page
if (!$this->user->isSuperuser()) unset($menuItemsOther['cleanup']);
foreach ($menuItemsOther as $key => $value) {
$on = $this->wire('input')->urlSegment1 == $key ? 'blog_menu_item onblog' : 'blog_menu_item';
// $menu .= "<li><a class='$on' href='./$key'>$value</a></li>";
/*
had to change to this because of issue with trailling slash and
_POST getting converted to _GET
http:// processwire.com/talk/topic/3777-post-empty-on-post-from-different-page/ AND
http:// processwire.com/talk/topic/3727-does-input-urlsegments-mess-with-post/
*/
$menu .= "<li><a class='$on' href='" . $this->wire('page')->url . $key . "/'>$value</a></li>";
}
$menu .= "</ul>";
return $menu;
}
/**
* Display default Blog view.
*
* Shows Blog's manager landing page.
*
* @access public
* @return mixed $form rendered form
*
*/
public function blogDashboard() {
// default blog view = dashboard
// CREATE A NEW FORM
$form = $this->modules->get('InputfieldForm');
$form->attr('id', 'blog');
$form->action = './';
$form->method = 'post';
// selector array for templates of pages for posts, categories and tags for use in dashboard. We create an assoc array just for convenience (cheeky!)
$selector = array(
'blog-post' =>'',
'blog-category' => '',
'blog-tag' => ''
);
$qn = array();
foreach ($selector as $k => $v) {$qn [$k] = $this->wire('pages')->count("template={$k}, parent!=7");}// exclude pages in trash
// add count of unpublished posts to array
$qn ['unpublished'] = $this->wire('pages')->count("template=blog-post, status=unpublished, parent!=7");
// ################ - Comments count for Blog Dashborad - ####################
$numApproved = '';
$numPending = '';
$numSpam = '';
$approvedComment = 1;
$pendingComment = 0;
$spamComment = -2;
$database = $this->wire('database');
$table = $database->escapeTable('field_blog_comments');// see /wire/core/Fieldtype.php. returns 'field_name_of_field' (i.e. as in the database)
$sql = "SELECT COUNT(*) FROM `$table` WHERE status=:status";
$query = $database->prepare($sql);// prepare statement
// approved comments count
$query->bindValue(":status", $approvedComment, PDO::PARAM_INT);
$query->execute();// execute our count
$numApproved = $query->fetchColumn();// fetch the count
// pending comments count
$query->bindValue(":status", $pendingComment, PDO::PARAM_INT);
$query->execute();// execute our count
$numPending = $query->fetchColumn();// fetch the count
// spam comments count
$query->bindValue(":status", $spamComment, PDO::PARAM_INT);
$query->execute();// execute our count
$numSpam = $query->fetchColumn();// fetch the count
$numTotal = $numApproved + $numPending + $numSpam;
// TABLE for quick view of posts
// prepare some variables we'll use here
$comments = $this->comments;
$categories = $this->categories;
$tags = $this->tags;
$archives = $this->archives;
$overPostsData = array(
array($this->_('Posts'), $qn['blog-post'], sprintf(__('%s'), $comments->title), $numTotal),
array($this->_('Unpublished'), $qn['unpublished'], $this->_('Approved'), $numApproved),
array(sprintf(__('%s'), $categories->title), $qn['blog-category'], $this->_('Pending'), $numPending ),
array(sprintf(__('%s'), $tags->title), $qn['blog-tag'], $this->_('Spam'), $numSpam ),
);
// remove comments-related data if commentsUse !=1
if ($this->commentsUse !=1) {
unset($overPostsData[0][2]);
unset($overPostsData[0][3]);
unset($overPostsData[1][2]);
unset($overPostsData[1][3]);
unset($overPostsData[2][2]);
unset($overPostsData[2][3]);
unset($overPostsData[3][2]);
unset($overPostsData[3][3]);
}
$overPosts = '';
$overPosts .= '<table id="posts_overview">';
foreach ($overPostsData as $key => $value) {
$overPosts .= '<tr>';
foreach ($value as $v) {
$class = is_int($v) ? 'class="large"' : '';
$overPosts .= "<td $class>" . $v . "</td>";
}
$overPosts .= '<tr>';
}
$overPosts .='</table>';
$w = new InputfieldWrapper();
$w->attr('title', $this->_('Overview'));
$fs1 = $this->modules->get("InputfieldFieldset");
$fs1->label = $this->_('Dashboard');
$fs1->setAttribute('id', 'dashboard');
$m = $this->modules->get('InputfieldMarkup');
$m->columnWidth = 50;
$m->label = $this->_('Quick view');
// $m->description = $this->_('At a glance');
$m->attr('value', $overPosts);
$m->skipLabel = Inputfield::skipLabelHeader;// we don't want a label displayed here
$fs1->add($m);// add post stats inputfield to fieldset
$w->add($fs1);
// ################ - Archives table for Dashboard - ####################
// modified from 'blog.inc' = getArchives
$oldest = $this->wire('pages')->get("template=blog-post, blog_date>0, sort=blog_date");
$newest = $this->wire('pages')->get("template=blog-post, blog_date>0, sort=-blog_date");
// if(!$newest->id) return '';// not sure how to deal with this; don't want blank dashboard - but first time run only when no posts, so ok for code below to run
$firstYear = date('Y', $oldest->getUnformatted('blog_date'));
$lastYear = date('Y', $newest->getUnformatted('blog_date'));
$years = array();
#prepare for looping over years and months
// prepare blog_date table
$table = $database->escapeTable('field_blog_date');// see /wire/core/Fieldtype.php. returns 'field_name_of_field' (i.e. as in the database)
$templateID = $this->wire('templates')->get('blog-post')->id;// we need the ID of the template 'blog-post' for our sql query below
for($y = $lastYear; $y >= $firstYear; $y--) {
$months = array();
$numPostsYear = 0;
for($month = 1; $month <= 12; $month++) {
$firstDay = strtotime("$y-$month-01");// 01 Jan 201x
$lastDay = strtotime("+1 month", $firstDay)-1;// 31 Jan 201x
// 'data' field in FieldtypeDatetime in PW is hardcoded to Y-m-d H:i:s - see the FieldtypeDatetime line#426 (___sleepValue)
// we make sure that is the format we'll use for comparison in the sql query below (blog_date is saved as datetime)
$firstDay = date('Y-m-d H:i:s', $firstDay);
$lastDay = date('Y-m-d H:i:s', $lastDay);
// our count based on data in two tables - 'pages' and 'field_blog_date'
$sql = "SELECT COUNT(*) FROM `$table` " .
"INNER JOIN pages ON pages.id=$table.pages_id " .
"WHERE pages.templates_id=:templates_id " .
"AND $table.data>=:first_day " .
"AND $table.data<=:last_day";
$query = $database->prepare($sql);
$query->bindValue(":first_day", $firstDay);// bind our named parameters
$query->bindValue(":last_day", $lastDay);
$query->bindValue(':templates_id', $templateID, PDO::PARAM_INT);
$query->execute();
// fetch count
$numPosts = $query->fetchColumn();
$months[] = $numPosts;// @kongondo - build a $months array to be added to $years array