b0c69931239eb467ad3a173ed40f6c423a5d4dd4
1 <?php
3 class fonreport extends plugin
4 {
5 /* Definitions */
6 var $plHeadline= "Phone Reports";
7 var $plDescription= "This does something";
9 /* For internal use */
10 var $start = 0;
11 var $search_for = "*";
12 var $search_base = "";
13 var $fields = array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
14 var $year = "";
15 var $month = "";
16 var $sort = 0;
17 var $sort_direction = "down";
18 var $report_list = array();
19 var $userfilter = "";
20 var $ui = NULL;
21 var $range = 20;
22 var $EntryPerPage = 20;
24 /* attribute list for save action */
25 var $attributes_SO = array("start","search_for","search_base","range","month","sort_direction","sort","year");
26 var $objectclasses = array();
29 /* Construct class */
30 function fonreport ($config, $ui)
31 {
32 /* Include config object */
33 $this->config = $config;
34 $this->ui = $ui;
35 $this->search_base= get_base_from_people($ui->dn);
37 $this->month = date("m");
38 $this->year = date("Y");
40 /* Use filter settings if we have already searched */
41 if (!is_global("fonfilter")){
42 $fonfilter = array();
43 foreach($this->attributes_SO as $name){
44 $fonfilter[$name]=$this->$name;
45 }
46 register_global("fonfilter", $fonfilter);
47 }else{
48 $fonfilter = get_global("fonfilter");
49 foreach($this->attributes_SO as $name){
50 $this->$name = $fonfilter[$name];
51 }
52 }
53 }
56 /* Save ui interactions and store results in session,
57 to remember settings */
58 function save_object()
59 {
60 $fonfilter= get_global("fonfilter");
61 if(isset($_POST['EntryPerPage'])){
62 $this->range = $_POST['EntryPerPage'];
63 }
64 if (isset($_GET['start'])){
65 $this->start= (int)$_GET['start'];
66 }
67 foreach( array("year", "month", "search_for", "search_base") as $type){
68 if (isset($_POST[$type])){
69 $this->$type= $_POST[$type];
70 }
71 }
73 /* Adapt sorting */
74 if (isset($_GET['sort'])){
75 if ($this->sort == (int)$_GET['sort']){
76 if ($this->sort_direction == "down"){
77 $this->sort_direction= "up";
78 } else {
79 $this->sort_direction= "down";
80 }
81 }
82 $this->sort= (int)$_GET['sort'];
83 if ($this->sort < 0 || $this->sort > 6){
84 $this->sort= 0;
85 }
86 }
88 /* remove unwanted tags */
89 $this->search_for = stripslashes(preg_replace("/[^0-9a-z]/i","",$this->search_for));
91 foreach($this->attributes_SO as $name){
92 $fonfilter[$name] = $this->$name;
93 }
94 register_global("fonfilter", $fonfilter);
95 }
98 /* Search & display results */
99 function execute()
100 {
101 /* Call parent execute */
102 plugin::execute();
104 /* GVet template engine */
105 $smarty= get_smarty();
108 /*****************
109 Variable Init
110 *****************/
112 $fields_str = "";
114 $months= array();
115 for($i = 1 ; $i <= 12 ; $i ++ ){
116 $months[$i] = _(date("F",gmmktime(0,0,0,$i)));
117 }
119 /* Prepare template */
120 $current= date("Y");
121 $years= array();
122 for ($y= $current - 5; $y<=$current; $y++){
123 $years[$y]= $y;
124 }
126 /*****************
127 Smarty
128 *****************/
130 foreach($this->fields as $field){
131 if(chkacl($this->acl,$field)==""){
132 $smarty->assign($field."ACL","");
133 }else{
134 $smarty->assign($field."ACL"," disabled ");
135 }
136 }
138 $smarty->assign("plug", "?plug=".validate($_GET['plug']));
139 $smarty->assign("launchimage", get_template_path('images/launch.png'));
140 $smarty->assign("search_image", get_template_path('images/search.png'));
141 $smarty->assign("search_for", $this->search_for);
142 $smarty->assign("bases", $this->config->idepartments);
143 $smarty->assign("base_select", $this->search_base);
144 $smarty->assign("months", $months);
145 $smarty->assign("month_select", $this->month);
146 $smarty->assign("years", $years);
147 $smarty->assign("year_select", $this->year);
148 $smarty->assign("search_result", "");
151 /*****************
152 Check Database , Table , Connection
153 *****************/
155 /* Connecting, selecting database */
156 if (!isset($this->config->data['SERVERS']['FON'])){
157 print_red(_("Can't connect to phone database, no reports can be shown!"));
158 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
159 }elseif(!is_callable("mysql_connect")){
160 print_red(_("There is no mysql extension available, please check your php setup."));
161 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
162 }else{
164 $cfg= $this->config->data['SERVERS']['FON'];
165 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
166 if ($link === FALSE){
167 print_red(_("Can't connect to phone database, no reports can be shown!"));
168 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
169 }
170 if (! @mysql_select_db("gophone")){
171 print_red(_("Can't select phone database for report generation!"));
172 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
173 }
174 }
177 /*****************
178 Get Query String && Search
179 *****************/
181 $query = $this->CreateQuerySyntax();
182 $cfg = $this->config->data['SERVERS']['FON'];
183 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
185 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,$query, "Database query");
187 $result = @mysql_query($query);
188 if ($result === false){
189 print_red(_("Query for phone database failed!"));
190 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
191 }
193 /*****************
194 Fetch results
195 *****************/
197 $report_list= array();
198 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
199 $append_str = "";
200 $hour= substr($line["calldate"], 11, 2);
201 $minute=substr($line["calldate"], 14, 2);
202 $format= _("Y-M-D");
203 $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
204 $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
205 $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
206 $append_str .= "<td>$date $hour:$minute</td>";
207 foreach(array("src","dst","channel","lastapp","disposition") as $atr){
208 if(isset($line[$atr])){
209 $append_str .= "<td>".$line[$atr]."</td>";
210 }
211 }
212 if(isset($line['duration'])){
213 $append_str .= "<td>".$this->gen_duration($line["duration"])."</td>";
214 }
215 $report_list[] = $append_str;
216 }
218 $this->report_list= $report_list;
219 @mysql_close($link);
221 /* Generate output */
222 $mod= 0;
225 /*****************
226 Create list of results
227 *****************/
229 $output= "";
230 foreach ($this->report_list as $val){
231 if ($mod < $this->start) {
232 $mod++;
233 continue;
234 }
235 if ($mod >= ($this->start + $this->range)){
236 $mod++;
237 break;
238 }
239 if ( ($mod++) & 1){
240 $col= "background-color: #ECECEC;";
241 } else {
242 $col= "background-color: #F5F5F5;";
243 }
244 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
245 }
248 /*****************
249 Tell smarty what we have found
250 *****************/
252 if ($output != ""){
253 $smarty->assign("search_result", $output);
254 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
255 } else {
256 $smarty->assign("search_result", "");
257 }
259 /* Show main page */
260 for($i= 0; $i<7; $i++){
261 $smarty->assign("mode$i", "");
262 }
263 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.".png\" border=0 align=middle>");
265 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
266 }
269 /* Create duration string 12'11" */
270 function gen_duration($seconds)
271 {
272 if ($seconds / 60 > 1){
273 $minutes= (int)($seconds / 60);
274 $seconds= $seconds % 60;
275 return ("$minutes’$seconds”");
276 }
277 return ("$seconds”");
278 }
281 /* Create WHERE part for our mysql serach */
282 function GetUidMatchingFilter()
283 {
284 $ldap = $this->config->get_ldap_link();
285 $ldap->cd ($this->search_base);
286 $s = $this->search_for;
288 $s = preg_replace("/\%/","",$s);
289 $s2 = preg_replace("/\*/","%",$s);
291 /* Perform ldap search for potential users */
292 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))(objectClass=gofonAccount)".
293 "(|(uid=$s)(l=$s)(homePhone=$s)(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
294 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)(title=$s)))" ,array("uid"));
295 $str = " AND (";
296 $fields = array("dstchannel","channel");
297 if($ldap->count()){
298 while ($attrs= $ldap->fetch()){
299 $uid = $attrs["uid"][0];
300 foreach($fields as $name){
301 $str .= $name." like '%".$uid."%' OR ";
302 }
303 }
304 }
305 $str .= " dst like '".$s2."' OR src like '".$s2."' OR lastapp like '".$s2."')";
306 return($str);
307 }
310 /* Create query string */
311 function CreateQuerySyntax()
312 {
313 /* Get extended search filter which contain uids and so on */
314 $uidstring = $this->GetUidMatchingFilter();
316 /* Create string with all fields seperated by ,*/
317 $fields_str ="";
318 foreach($this->fields as $field){
319 if($field == "calldate") continue;
320 if(chkacl($this->acl,$field)==""){
321 $fields_str .= $field.", ";
322 }
323 }
324 $fields_str = preg_replace("/, $/","",$fields_str);
326 /* Create Sort tag */
327 if ($this->sort_direction == "down"){
328 $desc= "DESC";
329 } else {
330 $desc= "ASC";
331 }
332 /* Create times */
333 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
334 $end= date ("YmdHis", mktime(23,59,59,($this->month +1),0,$this->year));
335 $query = "SELECT ".$fields_str.",calldate FROM cdr ".
336 "WHERE
337 calldate <= $end
338 AND
339 calldate >= $start
340 ". $uidstring."
341 ORDER BY ".$this->fields[$this->sort]." $desc;";
342 return($query);
343 }
344 }
345 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
346 ?>