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