Commit | Line | Data |
---|---|---|
5d5d3b35 TO |
1 | <?php |
2 | ||
db01bf2f | 3 | /** |
4 | * Class CRM_Cxn_CiviCxnStore | |
5 | */ | |
5d5d3b35 TO |
6 | class CRM_Cxn_CiviCxnStore implements Civi\Cxn\Rpc\CxnStore\CxnStoreInterface { |
7 | ||
8 | protected $cxns = array(); | |
9 | ||
10 | /** | |
11 | * @inheritDoc | |
12 | */ | |
13 | public function getAll() { | |
14 | if (!$this->cxns) { | |
15 | $this->cxns = array(); | |
16 | $dao = new CRM_Cxn_DAO_Cxn(); | |
17 | $dao->find(); | |
18 | while ($dao->fetch()) { | |
19 | $cxn = $this->convertDaoToCxn($dao); | |
20 | $this->cxns[$cxn['cxnId']] = $cxn; | |
21 | } | |
22 | } | |
23 | return $this->cxns; | |
24 | } | |
25 | ||
26 | /** | |
27 | * @inheritDoc | |
28 | */ | |
29 | public function getByCxnId($cxnId) { | |
30 | if (isset($this->cxns[$cxnId])) { | |
31 | return $this->cxns[$cxnId]; | |
32 | } | |
33 | $dao = new CRM_Cxn_DAO_Cxn(); | |
39151786 | 34 | $dao->cxn_guid = $cxnId; |
5d5d3b35 TO |
35 | if ($dao->find(TRUE)) { |
36 | $this->cxns[$cxnId] = $this->convertDaoToCxn($dao); | |
37 | return $this->cxns[$cxnId]; | |
38 | } | |
39 | else { | |
40 | return NULL; | |
41 | } | |
42 | } | |
43 | ||
44 | /** | |
45 | * @inheritDoc | |
46 | */ | |
47 | public function getByAppId($appId) { | |
48 | $dao = new CRM_Cxn_DAO_Cxn(); | |
39151786 | 49 | $dao->app_guid = $appId; |
5d5d3b35 | 50 | if ($dao->find(TRUE)) { |
39151786 TO |
51 | $this->cxns[$dao->cxn_guid] = $this->convertDaoToCxn($dao); |
52 | return $this->cxns[$dao->cxn_guid]; | |
5d5d3b35 TO |
53 | } |
54 | else { | |
55 | return NULL; | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * @inheritDoc | |
61 | */ | |
62 | public function add($cxn) { | |
63 | $dao = new CRM_Cxn_DAO_Cxn(); | |
39151786 | 64 | $dao->cxn_guid = $cxn['cxnId']; |
5d5d3b35 TO |
65 | $dao->find(TRUE); |
66 | $this->convertCxnToDao($cxn, $dao); | |
67 | $dao->save(); | |
68 | ||
69 | $sql = ' | |
70 | UPDATE civicrm_cxn SET created_date = modified_date | |
71 | WHERE created_date IS NULL | |
72 | AND cxn_guid = %1 | |
73 | '; | |
74 | CRM_Core_DAO::executeQuery($sql, array( | |
75 | 1 => array($cxn['cxnId'], 'String'), | |
76 | )); | |
77 | ||
78 | $this->cxns[$cxn['cxnId']] = $cxn; | |
79 | } | |
80 | ||
81 | /** | |
82 | * @inheritDoc | |
83 | */ | |
84 | public function remove($cxnId) { | |
39151786 | 85 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_cxn WHERE cxn_guid = %1', array( |
5d5d3b35 TO |
86 | 1 => array($cxnId, 'String'), |
87 | )); | |
88 | unset($this->cxns[$cxnId]); | |
89 | } | |
90 | ||
91 | /** | |
92 | * @param CRM_Cxn_DAO_Cxn $dao | |
93 | * @return array | |
94 | * Array-encoded connection details. | |
95 | */ | |
96 | protected function convertDaoToCxn($dao) { | |
97 | $appMeta = json_decode($dao->app_meta, TRUE); | |
98 | return array( | |
39151786 | 99 | 'cxnId' => $dao->cxn_guid, |
5d5d3b35 | 100 | 'secret' => $dao->secret, |
39151786 | 101 | 'appId' => $dao->app_guid, |
5d5d3b35 TO |
102 | 'appUrl' => $appMeta['appUrl'], |
103 | 'siteUrl' => CRM_Cxn_BAO_Cxn::getSiteCallbackUrl(), | |
104 | 'perm' => json_decode($dao->perm, TRUE), | |
105 | ); | |
106 | } | |
107 | ||
108 | /** | |
109 | * @param array $cxn | |
110 | * Array-encoded connection details. | |
111 | * @param CRM_Cxn_DAO_Cxn $dao | |
112 | */ | |
113 | protected function convertCxnToDao($cxn, $dao) { | |
39151786 | 114 | $dao->cxn_guid = $cxn['cxnId']; |
5d5d3b35 | 115 | $dao->secret = $cxn['secret']; |
39151786 | 116 | $dao->app_guid = $cxn['appId']; |
5d5d3b35 TO |
117 | $dao->perm = json_encode($cxn['perm']); |
118 | ||
119 | // Note: we don't save siteUrl because it's more correct to regenerate on-demand. | |
120 | // Note: we don't save appUrl, but other processes will update appMeta. | |
121 | } | |
39151786 | 122 | |
5d5d3b35 | 123 | } |