1<?php
2/**
3 *@author    Myron Turner <turnermm02@shaw.ca>
4 DOKU_BASE + 'lib/plugins/numbering/scripts/getnum.php',
5 */
6
7if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../../').'/');
8if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
9define ('NUMBERING_GETNUM', DOKU_PLUGIN . 'numbering/scripts/getnum.php');
10define ('NUMBERING_ICON',  DOKU_REL . 'lib/plugins/numbering/sernum_2.png');
11
12require_once(DOKU_PLUGIN.'action.php');
13class action_plugin_numbering extends DokuWiki_Action_Plugin {
14   var $helper;
15        public function __construct()   {
16              $this->helper = plugin_load('helper', 'numbering');
17        }
18        public function register(Doku_Event_Handler $controller) {
19          $controller->register_hook('COMMON_WIKIPAGE_SAVE', 'BEFORE', $this, 'handle_save',array('before'));
20          $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, 'handle_read',array('before'));
21 	      $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this,'_ajax_call');
22          $controller->register_hook('DOKUWIKI_STARTED', 'AFTER',  $this, '_setjsinfo');
23        }
24
25        function _setjsinfo(Doku_Event $event, $param) {
26             global $JSINFO;
27             $JSINFO['nmbring_multi_db'] = $this->getConf('multi_db');
28          }
29        function handle_save(Doku_Event $event, $param) {
30         if ($event->data['revertFrom']) return;
31          if(!$event->data['contentChanged'] ) return;
32          if(strpos($event->data['newContent'], '~~GetNextNumber~~') === false) return;
33          $event->data['newContent'] = str_replace('~~GetNextNumber~~', $this->format_number(),$event->data['newContent']);
34        }
35
36        function handle_read(Doku_Event $event, $param){
37             global $num;
38             $num = 0;
39            if(strpos($event->data,'bureaucracy') == false) return;
40
41            $numfield = str_replace(',','|',$this->getConf('bureaucracy'));
42            $numfield = preg_replace("/\s+/","",$numfield );
43		  $event->data = preg_replace_callback(
44			'#<label>\s*<span>\s*('. $numfield .').*?</span>\s*(<input.*?\>)\s*</label>#',
45			function ($matches) {
46                  if(strpos($matches[0],'bureaucracy') == false) return $matches[0];
47                  global $num;
48                  $matches[2] = preg_replace('#class=\"edit.*?\"#', 'value = "" data-db="'. $matches[1] . '" id="' .'bureau_nmbr_' .  $num  .   '"',$matches[2]) ;
49                 $retv = '<label>' .$matches[1] .' ' . $matches[2].  '&nbsp;&nbsp;<img src="' . NUMBERING_ICON  . '" id = "bureau_num_' . $num .'" class = "numbering_clk">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>' ;
50                 $num++;
51                 return $retv;
52			},
53			$event->data
54		);
55		}
56
57	function _ajax_call(Doku_Event $event, $param) {
58
59       if ($event->data != 'numbr_bureau') return;
60
61       $event->stopPropagation();
62      $event->preventDefault();
63	   $num = $this->format_number() ;
64
65	  echo "$num";
66	}
67        function format_number(){
68          $padding =  $this->helper->getConfValue('padding');
69		  $len = (int)  $this->helper->getConfValue('pad_length');
70          $number = $this->getNextNumber();
71		  return  str_pad((string)$number, (int)$len, $padding, STR_PAD_LEFT);
72        }
73
74        function numberingDB() {
75         global $INPUT;
76         $db_name = $INPUT->str('data-db');
77         if($db_name) {
78             $db  = metaFN("numbering:$db_name",'.ser');
79         }
80         else  $db  = metaFN("numbering:seqnum",'.ser');
81
82        if(!file_exists($db)) {
83            io_saveFile($db,"", array());
84        }
85        return $db;
86      }
87
88        function getNextNumber() {
89            $db = $this->numberingDB();
90            $start = $this->helper->getConfValue('nstart');
91            io_lock($db);
92            $ar = unserialize(io_readFile($db,false));
93            if(!$ar) {
94                $ar['saved'] = $start;
95                $ar['start'] = $start;
96            }
97            else {
98                $number = $ar['saved'];
99                if($ar['start'] != $start) {
100                     $ar['start'] = $start;
101                     $number = $start;
102                }
103                else $number = $ar['saved'];
104            }
105            if($number < $start) $number = $start-1;
106            $ar['saved'] =  ++$number;
107
108            file_put_contents($db,serialize($ar));
109            io_unlock($db);
110            return "$number";
111        }
112
113function write_debug($data) {
114  return;
115  if (!$handle = fopen(DOKU_INC .'ajax.txt', 'a')) {
116    return;
117    }
118
119    // Write $somecontent to our opened file.
120    fwrite($handle, "$data\n");
121    fclose($handle);
122
123}
124}