corregido el manejo de parámetros en la unfión de utilidad url()
[qcms.git] / controllers / setup_controller.php
blob6bc72696968a30126b0b31ae28bcbe5998f8452c
1 <?php
2 /**
3 * @author dardo
4 * @since 05/01/2008
5 */
6 class SetupController extends AppController {
7 var $name = 'Setup';
8 var $uses = array('DbConnection');
10 var $skipComponents = array('start', 'db_connection', 'db_schema');
12 /**
13 * @var SessionComponent
15 var $Session;
17 /**
18 * @var DbConnection
20 var $DbConnection;
22 function start() {
25 function db_connection($action = '') {
26 if (!empty($this->data)) {
27 $this->DbConnection->save($this->data);
28 $this->redirect(array('action' => 'db_connection'));
31 if (!$this->DbConnection->exists()) {
32 $connection = array( 'DbConnection' => array(
33 'driver' => 'mysql',
34 'persistent' => false,
35 'host' => 'localhost',
36 'port' => '',
37 'login' => 'root',
38 'password' => '',
39 'database' => strtolower(Configure::read('Settings.name')),
40 'prefix' => ''
41 ));
43 $this->DbConnection->save($connection);
44 $this->redirect(array('action' => 'db_connection'));
47 $connection = $this->DbConnection->read();
48 $this->data = $this->DbConnection->read();
50 $connected = $this->_checkDbConnection();
52 $edit = ($action == 'edit') || !$connected;
54 $this->set(compact('connected', 'connection', 'edit'));
57 function db_schema($action = null) {
58 $success = false;
59 $run = false;
60 if ($action == 'create') {
61 $success = true;
62 $run = true;
64 uses('file', 'model' . DS . 'connection_manager', 'model' . DS . 'schema');
66 $name = null;
67 $path = null;
68 $file = null;
70 $this->Schema =& new CakeSchema(compact('name', 'path', 'file'));
72 $options = array('name' => ucfirst(Configure::read('Settings.name')), 'file' => $this->Schema->file);
73 $dbSchema = $this->Schema->load($options);
75 $db =& ConnectionManager::getDataSource($this->Schema->connection);
77 $drop = $create = array();
79 foreach ($dbSchema->tables as $table => $fields) {
80 $drop[$table] = $db->dropSchema($dbSchema, $table);
81 $create[$table] = $db->createSchema($dbSchema, $table);
84 //debug($drop);
85 //debug($create);
87 $querys = array();
89 foreach($drop as $query) {
90 $querys[] = $query;
93 foreach($create as $query) {
94 $querys[] = $query;
97 if (!$this->__runSql($drop, 'drop')) {
98 $success = false;
101 if (!$this->__runSql($create, 'create')) {
102 $success = false;
105 $this->set('querys', $querys);
106 $this->set('errors', $this->__sqlErrors);
108 $this->set(compact('success', 'run'));
111 function create_users() {
112 if (App::import('model', 'User')) {
113 $User = new User();
114 } else {
115 $this->Session->setFlash('An error has occurred. Check your setup.');
116 $this->redirect(array('action' => 'db_connection'));
119 if ($User->find('count')) {
120 $this->Session->setFlash('A user already exists in the database. Cannot create another in the setup.');
121 $this->redirect(array('action' => 'end'));
124 if(!empty($this->data)) {
125 App::import('component', 'Security');
127 $userdata = $this->data['User'];
128 $userdata['enabled'] = '1';
129 $userdata['password'] = Security::hash(Configure::read('Security.salt') . $userdata['password']);
131 if ($User->save($userdata)) {
132 $this->_setupACL($User->id);
133 $this->redirect(array('action' => 'end'));
138 function end() {
139 $setup = Configure::read('Setup');
140 $setup['has_run'] = true;
142 $this->_storeConfig('Setup', $setup, true);
146 * Checks database connection
148 * @return Boolean
150 function _checkDbConnection($datasource = 'default') {
151 uses('model' . DS . 'connection_manager');
152 $db =& ConnectionManager::getInstance();
153 $ds =& $db->getDataSource($datasource);
155 return $ds->isConnected();
158 function __runSql($contents, $event) {
160 if (empty($contents)) {
161 return true;
164 Configure::write('debug', 2);
166 $db =& ConnectionManager::getDataSource($this->Schema->connection);
167 $db->fullDebug = true;
169 $errors = array();
171 foreach($contents as $table => $sql) {
172 if (!empty($sql)) {
173 if (!$this->Schema->before(array($event => $table))) {
174 return false;
176 if (!$db->_execute($sql)) {
177 $error = $db->lastError();
178 $errors[] = $error;
181 $this->Schema->after(array($event => $table, 'errors' => $errors));
185 $this->__sqlErrors = $errors;
187 return empty($errors);
190 function _setupAcl($id) {
191 if (App::import('Component', 'Acl')) {
192 $this->Acl =& new AclComponent();
193 $this->Acl->startup($this);
196 $options = Configure::read('Settings.auth.options');
197 $actionPath = $options['actionPath'];
199 $parentId = null;
201 $aco = $this->Acl->Aco;
202 if ($actionPath != '/') {
203 $actionPath = str_replace('/', '', $actionPath);
204 $aco->create(array('alias' => $actionPath));
205 $aco->save();
206 $parentId = $aco->id;
209 Configure::load('controllers');
211 $controllers = Configure::read('Controllers');
213 foreach ($controllers as $controller => $actions) {
214 $aco->create(array('alias' => $controller, 'parent_id' => $parentId));
215 $aco->save();
216 $controllerId = $aco->id;
217 foreach ($actions as $action) {
218 $aco->create(array('alias' => $action, 'parent_id' => $controllerId));
219 $aco->save();
223 $this->Acl->allow(array('model' => 'User', 'foreign_key' => $id), $actionPath);
226 function _storeConfig($name, $data = array(), $reRead = false) {
228 $content = '';
229 foreach ($data as $key => $value) {
230 $content .= sprintf("\$config['%s']['%s'] = %s;\n", $name, $key, var_export($value, true));
233 $content = "<?php\n".$content."?>";
235 uses('File');
236 $name = strtolower($name);
237 $file = new File(CONFIGS.$name.'.php');
238 if ($file->open('w')) {
239 $file->append($content);
241 $file->close();
243 if ($reRead) {
244 Configure::load($name);
248 function _initComponents() {
249 if(!Configure::read('Setup.has_run')) {
250 $components = array_flip($this->components);
251 $blackList = array('Acl', 'Auth');
252 foreach ($blackList as $key) {
253 if (isset($components[$key])) {
254 unset($components[$key]);
258 $this->components = array_flip($components);
260 return parent::_initComponents();