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