~eventum-developers/eventum/trunk

« back to all changes in this revision

Viewing changes to lib/eventum/class.crm.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
1
<?php
2
2
 
 
3
define('CRM_EXCLUDE_EXPIRED', 'exclude_expired');
 
4
 
3
5
abstract class CRM
4
6
{
5
7
    /**
6
8
     * The connection to the database
7
9
     *
8
 
     * @var resource
 
10
     * @var MDB2_Driver_Common
9
11
     */
10
12
    protected $connection;
11
13
 
29
31
    protected $support_levels;
30
32
 
31
33
    /**
32
 
     * An array containing information about options.
33
 
     * $option_id = array(
34
 
     *          'name'  =>  $option_name,
35
 
     *          'per_incident'  =   1 or 0
36
 
     * )
37
 
     *
38
 
     * @var array
39
 
     */
40
 
    protected $options_info = array();
41
 
 
42
 
    /**
43
34
     * Setups a new instance for the specified project. If the instance already exists,
44
35
     * return the current instance.
45
36
     *
55
46
    }
56
47
 
57
48
 
58
 
    public static function destroyInstances()
59
 
    {
60
 
        foreach (self::$instances as $prj_id => $instance) {
61
 
            $instance->destroy($prj_id);
62
 
        }
63
 
        self::$instances = array();
64
 
    }
65
 
 
66
 
 
67
 
    public static function authenticateCustomer($prj_id = false)
68
 
    {
69
 
        // Create later
70
 
    }
 
49
    abstract public function authenticateCustomer();
71
50
 
72
51
 
73
52
    /**
74
 
     * If a single customer ID is passed in a single Customer object is returned. If an array
75
 
     * is passed in an array of customer objects are returned.
 
53
     * Returns the customer object for the specified ID
76
54
     *
77
 
     * @param   integer $customer_id A customer id or array of ids
78
 
     * @return  CRM_Customer A customer object or an array of customer objects
 
55
     * @param   string $customer_id A customer ID
 
56
     * @return  Customer A customer object
79
57
     */
80
 
    abstract public function &getCustomer($customer_id);
 
58
    abstract public function getCustomer($customer_id);
81
59
 
82
60
 
83
61
    /**
86
64
     * @param   integer $contract_id A contract id
87
65
     * @return  Contract A contract object
88
66
     */
89
 
    abstract public function &getContract($contract_id);
90
 
    
 
67
    abstract public function getContract($contract_id);
 
68
 
91
69
 
92
70
    /**
93
71
     * Returns a contact object for the specified contact ID
95
73
     * @param   integer $email
96
74
     * @return  Contact A contact object
97
75
     */
98
 
    abstract public function &getContactByEmail($email);
 
76
    abstract public function getContactByEmail($email);
99
77
 
100
78
 
101
79
    /**
104
82
     * @param   integer $contact_id
105
83
     * @return  Contact A contact object
106
84
     */
107
 
    abstract public function &getContact($contact_id);
 
85
    abstract public function getContact($contact_id);
108
86
 
109
87
    /**
110
88
     * Returns the name of the backend.
120
98
     *
121
99
     * @param   string $field The field that we are trying to search against
122
100
     * @param   string $value The value that we are searching for
123
 
     * @param   boolean $include_expired Whether to include expired/cancelled customers or not (optional)
124
 
     * @param   boolean $include_future Whether to include expired/cancelled customers or not (optional)
 
101
     * @param   $options
125
102
     * @return  array The list of customers
126
103
     */
127
 
    abstract public function lookup($field, $value, $include_expired = FALSE, $include_future = false);
 
104
    abstract public function lookup($field, $value, $options);
128
105
 
129
106
 
130
107
    /**
148
125
 
149
126
 
150
127
    /**
151
 
     * destroys the backend
152
 
     *
153
 
     * @param   integer $prj_id
154
 
     */
155
 
    abstract protected function destroy($prj_id);
156
 
 
157
 
 
158
 
    /**
159
 
     * Re-initializes the object. This is useful for long running processes where the connection may time out
160
 
     *
161
 
     */
162
 
    abstract public function reinitialize();
163
 
 
164
 
 
165
 
    /**
166
128
     * Returns an array of incident types supported.
167
129
     *
168
130
     * @return  array An array of per incident types
171
133
 
172
134
 
173
135
    /**
174
 
     * Checks whether the given issue ID was marked as a redeemed incident or
175
 
     * not.
176
 
     *
177
 
     * @param   integer $issue_id The issue ID
178
 
     * @param   integer $incident_type The type of incident
179
 
     * @return  boolean
180
 
     */
181
 
    abstract public function isRedeemedIncident($issue_id, $incident_type);
182
 
 
183
 
 
184
 
    /**
185
 
     * Returns an array of the curently redeemed incident types for the issue.
186
 
     *
187
 
     * @access  public
188
 
     * @param   integer $issue_id The issue ID
189
 
     * @return  array An array containing the redeemed incident types
190
 
     */
191
 
    abstract public function getRedeemedIncidentDetails($issue_id);
192
 
 
193
 
 
194
 
    /**
195
 
     * Returns an array of support levels for a specific type
196
 
     *
197
 
     * @param   string $type The type of level we want to return
198
 
     * @return  array()
199
 
     */
200
 
    abstract public function getLevelsByType($type);
201
 
 
202
 
 
203
 
    /**
204
136
     * Returns an associative array of support level IDs => names
205
137
     *
206
 
     * @param   mixed   $type The type of levels to return (optional)
207
138
     * @return array
208
139
     */
209
 
    abstract public function getSupportLevelAssocList($type = false);
 
140
    abstract public function getSupportLevelAssocList();
210
141
 
211
142
 
212
143
    /**
213
144
     * Returns information on the specified support level
214
145
     *
215
 
     * @param   integer $level_id The level to return info for.
216
 
     * @return  array An array of information about the level
 
146
     * @param   string $level_id The level to return info for.
 
147
     * @throws  SupportLevelNotFoundException
 
148
     * @return  Support_Level
217
149
     */
218
 
    abstract public function getSupportLevelDetails($level_id);
 
150
    abstract public function getSupportLevel($level_id);
219
151
 
220
152
 
221
153
    /**
222
 
     * Returns an array of support levels grouped together.
 
154
     * Returns support levels grouped together
223
155
     *
224
156
     * @return array
225
157
     */
226
158
    abstract public function getGroupedSupportLevels();
227
159
 
 
160
 
228
161
    /**
229
 
     * Retrieves the customer titles associated with the given list of issues.
 
162
     * Retrieves the customer titles and support levels associated with the given list of issues. Should set
 
163
     * the following keys for each row, 'customer_title', 'support_level'
230
164
     *
231
165
     * @param   array $result The list of issues
232
166
     * @see     Search::getListing()
233
167
     */
234
 
    abstract public function getCustomerTitlesByIssues(&$result);
 
168
    abstract public function processListIssuesResult(&$result);
235
169
 
236
170
 
237
171
    /**
243
177
 
244
178
 
245
179
    /**
246
 
     * Retrieves the customer support levels associated with the
247
 
     * given list of issues.
248
 
     *
249
 
     * @param   array $result The list of issues
250
 
     * @see     Search::getListing()
251
 
     */
252
 
    abstract public function getSupportLevelsByIssues(&$result);
253
 
 
254
 
 
255
 
    /**
256
180
     * Retrieves the response countdown deadline associated with the
257
181
     * given list of issues.
258
182
     *
298
222
 
299
223
    /**
300
224
     * Returns the list of customer IDs that are associated with the given
301
 
     * email value (wildcards welcome).
 
225
     * keyword value (wildcards welcome). This can search name, emails, etc
302
226
     *
303
 
     * @param   string $email The email value
304
 
     * @param   boolean $include_expired If expired contracts should be included
 
227
     * @param   string $keyword The string to search by value
 
228
     * @param array $options
305
229
     * @return  array The list of customer IDs
306
230
     */
307
 
    abstract public function getCustomerIDsLikeEmail($email, $include_expired = false);
 
231
    abstract public function getCustomerIDsByString($keyword, $options = array());
308
232
 
309
233
 
310
234
    /**
320
244
 
321
245
 
322
246
    /**
323
 
     * Method used to get the customer and customer contact IDs associated
324
 
     * with a given list of email addresses.
325
 
     *
326
 
     * @param   array $emails The list of email addresses
327
 
     * @param   boolean $include_expired If expired contacts should be excluded
328
 
     * @return  array The customer and customer contact ID
329
 
     */
330
 
    abstract public function getCustomerAndContactIDByEmails($emails, $include_expired = false);
331
 
 
332
 
 
333
 
    /**
334
247
     * Method used to send an email notification to the sender of an
335
248
     * email message that was automatically converted into an issue.
336
249
     *
359
272
    /**
360
273
     * Returns a list of customer IDS belonging to the specified support level
361
274
     *
362
 
     * @param   integer $support_level_id The support Level ID
 
275
     * @param   string|array $levels The support Level ID or an array of support level ids
363
276
     * @param   mixed $support_options An integer or array of integers indicating various options to get customers with.
364
277
     * @return  array
365
278
     */
366
 
    abstract public function getCustomerIDsBySupportLevel($support_level_id, $support_options = false);
 
279
    abstract public function getCustomerIDsBySupportLevel($levels, $support_options = false);
367
280
 
368
281
 
369
282
    /**
377
290
    /**
378
291
     * Returns the list of contract IDs for a given support contract level.
379
292
     *
380
 
     * @param   integer $support_level_id The support level ID
 
293
     * @param   integer $level_id The support level ID
381
294
     * @param   mixed $support_options An integer or array of integers indicating various options to get customers with.
382
295
     * @return  array The list of contract IDs
383
296
     */
384
 
    abstract public function getContractIDsBySupportLevel($support_level_id, $support_options = FALSE);
 
297
    abstract public function getContractIDsBySupportLevel($level_id, $support_options = FALSE);
385
298
 
386
299
    /**
387
300
     * Checks whether the given project ID is setup to use customer integration
480
393
    /**
481
394
     * Returns the backend for the specified class name
482
395
     *
483
 
     * @param   string $class_name The name of the class.
 
396
     * @param $backend_class
 
397
     * @param int $prj_id
 
398
     * @internal param string $class_name The name of the class.
484
399
     * @return  Customer
485
400
     */
486
 
    private static function getBackend($backend_class, $prj_id = 0)
 
401
    private static function getBackend($backend_class, $prj_id)
487
402
    {
488
403
        $file_name_chunks = explode(".", $backend_class);
489
404
        $class_name = $file_name_chunks[1];
497
412
 
498
413
        $backend = new $class_name;
499
414
        $backend->setup($prj_id);
 
415
        $backend->prj_id = $prj_id;
500
416
        return $backend;
501
417
    }
502
418
 
512
428
        $stmt = "SELECT
513
429
                    cam_id,
514
430
                    cam_prj_id,
515
 
                    cam_customer_contract_id,
 
431
                    cam_customer_id,
516
432
                    cam_type,
517
433
                    usr_full_name
518
434
                 FROM
527
443
        } else {
528
444
            for ($i = 0; $i < count($res); $i++) {
529
445
                $crm = CRM::getInstance($res[$i]['cam_prj_id']);
530
 
                $contract = $crm->getContract($res[$i]['cam_customer_contract_id']);
531
 
                $res[$i]['contract_title'] = $contract->getTitle();
 
446
                try {
 
447
                    $customer = $crm->getCustomer($res[$i]['cam_customer_id']);
 
448
                    $res[$i]['customer_title'] = $customer->getName();
 
449
                } catch (CRMException $e) {}
532
450
            }
533
451
            return $res;
534
452
        }
