2 namespace callnotifier;
5 * Fetch caller names from a CSV file.
7 * The entries need to be separated by semicolons ";".
8 * The first line is interpreted as table head with column names.
10 * By default, column names "number" and "name" are used.
12 * Sets "toName" or "fromName", depending on call type.
14 class CallMonitor_Detailler_CSV implements CallMonitor_Detailler
16 protected $data = array();
19 * Create new csv name resolver
21 * @param string $file Path to CSV file
22 * @param array $columns Names of the CSV columns that contain "number"
24 * array('number' => 'telephone', 'name' => 'name')
26 public function __construct($file, $columns = null)
28 if ($columns === null) {
29 $columns = array('number' => 'number', 'name' => 'name');
31 $columns = array_merge(
32 array('number' => 'number', 'name' => 'name'),
35 $this->loadFile($file, $columns);
38 protected function loadFile($file, $columns)
40 if (!is_readable($file)) {
41 throw new \Exception('CSV file not readable: ' . $file);
43 $handle = fopen($file, 'r');
44 if ($handle === false) {
45 throw new \Exception('Error opening CSV file: ' . $file);
49 $head = fgetcsv($handle, 1000, ';');
50 foreach ($columns as $key => $colName) {
51 $pos = array_search($colName, $head);
54 'CSV file does not have a colum with name: ' . $colName
60 while (($lineData = fgetcsv($handle, 1000, ';')) !== false) {
61 $this->data[$lineData[$colPos['number']]]
62 = $lineData[$colPos['name']];
66 public function loadCallDetails(CallMonitor_Call $call)
68 if ($call->type == CallMonitor_Call::INCOMING) {
69 if (!isset($call->fromName) || $call->fromName === null) {
70 $call->fromName = $this->loadName($call->from);
73 if (!isset($call->toName) || $call->toName === null) {
74 $call->toName = $this->loadName($call->to);
79 protected function loadName($number)
81 if (isset($this->data[$number])) {
82 return $this->data[$number];