Merge branch 4.6 into master
[civicrm-core.git] / CRM / Core / BAO / Preferences.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Core_BAO_Preferences {
40
b5c2afd0 41 /**
c490a46a 42 * @param array $params
b5c2afd0 43 */
00be9182 44 public static function fixAndStoreDirAndURL(&$params) {
6a488035
TO
45 $sql = "
46SELECT v.name as valueName, g.name as optionName
47FROM civicrm_option_value v,
48 civicrm_option_group g
49WHERE ( g.name = 'directory_preferences'
50OR g.name = 'url_preferences' )
51AND v.option_group_id = g.id
52AND v.is_active = 1
53";
54
55 $dirParams = array();
56 $urlParams = array();
353ffa53 57 $dao = CRM_Core_DAO::executeQuery($sql);
6a488035
TO
58 while ($dao->fetch()) {
59 if (!isset($params[$dao->valueName])) {
60 continue;
61 }
62 if ($dao->optionName == 'directory_preferences') {
63 $dirParams[$dao->valueName] = CRM_Utils_Array::value($dao->valueName, $params, '');
64 }
65 else {
66 $urlParams[$dao->valueName] = CRM_Utils_Array::value($dao->valueName, $params, '');
67 }
68 unset($params[$dao->valueName]);
69 }
70
71 if (!empty($dirParams)) {
72 CRM_Core_BAO_Preferences::storeDirectoryOrURLPreferences($dirParams, 'directory');
73 }
74
75 if (!empty($urlParams)) {
76 CRM_Core_BAO_Preferences::storeDirectoryOrURLPreferences($urlParams, 'url');
77 }
78 }
79
b5c2afd0 80 /**
c490a46a 81 * @param array $params
b5c2afd0
EM
82 * @param string $type
83 */
00be9182 84 public static function storeDirectoryOrURLPreferences(&$params, $type = 'directory') {
6a488035
TO
85 $optionName = ($type == 'directory') ? 'directory_preferences' : 'url_preferences';
86
87 $sql = "
88UPDATE civicrm_option_value v,
89 civicrm_option_group g
90SET v.value = %1,
91 v.is_active = 1
92WHERE g.name = %2
93AND v.option_group_id = g.id
94AND v.name = %3
95";
96
97 foreach ($params as $name => $value) {
98 // always try to store relative directory or url from CMS root
99 if ($type == 'directory') {
100 $value = CRM_Utils_File::relativeDirectory($value);
101 }
102 else {
103 $value = CRM_Utils_System::relativeURL($value);
104 }
6ea503d4
TO
105 $sqlParams = array(
106 1 => array($value, 'String'),
6a488035
TO
107 2 => array($optionName, 'String'),
108 3 => array($name, 'String'),
109 );
110 CRM_Core_DAO::executeQuery($sql, $sqlParams);
111 }
112 }
113
b5c2afd0 114 /**
c490a46a 115 * @param array $params
b5c2afd0
EM
116 * @param bool $setInConfig
117 */
00be9182 118 public static function retrieveDirectoryAndURLPreferences(&$params, $setInConfig = FALSE) {
6a488035
TO
119 if ($setInConfig) {
120 $config = CRM_Core_Config::singleton();
121 }
122
123 $sql = "
124SELECT v.name as valueName, v.value, g.name as optionName
125FROM civicrm_option_value v,
126 civicrm_option_group g
127WHERE ( g.name = 'directory_preferences'
128OR g.name = 'url_preferences' )
129AND v.option_group_id = g.id
130AND v.is_active = 1
131";
132
6a488035
TO
133 $dao = CRM_Core_DAO::executeQuery($sql);
134 while ($dao->fetch()) {
135 if (!$dao->value) {
136 continue;
137 }
138 if ($dao->optionName == 'directory_preferences') {
139 $value = CRM_Utils_File::absoluteDirectory($dao->value);
140 }
141 else {
142 // CRM-7622: we need to remove the language part
143 $value = CRM_Utils_System::absoluteURL($dao->value, TRUE);
144 }
145 $params[$dao->valueName] = $value;
146 if ($setInConfig) {
147 $config->{$dao->valueName} = $value;
148 }
149 }
150 }
96025800 151
6a488035 152}