548
466
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager
549
467
                 (
550
468
                    cam_prj_id,
551
 
                    cam_customer_contract_id,
 
469
                    cam_customer_id,
552
470
                    cam_usr_id,
553
471
                    cam_type
554
472
                 ) VALUES (
555
473
                    " . $_POST['project'] . ",
556
 
                    " . $_POST['contract'] . ",
 
474
                    " . $_POST['customer'] . ",
557
475
                    " . $_POST['manager'] . ",
558
476
                    '" . $_POST['type'] . "'
559
477
                 )";
602
520
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_account_manager
603
521
                 SET
604
522
                    cam_prj_id=" . Misc::escapeInteger($_POST['project']) . ",
605
 
                    cam_customer_contract_id=" . Misc::escapeInteger($_POST['contract']) . ",
 
523
                    cam_customer_id=" . Misc::escapeInteger($_POST['customer']) . ",
606
524
                    cam_usr_id=" . Misc::escapeInteger($_POST['manager']) . ",
607
525
                    cam_type='" . Misc::escapeString($_POST['type']) . "'
608
526
                 WHERE
673
591
            }
674
592
        }
675
593
    }
676
 
    
 
594
 
 
595
 
 
596
    /**
 
597
     * Returns any notes for for the specified customer.
 
598
     *
 
599
     * @access  public
 
600
     * @param   integer $customer_id The customer ID
 
601
     * @return  array An array containg the note details.
 
602
     */
 
