Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-03-23-46-36
[civicrm-core.git] / CRM / Core / BAO / Preferences.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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 *
38 */
39 class CRM_Core_BAO_Preferences {
40
41 static function fixAndStoreDirAndURL(&$params) {
42 $sql = "
43 SELECT v.name as valueName, g.name as optionName
44 FROM civicrm_option_value v,
45 civicrm_option_group g
46 WHERE ( g.name = 'directory_preferences'
47 OR g.name = 'url_preferences' )
48 AND v.option_group_id = g.id
49 AND v.is_active = 1
50 ";
51
52 $dirParams = array();
53 $urlParams = array();
54 $dao = CRM_Core_DAO::executeQuery($sql);
55 while ($dao->fetch()) {
56 if (!isset($params[$dao->valueName])) {
57 continue;
58 }
59 if ($dao->optionName == 'directory_preferences') {
60 $dirParams[$dao->valueName] = CRM_Utils_Array::value($dao->valueName, $params, '');
61 }
62 else {
63 $urlParams[$dao->valueName] = CRM_Utils_Array::value($dao->valueName, $params, '');
64 }
65 unset($params[$dao->valueName]);
66 }
67
68 if (!empty($dirParams)) {
69 CRM_Core_BAO_Preferences::storeDirectoryOrURLPreferences($dirParams, 'directory');
70 }
71
72 if (!empty($urlParams)) {
73 CRM_Core_BAO_Preferences::storeDirectoryOrURLPreferences($urlParams, 'url');
74 }
75 }
76
77 static function storeDirectoryOrURLPreferences(&$params, $type = 'directory') {
78 $optionName = ($type == 'directory') ? 'directory_preferences' : 'url_preferences';
79
80 $sql = "
81 UPDATE civicrm_option_value v,
82 civicrm_option_group g
83 SET v.value = %1,
84 v.is_active = 1
85 WHERE g.name = %2
86 AND v.option_group_id = g.id
87 AND v.name = %3
88 ";
89
90 foreach ($params as $name => $value) {
91 // always try to store relative directory or url from CMS root
92 if ($type == 'directory') {
93 $value = CRM_Utils_File::relativeDirectory($value);
94 }
95 else {
96 $value = CRM_Utils_System::relativeURL($value);
97 }
98 $sqlParams = array(1 => array($value, 'String'),
99 2 => array($optionName, 'String'),
100 3 => array($name, 'String'),
101 );
102 CRM_Core_DAO::executeQuery($sql, $sqlParams);
103 }
104 }
105
106 static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
107 if ($setInConfig) {
108 $config = CRM_Core_Config::singleton();
109 }
110
111 $sql = "
112 SELECT v.name as valueName, v.value, g.name as optionName
113 FROM civicrm_option_value v,
114 civicrm_option_group g
115 WHERE ( g.name = 'directory_preferences'
116 OR g.name = 'url_preferences' )
117 AND v.option_group_id = g.id
118 AND v.is_active = 1
119 ";
120
121
122 $dao = CRM_Core_DAO::executeQuery($sql);
123 while ($dao->fetch()) {
124 if (!$dao->value) {
125 continue;
126 }
127 if ($dao->optionName == 'directory_preferences') {
128 $value = CRM_Utils_File::absoluteDirectory($dao->value);
129 }
130 else {
131 // CRM-7622: we need to remove the language part
132 $value = CRM_Utils_System::absoluteURL($dao->value, TRUE);
133 }
134 $params[$dao->valueName] = $value;
135 if ($setInConfig) {
136 $config->{$dao->valueName} = $value;
137 }
138 }
139 }
140 }
141