INFRA-132 - CRM/ - PHPStorm cleanup
[civicrm-core.git] / CRM / Core / BAO / OpenID.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2014
32 * $Id$
33 *
34 */
35
36 /**
37 * This class contains function for Open Id
38 */
39 class CRM_Core_BAO_OpenID extends CRM_Core_DAO_OpenID {
40
41 /**
42 * Takes an associative array and adds OpenID
43 *
44 * @param array $params
45 * (reference ) an assoc array of name/value pairs.
46 *
47 * @return object
48 * CRM_Core_BAO_OpenID object on success, null otherwise
49 * @static
50 */
51 public static function add(&$params) {
52 $hook = empty($params['id']) ? 'create' : 'edit';
53 CRM_Utils_Hook::pre($hook, 'OpenID', CRM_Utils_Array::value('id', $params), $params);
54
55 $openId = new CRM_Core_DAO_OpenID();
56 $openId->copyValues($params);
57 $openId->save();
58
59 CRM_Utils_Hook::post($hook, 'OpenID', $openId->id, $openId);
60 return $openId;
61 }
62
63 /**
64 * Given the list of params in the params array, fetch the object
65 * and store the values in the values array
66 *
67 * @param array $entityBlock
68 * Input parameters to find object.
69 *
70 * @return mixed
71 * @static
72 */
73 public static function &getValues($entityBlock) {
74 return CRM_Core_BAO_Block::getValues('openid', $entityBlock);
75 }
76
77 /**
78 * Returns whether or not this OpenID is allowed to login
79 *
80 * @param string $identity_url
81 * The OpenID to check.
82 *
83 * @return boolean
84 * @static
85 */
86 public static function isAllowedToLogin($identity_url) {
87 $openId = new CRM_Core_DAO_OpenID();
88 $openId->openid = $identity_url;
89 if ($openId->find(TRUE)) {
90 return $openId->allowed_to_login == 1;
91 }
92 return FALSE;
93 }
94
95 /**
96 * Get all the openids for a specified contact_id, with the primary openid being first
97 *
98 * @param int $id
99 * The contact id.
100 *
101 * @param bool $updateBlankLocInfo
102 *
103 * @return array
104 * the array of openid's
105 * @static
106 */
107 public static function allOpenIDs($id, $updateBlankLocInfo = FALSE) {
108 if (!$id) {
109 return NULL;
110 }
111
112 $query = "
113 SELECT civicrm_openid.openid, civicrm_location_type.name as locationType, civicrm_openid.is_primary as is_primary,
114 civicrm_openid.allowed_to_login as allowed_to_login, civicrm_openid.id as openid_id,
115 civicrm_openid.location_type_id as locationTypeId
116 FROM civicrm_contact
117 LEFT JOIN civicrm_openid ON ( civicrm_openid.contact_id = civicrm_contact.id )
118 LEFT JOIN civicrm_location_type ON ( civicrm_openid.location_type_id = civicrm_location_type.id )
119 WHERE
120 civicrm_contact.id = %1
121 ORDER BY
122 civicrm_openid.is_primary DESC, openid_id ASC ";
123 $params = array(1 => array($id, 'Integer'));
124
125 $openids = $values = array();
126 $dao = CRM_Core_DAO::executeQuery($query, $params);
127 $count = 1;
128 while ($dao->fetch()) {
129 $values = array(
130 'locationType' => $dao->locationType,
131 'is_primary' => $dao->is_primary,
132 'id' => $dao->openid_id,
133 'openid' => $dao->openid,
134 'locationTypeId' => $dao->locationTypeId,
135 'allowed_to_login' => $dao->allowed_to_login,
136 );
137
138 if ($updateBlankLocInfo) {
139 $openids[$count++] = $values;
140 }
141 else {
142 $openids[$dao->openid_id] = $values;
143 }
144 }
145 return $openids;
146 }
147
148 /**
149 * Call common delete function
150 */
151 public static function del($id) {
152 return CRM_Contact_BAO_Contact::deleteObjectWithPrimary('OpenID', $id);
153 }
154 }