603
    public static function getNoteDetailsByCustomer($customer_id)
 
604
    {
 
605
        $stmt = "SELECT
 
606
                    cno_id,
 
607
                    cno_prj_id,
 
608
                    cno_customer_id,
 
609
                    cno_note
 
610
                FROM
 
611
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
 
612
                WHERE
 
613
                    cno_customer_id = '" . Misc::escapeString($customer_id) . "'";
 
614
        $res = DB_Helper::getInstance()->getRow($stmt, DB_FETCHMODE_ASSOC);
 
615
        if (PEAR::isError($res)) {
 
616
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 
617
            return array();
 
618
        } else {
 
619
            return $res;
 
620
        }
 
621
    }
 
622
 
 
623
 
 
624
    /**
 
625
     * Returns any note details for for the specified id.
 
626
     *
 
627
     * @param $cno_id
 
628
     * @return  array An array containg the note details.
 
629
     */
 
630
    public static function getNoteDetailsByID($cno_id)
 
631
    {
 
632
        $stmt = "SELECT
 
633
                    cno_prj_id,
 
634
                    cno_customer_id,
 
635
                    cno_note
 
636
                FROM
 
637
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
 
638
                WHERE
 
639
                    cno_id = " . Misc::escapeInteger($cno_id);
 
640
        $res = DB_Helper::getInstance()->getRow($stmt, DB_FETCHMODE_ASSOC);
 
641
        if (PEAR::isError($res)) {
 
642
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 
643
            return array();
 
644
        } else {
 
645
            return $res;
 
646
        }
 
647
    }
 
