960c4368252520ce619f205660ae43f0cb395916
[atutor.git] / mods / wiki / plugins / aview / posts.php
1 <?php
2
3 #  this plugin appends a class of wiki pages to the bottom of the
4 # current page this will grow/merge into or support the forums
5 # plugin
6 #
7 #  you could alternatively define EWIKI_AUTOVIEW to 0, and call the
8 #  ewiki_posts() wrapper function anywhere on yoursite.php
9
10 #-- text
11 $ewiki_t["en"] = array_merge($ewiki_t["en"], array(
12    "EDITTHISPOST" => "Edit This Post",
13    "INFOABOUTPOST" => "Information about post",
14    "COMPVERPOST" => "See Changes"
15    ));
16
17 #-- entitle actions
18 $ewiki_config['posts_action_links'] = array_merge(array(
19     "edit" => "EDITTHISPOST",
20     "info" => "INFOABOUTPOST",
21     "diff" => "COMPVERPOST",
22
23 ), $ewiki_config['posts_action_links']);
24
25 if (!defined("EWIKI_AUTOVIEW") || EWIKI_AUTOVIEW) {
26    $ewiki_plugins["view_append"]['view_append_posts'] = "ewiki_view_append_posts";
27 }
28
29 $ewiki_t["en"]["POSTS"] = "posts";
30 $ewiki_t["en"]["POST"] = "Post: ";
31 $ewiki_t["en"]["ADDPOST"] = "Add a post";
32 $ewiki_t["en"]["TOO_MANY_POSTS"] = 'We are sorry.  The maximum number of posts has been exceeded.<br /><br />Return to [$id]';
33
34 $ewiki_config["action_links"]["view"]["addpost"] =  $ewiki_t["en"]["ADDPOST"];
35
36 $ewiki_plugins["action"]["addpost"] = "ewiki_add_post";
37 $ewiki_plugins["handler"][] = 'ewiki_adjust_thread_controls';
38 $ewiki_plugins["handler"][] = 'ewiki_view_post_parent';
39
40 define ('EWIKI_POST_SEPARATOR','_POST');
41 define("EWIKI_POSTID_PARSE_REGEX",'#(.*)'.preg_quote(EWIKI_POST_SEPARATOR) .'(\d{3})'.'#');
42 define ('EWIKI_VIEWPOSTACTION','view');
43
44 function ewiki_view_post_parent($id, $data, $action){
45     global $ewiki_plugins;
46
47     if(!preg_match(EWIKI_POSTID_PARSE_REGEX,$id,$matches)||($action!='view'))
48         return;
49         
50     //Authentication for the new page is handled within this call.
51     $o=ewiki_page('view/'.$matches[1]);
52     
53     //page_final plugins have been run, unset them
54         unset($ewiki_plugins["page_final"]);    
55     
56     return($o);
57 }
58
59 function ewiki_adjust_thread_controls($id, $data, $action){
60         global $ewiki_config;
61
62         if($action!='view')
63                 return;
64         if(!posts_exist())
65                 return;
66                 
67         //Move add post action to first position in control links
68         $temp = $ewiki_config["action_links"]['view']['addpost'];
69         unset($ewiki_config["action_links"]['view']['addpost']);
70         $ewiki_config["action_links"]['view'] = array_merge(array('addpost'=>$temp),$ewiki_config["action_links"]['view']);
71 }
72
73 function ewiki_add_post($id, $data, $action){
74     global $ewiki_plugins,$ewiki_config;
75     
76     $postNum=getPostCount($id)+1; 
77     $postNum=str_pad($postNum, 3, "0", STR_PAD_LEFT);
78     
79     if($postNum>999){
80         $o=ewiki_format(ewiki_t("TOO_MANY_POSTS", array("id"=>$id)));
81     } else {
82         $id=$id.EWIKI_POST_SEPARATOR.($postNum);
83         $ewiki_config["create"] = $id;
84         //echo("Calling edit function $id");
85         $o=$ewiki_plugins["action"]["edit"]($id,array("id"=>$id) ,$action);
86     }
87     
88     return($o);
89 }
90
91 function ewiki_view_append_posts($id, $data, $action) {
92         global $ewiki_plugins,$ewiki_config;
93
94 ## Since this function calls ewiki_page_view it must disable itself when it is called
95 ##  I do this by having it set separate view_append and view final actions for posts
96 ##  but at a minimum it removes itself
97
98    $std_view_append=$ewiki_plugins["view_append"];
99    $std_view_final=$ewiki_plugins["view_final"];
100    $std_action_links=$ewiki_config["action_links"]["view"];
101    $thread_page_title=$GLOBALS["ewiki_title"];
102
103    if(!isset($ewiki_config['posts_view_append'])){
104         unset($ewiki_plugins["view_append"]['view_append_posts']);
105    }else{
106         $ewiki_plugins["view_append"]=$ewiki_config['posts_view_append'];
107    }
108    
109    if(isset($ewiki_config['posts_view_final'])){
110         $ewiki_plugins["view_final"]=$ewiki_config['posts_view_final'];
111    }
112    
113    if(!isset($ewiki_config['posts_action_links'])){
114         unset($GLOBALS['ewiki_config']["action_links"]["view"]["addpost"]);
115    }else{
116         $ewiki_config["action_links"]["view"]=$ewiki_config['posts_action_links'];
117    }
118
119     unset($_REQUEST["thankyou"]);               // Prevent thank you message from appearing in the menu
120                                                 //  bar
121
122 /**
123  * Code for the new database layer */
124    $result = ewiki_db::SEARCH("id", $id.'_POST');
125
126         #sort by post number
127    $ord = array();
128    while ($row = $result->get()) {
129      if(preg_match('#_POST(\d{3})#', $row["id"],$matches = array())){
130        $ord[$matches[1]] = $row['id'];
131       }
132    }
133    asort($ord);
134    
135    foreach ($ord as $postNum => $id) {
136         $GLOBALS["ewiki_title"]=ewiki_t('POST').$postNum;
137
138         $row = ewiki_db::GET($id);
139         
140         #-- require auth
141         if (EWIKI_PROTECTED_MODE) {
142           if (!ewiki_auth($id, $row, EWIKI_VIEWPOSTACTION, $ring=false, $force=0)) {
143              continue;
144           }
145         }
146         
147         $postOut = ewiki_page_view($id, $row, EWIKI_VIEWPOSTACTION);
148         ewiki_page_css_container($postOut , $id, $row, $oo=EWIKI_VIEWPOSTACTION);        
149         $o.=$postOut ;
150    }
151
152 ## Restore normal actions
153         $ewiki_plugins["view_append"]=   $std_view_append;
154         $ewiki_plugins["view_final"]=   $std_view_final;
155         $ewiki_config["action_links"]["view"]=$std_action_links;
156         $GLOBALS["ewiki_title"]=$thread_page_title;
157         
158    return("<div class='wiki aview posts'>".$o.'</div>');
159 }
160
161 function isPost($id){
162         return(preg_match(EWIKI_POSTID_PARSE_REGEX,$id));
163 }
164 function getPostNumber($id){
165         preg_match(EWIKI_POSTID_PARSE_REGEX,$id,$match=array());
166         return($match[2]);
167 }
168
169 function getPostCount($id){
170 /**
171  * Code for the new database layer*/
172         #sort by post number
173    $ord = array();
174
175    $scan =  $id.EWIKI_POST_SEPARATOR; //Should I escape $id here?
176    $result = ewiki_db::SEARCH("id", $scan);
177    while ($row = $result->get()) {
178         preg_match('#_POST(\d{3})#', $row["id"],$matches=array($matches));
179         $ord[$row["id"]] = $matches[1];
180    }
181    rsort($ord);
182    return($ord[0]);
183 }
184
185 function posts_exist($always=false) {
186     $id = $GLOBALS["ewiki_id"];
187     
188     $result = $always || ($id)
189         && ($result = ewiki_db::SEARCH("id", $id.'_POST'))
190         && ($result->count());
191     return( ($id) && ($id != EWIKI_PAGE_CALENDAR) && ($id != EWIKI_PAGE_YEAR_CALENDAR)
192         && !isPost($id)
193         && empty($_REQUEST["year"]) && $result );
194 }
195
196 ?>