PageRenderTime 110ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/ser-0.9.6/serweb-0.9.4/application_layer/apu_accounting.php

#
PHP | 390 lines | 219 code | 69 blank | 102 comment | 37 complexity | bd8862673ac7117410f5d1581d26db24 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?
  2. /**
  3. * Application unit accounting
  4. *
  5. * @author Karel Kozlik
  6. * @version $Id: apu_accounting.php,v 1.6.2.1 2005/09/01 15:17:37 kozlik Exp $
  7. * @package serweb
  8. */
  9. /** Application unit accounting
  10. *
  11. *
  12. * This application unit is used for view incoming and outgoing calls of user
  13. *
  14. * Configuration:
  15. * --------------
  16. * 'use_filter' (bool) default: false
  17. * if should be used filter for displaying incoming/outgoing/all calls
  18. *
  19. * 'filter_labels' (associative array)
  20. * text labels for filter select element.
  21. * Keys of array: all, incoming, outgoing
  22. *
  23. * 'display_incoming' (bool) default: true
  24. * have effect only when 'use_filter' is false
  25. * if is true, incoming calls are displayed
  26. *
  27. * notice: working only if users are indexed by UUID
  28. * '$config->users_indexed_by' option in config_data_layer.php
  29. *
  30. * 'display_outgoing' (bool) default: true
  31. * have effect only when 'use_filter' is false
  32. * if is true, outgoing calls are displayed
  33. *
  34. * 'display_missed' (bool) default: true
  35. * have effect only when 'use_filter' is false
  36. * if is true, missed calls are displayed
  37. *
  38. *
  39. * 'get_user_status' (bool) default: false
  40. * should output array contain status of user?
  41. *
  42. * 'get_phonebook_names' (bool) default: false
  43. * should output contain names from phonebook?
  44. *
  45. * 'convert_sip_addresses_to_phonenumbers' (bool) default: false
  46. * true if numerical username part of sip address from our domain should be
  47. * displayed without domain and initial 'sip:'
  48. *
  49. * 'cvs_export_template' (string) default: 'acc_cvs.tpl'
  50. * filename of smarty template for output in CSV format
  51. *
  52. * 'msg_delete' default: $lang_str['msg_calls_deleted_s'] and $lang_str['msg_calls_deleted_l']
  53. * message which should be showed on calls delete - assoc array with keys 'short' and 'long'
  54. *
  55. * 'form_name' (string) default: ''
  56. * name of html form
  57. *
  58. * 'smarty_url_delete' name of smarty variable - see below
  59. * 'smarty_url_cvs_export' name of smarty variable - see below
  60. * 'smarty_result' name of smarty variable - see below
  61. * 'smarty_form' name of smarty variable - see below
  62. * 'smarty_action' name of smarty variable - see below
  63. * 'smarty_pager' name of smarty variable - see below
  64. *
  65. * Exported smarty variables:
  66. * --------------------------
  67. * opt['smarty_url_delete'] (url_delete)
  68. * url for delete acc records of user
  69. *
  70. * opt['smarty_url_cvs_export'] (url_export)
  71. * url for get acc records in CSV format
  72. *
  73. * opt['smarty_form'] (form)
  74. * phplib html form
  75. *
  76. * opt['smarty_action'] (action)
  77. * tells what should smarty display. Values:
  78. * 'default' -
  79. *
  80. * opt['smarty_result'] (acc_res)
  81. * associative array containing accounting informations
  82. * The array have same keys as function get_acc_entries (from data layer) returned.
  83. * If convert_sip_addresses_to_phonenumbers is true, array extra contains key sip_address
  84. *
  85. * opt['smarty_pager'] (pager)
  86. * associative array containing size of result and which page is returned
  87. */
  88. class apu_accounting extends apu_base_class{
  89. var $smarty_action='default';
  90. var $acc_res;
  91. var $pager=array();
  92. var $reg;
  93. /* return required data layer methods - static class */
  94. function get_required_data_layer_methods(){
  95. global $config;
  96. if ($config->acc_use_cdr_table){
  97. return array('get_cdr_entries', 'delete_user_acc', 'delete_user_missed_calls');
  98. }
  99. else{
  100. return array('get_acc_entries', 'delete_user_acc', 'delete_user_missed_calls');
  101. }
  102. }
  103. /* return array of strings - required javascript files */
  104. function get_required_javascript(){
  105. return array("click_to_dial.js.php");
  106. }
  107. function apu_accounting(){
  108. global $lang_str;
  109. parent::apu_base_class();
  110. $this->opt['use_filter'] = false;
  111. $this->opt['filter_labels'] = array('all' => $lang_str['sel_item_all_calls'],
  112. 'incoming' => $lang_str['sel_item_incoming_cals'],
  113. 'outgoing' => $lang_str['sel_item_outgoing_calls']);
  114. $this->opt['display_incoming'] = true;
  115. $this->opt['display_outgoing'] = true;
  116. $this->opt['display_missed'] = true;
  117. $this->opt['get_user_status'] = false;
  118. $this->opt['get_phonebook_names'] = false;
  119. $this->opt['convert_sip_addresses_to_phonenumbers'] = false;
  120. $this->opt['cvs_export_template'] = 'acc_cvs.tpl';
  121. $this->opt['msg_delete']['short'] = &$lang_str['msg_calls_deleted_s'];
  122. $this->opt['msg_delete']['long'] = &$lang_str['msg_calls_deleted_l'];
  123. /* form */
  124. $this->opt['smarty_form'] = 'form';
  125. /* smarty action */
  126. $this->opt['smarty_action'] = 'action';
  127. /* name of html form */
  128. $this->opt['form_name'] = '';
  129. /*** names of variables assigned to smarty ***/
  130. /* array containing accounting table */
  131. $this->opt['smarty_result'] = 'acc_res';
  132. /* pager */
  133. $this->opt['smarty_pager'] = 'pager';
  134. $this->opt['smarty_url_delete'] = 'url_delete';
  135. $this->opt['smarty_url_cvs_export'] = 'url_export';
  136. }
  137. /* this metod is called always at begining */
  138. function init(){
  139. global $sess, $sess_acc_act_row, $sess_acc_filter;
  140. parent::init();
  141. if (!$sess->is_registered('sess_acc_act_row')) $sess->register('sess_acc_act_row');
  142. if (!$sess->is_registered('sess_acc_filter')) $sess->register('sess_acc_filter');
  143. if (!isset($sess_acc_act_row)) $sess_acc_act_row=0;
  144. if (!isset($sess_acc_filter)) $sess_acc_filter='all';
  145. if (isset($_GET['act_row'])) $sess_acc_act_row=$_GET['act_row'];
  146. $this->acc_res=array();
  147. $this->reg = new Creg();
  148. }
  149. function prepare_opt_param_for_get_acc_entries(){
  150. global $sess_acc_filter;
  151. $opt=array('get_phonebook_names' => $this->opt['get_phonebook_names'],
  152. 'get_user_status' => $this->opt['get_user_status']);
  153. if ($this->opt['use_filter']){
  154. switch ($sess_acc_filter){
  155. case 'incoming':
  156. $opt['filter_outgoing'] = false and $this->opt['display_outgoing'];
  157. $opt['filter_incoming'] = true and $this->opt['display_incoming'];
  158. $opt['filter_missed'] = true and $this->opt['display_missed'];
  159. break;
  160. case 'outgoing':
  161. $opt['filter_outgoing'] = true and $this->opt['display_outgoing'];
  162. $opt['filter_incoming'] = false and $this->opt['display_incoming'];
  163. $opt['filter_missed'] = false and $this->opt['display_missed'];
  164. break;
  165. case 'all':
  166. default:
  167. $opt['filter_outgoing'] = true and $this->opt['display_outgoing'];
  168. $opt['filter_incoming'] = true and $this->opt['display_incoming'];
  169. $opt['filter_missed'] = true and $this->opt['display_missed'];
  170. break;
  171. }
  172. }
  173. else {
  174. $opt['filter_outgoing'] = $this->opt['display_outgoing'];
  175. $opt['filter_incoming'] = $this->opt['display_incoming'];
  176. $opt['filter_missed'] = $this->opt['display_missed'];
  177. }
  178. return $opt;
  179. }
  180. function convert_sip_addresses_to_phonenumbers(){
  181. global $config;
  182. foreach($this->acc_res as $key => $val){
  183. $uname = $this->reg->get_username($val['to_uri']);
  184. $domain = $this->reg->get_domainname($val['to_uri']);
  185. if (ereg($this->reg->phonenumber_strict, $uname) and $domain == $config->domain)
  186. $this->acc_res[$key]['sip_address'] = $uname;
  187. else $this->acc_res[$key]['sip_address'] = $val['to_uri'];
  188. }
  189. }
  190. /* realize action */
  191. function action_default(&$errors){
  192. global $data, $sess_acc_act_row, $config;
  193. do{
  194. $this->controler->set_timezone();
  195. $data->set_act_row($sess_acc_act_row);
  196. $opt = $this->prepare_opt_param_for_get_acc_entries();
  197. if ($config->acc_use_cdr_table){
  198. if (false === $this->acc_res = $data->get_cdr_entries($this->user_id, $opt, $errors)) return false;
  199. }
  200. else {
  201. if (false === $this->acc_res = $data->get_acc_entries($this->user_id, $opt, $errors)) return false;
  202. }
  203. $this->pager['url']=$_SERVER['PHP_SELF']."?kvrk=".uniqid("")."&act_row=";
  204. $this->pager['pos']=$data->get_act_row();
  205. $this->pager['items']=$data->get_num_rows();
  206. $this->pager['limit']=$data->get_showed_rows();
  207. $this->pager['from']=$data->get_res_from();
  208. $this->pager['to']=$data->get_res_to();
  209. if ($this->opt['convert_sip_addresses_to_phonenumbers'])
  210. $this->convert_sip_addresses_to_phonenumbers();
  211. }while (false);
  212. }
  213. function action_export(&$errors){
  214. global $data, $smarty, $config;
  215. $this->controler->set_timezone();
  216. $opt = $this->prepare_opt_param_for_get_acc_entries();
  217. $opt['limit_query'] = false;
  218. if ($config->acc_use_cdr_table){
  219. if (false === $this->acc_res = $data->get_cdr_entries($this->user_id, $opt, $errors)) return false;
  220. }
  221. else {
  222. if (false === $this->acc_res = $data->get_acc_entries($this->user_id, $opt, $errors)) return false;
  223. }
  224. if ($this->opt['convert_sip_addresses_to_phonenumbers'])
  225. $this->convert_sip_addresses_to_phonenumbers();
  226. header("Content-Type: text/plain\n");
  227. header("Content-Transfer-Encoding: 8bit\n");
  228. Header("Content-Disposition: attachment;filename=calls.csv");
  229. $smarty->assign_by_ref($this->opt['smarty_result'], $this->acc_res);
  230. $smarty->display($this->opt['cvs_export_template']);
  231. page_close();
  232. exit;
  233. }
  234. function action_delete(&$errors){
  235. global $data, $config;
  236. $opt = array();
  237. if (isset($_GET['timestamp'])) $opt['timestamp'] = $_GET['timestamp'];
  238. if ($this->opt['display_incoming'] or $this->opt['display_outgoing']){
  239. $opt['del_incoming'] = $this->opt['display_incoming'];
  240. $opt['del_outgoing'] = $this->opt['display_outgoing'];
  241. if (false === $data->delete_user_acc($this->user_id, $opt, $errors)) return false;
  242. }
  243. /* if missed calls are displayed, delete its too */
  244. if ($this->opt['display_missed']){
  245. if (!$opt['timestamp']) $opt['timestamp']=null;
  246. if (false === $data->delete_user_missed_calls($this->user_id, $opt['timestamp'], $errors)) return false;
  247. }
  248. return array("m_acc_calls_deleted=".RawURLEncode($this->opt['instance_id']));
  249. }
  250. /* check _get and _post arrays and determine what we will do */
  251. function determine_action(){
  252. global $sess_acc_filter, $sess_acc_act_row;
  253. if (isset($_GET['acc_delete']) and $_GET['acc_delete'] == $this->opt['instance_id']){
  254. $this->action=array('action'=>"delete",
  255. 'validate_form'=>false,
  256. 'reload'=>true);
  257. return;
  258. }
  259. if (isset($_GET['acc_export']) and $_GET['acc_export'] == $this->opt['instance_id']){
  260. $this->action=array('action'=>"export",
  261. 'validate_form'=>false,
  262. 'reload'=>false,
  263. 'alone'=>true);
  264. return;
  265. }
  266. if ($this->was_form_submited()){ // Is there data to process?
  267. $sess_acc_filter = $_POST['filter'];
  268. $sess_acc_act_row = 0;
  269. }
  270. $this->action=array('action'=>"default",
  271. 'validate_form'=>false,
  272. 'reload'=>false);
  273. }
  274. /* create html form */
  275. function create_html_form(&$errors){
  276. global $sess_acc_filter;
  277. parent::create_html_form($errors);
  278. if ($this->opt['use_filter']){
  279. $opt = array(
  280. array('label' => $this->opt['filter_labels']['all'], 'value' => 'all'),
  281. array('label' => $this->opt['filter_labels']['incoming'], 'value' => 'incoming'),
  282. array('label' => $this->opt['filter_labels']['outgoing'], 'value' => 'outgoing')
  283. );
  284. $this->f->add_element(array("type"=>"select",
  285. "name"=>"filter",
  286. "size"=>1,
  287. "value"=>$sess_acc_filter,
  288. "options"=>$opt));
  289. }
  290. }
  291. /* add messages to given array */
  292. function return_messages(&$msgs){
  293. global $_GET;
  294. if (isset($_GET['m_acc_calls_deleted']) and $_GET['m_acc_calls_deleted'] == $this->opt['instance_id']){
  295. $msgs[]=&$this->opt['msg_delete'];
  296. $this->smarty_action="was_deleted";
  297. }
  298. }
  299. /* assign variables to smarty */
  300. function pass_values_to_html(){
  301. global $smarty, $sess;
  302. if(!$this->acc_res) $this->acc_res = array();
  303. $smarty->assign_by_ref($this->opt['smarty_pager'], $this->pager);
  304. $smarty->assign_by_ref($this->opt['smarty_result'], $this->acc_res);
  305. $smarty->assign($this->opt['smarty_url_delete'],
  306. $sess->url($_SERVER['PHP_SELF']."?kvrk=".uniqID("").
  307. "&acc_delete=".RawURLEncode($this->opt['instance_id']).
  308. "&timestamp=".time()));
  309. $smarty->assign($this->opt['smarty_url_cvs_export'],
  310. $sess->url($_SERVER['PHP_SELF']."?kvrk=".uniqID("").
  311. "&acc_export=".RawURLEncode($this->opt['instance_id'])));
  312. }
  313. /* return info need to assign html form to smarty */
  314. function pass_form_to_html(){
  315. return array('smarty_name' => $this->opt['smarty_form'],
  316. 'form_name' => $this->opt['form_name'],
  317. 'after' => '',
  318. 'before' => '');
  319. }
  320. }
  321. ?>