648
 
 
649
 
 
650
    /**
 
651
     * Returns an array of notes for all customers.
 
652
     *
 
653
     * @return  array An array of notes.
 
654
     */
 
655
    public static function getNoteList()
 
656
    {
 
657
        $stmt = "SELECT
 
658
                    cno_id,
 
659
                    cno_prj_id,
 
660
                    cno_customer_id,
 
661
                    cno_note
 
662
                FROM
 
663
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
 
664
                ORDER BY
 
665
                    cno_customer_id ASC";
 
666
        $res = DB_Helper::getInstance()->getAll($stmt, DB_FETCHMODE_ASSOC);
 
667
        if (PEAR::isError($res)) {
 
668
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 
669
            return array();
 
670
        } else {
 
671
            for ($i = 0; $i < count($res); $i++) {
 
672
                try {
 
673
                    $crm = CRM::getInstance($res[$i]['cno_prj_id']);
 
674
                    $res[$i]['customer_title'] = $crm->getCustomer($res[$i]['cno_customer_id'])->getName();
 
675
                } catch (Exception $e) {}
 
676
            }
 
677
            return $res;
 
678
        }
 
679
    }
 
680
 
 
681
 
 
682
    /**
 
683
     * Updates a note.
 
684
     *
 
685
     * @param   integer $cno_id The id of this note.
 
686
     * @param   integer $prj_id The project ID
 
687
     * @param   integer $customer_id The id of the customer.
 
688
     * @param   string $note The text of this note.
 
689
     * @return int
 
690
     */
 
691
    public static function updateNote($cno_id, $prj_id, $customer_id, $note)
 
