1<?php
2/******************************************************************************
3**
4**  action script related to anewssystem
5**  Action to display the archive page
6*/
7/******************************************************************************
8**  must run within Dokuwiki
9**/
10if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14
15/******************************************************************************/
16class action_plugin_anewssystem extends DokuWiki_Action_Plugin {
17
18    var $parameter = "";
19
20  /**
21   * return some info
22   */
23  function getInfo(){
24    return array(
25         'author' => 'Taggic',
26         'email'  => 'Taggic@t-online.de',
27         'date'   => '2016-06-02',
28         'name'   => 'News archive page (action plugin component)',
29         'desc'   => 'to show the News aechive alone on a page.',
30         'url'    => 'http://www.dokuwiki.org/plugin:anewssystem',
31         );
32  }
33/******************************************************************************
34**  Register its handlers with the dokuwiki's event controller
35*/
36     function register(Doku_Event_Handler &$controller) {
37         $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
38         $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, 'output', array());
39     }
40
41/******************************************************************************
42**  Handle the action
43*/
44     function _handle_act(&$event, $param) {
45         if($event->data !== 'shownewsarchive') { return; }
46            $event->preventDefault(); // https://www.dokuwiki.org/devel:events#event_object
47            return true;
48     }
49/******************************************************************************
50**  Generate output
51*/
52    function output(&$event, $param) {
53        if($event->data !== 'shownewsarchive') { return; }
54        global $ID;
55
56        $target       = $this->getConf('news_datafile');
57        $targetpage   = htmlspecialchars(trim($target));
58        $prefix       = 'anss';
59        $del          = 'anss_del';
60        $cut_prefx    = 'news_input_';
61        $allnewsdata1 = $this->getConf('news_output');
62        $allnewsdata  = wl( (isset($allnewsdata1) ? $allnewsdata1 : 'news:newsdata') );
63        $i            = strripos($allnewsdata, ":");
64        $news_root    = substr($allnewsdata, 0, $i);
65
66        // necessary for the back link of a show one article per page (SOAPP)
67        if(stripos($_GET['archive'],'archive')!== false) $ans_conf['param'] = $_GET['archive'];
68        $_GET['archive']="";
69
70        // 1. read template (plugins/anewssystem/template.php)
71        $template   = file_get_contents(DOKU_PLUGIN.'anewssystem/tpl/newstemplate.txt');
72      /*------- add news action part -----------------------------------------*/
73            $post_prefix   = $_POST["xs-".$prefix];
74            $delete_record = $_POST["anss_del_record"];
75            $delete_anchor = $_POST["anss_del_anchor"];
76
77          // date  ... consider all news of a defined month of a year (mm.yyyy, empty per default)
78          // qty   ... limits the number of news headlines starting with most recent (either integer or all, default:all)
79          // tag   ... consider all news where news article owns the given tag string (empty per default) tag delimiter is "|"
80          // style ... css style string as used in HTML (except quotation marks) for the outer element div
81          // class ... css style for usecase toc, page or box
82          // ho    ... headlinesonly will list the news headlines without timestamp and author (on/off, default: off)
83
84          // check if page ID was called with tag filter
85//          $tmp         .= ','.$_GET['tag'];    // this will overrule the page syntax setting
86          if(strlen($tmp)<2) {
87              // strip parameter to get set of add parameter
88              $tmp     = substr($ans_conf['param'],strlen('allnews'));
89          }
90          $split_array = explode(',',$tmp); // one or multiple tag filters: $prefs[1] ... [n]
91          $archive_options = array();
92
93          // split parameter into array with key and data
94          foreach ($split_array as $item) {
95            list($key, $value) = split("=",trim($item),2);
96            $archive_options = $archive_options + array($key => $value);
97          }
98
99          if(($archive_options['qty']=='') || ($archive_options['qty']<1)) $archive_options['qty']   = 'all';
100          if(array_key_exists('class',$archive_options) === false)         $archive_options['class'] = 'page';
101          if(array_key_exists('ho',$archive_options) === false)            $archive_options['ho']    = 'off';
102          $page        = wl( (isset($targetpage) ? $targetpage : 'news:newsdata') );
103
104          // load raw news file (e.g. news:newsdata.txt)
105          $av = 0;
106          $oldrecord = rawWiki($targetpage);
107
108          // split the news articles
109          $newsitems = explode("======",$oldrecord);
110          $info = array();
111
112          // get the headline level from config
113          $yh_level = $this->getConf('yh_level');
114          $mh_level = $this->getConf('mh_level');
115          $h_level = $this->getConf('h_level');
116
117          // 1. read news file (e.g. news:newsdata.txt)
118          foreach($newsitems as $article) {
119             // split news block into line items
120             $article_array = explode("\n  * ",$article);
121             unset($article_array[0]);
122
123             // 2. create output
124             // split line items into key and data
125             $aFlag = false;   // flag: start date value exists and start is not in future
126
127                 foreach ($article_array as $item) {
128                        list($key, $value) = split(":",trim($item),2);
129                        $tag_flag = false;
130                        if($key=='anchor') {
131                            $anchor = trim($value);
132                        }
133                        elseif(($key=='start') && strtotime(trim($value)) < time()) {
134                            $value = date($this->getConf('d_format'), strtotime($value));
135                            $news_date = '<span class="news_date_a"> ('. $value;
136                            // get month and year to compare with $archive_options['date']
137                            if(isset($archive_options['date']) && ($archive_options['date'] !== date('m.Y',strtotime($value)))) break;
138                            $aFlag = true;
139                        }
140                        // head has to be before the link in the template !
141                        elseif($key=='head'){
142                             $news_head = trim($value);
143                        }
144                        elseif($key=='subtitle'){
145                             $news_subtitle = '<br /><span class="news_subtitle">'.trim($value).'</span>'.NL;
146                        }
147                        elseif($key=='link'){
148                            $news_head = '<a href="'.$value.'" id="'.$value.'" name="'.$value.'">'. trim($news_head) .'</a>'.NL;
149                        }
150                        elseif($key=='author'){
151                            $news_date .= ', '. $value;
152                        }
153                        elseif(($key=='tags') && (isset($archive_options['tag']) !== false)) {
154//                            echo $value.'<br />';
155                            $tags = explode(',',$value);
156                            foreach($tags as $tag) {
157                                if(($tag!==false) && (stripos($archive_options['tag'],trim($tag))!==false)){
158                                    $tag_flag = true;
159                                    break;
160                                }
161                            }
162                        }
163                 }
164
165                 $news_date .=  ')</span>'.NL;
166
167                 if((isset($archive_options['tag']) === false) || (strlen($archive_options['tag']) <2)) $tag_flag = true;
168
169                 if (($aFlag === true) && ($tag_flag === true)) {
170                    //stop adding older news articles if quantity is reached
171//                    echo intval($archive_options['qty']).' >= '.$qty.'<br>';
172                    $qty++;
173                    if(($qty > intval($archive_options['qty'])) && ($archive_options['qty']!=='all')) break;
174
175                    // list all news stories as headline linked to the story itself
176                    $elt = explode(",",$news_date);
177                    $elt[0] = trim(strip_tags(str_replace('(','',$elt[0])));
178                    $elt[0] = date('F,Y',strtotime($elt[0]));
179                    list($new_month,$new_year) = explode(',',$elt[0]);
180
181                    // idea is that all stories are created one after the other
182                    // and the order within newsdata is according the start date
183                    // manipulation of Start/Perishing date possible but not expected
184                    // !!! There is no sort algorithm for year and month implemented !!!
185                    // to do such would lead into re-development of the plugin
186                    if(($old_year  !== $new_year) && (($archive_options['class']==='page') || ($archive_options['ho']==='off')))  {
187                      if(trim($old_year) !== '') $close_ytag = "</li></ul>".NL;
188                      $output .= $close_ytag.'<ul><li class="level1"><div class="li">'.$new_year.'</div><ul class="n_box">';
189                      $old_year  = $new_year;
190                    }
191
192                    if(($old_month  !== $new_month) && (($archive_options['class']==='page') || ($archive_options['ho']==='off'))) {
193                      if(trim($old_month) !== '') $close_mtag = "</li></ul>".NL;
194                      $output .= $close_mtag.'<ul><li class="level2"><div class="li">'.$new_month.'</div>';
195                      $old_month = $new_month;
196                    }
197
198                    if($archive_options['ho']==='on') $news_date='';
199                    else $news_date .= '<br />';
200
201                    if(($archive_options['tag']!==false) && ($archive_options['tag']!=='off') && ($archive_options['class']=='page')) $output .= '<div class="archive_item">'.trim($news_date).$news_head.$news_subtitle.'</div>'.NL;
202                    else $output .= '<ul><li class="level3"><div class="li">'.trim($news_date).$news_head.'</div></li></ul>'.NL;
203
204                    $close_ytag    = "";
205                    $close_mtag    = "";
206                    $anchor        = "";
207                    $news_date     = "";
208                    $news_head     = "";
209                    $news_subtitle = "";
210                    $tags          = "";
211                }
212          }
213          $blink_id = "news_items";
214          $img_ID   = "img_archive__toc";
215
216        $archive_lnkTitle = $this->getConf('lnk_newsarchive');
217        if($archive_lnkTitle=='') $archive_lnkTitle = "News Archive";
218
219        $backlink = '<a href="javascript:history.back(-1)">'.$this->getLang('lnk_back').'</a>';
220        $backlink .= '<span class="anss_sep"> &nbsp;|&nbsp;</span>
221                      <a href="'.DOKU_URL.'doku.php?id='.$this->getConf('news_output').'">'.$this->getLang('allnews').' &raquo;</a>';
222        $output = '<div class="backlinkDiv" style="font-size:.85em;">'.$backlink.'</div><br />'.NL.
223                  '<div class="archive_section" id="news_archive_head"  style="'.$archive_options['style'].'">
224                      <div id="news_items">
225                          '.$output.'
226                       </div>
227                   </div>'.NL.
228                  '<div class="backlinkDiv" style="font-size:.85em;">'.$backlink.'</div><br />'.NL;
229
230        echo $output;
231        $event->preventDefault();
232    }
233/******************************************************************************/
234}