1: <?php
2: /**
3: * Operator_Driver:: defines an API for implementing storage backends for
4: * Operator.
5: *
6: * Copyright 2007-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (GPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/gpl.
10: *
11: * @author Your Name <you@example.com>
12: * @package Operator
13: */
14: class Operator_Driver {
15:
16: /**
17: * Search the database for call detail records, taking permissions into
18: * consideration.
19: *
20: * @return array [0] contains summary statistics; [1] is an array of the
21: * actual call records.
22: * @throws Operator_Exception
23: */
24: public function getRecords($start, $end, $accountcode = null, $dcontext = null,
25: $rowstart = 0, $rowlimit = 100)
26: {
27: // Start Date
28: if (!is_a($start, 'Horde_Date')) {
29: $start = new Horde_Date($start);
30: }
31:
32: // End Date
33: if (!is_a($end, 'Horde_Date')) {
34: $end = new Horde_Date($end);
35: }
36:
37: if ($start->compareDate($end) > 0) {
38: throw new Operator_Exception(_("\"Start\" date must be on or before \"End\" date."));
39: }
40:
41: if (empty($accountcode) || $accountcode == '%') {
42: $permentry = 'operator:accountcodes';
43: } else {
44: $permentry = 'operator:accountcodes:' . $accountcode;
45: }
46:
47: $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
48: if ($GLOBALS['registry']->isAdmin() ||
49: $perms->hasPermission('operator:accountcodes',
50: $GLOBALS['registry']->getAuth(),
51: Horde_Perms::READ) ||
52: $perms->hasPermission($permentry, $GLOBALS['registry']->getAuth(),
53: Horde_Perms::READ)) {
54: return $this->_getRecords($start, $end, $accountcode, $dcontext,
55: $rowstart, $rowlimit);
56: }
57: throw new Operator_Exception(_("You do not have permission to view call detail records for that account code."));
58: }
59:
60: /**
61: * Get summary call statistics per-month for a given time range, account and
62: * destination.
63: *
64: * @param Horde_Date startdate Start of the statistics window
65: * @param Horde_Date enddate End of the statistics window
66: * @param string accountcode Name of the accont for statistics. Defaults
67: * to null meaning all accounts.
68: * @param string dcontext Destination of calls. Defaults to null.
69: *
70: *
71: * @return array Array of call statistics. The key of each
72: * element is the month name in date('Y-m')
73: * format and the value being an array of
74: * statistics for calls placed that month.
75: * @throws Operator_Exception|Horde_Date_Exception
76: */
77: public function getMonthlyCallStats($start, $end, $accountcode = null,
78: $dcontext = null){
79: if (empty($accountcode) || $accountcode == '%') {
80: $permentry = 'operator:accountcodes';
81: } else {
82: $permentry = 'operator:accountcodes:' . $accountcode;
83: }
84: $perms = $GLOBALS['injector']->getInstance('Horde_Perms');
85: if ($GLOBALS['registry']->isAdmin() ||
86: $perms->hasPermission('operator:accountcodes',
87: $GLOBALS['registry']->getAuth(),
88: Horde_Perms::READ) ||
89: $perms->hasPermission($permentry, $GLOBALS['registry']->getAuth(),
90: Horde_Perms::READ)) {
91: return $this->_getMonthlyCallStats($start, $end, $accountcode,
92: $dcontext);
93: }
94:
95: throw new Operator_Exception(_("You do not have permission to view call detail records for that account code."));
96: }
97:
98: /**
99: * Attempts to return a concrete Operator_Driver instance based on $driver.
100: *
101: * @param string $driver The type of the concrete Operator_Driver subclass
102: * to return. The class name is based on the
103: * storage driver ($driver). The code is
104: * dynamically included.
105: *
106: * @param array $params A hash containing any additional configuration
107: * or connection parameters a subclass might need.
108: *
109: * @return Operator_Driver The newly created concrete Operator_Driver
110: * instance, or false on an error.
111: */
112: public function factory($driver = null, $params = null)
113: {
114: if ($driver === null) {
115: $driver = $GLOBALS['conf']['storage']['driver'];
116: }
117: $driver = basename($driver);
118:
119: if (is_null($params)) {
120: // Since we have more than one backend that uses SQL make sure
121: // all of them have a chance to inherit the site-wide config.
122: $sqldrivers = array('sql', 'asterisksql');
123: if (in_array($driver, $sqldrivers)) {
124: $params = Horde::getDriverConfig('storage', 'sql');
125: } else {
126: $params = Horde::getDriverConfig('storage', $driver);
127: }
128: }
129:
130: $class = 'Operator_Driver_' . $driver;
131: if (!class_exists($class)) {
132: include dirname(__FILE__) . '/Driver/' . $driver . '.php';
133: }
134: if (class_exists($class)) {
135: return new $class($params);
136: } else {
137: return false;
138: }
139: }
140:
141: }
142: