~eventum-developers/eventum/trunk

« back to all changes in this revision

Viewing changes to lib/eventum/class.customer.php

  • Committer: Bryan Alsdorf
  • Date: 2013-08-23 03:50:34 UTC
  • mto: (4033.1.168 eventum-skysql)
  • mto: This revision was merged to the branch mainline in revision 4660.
  • Revision ID: bryan@montyprogram.com-20130823035034-7f5r3pt1xg0xnnhk
Initial commit of CRM migration

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
3
 
// +----------------------------------------------------------------------+
4
 
// | Eventum - Issue Tracking System                                      |
5
 
// +----------------------------------------------------------------------+
6
 
// | Copyright (c) 2003 - 2008 MySQL AB                                   |
7
 
// | Copyright (c) 2008 - 2010 Sun Microsystem Inc.                       |
8
 
// | Copyright (c) 2011 - 2013 Eventum Team.                              |
9
 
// |                                                                      |
10
 
// | This program is free software; you can redistribute it and/or modify |
11
 
// | it under the terms of the GNU General Public License as published by |
12
 
// | the Free Software Foundation; either version 2 of the License, or    |
13
 
// | (at your option) any later version.                                  |
14
 
// |                                                                      |
15
 
// | This program is distributed in the hope that it will be useful,      |
16
 
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
17
 
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
18
 
// | GNU General Public License for more details.                         |
19
 
// |                                                                      |
20
 
// | You should have received a copy of the GNU General Public License    |
21
 
// | along with this program; if not, write to:                           |
22
 
// |                                                                      |
23
 
// | Free Software Foundation, Inc.                                       |
24
 
// | 59 Temple Place - Suite 330                                          |
25
 
// | Boston, MA 02111-1307, USA.                                          |
26
 
// +----------------------------------------------------------------------+
27
 
// | Authors: João Prado Maia <jpm@mysql.com>                             |
28
 
// +----------------------------------------------------------------------+
29
 
 
30
 
 
31
 
// Constants used by customer class.
32
 
define("CUSTOMER_EXCLUDE_EXPIRED", 1);
33
 
 
34
 
class Customer
35
 
{
36
 
    /**
37
 
     * Returns the list of available customer backends by listing the class
38
 
     * files in the backend directory.
39
 
     *
40
 
     * @access  public
41
 
     * @return  array Associative array of filename => name
42
 
     */
43
 
    function getBackendList()
44
 
    {
45
 
        $files = Misc::getFileList(APP_INC_PATH . '/customer');
46
 
        $files = array_merge($files, Misc::getFileList(APP_LOCAL_PATH. '/customer'));
47
 
        $list = array();
48
 
        for ($i = 0; $i < count($files); $i++) {
49
 
            // make sure we only list the customer backends
50
 
            if (preg_match('/^class\.(.*)\.php$/', $files[$i], $matches)) {
51
 
                // display a prettyfied backend name in the admin section
52
 
                if ($matches[1] == "abstract_customer_backend") {
53
 
                    continue;
54
 
                }
55
 
                $name = ucwords(str_replace('_', ' ', $matches[1]));
56
 
                $list[$files[$i]] = $name;
57
 
            }
58
 
        }
59
 
        return $list;
60
 
    }
61
 
 
62
 
 
63
 
    /**
64
 
     * Returns the customer backend class file associated with the given
65
 
     * project ID.
66
 
     *
67
 
     * @param   integer $prj_id The project ID
68
 
     * @return  string The customer backend class filename
69
 
     */
70
 
    private static function _getBackendNameByProject($prj_id)
71
 
    {
72
 
        static $backends;
73
 
 
74
 
        if (isset($backends[$prj_id])) {
75
 
            return $backends[$prj_id];
76
 
        }
77
 
 
78
 
        $stmt = "SELECT
79
 
                    prj_id,
80
 
                    prj_customer_backend
81
 
                 FROM
82
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project
83
 
                 ORDER BY
84
 
                    prj_id";
85
 
        $res = DB_Helper::getInstance()->getAssoc($stmt);
86
 
        if (PEAR::isError($res)) {
87
 
                /** @var $res PEAR_Error */
88
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
89
 
            return '';
90
 
        } else {
91
 
            $backends = $res;
92
 
            return @$backends[$prj_id];
93
 
        }
94
 
    }
95
 
 
96
 
 
97
 
    /**
98
 
     * Includes the appropriate customer backend class associated with the
99
 
     * given project ID, instantiates it and returns the class.
100
 
     *
101
 
     * @param   integer $prj_id The project ID
102
 
     * @return  Abstract_Customer_Backend
103
 
     */
104
 
    public static function &_getBackend($prj_id)
105
 
    {
106
 
        static $setup_backends;
107
 
 
108
 
        if (empty($setup_backends[$prj_id])) {
109
 
            $backend_class = self::_getBackendNameByProject($prj_id);
110
 
            if (empty($backend_class)) {
111
 
                $setup_backends[$prj_id] = false;
112
 
            } else {
113
 
                $file_name_chunks = explode(".", $backend_class);
114
 
                $class_name = $file_name_chunks[1] . "_Customer_Backend";
115
 
 
116
 
 
117
 
                if (file_exists(APP_LOCAL_PATH . "/customer/$backend_class")) {
118
 
                    require_once(APP_LOCAL_PATH . "/customer/$backend_class");
119
 
                } else {
120
 
                    require_once APP_INC_PATH . "/customer/$backend_class";
121
 
                }
122
 
 
123
 
                $setup_backends[$prj_id] = new $class_name;
124
 
                $setup_backends[$prj_id]->connect();
125
 
            }
126
 
        }
127
 
        return $setup_backends[$prj_id];
128
 
    }
129
 
 
130
 
 
131
 
    /**
132
 
     * Checks whether the given project ID is setup to use customer integration
133
 
     * or not.
134
 
     *
135
 
     * @param   integer $prj_id The project ID
136
 
     * @return  boolean
137
 
     */
138
 
    public static function hasCustomerIntegration($prj_id)
139
 
    {
140
 
        $backend = self::_getBackendNameByProject($prj_id);
141
 
        if (empty($backend)) {
142
 
            return false;
143
 
        } else {
144
 
            return true;
145
 
        }
146
 
    }
147
 
 
148
 
 
149
 
    // XXX: put documentation here
150
 
    public static function getBackendImplementationName($prj_id)
151
 
    {
152
 
        if (!self::hasCustomerIntegration($prj_id)) {
153
 
            return '';
154
 
        }
155
 
        $backend =& self::_getBackend($prj_id);
156
 
        return $backend->getName();
157
 
    }
158
 
 
159
 
 
160
 
    /**
161
 
     * Returns true if the backend uses support levels, false otherwise
162
 
     *
163
 
     * @access  public
164
 
     * @param   integer $prj_id The project ID
165
 
     * @return  boolean True if the project uses support levels.
166
 
     */
167
 
    function doesBackendUseSupportLevels($prj_id)
168
 
    {
169
 
        $backend =& self::_getBackend($prj_id);
170
 
        if ($backend === FALSE) {
171
 
            return false;
172
 
        } else {
173
 
            return $backend->usesSupportLevels();
174
 
        }
175
 
    }
176
 
 
177
 
 
178
 
    /**
179
 
     * Returns the contract status associated with the given customer ID.
180
 
     * Possible return values are 'active', 'in_grace_period' and 'expired'.
181
 
     *
182
 
     * @param   integer $prj_id The project ID
183
 
     * @param   integer $customer_id The customer ID
184
 
     * @param   integer $contract_id The contract ID
185
 
     * @return  string The contract status
186
 
     */
187
 
    public static function getContractStatus($prj_id, $customer_id, $contract_id = false)
188
 
    {
189
 
        $backend =& self::_getBackend($prj_id);
190
 
        return $backend->getContractStatus($customer_id, $contract_id);
191
 
    }
192
 
 
193
 
 
194
 
 
195
 
    /**
196
 
     * Retrieves the customer titles associated with the given list of issues.
197
 
     *
198
 
     * @access  public
199
 
     * @param   integer $prj_id The project ID
200
 
     * @param   array $result The list of issues
201
 
     * @see     Search::getListing()
202
 
     */
203
 
    function getCustomerTitlesByIssues($prj_id, &$result)
204
 
    {
205
 
        $backend =& self::_getBackend($prj_id);
206
 
        $backend->getCustomerTitlesByIssues($result);
207
 
    }
208
 
 
209
 
 
210
 
 
211
 
    /**
212
 
     * Retrieves the support levels associated with the given list of issues.
213
 
     *
214
 
     * @access  public
215
 
     * @param   integer $prj_id The project ID
216
 
     * @param   array $result The list of issues
217
 
     * @see     Search::getListing()
218
 
     */
219
 
    function getSupportLevelsByIssues($prj_id, &$result)
220
 
    {
221
 
        $backend =& self::_getBackend($prj_id);
222
 
        $backend->getSupportLevelsByIssues($result);
223
 
    }
224
 
 
225
 
 
226
 
    /**
227
 
     * Method used to get the details of the given customer.
228
 
     *
229
 
     * @param   integer $prj_id The project ID
230
 
     * @param   integer $customer_id The customer ID
231
 
     * @param   boolean $force_refresh If the cache should not be used.
232
 
     * @param   bool|int $contract_id The contract ID
233
 
     * @param   bool|int $contact_id
234
 
     * @return  array The customer details
235
 
     */
236
 
    public static function getDetails($prj_id, $customer_id, $force_refresh = false, $contract_id = false,
237
 
                                      $contact_id = false)
238
 
    {
239
 
        $backend =& self::_getBackend($prj_id);
240
 
        return $backend->getDetails($customer_id, $force_refresh, $contract_id, $contact_id);
241
 
    }
242
 
 
243
 
 
244
 
    /**
245
 
     * Returns true if this issue has been counted a valid incident
246
 
     *
247
 
     * @see /docs/Customer_API.html
248
 
     * @access  public
249
 
     * @param   integer $prj_id The project ID
250
 
     * @param   integer $issue_id The ID of the issue
251
 
     * @param   integer $incident_type The type of incident
252
 
     * @return  boolean True if this is a redeemed incident.
253
 
     */
254
 
    function isRedeemedIncident($prj_id, $issue_id, $incident_type = false)
255
 
    {
256
 
        $backend =& self::_getBackend($prj_id);
257
 
        return $backend->isRedeemedIncident($issue_id, $incident_type);
258
 
    }
259
 
 
260
 
 
261
 
    /**
262
 
     * Returns an array of the curently redeemed incident types for the issue.
263
 
     *
264
 
     * @see /docs/Customer_API.html
265
 
     * @access  public
266
 
     * @param   integer $prj_id The project ID
267
 
     * @return  array An array containing the redeemed incident types
268
 
     */
269
 
    function getRedeemedIncidentDetails($prj_id, $issue_id)
270
 
    {
271
 
        $types = self::getIncidentTypes($prj_id);
272
 
        $data = array();
273
 
        foreach ($types as $id => $title) {
274
 
            if (self::isRedeemedIncident($prj_id, $issue_id, $id)) {
275
 
                $data[$id] = array(
276
 
                    'title' =>  $title,
277
 
                    'is_redeemed'   =>  1
278
 
                );
279
 
            }
280
 
        }
281
 
        return $data;
282
 
    }
283
 
 
284
 
 
285
 
    /**
286
 
     * Updates the incident counts
287
 
     *
288
 
     * @access  public
289
 
     * @param   integer $prj_id The project ID
290
 
     * @param   integer $issue_id The issue ID
291
 
     * @param   array $data An array of data containing which incident types to update.
292
 
     * @return  integer 1 if all updates were successful, -1 or -2 otherwise.
293
 
     */
294
 
    function updateRedeemedIncidents($prj_id, $issue_id, $data)
295
 
    {
296
 
        $details = self::getDetails($prj_id, Issue::getCustomerID($issue_id), Issue::getContractID($issue_id));
297
 
        foreach ($details['incident_details'] as $type_id => $type_details) {
298
 
            $is_redeemed = self::isRedeemedIncident($prj_id, $issue_id, $type_id);
299
 
            if (($is_redeemed) && (@$data[$type_id] != 1)) {
300
 
                // un-redeem issue
301
 
                $res = self::unflagIncident($prj_id, $issue_id, $type_id);
302
 
            } elseif ((!$is_redeemed) && (@$data[$type_id] == 1)) {
303
 
                // redeem issue
304
 
                if (($type_details['total'] - $type_details['redeemed']) > 0) {
305
 
                    $res = self::flagIncident($prj_id, $issue_id, $type_id);
306
 
                } else {
307
 
                    $res = -1;
308
 
                }
309
 
            } else {
310
 
                $res = 1;
311
 
            }
312
 
            if ($res != 1) {
313
 
                return $res;
314
 
            }
315
 
        }
316
 
        return $res;
317
 
    }
318
 
 
319
 
 
320
 
    /**
321
 
     * Marks an issue as a redeemed incident.
322
 
     * @see /docs/Customer_API.html
323
 
     * @param   integer $prj_id The project ID
324
 
     * @param   integer $issue_id The ID of the issue
325
 
     * @param   integer $incident_type The type of incident
326
 
     */
327
 
    public static function flagIncident($prj_id, $issue_id, $incident_type)
328
 
    {
329
 
        $backend =& self::_getBackend($prj_id);
330
 
        return $backend->flagIncident($issue_id, $incident_type);
331
 
    }
332
 
 
333
 
 
334
 
    /**
335
 
     * Marks an issue as not a redeemed incident.
336
 
     *
337
 
     * @see /docs/Customer_API.html
338
 
     * @access  public
339
 
     * @param   integer $prj_id The project ID
340
 
     * @param   integer $issue_id The ID of the issue
341
 
     * @param   integer $incident_type The type of incident
342
 
     */
343
 
    function unflagIncident($prj_id, $issue_id, $incident_type)
344
 
    {
345
 
        $backend =& self::_getBackend($prj_id);
346
 
        return $backend->unflagIncident($issue_id, $incident_type);
347
 
    }
348
 
 
349
 
 
350
 
    /**
351
 
     * Checks whether the active per-incident contract associated with the given
352
 
     * customer ID has any incidents available to be redeemed.
353
 
     *
354
 
     * @access  public
355
 
     * @param   integer $prj_id The project ID
356
 
     * @param   integer $customer_id The customer ID
357
 
     * @param   integer $incident_type The type of incident
358
 
     * @return  boolean
359
 
     */
360
 
    function hasIncidentsLeft($prj_id, $customer_id, $incident_type = false)
361
 
    {
362
 
        $backend =& self::_getBackend($prj_id);
363
 
        return $backend->hasIncidentsLeft($customer_id, $incident_type);
364
 
    }
365
 
 
366
 
 
367
 
    /**
368
 
     * Checks whether the active contract associated with the given customer ID
369
 
     * is a per-incident contract or not.
370
 
     *
371
 
     * @access  public
372
 
     * @param   integer $prj_id The project ID
373
 
     * @param   integer $customer_id The customer ID
374
 
     * @return  boolean
375
 
     */
376
 
    function hasPerIncidentContract($prj_id, $customer_id)
377
 
    {
378
 
        $backend =& self::_getBackend($prj_id);
379
 
        return $backend->hasPerIncidentContract($customer_id);
380
 
    }
381
 
 
382
 
 
383
 
    /**
384
 
     * Returns the total number of allowed incidents for the given support
385
 
     * contract ID.
386
 
     *
387
 
     * @access  public
388
 
     * @param   integer $prj_id The project ID
389
 
     * @param   integer $support_no The support contract ID
390
 
     * @param   integer $incident_type The type of incident
391
 
     * @return  integer The total number of incidents
392
 
     */
393
 
    function getTotalIncidents($prj_id, $support_no, $incident_type)
394
 
    {
395
 
        $backend =& self::_getBackend($prj_id);
396
 
        return $backend->getTotalIncidents($support_no, $incident_type);
397
 
    }
398
 
 
399
 
 
400
 
    /**
401
 
     * Returns the number of incidents remaining for the given support
402
 
     * contract ID.
403
 
     *
404
 
     * @access  public
405
 
     * @param   integer $prj_id The project ID
406
 
     * @param   integer $support_no The support contract ID
407
 
     * @param   integer $incident_type The type of incident
408
 
     * @return  integer The number of incidents remaining.
409
 
     */
410
 
    function getIncidentsRemaining($prj_id, $support_no, $incident_type)
411
 
    {
412
 
        $backend =& self::_getBackend($prj_id);
413
 
        return $backend->getIncidentsRemaining($support_no, $incident_type);
414
 
    }
415
 
 
416
 
 
417
 
    /**
418
 
     * Returns the incident types available.
419
 
     *
420
 
     * @access  public
421
 
     * @param   integer $prj_id The project ID
422
 
     * @return  array An array of per incident types
423
 
     */
424
 
    function getIncidentTypes($prj_id)
425
 
    {
426
 
        $backend =& self::_getBackend($prj_id);
427
 
        return $backend->getIncidentTypes();
428
 
    }
429
 
 
430
 
 
431
 
    /**
432
 
     * Method used to send a notice that the per-incident limit being reached.
433
 
     *
434
 
     * @access  public
435
 
     * @param   integer $prj_id The project ID
436
 
     * @param   integer $contact_id The customer contact ID
437
 
     * @param   integer $customer_id The customer ID
438
 
     * @param   boolean $new_issue If the customer just tried to create a new issue.
439
 
     * @return  void
440
 
     */
441
 
    function sendIncidentLimitNotice($prj_id, $contact_id, $customer_id, $new_issue = false)
442
 
    {
443
 
        $backend =& self::_getBackend($prj_id);
444
 
        return $backend->sendIncidentLimitNotice($contact_id, $customer_id, $new_issue);
445
 
    }
446
 
 
447
 
 
448
 
    /**
449
 
     * Returns a list of customers (companies) in the customer database.
450
 
     *
451
 
     * @access  public
452
 
     * @param   integer $prj_id The project ID
453
 
     * @return  array An associated array of customers.
454
 
     */
455
 
    function getAssocList($prj_id)
456
 
    {
457
 
        $backend =& self::_getBackend($prj_id);
458
 
        return $backend->getAssocList();
459
 
    }
460
 
 
461
 
 
462
 
    /**
463
 
     * Method used to get the customer names for the given customer id.
464
 
     *
465
 
     * @param   integer $customer_id The customer ID
466
 
     * @return  string The customer name
467
 
     */
468
 
    public static function getTitle($prj_id, $customer_id)
469
 
    {
470
 
        $backend =& self::_getBackend($prj_id);
471
 
        if ($backend === FALSE) {
472
 
            return '';
473
 
        } else {
474
 
            return $backend->getTitle($customer_id);
475
 
        }
476
 
    }
477
 
 
478
 
 
479
 
    /**
480
 
     * Method used to get an associative array of the customer names
481
 
     * for the given list of customer ids.
482
 
     *
483
 
     * @access  public
484
 
     * @param   array $customer_ids The list of customers
485
 
     * @return  array The associative array of customer id => customer name
486
 
     */
487
 
    function getTitles($prj_id, $customer_ids)
488
 
    {
489
 
            // TODO: this or backend prototype mismatches
490
 
        $backend =& self::_getBackend($prj_id);
491
 
        return $backend->getTitles($customer_ids);
492
 
    }
493
 
 
494
 
 
495
 
    /**
496
 
     * Method used to get the list of email addresses associated with the
497
 
     * contacts of a given customer.
498
 
     *
499
 
     * @access  public
500
 
     * @param   integer $customer_id The customer ID
501
 
     * @return  array The list of email addresses
502
 
     */
503
 
    function getContactEmailAssocList($prj_id, $customer_id)
504
 
    {
505
 
        $backend =& self::_getBackend($prj_id);
506
 
        return $backend->getContactEmailAssocList($customer_id);
507
 
    }
508
 
 
509
 
 
510
 
    /**
511
 
     * Method used to get the customer and customer contact IDs associated
512
 
     * with a given list of email addresses.
513
 
     *
514
 
     * @access  public
515
 
     * @param   array $emails The list of email addresses
516
 
     * @return  array The customer and customer contact ID
517
 
     */
518
 
    function getCustomerIDByEmails($prj_id, $emails)
519
 
    {
520
 
        $backend =& self::_getBackend($prj_id);
521
 
        return $backend->getCustomerIDByEmails($emails);
522
 
    }
523
 
 
524
 
 
525
 
    /**
526
 
     * Method used to get the overall statistics of issues in the system for a
527
 
     * given customer.
528
 
     *
529
 
     * @access  public
530
 
     * @param   integer $customer_id The customer ID
531
 
     * @return  array The customer related issue statistics
532
 
     */
533
 
    function getOverallStats($prj_id, $customer_id)
534
 
    {
535
 
        $backend =& self::_getBackend($prj_id);
536
 
        return $backend->getOverallStats($customer_id);
537
 
    }
538
 
 
539
 
 
540
 
    /**
541
 
     * Method used to build the overall customer profile from the information
542
 
     * stored in the customer database.
543
 
     *
544
 
     * @access  public
545
 
     * @param   integer $usr_id The Eventum user ID
546
 
     * @return  array The customer profile information
547
 
     */
548
 
    function getProfile($prj_id, $usr_id)
549
 
    {
550
 
        $backend =& self::_getBackend($prj_id);
551
 
        return $backend->getProfile($usr_id);
552
 
    }
553
 
 
554
 
 
555
 
    /**
556
 
     * Method used to get the contract details for a given customer contact.
557
 
     *
558
 
     * @access  public
559
 
     * @param   integer $contact_id The customer contact ID
560
 
     * @return  array The customer contract details
561
 
     */
562
 
    function getContractDetails($prj_id, $contact_id, $restrict_expiration = TRUE)
563
 
    {
564
 
        $backend =& self::_getBackend($prj_id);
565
 
        return $backend->getContractDetails($contact_id, $restrict_expiration);
566
 
    }
567
 
 
568
 
 
569
 
    /**
570
 
     * Method used to get the details associated with a customer contact.
571
 
     *
572
 
     * @access  public
573
 
     * @param   integer $prj_id The project ID
574
 
     * @param   integer $contact_id The customer contact ID
575
 
     * @return  array The contact details
576
 
     */
577
 
    function getContactDetails($prj_id, $contact_id)
578
 
    {
579
 
        $backend =& self::_getBackend($prj_id);
580
 
        return $backend->getContactDetails($contact_id);
581
 
    }
582
 
 
583
 
 
584
 
    /**
585
 
     * Returns the list of customer IDs that are associated with the given
586
 
     * email value (wildcards welcome).
587
 
     *
588
 
     * @access  public
589
 
     * @param   integer $prj_id The project ID
590
 
     * @param   string $email The email value
591
 
     * @return  array The list of customer IDs
592
 
     */
593
 
    function getCustomerIDsLikeEmail($prj_id, $email)
594
 
    {
595
 
        $backend =& self::_getBackend($prj_id);
596
 
        return $backend->getCustomerIDsLikeEmail($email);
597
 
    }
598
 
 
599
 
 
600
 
    /**
601
 
     * Method used to notify the customer contact that an existing issue
602
 
     * associated with him was just marked as closed.
603
 
     *
604
 
     * @access  public
605
 
     * @param   integer $prj_id The project ID
606
 
     * @param   integer $issue_id The issue ID
607
 
     * @param   integer $contact_id The customer contact ID
608
 
     * @param   boolean $send_notification Whether to send a notification about this action or not
609
 
     * @param   integer $resolution_id The resolution ID
610
 
     * @param   integer $status_id The status ID
611
 
     * @param   string $reason The reason for closing this issue
612
 
     * @return  void
613
 
     */
614
 
    function notifyIssueClosed($prj_id, $issue_id, $contact_id, $send_notification, $resolution_id, $status_id, $reason)
615
 
    {
616
 
        $backend =& self::_getBackend($prj_id);
617
 
        return $backend->notifyIssueClosed($issue_id, $contact_id, $send_notification, $resolution_id, $status_id, $reason);
618
 
    }
619
 
 
620
 
 
621
 
    /**
622
 
     * Performs a customer lookup and returns the matches, if
623
 
     * appropriate.
624
 
     *
625
 
     * @access  public
626
 
     * @param   integer $prj_id The project ID
627
 
     * @param   string $field The field that we are trying to search against
628
 
     * @param   string $value The value that we are searching for
629
 
     * @param   array  $options An array of options for search
630
 
     * @return  array The list of customers
631
 
     */
632
 
    function lookup($prj_id, $field, $value, $options=array())
633
 
    {
634
 
        $backend =& self::_getBackend($prj_id);
635
 
        return $backend->lookup($field, $value, $options);
636
 
    }
637
 
 
638
 
 
639
 
    /**
640
 
     * Method used to notify the customer contact that a new issue was just
641
 
     * created and associated with his Eventum user.
642
 
     *
643
 
     * @access  public
644
 
     * @param   integer $prj_id The project ID
645
 
     * @param   integer $issue_id The issue ID
646
 
     * @param   integer $contact_id The customer contact ID
647
 
     * @return  void
648
 
     */
649
 
    function notifyCustomerIssue($prj_id, $issue_id, $contact_id)
650
 
    {
651
 
        $backend =& self::_getBackend($prj_id);
652
 
        return $backend->notifyCustomerIssue($issue_id, $contact_id);
653
 
    }
654
 
 
655
 
 
656
 
    /**
657
 
     * Method used to get the list of available support levels.
658
 
     *
659
 
     * @access  public
660
 
     * @param   integer $prj_id The project ID
661
 
     * @return  array The list of available support levels
662
 
     */
663
 
    function getSupportLevelAssocList($prj_id)
664
 
    {
665
 
        $backend =& self::_getBackend($prj_id);
666
 
        return $backend->getSupportLevelAssocList();
667
 
    }
668
 
 
669
 
 
670
 
    /**
671
 
     * Returns the support level of the current support contract for a given
672
 
     * customer ID.
673
 
     *
674
 
     * @access  public
675
 
     * @param   integer $prj_id The project ID
676
 
     * @param   integer $customer_id The customer ID
677
 
     * @param   integer $contract_id The contract ID
678
 
     * @return  string The support contract level
679
 
     */
680
 
    function getSupportLevelID($prj_id, $customer_id, $contract_id = false)
681
 
    {
682
 
        $backend =& self::_getBackend($prj_id);
683
 
        return $backend->getSupportLevelID($customer_id, $contract_id);
684
 
    }
685
 
 
686
 
 
687
 
    /**
688
 
     * Returns the list of customer IDs for a given support contract level.
689
 
     *
690
 
     * @access  public
691
 
     * @param   integer $prj_id The project ID
692
 
     * @param   integer $support_level_id The support level ID
693
 
     * @param   mixed $support_options An integer or array of integers indicating various options to get customers with.
694
 
     * @return  array The list of customer IDs
695
 
     */
696
 
    function getListBySupportLevel($prj_id, $support_level_id, $support_options = false)
697
 
    {
698
 
        $backend =& self::_getBackend($prj_id);
699
 
        return $backend->getListBySupportLevel($support_level_id, $support_options);
700
 
    }
701
 
 
702
 
 
703
 
    /**
704
 
     * Returns an array of support levels grouped together.
705
 
     *
706
 
     * @param   integer $prj_id The project ID
707
 
     * @return  array an array of support levels.
708
 
     */
709
 
    public static function getGroupedSupportLevels($prj_id)
710
 
    {
711
 
        $backend =& self::_getBackend($prj_id);
712
 
        return $backend->getGroupedSupportLevels($prj_id);
713
 
    }
714
 
 
715
 
 
716
 
    /**
717
 
     * Method used to send an expiration notice.
718
 
     *
719
 
     * @access  public
720
 
     * @param   integer $prj_id The project ID
721
 
     * @param   integer $contact_id The customer contact ID
722
 
     * @param   boolean $is_expired Whether this customer is expired or not
723
 
     * @param   string  $contract_id The contract ID
724
 
     * @return  void
725
 
     */
726
 
    function sendExpirationNotice($prj_id, $contact_id, $is_expired = FALSE, $contract_id = false)
727
 
    {
728
 
        $backend =& self::_getBackend($prj_id);
729
 
        return $backend->sendExpirationNotice($contact_id, $is_expired, $contract_id);
730
 
    }
731
 
 
732
 
 
733
 
    /**
734
 
     * Checks whether the given technical contact ID is allowed in the current
735
 
     * support contract or not.
736
 
     *
737
 
     * @access  public
738
 
     * @param   integer $prj_id The project ID
739
 
     * @param   integer $customer_contact_id The customer technical contact ID
740
 
     * @return  boolean
741
 
     */
742
 
    function isAllowedSupportContact($prj_id, $customer_contact_id)
743
 
    {
744
 
        $backend =& self::_getBackend($prj_id);
745
 
        return $backend->isAllowedSupportContact($customer_contact_id);
746
 
    }
747
 
 
748
 
 
749
 
    /**
750
 
     * Method used to get the associated customer and customer contact from
751
 
     * a given set of support emails. This is especially useful to automatically
752
 
     * associate an issue to the appropriate customer contact that sent a
753
 
     * support email.
754
 
     *
755
 
     * @access  public
756
 
     * @param   integer $prj_id The project ID
757
 
     * @param   array $sup_ids The list of support email IDs
758
 
     * @return  array The customer and customer contact ID
759
 
     */
760
 
    function getCustomerInfoFromEmails($prj_id, $sup_ids)
761
 
    {
762
 
        $backend =& self::_getBackend($prj_id);
763
 
        return $backend->getCustomerInfoFromEmails($sup_ids);
764
 
    }
765
 
 
766
 
 
767
 
    /**
768
 
     * Method used to send an email notification to the sender of a
769
 
     * set of email messages that were manually converted into an
770
 
     * issue.
771
 
     *
772
 
     * @access  public
773
 
     * @param   integer $prj_id The project ID
774
 
     * @param   integer $issue_id The issue ID
775
 
     * @param   array $sup_ids The email IDs
776
 
     * @param   integer $customer_id The customer ID
777
 
     * @return  array The list of recipient emails
778
 
     */
779
 
    function notifyEmailConvertedIntoIssue($prj_id, $issue_id, $sup_ids, $customer_id = FALSE)
780
 
    {
781
 
        $backend =& self::_getBackend($prj_id);
782
 
        return $backend->notifyEmailConvertedIntoIssue($issue_id, $sup_ids, $customer_id);
783
 
    }
784
 
 
785
 
 
786
 
    /**
787
 
     * Method used to send an email notification to the sender of an
788
 
     * email message that was automatically converted into an issue.
789
 
     *
790
 
     * @access  public
791
 
     * @param   integer $prj_id The project ID
792
 
     * @param   integer $issue_id The issue ID
793
 
     * @param   string $sender The sender of the email message (and the recipient of this notification)
794
 
     * @param   string $date The arrival date of the email message
795
 
     * @param   string $subject The subject line of the email message
796
 
     * @return  void
797
 
     */
798
 
    function notifyAutoCreatedIssue($prj_id, $issue_id, $sender, $date, $subject)
799
 
    {
800
 
        $backend =& self::_getBackend($prj_id);
801
 
        return $backend->notifyAutoCreatedIssue($issue_id, $sender, $date, $subject);
802
 
    }
803
 
 
804
 
 
805
 
    /**
806
 
     * Method used to get the customer login grace period (number of days).
807
 
     *
808
 
     * @access  public
809
 
     * @param   integer $prj_id The project ID
810
 
     * @return  integer The customer login grace period
811
 
     */
812
 
    function getExpirationOffset($prj_id)
813
 
    {
814
 
        $backend =& self::_getBackend($prj_id);
815
 
        return $backend->_getExpirationOffset();
816
 
    }
817
 
 
818
 
 
819
 
    /**
820
 
     * Method used to get the details of the given customer contact.
821
 
     *
822
 
     * @access  public
823
 
     * @param   integer $prj_id The project ID
824
 
     * @param   integer $contact_id The customer contact ID
825
 
     * @return  array The customer details
826
 
     */
827
 
    function getContactLoginDetails($prj_id, $contact_id)
828
 
    {
829
 
        $backend =& self::_getBackend($prj_id);
830
 
        return $backend->getContactLoginDetails($contact_id);
831
 
    }
832
 
 
833
 
 
834
 
    /**
835
 
     * Returns the end date of the current support contract for a given
836
 
     * customer ID.
837
 
     *
838
 
     * @access  public
839
 
     * @param   integer $prj_id The project ID
840
 
     * @param   integer $customer_id The customer ID
841
 
     * @param   integer $contract_id The contract ID
842
 
     * @return  string The support contract end date
843
 
     */
844
 
    function getContractEndDate($prj_id, $customer_id, $contract_id = false)
845
 
    {
846
 
        $backend =& self::_getBackend($prj_id);
847
 
        return $backend->getContractEndDate($customer_id, $contract_id);
848
 
    }
849
 
 
850
 
 
851
 
    /**
852
 
     * Returns the name and email of the sales account manager of the given customer ID.
853
 
     *
854
 
     * @access  public
855
 
     * @param   integer $prj_id The project ID
856
 
     * @param   integer $customer_id The customer ID
857
 
     * @return  array An array containing the name and email of the sales account manager
858
 
     */
859
 
    function getSalesAccountManager($prj_id, $customer_id)
860
 
    {
861
 
        $backend =& self::_getBackend($prj_id);
862
 
        return $backend->getSalesAccountManager($customer_id);
863
 
    }
864
 
 
865
 
 
866
 
    /**
867
 
     * Returns the start date of the current support contract for a given
868
 
     * customer ID.
869
 
     *
870
 
     * @access  public
871
 
     * @param   integer $prj_id The project ID
872
 
     * @param   integer $customer_id The customer ID
873
 
     * @param   integer $contract_id The contract ID
874
 
     * @return  string The support contract start date
875
 
     */
876
 
    function getContractStartDate($prj_id, $customer_id, $contract_id = false)
877
 
    {
878
 
        $backend =& self::_getBackend($prj_id);
879
 
        return $backend->getContractStartDate($customer_id, $contract_id);
880
 
    }
881
 
 
882
 
 
883
 
    /**
884
 
     * Returns a message to be displayed to a customer on the top of the issue creation page.
885
 
     *
886
 
     * @param   integer $prj_id The project ID
887
 
     * @param   array $customer_id Customer ID.
888
 
     */
889
 
    function getNewIssueMessage($prj_id, $customer_id)
890
 
    {
891
 
        $backend =& self::_getBackend($prj_id);
892
 
        return $backend->getNewIssueMessage($customer_id);
893
 
    }
894
 
 
895
 
 
896
 
    /**
897
 
     * Return what business hours a customer falls into. Mainly used for international
898
 
     * customers.
899
 
     *
900
 
     * @access  public
901
 
     * @param   integer $prj_id The project ID
902
 
     * @param   integer $customer_id The customer ID
903
 
     * @return  string The business hours
904
 
     */
905
 
    function getBusinessHours($prj_id, $customer_id)
906
 
    {
907
 
        $backend =& self::_getBackend($prj_id);
908
 
        return $backend->getBusinessHours($customer_id);
909
 
    }
910
 
 
911
 
 
912
 
    /**
913
 
     * Checks whether the given customer has a support contract that
914
 
     * enforces limits for the minimum first response time or not.
915
 
     *
916
 
     * @access  public
917
 
     * @param   integer $prj_id The project ID
918
 
     * @param   integer $customer_id The customer ID
919
 
     * @param   integer $contract_id The contract ID
920
 
     * @return  boolean
921
 
     */
922
 
    function hasMinimumResponseTime($prj_id, $customer_id, $contract_id = false)
923
 
    {
924
 
        $backend =& self::_getBackend($prj_id);
925
 
        return $backend->hasMinimumResponseTime($customer_id, $contract_id);
926
 
    }
927
 
 
928
 
 
929
 
    /**
930
 
     * Returns the minimum first response time in seconds for the
931
 
     * support level associated with the given customer.
932
 
     *
933
 
     * @access  public
934
 
     * @param   integer $customer_id The customer ID
935
 
     * @param   integer $contract_id The contract ID
936
 
     * @return  integer The minimum first response time
937
 
     */
938
 
    function getMinimumResponseTime($prj_id, $customer_id, $contract_id = false)
939
 
    {
940
 
        $backend =& self::_getBackend($prj_id);
941
 
        return $backend->getMinimumResponseTime($customer_id, $contract_id);
942
 
    }
943
 
 
944
 
 
945
 
    /**
946
 
     * Returns the maximum first response time associated with the
947
 
     * support contract of the given customer.
948
 
     *
949
 
     * @access  public
950
 
     * @param   integer $customer_id The customer ID
951
 
     * @param   integer $contract_id The contract ID
952
 
     * @return  integer The maximum first response time, in seconds
953
 
     */
954
 
    function getMaximumFirstResponseTime($prj_id, $customer_id, $contract_id = false)
955
 
    {
956
 
        $backend =& self::_getBackend($prj_id);
957
 
        return $backend->getMaximumFirstResponseTime($customer_id, $contract_id);
958
 
    }
959
 
 
960
 
 
961
 
    /**
962
 
     * Returns the path php files for this customer backend are stored in.
963
 
     *
964
 
     * @param   integer $prj_id The project ID
965
 
     */
966
 
    function getPath($prj_id)
967
 
    {
968
 
        $backend =& self::_getBackend($prj_id);
969
 
        if (method_exists($backend, 'getPath')) {
970
 
            return $backend->getPath();
971
 
        } else {
972
 
            return 'customer/' . $backend->getName() . '/';
973
 
        }
974
 
    }
975
 
 
976
 
 
977
 
    /**
978
 
     * Returns the path templates for this customer backend are stored in.
979
 
     *
980
 
     * @param   integer $prj_id The project ID
981
 
     */
982
 
    function getTemplatePath($prj_id)
983
 
    {
984
 
        $backend =& self::_getBackend($prj_id);
985
 
        if (method_exists($backend, 'getTemplatePath')) {
986
 
            return $backend->getTemplatePath();
987
 
        } else {
988
 
            return 'customer/' . Customer::getBackendImplementationName($prj_id) . '/';
989
 
        }
990
 
    }
991
 
 
992
 
 
993
 
    /**
994
 
     * Returns if the specified customer / contract has the specified feature
995
 
     *
996
 
     * @param   integer $prj_id The project ID
997
 
     * @param   string  $customer_id
998
 
     * @param   string  $contract_id
999
 
     * @param   string  $feature
1000
 
     * @return  boolean True if the contract has the feature, false otherwise.
1001
 
     */
1002
 
    function hasFeature($prj_id, $customer_id, $contract_id, $feature)
1003
 
    {
1004
 
        $backend =& self::_getBackend($prj_id);
1005
 
        if (method_exists($backend, 'hasFeature')) {
1006
 
            return $backend->hasFeature($customer_id, $contract_id, $feature);
1007
 
        } else {
1008
 
            return false;
1009
 
        }
1010
 
    }
1011
 
 
1012
 
 
1013
 
    /**
1014
 
     * Performs needed checks to see if a contact can login. Performs some default
1015
 
     * checks if the backend does not implement checks
1016
 
     *
1017
 
     * @param   integer $prj_id
1018
 
     * @param   integer $customer_id
1019
 
     * @param   integer $contact_id
1020
 
     */
1021
 
    function authenticateCustomer($prj_id, $customer_id, $contact_id)
1022
 
    {
1023
 
        $tpl = new Template_Helper();
1024
 
        $backend =& self::_getBackend($prj_id);
1025
 
        if (method_exists($backend, 'authenticateCustomer')) {
1026
 
            $backend->authenticateCustomer($customer_id, $contact_id);
1027
 
        } else {
1028
 
            // check if customer is expired
1029
 
            $usr_id = Auth::getUserID();
1030
 
            $contact_id = User::getCustomerContactID($usr_id);
1031
 
            if ((!empty($contact_id)) && ($contact_id != -1)) {
1032
 
                $status = self::getContractStatus($prj_id, User::getCustomerID($usr_id));
1033
 
                $email = User::getEmailByContactID($contact_id);
1034
 
                if ($status == 'expired') {
1035
 
                    self::sendExpirationNotice($prj_id, $contact_id, true);
1036
 
                    Auth::saveLoginAttempt($email, 'failure', 'expired contract');
1037
 
 
1038
 
                    Auth::removeCookie(APP_PROJECT_COOKIE);
1039
 
 
1040
 
                    $contact_id = User::getCustomerContactID($usr_id);
1041
 
                    $tpl->setTemplate(Customer::getTemplatePath($prj_id) . "/customer_expired.tpl.html");
1042
 
                    $tpl->assign('customer', self::getContractDetails($prj_id, $contact_id, false));
1043
 
                    $tpl->displayTemplate();
1044
 
                    exit;
1045
 
                } elseif ($status == 'in_grace_period') {
1046
 
                    self::sendExpirationNotice($prj_id, $contact_id);
1047
 
                    $tpl->setTemplate(Customer::getTemplatePath($prj_id) . "/grace_period.tpl.html");
1048
 
                    $tpl->assign('customer', self::getContractDetails($prj_id, $contact_id, false));
1049
 
                    $tpl->assign('expiration_offset', self::getExpirationOffset($prj_id));
1050
 
                    $tpl->displayTemplate();
1051
 
                    exit;
1052
 
                }
1053
 
                // check with cnt_support to see if this contact is allowed in this support contract
1054
 
                if (!self::isAllowedSupportContact($prj_id, $contact_id)) {
1055
 
                    Auth::saveLoginAttempt($email, 'failure', 'not allowed as technical contact');
1056
 
                    Auth::removeCookie(APP_COOKIE);
1057
 
                    Auth::redirect("index.php?err=4&email=" . $email);
1058
 
                }
1059
 
            }
1060
 
        }
1061
 
    }
1062
 
 
1063
 
    /**
1064
 
     * Method used to get the list of technical account managers
1065
 
     * currently available in the system.
1066
 
     *
1067
 
     * @access  public
1068
 
     * @return  array The list of account managers
1069
 
     */
1070
 
    function getAccountManagerList()
1071
 
    {
1072
 
        $stmt = "SELECT
1073
 
                    cam_id,
1074
 
                    cam_prj_id,
1075
 
                    cam_customer_id,
1076
 
                    cam_type,
1077
 
                    usr_full_name
1078
 
                 FROM
1079
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager,
1080
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "user
1081
 
                 WHERE
1082
 
                    cam_usr_id=usr_id";
1083
 
        $res = DB_Helper::getInstance()->getAll($stmt, DB_FETCHMODE_ASSOC);
1084
 
        if (PEAR::isError($res)) {
1085
 
                /** @var $res PEAR_Error */
1086
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1087
 
            return "";
1088
 
        } else {
1089
 
            for ($i = 0; $i < count($res); $i++) {
1090
 
                $res[$i]['customer_title'] = self::getTitle($res[$i]['cam_prj_id'], $res[$i]['cam_customer_id']);
1091
 
            }
1092
 
            return $res;
1093
 
        }
1094
 
    }
1095
 
 
1096
 
 
1097
 
    /**
1098
 
     * Method used to add a new association of Eventum user =>
1099
 
     * customer ID. This association will provide the basis for a
1100
 
     * new role of technical account manager in Eventum.
1101
 
     *
1102
 
     * @access  public
1103
 
     * @return  integer 1 if the insert worked properly, any other value otherwise
1104
 
     */
1105
 
    function insertAccountManager()
1106
 
    {
1107
 
        $stmt = "INSERT INTO
1108
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager
1109
 
                 (
1110
 
                    cam_prj_id,
1111
 
                    cam_customer_id,
1112
 
                    cam_usr_id,
1113
 
                    cam_type
1114
 
                 ) VALUES (
1115
 
                    " . Misc::escapeInteger($_POST['project']) . ",
1116
 
                    " . Misc::escapeInteger($_POST['customer']) . ",
1117
 
                    " . Misc::escapeInteger($_POST['manager']) . ",
1118
 
                    '" . Misc::escapeString($_POST['type']) . "'
1119
 
                 )";
1120
 
        $res = DB_Helper::getInstance()->query($stmt);
1121
 
        if (PEAR::isError($res)) {
1122
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1123
 
            return -1;
1124
 
        } else {
1125
 
            return 1;
1126
 
        }
1127
 
    }
1128
 
 
1129
 
 
1130
 
    /**
1131
 
     * Method used to get the details of a given account manager.
1132
 
     *
1133
 
     * @access  public
1134
 
     * @param   integer $cam_id The account manager ID
1135
 
     * @return  array The account manager details
1136
 
     */
1137
 
    function getAccountManagerDetails($cam_id)
1138
 
    {
1139
 
        $stmt = "SELECT
1140
 
                    *
1141
 
                 FROM
1142
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager
1143
 
                 WHERE
1144
 
                    cam_id=" . Misc::escapeInteger($cam_id);
1145
 
        $res = DB_Helper::getInstance()->getRow($stmt, DB_FETCHMODE_ASSOC);
1146
 
        if (PEAR::isError($res)) {
1147
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1148
 
            return array();
1149
 
        } else {
1150
 
            return $res;
1151
 
        }
1152
 
    }
1153
 
 
1154
 
 
1155
 
    /**
1156
 
     * Method used to update the details of an account manager.
1157
 
     *
1158
 
     * @access  public
1159
 
     * @return  integer 1 if the update worked properly, any other value otherwise
1160
 
     */
1161
 
    function updateAccountManager()
1162
 
    {
1163
 
        $stmt = "UPDATE
1164
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager
1165
 
                 SET
1166
 
                    cam_prj_id=" . Misc::escapeInteger($_POST['project']) . ",
1167
 
                    cam_customer_id='" . Misc::escapeString($_POST['customer']) . "',
1168
 
                    cam_usr_id=" . Misc::escapeInteger($_POST['manager']) . ",
1169
 
                    cam_type='" . Misc::escapeString($_POST['type']) . "'
1170
 
                 WHERE
1171
 
                    cam_id=" . $_POST['id'];
1172
 
        $res = DB_Helper::getInstance()->query($stmt);
1173
 
        if (PEAR::isError($res)) {
1174
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1175
 
            return -1;
1176
 
        } else {
1177
 
            return 1;
1178
 
        }
1179
 
    }
1180
 
 
1181
 
 
1182
 
    /**
1183
 
     * Method used to remove a technical account manager from the
1184
 
     * system.
1185
 
     *
1186
 
     * @access  public
1187
 
     * @return  boolean
1188
 
     */
1189
 
    function removeAccountManager()
1190
 
    {
1191
 
        $items = @implode(", ", Misc::escapeInteger($_POST["items"]));
1192
 
        $stmt = "DELETE FROM
1193
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager
1194
 
                 WHERE
1195
 
                    cam_id IN ($items)";
1196
 
        $res = DB_Helper::getInstance()->query($stmt);
1197
 
        if (PEAR::isError($res)) {
1198
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1199
 
            return false;
1200
 
        } else {
1201
 
            return true;
1202
 
        }
1203
 
    }
1204
 
 
1205
 
 
1206
 
    /**
1207
 
     * Method used to get the list of technical account managers for
1208
 
     * a given customer ID.
1209
 
     *
1210
 
     * @access  public
1211
 
     * @param   integer $prj_id The project ID
1212
 
     * @param   integer $customer_id The customer ID
1213
 
     * @return  array The list of account managers
1214
 
     */
1215
 
    function getAccountManagers($prj_id, $customer_id)
1216
 
    {
1217
 
        $stmt = "SELECT
1218
 
                    cam_usr_id,
1219
 
                    usr_email,
1220
 
                    cam_type
1221
 
                 FROM
1222
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager,
1223
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "user
1224
 
                 WHERE
1225
 
                    cam_usr_id=usr_id AND
1226
 
                    cam_prj_id=" . Misc::escapeInteger($prj_id) . " AND
1227
 
                    cam_customer_id='" . Misc::escapeString($customer_id) . "'";
1228
 
        $res = DB_Helper::getInstance()->getAll($stmt, DB_FETCHMODE_ASSOC);
1229
 
        if (PEAR::isError($res)) {
1230
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1231
 
            return array();
1232
 
        } else {
1233
 
            if (empty($res)) {
1234
 
                return array();
1235
 
            } else {
1236
 
                return $res;
1237
 
            }
1238
 
        }
1239
 
    }
1240
 
 
1241
 
 
1242
 
    /**
1243
 
     * Returns any notes for for the specified customer.
1244
 
     *
1245
 
     * @access  public
1246
 
     * @param   integer $customer_id The customer ID
1247
 
     * @return  array An array containg the note details.
1248
 
     */
1249
 
    function getNoteDetailsByCustomer($customer_id)
1250
 
    {
1251
 
        $stmt = "SELECT
1252
 
                    cno_id,
1253
 
                    cno_prj_id,
1254
 
                    cno_customer_id,
1255
 
                    cno_note
1256
 
                FROM
1257
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
1258
 
                WHERE
1259
 
                    cno_customer_id = '" . Misc::escapeString($customer_id) . "'";
1260
 
        $res = DB_Helper::getInstance()->getRow($stmt, DB_FETCHMODE_ASSOC);
1261
 
        if (PEAR::isError($res)) {
1262
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1263
 
            return array();
1264
 
        } else {
1265
 
            return $res;
1266
 
        }
1267
 
    }
1268
 
 
1269
 
 
1270
 
    /**
1271
 
     * Returns any note details for for the specified id.
1272
 
     *
1273
 
     * @access  public
1274
 
     * @param   integer $customer_id The customer ID
1275
 
     * @return  array An array containg the note details.
1276
 
     */
1277
 
    function getNoteDetailsByID($cno_id)
1278
 
    {
1279
 
        $stmt = "SELECT
1280
 
                    cno_prj_id,
1281
 
                    cno_customer_id,
1282
 
                    cno_note
1283
 
                FROM
1284
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
1285
 
                WHERE
1286
 
                    cno_id = " . Misc::escapeInteger($cno_id);
1287
 
        $res = DB_Helper::getInstance()->getRow($stmt, DB_FETCHMODE_ASSOC);
1288
 
        if (PEAR::isError($res)) {
1289
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1290
 
            return array();
1291
 
        } else {
1292
 
            return $res;
1293
 
        }
1294
 
    }
1295
 
 
1296
 
 
1297
 
    /**
1298
 
     * Returns an array of notes for all customers.
1299
 
     *
1300
 
     * @access  public
1301
 
     * @return  array An array of notes.
1302
 
     */
1303
 
    function getNoteList()
1304
 
    {
1305
 
        $stmt = "SELECT
1306
 
                    cno_id,
1307
 
                    cno_prj_id,
1308
 
                    cno_customer_id,
1309
 
                    cno_note
1310
 
                FROM
1311
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
1312
 
                ORDER BY
1313
 
                    cno_customer_id ASC";
1314
 
        $res = DB_Helper::getInstance()->getAll($stmt, DB_FETCHMODE_ASSOC);
1315
 
        if (PEAR::isError($res)) {
1316
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1317
 
            return array();
1318
 
        } else {
1319
 
            for ($i = 0; $i < count($res); $i++) {
1320
 
                $res[$i]['customer_title'] = self::getTitle($res[$i]['cno_prj_id'], $res[$i]['cno_customer_id']);
1321
 
            }
1322
 
            return $res;
1323
 
        }
1324
 
    }
1325
 
 
1326
 
 
1327
 
    /**
1328
 
     * Updates a note.
1329
 
     *
1330
 
     * @access  public
1331
 
     * @param   integer $cno_id The id of this note.
1332
 
     * @param   integer $prj_id The project ID
1333
 
     * @param   integer $customer_id The id of the customer.
1334
 
     * @param   string $note The text of this note.
1335
 
     */
1336
 
    function updateNote($cno_id, $prj_id, $customer_id, $note)
1337
 
    {
1338
 
        $stmt = "UPDATE
1339
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
1340
 
                 SET
1341
 
                    cno_note='" . Misc::escapeString($note) . "',
1342
 
                    cno_prj_id=" . Misc::escapeInteger($prj_id) . ",
1343
 
                    cno_customer_id='" . Misc::escapeString($customer_id) . "',
1344
 
                    cno_updated_date='" . Date_Helper::getCurrentDateGMT() . "'
1345
 
                 WHERE
1346
 
                    cno_id=" . Misc::escapeInteger($cno_id);
1347
 
        $res = DB_Helper::getInstance()->query($stmt);
1348
 
        if (PEAR::isError($res)) {
1349
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1350
 
            return -1;
1351
 
        } else {
1352
 
            return 1;
1353
 
        }
1354
 
    }
1355
 
 
1356
 
 
1357
 
    /**
1358
 
     * Adds a quick note for the specified customer.
1359
 
     *
1360
 
     * @access  public
1361
 
     * @param   integer $prj_id The project ID
1362
 
     * @param   integer $customer_id The id of the customer.
1363
 
     * @param   string  $note The note to add.
1364
 
     */
1365
 
    function insertNote($prj_id, $customer_id, $note)
1366
 
    {
1367
 
        $stmt = "INSERT INTO
1368
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
1369
 
                 (
1370
 
                    cno_prj_id,
1371
 
                    cno_customer_id,
1372
 
                    cno_created_date,
1373
 
                    cno_updated_date,
1374
 
                    cno_note
1375
 
                 ) VALUES (
1376
 
                    " . Misc::escapeInteger($prj_id) . ",
1377
 
                    " . Misc::escapeInteger($customer_id) . ",
1378
 
                    '" . Date_Helper::getCurrentDateGMT() . "',
1379
 
                    '" . Date_Helper::getCurrentDateGMT() . "',
1380
 
                    '" . Misc::escapeString($note) . "'
1381
 
                 )";
1382
 
        $res = DB_Helper::getInstance()->query($stmt);
1383
 
        if (PEAR::isError($res)) {
1384
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1385
 
            return -1;
1386
 
        } else {
1387
 
            return 1;
1388
 
        }
1389
 
    }
1390
 
 
1391
 
 
1392
 
    /**
1393
 
     * Removes the selected notes from the database.
1394
 
     *
1395
 
     * @access  public
1396
 
     * @param   array $ids An array of cno_id's to be deleted.
1397
 
     */
1398
 
    function removeNotes($ids)
1399
 
    {
1400
 
        $stmt = "DELETE FROM
1401
 
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
1402
 
                 WHERE
1403
 
                    cno_id IN (" . join(", ", Misc::escapeInteger($ids)) . ")";
1404
 
        $res = DB_Helper::getInstance()->query($stmt);
1405
 
        if (PEAR::isError($res)) {
1406
 
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
1407
 
            return -1;
1408
 
        } else {
1409
 
            return 1;
1410
 
        }
1411
 
    }
1412
 
 
1413
 
}