forked from rubythonode/iModule.module.board
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleBoard.class.php
More file actions
1919 lines (1615 loc) · 68 KB
/
ModuleBoard.class.php
File metadata and controls
1919 lines (1615 loc) · 68 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
/**
* 이 파일은 iModule 게시판모듈의 일부입니다. (https://www.imodules.io)
*
* 게시판과 관련된 모든 기능을 제어한다.
*
* @file /modules/board/ModuleBoard.class.php
* @author Arzz ([email protected])
* @license MIT License
* @version 3.0.0
* @modified 2019. 12. 11.
*/
class ModuleBoard {
/**
* iModule 및 Module 코어클래스
*/
private $IM;
private $Module;
/**
* DB 관련 변수정의
*
* @private object $DB DB접속객체
* @private string[] $table DB 테이블 별칭 및 원 테이블명을 정의하기 위한 변수
*/
private $DB;
private $table;
/**
* 언어셋을 정의한다.
*
* @private object $lang 현재 사이트주소에서 설정된 언어셋
* @private object $oLang package.json 에 의해 정의된 기본 언어셋
*/
private $lang = null;
private $oLang = null;
/**
* DB접근을 줄이기 위해 DB에서 불러온 데이터를 저장할 변수를 정의한다.
*
* @private $admins 관리자정보
* @private $boards 게시판설정정보
* @private $categories 카테고리정보
* @private $prefixes 말머리정보
* @private $posts 게시물정보
* @private $ments 댓글정보
*/
private $admins = array();
private $boards = array();
private $categories = array();
private $prefixes = array();
private $posts = array();
private $ments = array();
/**
* 기본 URL (다른 모듈에서 호출되었을 경우에 사용된다.)
*/
private $baseUrl = null;
/**
* class 선언
*
* @param iModule $IM iModule 코어클래스
* @param Module $Module Module 코어클래스
* @see /classes/iModule.class.php
* @see /classes/Module.class.php
*/
function __construct($IM,$Module) {
/**
* iModule 및 Module 코어 선언
*/
$this->IM = $IM;
$this->Module = $Module;
/**
* 모듈에서 사용하는 DB 테이블 별칭 정의
* @see 모듈폴더의 package.json 의 databases 참고
*/
$this->table = new stdClass();
$this->table->admin = 'board_admin_table';
$this->table->board = 'board_table';
$this->table->category = 'board_category_table';
$this->table->prefix = 'board_prefix_table';
$this->table->post = 'board_post_table';
$this->table->ment = 'board_ment_table';
$this->table->ment_depth = 'board_ment_depth_table';
$this->table->attachment = 'board_attachment_table';
$this->table->activity = 'board_activity_table';
}
/**
* 모듈 코어 클래스를 반환한다.
* 현재 모듈의 각종 설정값이나 모듈의 package.json 설정값을 모듈 코어 클래스를 통해 확인할 수 있다.
*
* @return Module $Module
*/
function getModule() {
return $this->Module;
}
/**
* 모듈 설치시 정의된 DB코드를 사용하여 모듈에서 사용할 전용 DB클래스를 반환한다.
*
* @return DB $DB
*/
function db() {
if ($this->DB == null || $this->DB->ping() === false) $this->DB = $this->IM->db($this->getModule()->getInstalled()->database);
return $this->DB;
}
/**
* 모듈에서 사용중인 DB테이블 별칭을 이용하여 실제 DB테이블 명을 반환한다.
*
* @param string $table DB테이블 별칭
* @return string $table 실제 DB테이블 명
*/
function getTable($table) {
return empty($this->table->$table) == true ? null : $this->table->$table;
}
/**
* URL 을 가져온다.
*
* @param string $view
* @param string $idx
* @return string $url
*/
function getUrl($view=null,$idx=null) {
$url = $this->baseUrl ? $this->baseUrl : $this->IM->getUrl(null,null,false);
$view = $view === null ? $this->getView($this->baseUrl) : $view;
if ($view == null || $view == false) return $url;
$url.= '/'.$view;
$idx = $idx === null ? $this->getIdx($this->baseUrl) : $idx;
if ($idx == null || $idx == false) return $url;
return $url.'/'.$idx;
}
/**
* 다른모듈에서 호출된 경우 baseUrl 을 설정한다.
*
* @param string $url
* @return $this
*/
function setUrl($url) {
$this->baseUrl = $this->IM->getUrl(null,null,$url,false);
return $this;
}
/**
* view 값을 가져온다.
*
* @return string $view
*/
function getView() {
return $this->IM->getView($this->baseUrl);
}
/**
* idx 값을 가져온다.
*
* @return string $idx
*/
function getIdx() {
return $this->IM->getIdx($this->baseUrl);
}
/**
* [코어] 사이트 외부에서 현재 모듈의 API를 호출하였을 경우, API 요청을 처리하기 위한 함수로 API 실행결과를 반환한다.
* 소스코드 관리를 편하게 하기 위해 각 요쳥별로 별도의 PHP 파일로 관리한다.
*
* @param string $protocol API 호출 프로토콜 (get, post, put, delete)
* @param string $api API명
* @param any $idx API 호출대상 고유값
* @param object $params API 호출시 전달된 파라메터
* @return object $datas API처리후 반환 데이터 (해당 데이터는 /api/index.php 를 통해 API호출자에게 전달된다.)
* @see /api/index.php
*/
function getApi($protocol,$api,$idx=null,$params=null) {
$data = new stdClass();
$values = (object)get_defined_vars();
$this->IM->fireEvent('beforeGetApi',$this->getModule()->getName(),$api,$values);
/**
* 모듈의 api 폴더에 $api 에 해당하는 파일이 있을 경우 불러온다.
*/
if (is_file($this->getModule()->getPath().'/api/'.$api.'.'.$protocol.'.php') == true) {
INCLUDE $this->getModule()->getPath().'/api/'.$api.'.'.$protocol.'.php';
}
unset($values);
$values = (object)get_defined_vars();
$this->IM->fireEvent('afterGetApi',$this->getModule()->getName(),$api,$values,$data);
return $data;
}
/**
* [사이트관리자] 모듈 관리자패널 구성한다.
*
* @return string $panel 관리자패널 HTML
*/
function getAdminPanel() {
/**
* 설정패널 PHP에서 iModule 코어클래스와 모듈코어클래스에 접근하기 위한 변수 선언
*/
$IM = $this->IM;
$Module = $this;
/**
* 회원모듈 관리자를 불러온다.
*/
$this->IM->getModule('admin')->loadModule('member');
ob_start();
INCLUDE $this->getModule()->getPath().'/admin/index.php';
$panel = ob_get_contents();
ob_end_clean();
return $panel;
}
/**
* [사이트관리자] 모듈의 전체 컨텍스트 목록을 반환한다.
*
* @return object $lists 전체 컨텍스트 목록
*/
function getContexts() {
$lists = $this->db()->select($this->table->board,'bid,title')->get();
for ($i=0,$loop=count($lists);$i<$loop;$i++) {
$lists[$i] = array('context'=>$lists[$i]->bid,'title'=>$lists[$i]->title);
}
return $lists;
}
/**
* 특정 컨텍스트에 대한 제목을 반환한다.
*
* @param string $context 컨텍스트명
* @return string $title 컨텍스트 제목
*/
function getContextTitle($context) {
$board = $this->getBoard($context);
if ($board == null) return '삭제된 게시판';
return $board->title.'('.$board->bid.')';
}
/**
* [사이트관리자] 모듈의 컨텍스트 환경설정을 구성한다.
*
* @param object $site 설정대상 사이트
* @param object $values 설정값
* @param string $context 설정대상 컨텍스트명
* @return object[] $configs 환경설정
*/
function getContextConfigs($site,$values,$context) {
$configs = array();
$templet = new stdClass();
$templet->title = $this->IM->getText('text/templet');
$templet->name = 'templet';
$templet->type = 'templet';
$templet->target = 'board';
$templet->use_default = true;
$templet->value = $values != null && isset($values->templet) == true ? $values->templet : '#';
$configs[] = $templet;
$templet = new stdClass();
$templet->title = '첨부파일 템플릿';
$templet->name = 'attachment';
$templet->type = 'templet';
$templet->target = 'attachment';
$templet->use_default = true;
$templet->value = $values != null && isset($values->attachment) == true ? $values->attachment : '#';
$configs[] = $templet;
$category = new stdClass();
$category->title = $this->getText('text/category');
$category->name = 'category';
$category->type = 'select';
$category->data = array();
$category->data[] = array(0,$this->getText('text/category_all'));
$categorys = $this->db()->select($this->table->category,'idx,title')->where('bid',$context)->orderBy('sort','asc')->get();
for ($i=0, $loop=count($categorys);$i<$loop;$i++) {
$category->data[] = array($categorys[$i]->idx,$categorys[$i]->title);
}
$category->value = $values != null && isset($values->category) == true ? $values->category : 0;
$configs[] = $category;
return $configs;
}
/**
* 사이트맵에 나타날 뱃지데이터를 생성한다.
*
* @param string $context 컨텍스트종류
* @param object $configs 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정
* @return object $badge 뱃지데이터 ($badge->count : 뱃지숫자, $badge->latest : 뱃지업데이트 시각(UNIXTIME), $badge->text : 뱃지텍스트)
* @todo check count information
*/
function getContextBadge($context,$config) {
/**
* null 일 경우 뱃지를 표시하지 않는다.
*/
return null;
}
/**
* 언어셋파일에 정의된 코드를 이용하여 사이트에 설정된 언어별로 텍스트를 반환한다.
* 코드에 해당하는 문자열이 없을 경우 1차적으로 package.json 에 정의된 기본언어셋의 텍스트를 반환하고, 기본언어셋 텍스트도 없을 경우에는 코드를 그대로 반환한다.
*
* @param string $code 언어코드
* @param string $replacement 일치하는 언어코드가 없을 경우 반환될 메세지 (기본값 : null, $code 반환)
* @return string $language 실제 언어셋 텍스트
*/
function getText($code,$replacement=null) {
if ($this->lang == null) {
if (is_file($this->getModule()->getPath().'/languages/'.$this->IM->language.'.json') == true) {
$this->lang = json_decode(file_get_contents($this->getModule()->getPath().'/languages/'.$this->IM->language.'.json'));
if ($this->IM->language != $this->getModule()->getPackage()->language && is_file($this->getModule()->getPath().'/languages/'.$this->getModule()->getPackage()->language.'.json') == true) {
$this->oLang = json_decode(file_get_contents($this->getModule()->getPath().'/languages/'.$this->getModule()->getPackage()->language.'.json'));
}
} elseif (is_file($this->getModule()->getPath().'/languages/'.$this->getModule()->getPackage()->language.'.json') == true) {
$this->lang = json_decode(file_get_contents($this->getModule()->getPath().'/languages/'.$this->getModule()->getPackage()->language.'.json'));
$this->oLang = null;
}
}
$returnString = null;
$temp = explode('/',$code);
$string = $this->lang;
for ($i=0, $loop=count($temp);$i<$loop;$i++) {
if (isset($string->{$temp[$i]}) == true) {
$string = $string->{$temp[$i]};
} else {
$string = null;
break;
}
}
if ($string != null) {
$returnString = $string;
} elseif ($this->oLang != null) {
if ($string == null && $this->oLang != null) {
$string = $this->oLang;
for ($i=0, $loop=count($temp);$i<$loop;$i++) {
if (isset($string->{$temp[$i]}) == true) {
$string = $string->{$temp[$i]};
} else {
$string = null;
break;
}
}
}
if ($string != null) $returnString = $string;
}
$this->IM->fireEvent('afterGetText',$this->getModule()->getName(),$code,$returnString);
/**
* 언어셋 텍스트가 없는경우 iModule 코어에서 불러온다.
*/
if ($returnString != null) return $returnString;
elseif (in_array(reset($temp),array('text','button','action')) == true) return $this->IM->getText($code,$replacement);
else return $replacement == null ? $code : $replacement;
}
/**
* 상황에 맞게 에러코드를 반환한다.
*
* @param string $code 에러코드
* @param object $value(옵션) 에러와 관련된 데이터
* @param boolean $isRawData(옵션) RAW 데이터 반환여부
* @return string $message 에러 메세지
*/
function getErrorText($code,$value=null,$isRawData=false) {
$message = $this->getText('error/'.$code,$code);
if ($message == $code) return $this->IM->getErrorText($code,$value,null,$isRawData);
$description = null;
switch ($code) {
default :
if (is_object($value) == false && $value) $description = $value;
}
$error = new stdClass();
$error->message = $message;
$error->description = $description;
$error->type = 'BACK';
if ($isRawData === true) return $error;
else return $this->IM->getErrorText($error);
}
/**
* 템플릿 정보를 가져온다.
*
* @param string $this->getTemplet($configs) 템플릿명
* @return string $package 템플릿 정보
*/
function getTemplet($templet=null) {
$templet = $templet == null ? '#' : $templet;
/**
* 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정일 경우
*/
if (is_object($templet) == true) {
$templet_configs = $templet !== null && isset($templet->templet_configs) == true ? $templet->templet_configs : null;
$templet = $templet !== null && isset($templet->templet) == true ? $templet->templet : '#';
} else {
$templet_configs = null;
}
/**
* 템플릿명이 # 이면 모듈 기본설정에 설정된 템플릿을 사용한다.
*/
if ($templet == '#') {
$templet = $this->getModule()->getConfig('templet');
$templet_configs = $this->getModule()->getConfig('templet_configs');
}
return $this->getModule()->getTemplet($templet,$templet_configs);
}
/**
* 페이지 컨텍스트를 가져온다.
*
* @param string $context 컨테이너 종류
* @param object $configs 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정
* @return string $html 컨텍스트 HTML
*/
function getContext($bid,$configs=null) {
/**
* 모듈 기본 스타일 및 자바스크립트
*/
$this->IM->addHeadResource('style',$this->getModule()->getDir().'/styles/style.css');
$this->IM->addHeadResource('script',$this->getModule()->getDir().'/scripts/script.js');
$view = $this->getView() == null ? 'list' : $this->getView();
$board = $this->getBoard($bid);
if ($board == null) return $this->getError('NOT_FOUND_PAGE');
if ($configs == null) $configs = new stdClass();
if (isset($configs->templet) == false) $configs->templet = '#';
if ($configs->templet == '#') {
$configs->templet = $board->templet;
$configs->templet_configs = $board->templet_configs;
} else {
$configs->templet_configs = isset($configs->templet_configs) == true ? $configs->templet_configs : null;
}
$html = PHP_EOL.'<!-- BOARD MODULE -->'.PHP_EOL.'<div data-role="context" data-type="module" data-module="'.$this->getModule()->getName().'" data-base-url="'.($this->baseUrl == null ? $this->IM->getUrl(null,null,false) : $this->baseUrl).'" data-bid="'.$bid.'" data-view="'.$view.'" data-configs="'.GetString(json_encode($configs),'input').'">'.PHP_EOL;
$html.= $this->getHeader($bid,$configs);
switch ($view) {
case 'list' :
$html.= $this->getListContext($bid,$configs);
break;
case 'view' :
$html.= $this->getViewContext($bid,$configs);
break;
case 'write' :
$html.= $this->getWriteContext($bid,$configs);
break;
}
$html.= $this->getFooter($bid,$configs);
/**
* 컨텍스트 컨테이너를 설정한다.
*/
$html.= PHP_EOL.'</div>'.PHP_EOL.'<!--// BOARD MODULE -->'.PHP_EOL;
return $html;
}
/**
* 모듈 외부컨테이너를 가져온다.
*
* @param string $container 컨테이너명
* @return string $html 컨텍스트 HTML
*/
function getContainer($container) {
$html = $this->getContext($container);
$this->IM->addHeadResource('style',$this->getModule()->getDir().'/styles/container.css');
$this->IM->removeTemplet();
$footer = $this->IM->getFooter();
$header = $this->IM->getHeader();
return $header.$html.$footer;
}
/**
* 컨텍스트 헤더를 가져온다.
*
* @param string $bid 게시판 ID
* @param object $configs 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정
* @return string $html 컨텍스트 HTML
*/
function getHeader($bid,$configs=null) {
/**
* 템플릿파일을 호출한다.
*/
return $this->getTemplet($configs)->getHeader(get_defined_vars());
}
/**
* 컨텍스트 푸터를 가져온다.
*
* @param string $context 컨테이너 종류
* @param object $configs 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정
* @return string $html 컨텍스트 HTML
*/
function getFooter($context,$configs=null) {
/**
* 템플릿파일을 호출한다.
*/
return $this->getTemplet($configs)->getFooter(get_defined_vars());
}
/**
* 에러메세지를 반환한다.
*
* @param string $code 에러코드 (에러코드는 iModule 코어에 의해 해석된다.)
* @param object $value 에러코드에 따른 에러값
* @return $html 에러메세지 HTML
*/
function getError($code,$value=null) {
/**
* iModule 코어를 통해 에러메세지를 구성한다.
*/
$error = $this->getErrorText($code,$value,true);
return $this->IM->getError($error);
}
/**
* 게시물 목록 컨텍스트를 가져온다.
*
* @param string $bid 게시판 ID
* @param object $configs 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정
* @return string $html 컨텍스트 HTML
*/
function getListContext($bid,$configs=null) {
if ($this->checkPermission($bid,'list') == false) return $this->IM->getModule('member')->isLogged() == true ? $this->getError('FORBIDDEN') : $this->getError('REQUIRED_LOGIN');
$this->IM->setRobots('noindex, follow');
$board = $this->getBoard($bid);
if ($board->use_category != 'NONE') {
$categories = $this->db()->select($this->table->category)->where('bid',$bid)->get();
} else {
$categories = array();
}
$idxes = $this->getIdx() ? explode('/',$this->getIdx()) : array(1);
$category = null;
if (count($idxes) == 2) list($category,$p) = $idxes;
elseif (count($idxes) == 1) list($p) = $idxes;
if ($configs != null && isset($configs->category) == true && $configs->category != 0) {
$category = $configs->category;
$categories = array();
}
if ($configs != null && isset($configs->p) == true) {
$p = $configs->p;
}
$p = is_numeric($p) == true && $p > 0 ? $p : 1;
$limit = $board->post_limit;
$start = ($p - 1) * $limit;
$use_notice_category = isset($board->use_notice_category)?$board->use_notice_category:'NO';
$notice = $this->db()->select($this->table->post)->where('bid',$bid)->where('is_notice','TRUE');
if ($use_notice_category == 'YES' && $category != null && $category != 0) $notice->where('category',$category);
$notice = $notice->count();
if ($board->view_notice_count == 'INCLUDE') {
if ($board->view_notice_page == 'FIRST') {
if (ceil($notice / $limit) >= $p) {
$notices = $this->db()->select($this->table->post)->where('bid',$bid)->where('is_notice','TRUE');
if ($use_notice_category == 'YES' && $category != null && $category != 0) $notices->where('category',$category);
$notices = $notices->orderBy('idx','desc')->limit($start,$limit)->get();
$start = 0;
$limit = $limit - count($notices);
} else {
$notices = array();
$start = $start - $notice;
}
$lists = $this->db()->select($this->table->post)->where('bid',$bid)->where('is_notice','FALSE');
} elseif ($board->view_notice_page == 'ALL') {
$notices = $this->db()->select($this->table->post)->where('bid',$bid)->where('is_notice','TRUE');
if ($use_notice_category == 'YES' && $category != null && $category != 0) $notices->where('category',$category);
$notices = $notices->orderBy('idx','desc')->limit(0,$limit)->get();
$start = ($p - 1) * ($limit - count($notices));
$limit = $limit - count($notices);
$lists = $this->db()->select($this->table->post)->where('bid',$bid)->where('is_notice','FALSE');
}
} else {
if ($p == 1 || $board->view_notice_page == 'ALL') {
$notices = $this->db()->select($this->table->post)->where('bid',$bid)->where('is_notice','TRUE');
if ($use_notice_category == 'YES' && $category != null && $category != 0) $notices->where('category',$category);
$notices = $notices->orderBy('idx','desc')->limit($start,$limit)->get();
} else {
$notices = array();
}
$lists = $this->db()->select($this->table->post)->where('bid',$bid)->where('is_notice','FALSE');
}
if ($category != null && $category != 0) $lists->where('category',$category);
$keyword = Request('keyword');
$search_type = Request('search_type');
if ($keyword) {
if( $search_type) {
if ($search_type == 'title') {
$lists->where('title','%'.$keyword.'%','like');
} else if ($search_type == 'content') {
$lists->where('content','%'.$keyword.'%','like');
} else if ($search_type == 'auth') {
$midxes = $this->IM->getModule('member')->getSearchResults($keyword,'BOTH');
$lists->where('midx',$midxes,'IN');
}
} else {
$lists->where('(');
$lists = $this->IM->getModule('keyword')->getWhere($lists,array('title','search'),$keyword);
$midxes = $this->IM->getModule('member')->getSearchResults($keyword,'BOTH');
if (count($midxes) > 0) $lists->orWhere('midx',$midxes,'IN');
$lists->orWhere('name',$keyword);
$lists->where(')');
}
$this->IM->getModule('keyword')->mark($keyword,'div[data-module=board] span[data-role=title], div[data-module=board] *[data-role=name]');
}
$total = $lists->copy()->count();
$idx = 0;
if ($configs != null && isset($configs->idx) == true) {
$idx = $configs->idx;
}
$lists = $lists->orderBy('idx','desc')->limit($start,$limit)->get();
for ($i=0, $loop=count($notices);$i<$loop;$i++) {
if ($board->use_content_list !== true) unset($notices[$i]->content);
$notices[$i] = $this->getPost($notices[$i]);
$notices[$i]->category = $notices[$i]->category == 0 ? null : $this->getCategory($notices[$i]->category);
$notices[$i]->prefix = $notices[$i]->prefix == 0 ? null : $this->getPrefix($notices[$i]->prefix);
$notices[$i]->link = $this->getUrl('view',$notices[$i]->idx).$this->IM->getQueryString(array('p'=>$p,'category'=>count($categories) > 0 ? $category : null)).($notices[$i]->is_secret == true ? '#secret-'.$notices[$i]->idx : '');
}
$loopnum = $total - $start;
for ($i=0, $loop=count($lists);$i<$loop;$i++) {
if ($board->use_content_list !== true) unset($lists[$i]->content);
$lists[$i] = $this->getPost($lists[$i]);
$lists[$i]->loopnum = $loopnum - $i;
$lists[$i]->category = $lists[$i]->category == 0 ? null : $this->getCategory($lists[$i]->category);
$lists[$i]->prefix = $lists[$i]->prefix == 0 ? null : $this->getPrefix($lists[$i]->prefix);
$lists[$i]->link = $this->getUrl('view',$lists[$i]->idx).$this->IM->getQueryString(array('p'=>$p,'category'=>count($categories) > 0 ? $category : null)).($lists[$i]->is_secret == true ? '#secret-'.$lists[$i]->idx : '');
if ($keyword) {
$lists[$i]->title = '<span data-role="title">'.$lists[$i]->title.'</span>';
}
}
$pagination = $this->getTemplet($configs)->getPagination($p,ceil(($total + $notice)/$board->post_limit),$board->page_limit,$this->getUrl('list',($category == null ? '' : $category.'/').'{PAGE}'),$board->page_type);
$link = new stdClass();
$link->list = $this->getUrl('list',($category == null ? '' : $category.'/').$p);
$link->write = $this->getUrl('write',false);
$permission = new stdClass();
$permission->write = $this->checkPermission($board->bid,'post_write');
$header = PHP_EOL.'<form id="ModuleBoardListForm">'.PHP_EOL;
$footer = PHP_EOL.'</form>'.PHP_EOL.'<script>Board.list.init("ModuleBoardListForm");</script>'.PHP_EOL;
/**
* 템플릿파일을 호출한다.
*/
return $this->getTemplet($configs)->getContext('list',get_defined_vars(),$header,$footer);
}
/**
* 게시물 보기 컨텍스트를 가져온다.
*
* @param string $bid 게시판 ID
* @param object $configs 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정
* @return string $html 컨텍스트 HTML
*/
function getViewContext($bid,$configs=null) {
if ($this->checkPermission($bid,'view') == false) return $this->IM->getModule('member')->isLogged() == true ? $this->getError('FORBIDDEN') : $this->getError('REQUIRED_LOGIN');
$board = $this->getBoard($bid);
$idx = $this->getIdx();
$post = $this->getPost($idx);
if ($post == null) return $this->getError('NOT_FOUND_PAGE');
if ($post->is_secret == true && $this->checkPermission($bid,'post_secret') == false) {
if ($post->midx != 0 && $post->midx != $this->IM->getModule('member')->getLogged()) {
return $this->getError($this->getErrorText('FORBIDDEN'));
} elseif ($post->midx == 0) {
$password = Request('password');
$mHash = new Hash();
if ($mHash->password_validate($password,$post->password) == false) {
$context = $this->getError($this->getErrorText('INCORRENT_PASSWORD'));
$context.= PHP_EOL.'<script>Board.view.secret('.$idx.');</script>'.PHP_EOL;
return $context;
}
}
}
$this->IM->setRobots('index, nofollow');
$this->IM->setCanonical($this->getUrl('view',$idx));
$this->IM->setSiteTitle($post->title);
$this->IM->setViewTitle($post->title);
$this->IM->setViewDescription($post->content);
if ($post->image != null) $this->IM->setViewImage($post->image);
elseif ($post->image_url != null) $this->IM->setViewImage($post->image_url);
/**
* 조회수 증가
*/
$readed = is_array(Request('IM_BOARD_READED','session')) == true ? Request('IM_BOARD_READED','session') : array();
if (in_array($idx,$readed) == false) {
$readed[] = $idx;
$this->db()->update($this->table->post,array('hit'=>$this->db()->inc()))->where('idx',$idx)->execute();
$post->hit = $post->hit + 1;
$_SESSION['IM_BOARD_READED'] = $readed;
}
if ($this->IM->getModule('member')->isLogged() == true) {
$voted = $this->db()->select($this->table->activity)->where('type','post')->where('parent',$idx)->where('midx',$this->IM->getModule('member')->getLogged())->where('code',array('GOOD','BAD'),'IN')->getOne();
$post->voted = $voted == null ? null : $voted->code;
} else {
$post->voted = null;
}
$post->category = $post->category == 0 ? null : $this->getCategory($post->category);
$post->prefix = $post->prefix == 0 ? null : $this->getPrefix($post->prefix);
/**
* 첨부파일
*/
$attachments = $this->db()->select($this->table->attachment)->where('type','POST')->where('parent',$idx)->get();
for ($i=0, $loop=count($attachments);$i<$loop;$i++) {
$attachments[$i] = $this->IM->getModule('attachment')->getFileInfo($attachments[$i]->idx);
}
/**
* 댓글 컴포넌트를 불러온다.
*/
$ment = $this->getMentComponent($idx,null,$configs);
/**
* 현재 게시물이 속한 페이지를 구한다.
*/
$p = Request('p') && is_numeric(Request('p')) == true && Request('p') > 0 ? Request('p') : null;
$keyword = Request('keyword');
$category = $board->use_category == 'NONE' ? null : Request('category');
if ($configs != null && isset($configs->category) == true && $configs->category != 0) {
$category = null;
}
if ($p == null) {
if ($post->is_notice == true && $board->view_notice_page == 'FIRST') {
$p = 1;
} else {
$sort = Request('sort') ? Request('sort') : 'idx';
$dir = Request('dir') ? Request('dir') : 'asc';
$previous = $this->db()->select($this->table->post.' p','p.*')->where('p.bid',$post->bid)->where('p.'.$sort,$post->{$sort},$dir == 'desc' ? '<=' : '>=');
if ($keyword) $this->IM->getModule('keyword')->getWhere($previous,array('title','search'),$keyword);
if ($configs != null && isset($configs->category) == true && $configs->category != 0) {
$previous->where('category',$configs->category);
}
$previous = $previous->count();
$notice = $this->db()->select($this->table->post)->where('bid',$post->bid)->where('is_notice','TRUE')->count();
if ($board->view_notice_count == 'INCLUDE') {
if ($board->view_notice_page == 'FIRST') {
$p = ceil(($previous + $notice)/$board->post_limit);
} elseif ($board->view_notice_page == 'ALL') {
$p = ceil($previous/($board->post_limit - $notice));
}
} else {
$p = ceil($previous/$board->post_limit);
}
}
}
$link = new stdClass();
$link->list = $this->getUrl('list',($category == null ? '' : $category.'/').$p).$this->IM->getQueryString(array('p'=>null,'category'=>null,'keyword'=>($keyword ? urlencode($keyword) : null)));
$link->write = $this->getUrl('write',false);
$permission = new stdClass();
$permission->modify = $post->midx == $this->IM->getModule('member')->getLogged() || $this->checkPermission($post->bid,'post_modify') == true;
$permission->delete = $post->midx == $this->IM->getModule('member')->getLogged() || $this->checkPermission($post->bid,'post_delete') == true;
if ($keyword) {
$post->title = '<span data-role="title">'.$post->title.'</span>';
$this->IM->getModule('keyword')->mark($keyword,'div[data-module=board] span[data-role=title], div[data-module=board] div[data-role=wysiwyg-content]');
}
$header = PHP_EOL.'<div id="ModuleBoardView" data-idx="'.$idx.'">'.PHP_EOL;
$footer = PHP_EOL.'</div>'.PHP_EOL.'<script>Board.view.init("ModuleBoardView");</script>';
$configs = $configs == null ? new stdClass() : $configs;
$configs->idx = $idx;
$configs->p = $p;
// $footer.= $this->getListContext($bid,$configs);
/**
* 템플릿파일을 호출한다.
*/
return $this->getTemplet($configs)->getContext('view',get_defined_vars(),$header,$footer);
}
/**
* 게시물 작성 컨텍스트를 가져온다.
*
* @param string $bid 게시판 ID
* @param object $configs 사이트맵 관리를 통해 설정된 페이지 컨텍스트 설정
* @return string $html 컨텍스트 HTML
*/
function getWriteContext($bid,$configs=null) {
if ($this->checkPermission($bid,'post_write') == false) return $this->IM->getModule('member')->isLogged() == true ? $this->getError('FORBIDDEN') : $this->getError('REQUIRED_LOGIN');
$this->IM->addHeadResource('meta',array('name'=>'robots','content'=>'noidex,nofollow'));
$board = $this->getBoard($bid);
$idx = $this->getIdx();
if ($board->use_category != 'NONE') {
$categories = $this->db()->select($this->table->category)->where('bid',$bid)->orderBy('sort','asc')->get();
} else {
$categories = array();
}
if ($board->use_prefix == 'TRUE') {
$prefixes = $this->db()->select($this->table->prefix)->where('bid',$bid)->orderBy('sort','asc')->get();
} else {
$prefixes = array();
}
/**
* 게시물 수정
*/
if ($idx !== null) {
$post = $this->db()->select($this->table->post)->where('idx',$idx)->getOne();
if ($post == null) {
return $this->getError('NOT_FOUND_PAGE');
}
if ($this->checkPermission($bid,'post_modify') == false) {
if ($post->midx != 0 && $post->midx != $this->IM->getModule('member')->getLogged()) {
return $this->getError('FORBIDDEN');
} elseif ($post->midx == 0) {
$password = Request('password');
$mHash = new Hash();
if ($mHash->password_validate($password,$post->password) == false) {
$context = $this->getError($this->getErrorText('INCORRENT_PASSWORD'));
$context.= PHP_EOL.'<script>Board.view.modify('.$idx.');</script>'.PHP_EOL;
return $context;
}
}
}
$post->content = $this->IM->getModule('wysiwyg')->decodeContent($post->content,false);
} else {
$post = null;
}
$header = PHP_EOL.'<form id="ModuleBoardWriteForm" data-autosave="'.$bid.'-new">'.PHP_EOL;
$header.= '<input type="hidden" name="templet" value="'.$this->getTemplet($configs)->getName().'">'.PHP_EOL;
$header.= '<input type="hidden" name="bid" value="'.$bid.'">'.PHP_EOL;
if ($post !== null) $header.= '<input type="hidden" name="idx" value="'.$post->idx.'">'.PHP_EOL;
if ($configs != null && isset($configs->category) == true && $configs->category != 0) {
$categories = array();
$header.= '<input type="hidden" name="category" value="'.$configs->category.'">'.PHP_EOL;
}
$footer = PHP_EOL.'</form>'.PHP_EOL.'<script>Board.write.init("ModuleBoardWriteForm");</script>'.PHP_EOL;
$wysiwyg = $this->IM->getModule('wysiwyg')->setModule('board')->setName('content')->setRequired(true)->setContent($post == null ? '' : $post->content);
$uploader = $this->IM->getModule('attachment');
if ($board->use_attachment == true) {
if ($configs == null || isset($configs->attachment) == null || $configs->attachment == '#') {
$attachment_templet_name = $board->attachment->templet;
$attachment_templet_configs = $board->attachment->templet_configs;
} else {
$attachment_templet_name = $configs->attachment;
$attachment_templet_configs = isset($configs->attachment_configs) == true ? $configs->attachment_configs : null;
}
if ($attachment_templet_name != '#') {
$attachment_templet = new stdClass();
$attachment_templet->templet = $attachment_templet_name;
$attachment_templet->templet_configs = $attachment_templet_configs;
} else {
$attachment_templet = '#';
}
$uploader = $uploader->setTemplet($attachment_templet)->setModule('board')->setWysiwyg('content')->setDeleteMode('MANUAL');
if ($post != null) {
$uploader->setLoader($this->IM->getProcessUrl('board','getFiles',array('idx'=>Encoder(json_encode(array('type'=>'POST','idx'=>$post->idx))))));
}
} else {
$uploader = $uploader->disable();
}
/**
* 템플릿파일을 호출한다.
*/
return $this->getTemplet($configs)->getContext('write',get_defined_vars(),$header,$footer);
}
/**
* 게시물 댓글 컴포넌트
*
* @param int $parent 댓글을 달린 게시물 번호
* @param int $page 댓글 페이지
* @param object $configs 설정값
* @return string $html
*/
function getMentComponent($parent,$page,$configs) {
$post = $this->getPost($parent);
$board = $this->getBoard($post->bid);
if ($this->checkPermission($board->bid,'ment_write') == true) {
$form = $this->getMentWriteComponent($parent,$configs);
} else {
if ($post->ment == 0) return '';
$form = '';
}
$ment = $this->getMentListComponent($parent,null,$configs);
$pagination = $this->getMentPagination($parent,null,$configs);
$total = '<span data-role="count">'.$post->ment.'</span>';
$header = PHP_EOL.'<div data-role="ment" data-parent="'.$parent.'">'.PHP_EOL;
$footer = PHP_EOL.'</div>'.PHP_EOL;
$footer.= PHP_EOL.'<script>Board.ment.init('.$parent.');</script>'.PHP_EOL;
return $this->getTemplet($configs)->getContext('ment',get_defined_vars(),$header,$footer);
}
/**
* 댓글 목록 컴포넌트
*
* @param int $parent 댓글을 달린 게시물 번호
* @param int $page 댓글 페이지
* @param object $configs 설정값
* @return string $html
*/
function getMentListComponent($parent,$page,$configs) {
$post = $this->getPost($parent);
$board = $this->getBoard($post->bid);
$total = $this->db()->select($this->table->ment)->where('parent',$parent)->count();
$page = is_numeric($page) == false || $page == null || $page > max(1,ceil($total/$board->ment_limit)) ? max(1,ceil($total/$board->ment_limit)) : $page;
$start = ($page - 1) * $board->ment_limit;
$lists = $this->db()->select($this->table->ment_depth.' d','d.*,m.*')->join($this->table->ment.' m','d.idx=m.idx','LEFT')->where('d.parent',$parent)->orderBy('head','asc')->orderBy('arrange','asc')->limit($start,$board->ment_limit)->get();
$context = PHP_EOL.'<div data-role="list" data-page="'.$page.'">'.PHP_EOL;
for ($i=0, $loop=count($lists);$i<$loop;$i++) {
$context.= $this->getMentItemComponent($lists[$i],$configs);
}
if (count($lists) == 0) $context.= '<div class="empty">'.$this->getText('ment/empty').'</div>'.PHP_EOL;
$context.= PHP_EOL.'</div>'.PHP_EOL;
return $context;
}
/**
* 댓글 페이징 컴포넌트