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 $year= "";
14 var $month= "";
15 var $sort= 0;
16 var $sort_direction= "down";
17 var $report_list= array();
18 var $userfilter= "";
19 var $ui= NULL;
20 var $fields= array("calldate", "src", "dst", "channel", "lastapp", "disposition", "duration");
21 var $range = 20;
23 /* attribute list for save action */
24 var $attributes= array();
25 var $objectclasses= array();
27 function fonreport ($config, $ui)
28 {
29 /* Include config object */
30 $this->config= $config;
31 $this->ui= $ui;
33 /* Try to get matching search base for user provided
34 by 'dn' */
35 $tmp= preg_replace ("/^[^,]+,[^,]+,/", "", $ui->dn);
36 $sb= array_search($tmp, $this->config->departments);
37 if ($sb === FALSE){
38 $sb= "/";
39 }
40 $this->search_base= $sb;
42 /* Get global filter config */
43 if (!is_global("fonfilter")){
44 $ui= get_userinfo();
45 $base= get_base_from_people($ui->dn);
46 $fonfilter= array("year" => date("Y"),
47 "month" => date("m"),
48 "search_base" => $base,
49 "search_for" => "*");
50 register_global("fonfilter", $fonfilter);
51 }
52 }
54 function execute()
55 {
56 /* Call parent execute */
57 plugin::execute();
59 /* Get template engine */
60 $smarty= get_smarty();
62 $months= array();
63 $months[1]= _("January"); $months[2]= _("February"); $months[3]= _("March"); $months[4]= _("April");
64 $months[5]= _("May"); $months[6]= _("June"); $months[7]= _("July"); $months[8]= _("August");
65 $months[9]= _("September"); $months[10]= _("October"); $months[11]= _("November"); $months[12]= _("December");
67 /* Prepare template */
68 $current= date("Y");
69 $years= array();
70 for ($y= $current - 5; $y<=$current; $y++){
71 $years[]= $y;
72 }
74 $smarty->assign("plug" , "?plug=".validate($_GET['plug']));
75 $smarty->assign("launchimage" , get_template_path('images/launch.png'));
76 $smarty->assign("search_image" , get_template_path('images/search.png'));
77 $smarty->assign("search_for" , $this->search_for);
78 $smarty->assign("bases" , $this->config->idepartments);
79 $smarty->assign("base_select" , $this->search_base);
80 $smarty->assign("months" , $months);
81 $smarty->assign("month_select" , $this->month);
82 $smarty->assign("years" , $years);
83 $smarty->assign("year_select" , $this->year);
84 $smarty->assign("search_result" , "");
86 /* Connecting, selecting database */
87 if (!isset($this->config->data['SERVERS']['FON'])){
88 print_red("There is no asterisk database defined. Can't query anything.");
89 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
90 }elseif(!is_callable("mysql_connect")){
91 print_red("There is no mysql extension available. Can't establish database connection.");
92 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
93 }else{
95 $cfg= $this->config->data['SERVERS']['FON'];
96 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
97 if ($link === FALSE){
98 print_red(_("Can't connect to phone database, no reports can be shown!. Check server,username and password combination."));
99 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
100 }
101 if (! @mysql_select_db("gophone")){
102 print_red(_("Can't select phone (gophone) database for report generation!"));
103 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
104 }
105 }
108 if(isset($_POST['EntryPerPage'])){
109 $this->range = $_POST['EntryPerPage'];
110 }
112 $fonfilter= get_global("fonfilter");
113 foreach( array("year", "month", "search_for", "search_base") as $type){
114 if (isset($_POST[$type])){
115 $fonfilter[$type]= $_POST[$type];
116 }
117 $this->$type= $fonfilter[$type];
118 }
119 register_global("fonfilter", $fonfilter);
121 /* Adapt sorting */
122 if (isset($_GET['sort'])){
123 if ($this->sort == (int)$_GET['sort']){
124 if ($this->sort_direction == "down"){
125 $this->sort_direction= "up";
126 } else {
127 $this->sort_direction= "down";
128 }
129 }
130 $this->sort= (int)$_GET['sort'];
131 if ($this->sort < 0 || $this->sort > 6){
132 $this->sort= 0;
133 }
134 }
136 /* Search button has been pressed */
137 if ($this->search_for != ""){
138 $this->start= 0;
140 if (is_integer (strpos($this->search_for, "*"))){
141 $s= $this->search_for;
142 } else {
143 $s= "*".$this->search_for."*";
144 }
145 $ldap= $this->config->get_ldap_link();
146 $ldap->cd ($this->search_base);
148 /* Perform ldap search for potential users */
149 $ldap->search ("(&(objectClass=gosaAccount)(!(objectClass=gosaUserTemplate))".
150 "(objectClass=gofonAccount)".
151 "(|(uid=$s)(l=$s)(homePhone=$s)".
152 "(telephoneNumber=$s)(facsimileTelephoneNumber=$s)(mobile=$s)".
153 "(pager=$s)(cn=$s)(givenName=$s)(sn=$s)(personalTitle=$s)".
154 "(title=$s)))");
156 $fon_users= array();
157 while ($attrs= $ldap->fetch()){
158 $acl= get_permissions ($ldap->getDN(), $this->ui->subtreeACL);
159 $acl= get_module_permission($acl, "fax", $ldap->getDN());
161 if (chkacl ($acl, "faxReport") == ""){
162 $fax_users[]= $attrs["uid"][0];
163 }
164 }
166 /* Prepare SQL query */
167 $this->userfilter= "";
168 foreach ($fon_users as $user){
169 $this->userfilter.= "uid = '$user' OR ";
170 }
171 $this->userfilter= preg_replace("/OR $/", "", $this->userfilter);
172 }
174 /* Perform SQL query */
175 ##### FIXME ACL, FILTER ######
176 # if ($this->userfilter){
177 if ($this->sort_direction == "down"){
178 $desc= "DESC";
179 } else {
180 $desc= "";
181 }
182 $start= date ("YmdHis", mktime(0,0,0,$this->month,1,$this->year));
183 $end= date ("YmdHis", mktime(23,59,59,$this->month+1,0,$this->year));
184 $query = "SELECT calldate, channel, src, clid, lastapp, lastdata, dst, ".
185 "disposition, duration FROM cdr ".
186 "WHERE calldate <= $end AND ".
187 "calldate >= $start ORDER BY ".$this->fields[$this->sort]." $desc;";
189 $cfg= $this->config->data['SERVERS']['FON'];
190 $link = @mysql_pconnect($cfg['SERVER'], $cfg['LOGIN'], $cfg['PASSWORD']);
192 @DEBUG (DEBUG_MYSQL, __LINE__, __FUNCTION__, __FILE__,
193 $query, "Database query");
195 $result = @mysql_query($query);
196 if ($result === false){
197 print_red(_("Query for phone database failed!"));
198 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
199 }
200 $report_list= array();
201 while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
202 $hour= substr($line["calldate"], 11, 2);
203 $minute=substr($line["calldate"], 14, 2);
204 $format= _("Y-M-D");
205 $date= preg_replace("/Y/", substr($line["calldate"], 0, 4), $format);
206 $date= preg_replace("/M/", substr($line["calldate"], 5, 2), $date);
207 $date= preg_replace("/D/", substr($line["calldate"], 8, 2), $date);
209 $report_list[]= "<td>$date $hour:$minute</td>".
210 "<td>".$line["src"]."</td>".
211 "<td>".$line["dst"]."</td>".
212 "<td>".$line["channel"]."</td>".
213 "<td>".$line["lastapp"]."</td>".
214 "<td>".$line["disposition"]."</td>".
215 "<td>".$this->gen_duration($line["duration"])."</td>";
216 }
218 $this->report_list= $report_list;
219 @mysql_close($link);
220 # }
222 /* Generate output */
223 $mod= 0;
224 if (isset($_GET['start'])){
225 $this->start= (int)$_GET['start'];
226 }
228 $output= "";
229 foreach ($this->report_list as $val){
230 if ($mod < $this->start) {
231 $mod++;
232 continue;
233 }
234 if ($mod >= ($this->start + $this->range)){
235 $mod++;
236 break;
237 }
238 if ( ($mod++) & 1){
239 $col= "background-color: #ECECEC;";
240 } else {
241 $col= "background-color: #F5F5F5;";
242 }
243 $output.= "<tr style=\"height:22px; $col\">$val</tr>";
244 }
247 if ($output != ""){
248 $smarty->assign("search_result", $output);
249 $smarty->assign("range_selector", range_selector(count($this->report_list), $this->start,$this->range,"EntryPerPage"));
250 } else {
251 $smarty->assign("search_result", "");
252 }
254 /* Show main page */
255 for($i= 0; $i<7; $i++){
256 $smarty->assign("mode$i", "");
257 }
258 $smarty->assign("mode".$this->sort, "<img alt=\"\" src=\"images/sort_".$this->sort_direction.
259 ".png\" border=0 align=middle>");
260 return($smarty->fetch(get_template_path('contents.tpl', TRUE)));
261 }
263 function gen_duration($seconds)
264 {
265 if ($seconds / 60 > 1){
266 $minutes= (int)($seconds / 60);
267 $seconds= $seconds % 60;
268 return ("$minutes’$seconds”");
269 }
271 return ("$seconds”");
272 }
274 }
276 ?>