Merge pull request #4896 from totten/master-movedep
[civicrm-core.git] / CRM / Admin / Form / Setting / Component.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
6a488035
TO
32 * $Id$
33 *
34 */
35
36/**
37 * This class generates form components for Component
38 */
39class CRM_Admin_Form_Setting_Component extends CRM_Admin_Form_Setting {
40 protected $_components;
41
42 /**
c490a46a 43 * Build the form object
6a488035 44 *
355ba699 45 * @return void
6a488035
TO
46 */
47 public function buildQuickForm() {
48 CRM_Utils_System::setTitle(ts('Settings - Enable Components'));
49 $components = $this->_getComponentSelectValues();
50 $include = &$this->addElement('advmultiselect', 'enableComponents',
51 ts('Components') . ' ', $components,
52 array(
53 'size' => 5,
54 'style' => 'width:150px',
55 'class' => 'advmultiselect',
56 )
57 );
58
59 $include->setButtonAttributes('add', array('value' => ts('Enable >>')));
60 $include->setButtonAttributes('remove', array('value' => ts('<< Disable')));
61
62 $this->addFormRule(array('CRM_Admin_Form_Setting_Component', 'formRule'), $this);
63
64 parent::buildQuickForm();
65 }
66
67 /**
100fef9d 68 * Global form rule
6a488035 69 *
5173bd95
TO
70 * @param array $fields
71 * The input form values.
72 * @param array $files
73 * The uploaded files if any.
74 * @param array $options
75 * Additional user data.
6a488035
TO
76 *
77 * @return true if no errors, else array of errors
6a488035
TO
78 * @static
79 */
00be9182 80 public static function formRule($fields, $files, $options) {
6a488035
TO
81 $errors = array();
82
6f98c8ae 83 if (array_key_exists('enableComponents', $fields) && is_array($fields['enableComponents'])) {
6a488035
TO
84 if (in_array('CiviPledge', $fields['enableComponents']) &&
85 !in_array('CiviContribute', $fields['enableComponents'])
86 ) {
87 $errors['enableComponents'] = ts('You need to enable CiviContribute before enabling CiviPledge.');
88 }
89 if (in_array('CiviCase', $fields['enableComponents']) &&
90 !CRM_Core_DAO::checkTriggerViewPermission(TRUE, FALSE)
91 ) {
92 $errors['enableComponents'] = ts('CiviCase requires CREATE VIEW and DROP VIEW permissions for the database.');
93 }
94 }
95
96 return $errors;
97 }
98
e0ef6999
EM
99 /**
100 * @return array
101 */
6a488035
TO
102 private function _getComponentSelectValues() {
103 $ret = array();
104 $this->_components = CRM_Core_Component::getComponents();
105 foreach ($this->_components as $name => $object) {
106 $ret[$name] = $object->info['translatedName'];
107 }
108
109 return $ret;
110 }
111
112 public function postProcess() {
113 $params = $this->controller->exportValues($this->_name);
114
2bc3bd8f 115 CRM_Case_Info::onToggleComponents($this->_defaults['enableComponents'], $params['enableComponents'], NULL);
6a488035
TO
116 parent::commonProcess($params);
117
118 // reset navigation when components are enabled / disabled
119 CRM_Core_BAO_Navigation::resetNavigation();
120 }
121
e0ef6999
EM
122 /**
123 * @param $dsn
100fef9d 124 * @param string $fileName
e0ef6999
EM
125 * @param bool $lineMode
126 */
e6d720bb 127 public static function loadCaseSampleData($dsn, $fileName, $lineMode = FALSE) {
6a488035
TO
128 $db = &DB::connect($dsn);
129 if (PEAR::isError($db)) {
130 die("Cannot open $dsn: " . $db->getMessage());
131 }
132
d72f7af5 133 $domain = new CRM_Core_DAO_Domain();
134 $domain->find(TRUE);
135 $multiLingual = (bool) $domain->locales;
136 $smarty = CRM_Core_Smarty::singleton();
137 $smarty->assign('multilingual', $multiLingual);
138 $smarty->assign('locales', explode(CRM_Core_DAO::VALUE_SEPARATOR, $domain->locales));
139
6a488035 140 if (!$lineMode) {
6a488035 141
d72f7af5 142 $string = $smarty->fetch($fileName);
6a488035
TO
143 // change \r\n to fix windows issues
144 $string = str_replace("\r\n", "\n", $string);
145
146 //get rid of comments starting with # and --
147
148 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
149 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
150
151 $queries = preg_split('/;$/m', $string);
152 foreach ($queries as $query) {
153 $query = trim($query);
154 if (!empty($query)) {
155 $res = &$db->query($query);
156 if (PEAR::isError($res)) {
157 die("Cannot execute $query: " . $res->getMessage());
158 }
159 }
160 }
161 }
162 else {
163 $fd = fopen($fileName, "r");
164 while ($string = fgets($fd)) {
165 $string = preg_replace("/^#[^\n]*$/m", "\n", $string);
166 $string = preg_replace("/^(--[^-]).*/m", "\n", $string);
167
168 $string = trim($string);
169 if (!empty($string)) {
170 $res = &$db->query($string);
171 if (PEAR::isError($res)) {
172 die("Cannot execute $string: " . $res->getMessage());
173 }
174 }
175 }
176 }
177 }
178}