692
    {
 
693
        $stmt = "UPDATE
 
694
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
 
695
                 SET
 
696
                    cno_note='" . Misc::escapeString($note) . "',
 
697
                    cno_prj_id=" . Misc::escapeInteger($prj_id) . ",
 
698
                    cno_customer_id='" . Misc::escapeString($customer_id) . "',
 
699
                    cno_updated_date='" . Date_Helper::getCurrentDateGMT() . "'
 
700
                 WHERE
 
701
                    cno_id=" . Misc::escapeInteger($cno_id);
 
702
        $res = DB_Helper::getInstance()->query($stmt);
 
703
        if (PEAR::isError($res)) {
 
704
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 
705
            return -1;
 
706
        } else {
 
707
            return 1;
 
708
        }
 
709
    }
 
710
 
 
711
 
 
712
    /**
 
713
     * Adds a quick note for the specified customer.
 
714
     *
 
715
     * @param   integer $prj_id The project ID
 
716
     * @param   integer $customer_id The id of the customer.
 
717
     * @param   string  $note The note to add.
 
718
     * @return int
 
719
     */
 
720
    public static function insertNote($prj_id, $customer_id, $note)
 
721
    {
 
722
        $stmt = "INSERT INTO
 
723
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
 
724
                 (
 
725
                    cno_prj_id,
 
726
                    cno_customer_id,
 
727
                    cno_created_date,
 
728
                    cno_updated_date,
 
729
                    cno_note
 
730
                 ) VALUES (
 
731
                    " . Misc::escapeInteger($prj_id) . ",
 
732
                    " . Misc::escapeInteger($customer_id) . ",
 
733
                    '" . Date_Helper::getCurrentDateGMT() . "',
 
734
                    '" . Date_Helper::getCurrentDateGMT() . "',
 
735
                    '" . Misc::escapeString($note) . "'
 
736
                 )";
 
737
        $res = DB_Helper::getInstance()->query($stmt);
 
738
        if (PEAR::isError($res)) {
 
739
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 
740
            return -1;
 
741
        } else {
 
742
            return 1;
 
743
        }
 
744
    }
 
745
 
 
746
 
 
747
    /**
 
748
     * Removes the selected notes from the database.
 
749
     *
 
750
     * @param   array $ids An array of cno_id's to be deleted.
 
751
     * @return int
 
752
     */
 
753
    public static function removeNotes($ids)
 
754
    {
 
755
        $stmt = "DELETE FROM
 
756
                    " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "customer_note
 
757
                 WHERE
 
758
                    cno_id IN (" . join(", ", Misc::escapeInteger($ids)) . ")";
 
759
        $res = DB_Helper::getInstance()->query($stmt);
 
760
        if (PEAR::isError($res)) {
 
761
            Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 
762
            return -1;
 
763
        } else {
 
764
            return 1;
 
765
        }
 
766
    }
 
767
 
677
768
 
678
769
    public function getConnection()
679
770
    {
685
776
        return $this->prj_id;
686
777
    }
687
778
 
688
 
    public function getOptionsInfo($opt_id)
689
 
    {
690
 
        return $this->options_info[$opt_id];
691
 
    }
692
 
 
693
 
 
694
779
    /**
695
780
     * Returns the number of days expired contracts are allowed to login.
696
781
     *
699
784
    abstract public function getExpirationOffset();
700
785
 
701
786
 
 
787
    abstract public function getTemplatePath();
 
788
 
 
789
    abstract public function getHtdocsPath();
 
790
 
 
791
 
702
792
    public function __toString()
703
793
    {
704
794
        return "CRM Instance\nProject ID: " . $this->prj_id . "\nClass Name: " . get_class($this);
705
795
    }
 
796
 
 
797
 
 
798
    /**
 
799
     * Helper function to return customer name.
 
800
     * @param integer $prj_id
 
801
     * @param string $customer_id
 
802
     * @return string
 
803
     */
 
804
    public static function getCustomerName($prj_id, $customer_id)
 
805
    {
 
806
        try {
 
807
            $crm = self::getInstance($prj_id);
 
808
            $customer = $crm->getCustomer($customer_id);
 
809
            return $customer->getName();
 
810
        } catch (CRMException $e) {
 
811
            return null;
 
812
        }
 
813
    }
 
814
}
 
815
 
 
816
 
 
817
class CRMException extends Exception
 
818
{
706
819
}
 
 
b'\\ No newline at end of file'