/** * Execute specific controller action * * @access public * @param string $action * @return InvalidControllerActionError if action name is not valid or true */ function execute($action) { // Prepare action name $action = trim(strtolower($action)); // If we have valid action execute and done... Else throw exception if($this->validAction($action)) { $this->setAction($action); $ret = true; Hook::fire('before_action', array( 'controller' => $this, 'action' => $action ), $ret); if ($ret) { TimeIt::start("Action"); $this->$action(); TimeIt::stop(); Hook::fire('after_action', array( 'controller' => $this, 'action' => $action ), $ret); } return true; } else { throw new InvalidControllerActionError($this->getControllerName(), $action); } // if } // execute
/** * Display template and retur output as string * * @param string $template Template path (absolute path or path relative to * the templates dir) * @return string * @throws FileDnxError */ function fetch($template) { ob_start(); try { TimeIt::start("Template"); $this->includeTemplate($template); TimeIt::stop(); } catch(Exception $e) { ob_end_clean(); throw $e; } // try return ob_get_clean(); } // fetch
/** * Contruct controller and execute specific action * * @access public * @param string $controller_name * @param string $action * @return null */ static function executeAction($controller_name, $action) { $max_users = config_option('max_users'); if ($max_users && Contacts::count() > $max_users) { echo lang("error").": ".lang("maximum number of users exceeded error"); return; } ajx_check_login(); Env::useController($controller_name); $controller_class = Env::getControllerClass($controller_name); if(!class_exists($controller_class, false)) { throw new ControllerDnxError($controller_name); } // if $controller = new $controller_class(); if(!instance_of($controller, 'Controller')) { throw new ControllerDnxError($controller_name); } // if if (is_ajax_request()) { // if request is an ajax request return a json response // execute the action $controller->setAutoRender(false); $controller->execute($action); // fill the response $response = AjaxResponse::instance(); if (!$response->hasCurrent()) { // set the current content $response->setCurrentContent("html", $controller->getContent(), page_actions(), ajx_get_panel()); } $response->setEvents(evt_pop()); $error = flash_pop('error'); $success = flash_pop('success'); if (!is_null($error)) { $response->setError(1, clean($error)); } else if (!is_null($success)) { $response->setError(0, clean($success)); } // display the object as json tpl_assign("object", $response); $content = tpl_fetch(Env::getTemplatePath("json")); tpl_assign("content_for_layout", $content); TimeIt::start("Transfer"); if (is_iframe_request()) { tpl_display(Env::getLayoutPath("iframe")); } else { tpl_display(Env::getLayoutPath("json")); } TimeIt::stop(); } else { return $controller->execute($action); } } // executeAction
/** * Execute query and return all rows * * @access public * @param string $sql * @return array * @throws DBQueryError */ static function executeAll($sql) { $arguments = func_get_args(); array_shift($arguments); $arguments = count($arguments) ? array_flat($arguments) : null; try { $start = microtime(true); $result = self::connection()->executeAll($sql, $arguments); $end = microtime(true); if (Env::isDebuggingDB()) { Logger::log(number_format($end - $start, 4) . " - " . DB::prepareString($sql, $arguments)); } if (Env::isDebuggingTime()) { TimeIt::add("DB", $end - $start, $start, $end); } } catch (Exception $e) { Logger::log("SQL ERROR: " . $e->getMessage() . " - " . DB::prepareString($sql, $arguments)); throw $e; } return $result; }
/** * Render content... If template and/layout are NULL script will resolve * their names based on controller name and action. * * PageController::index will map with: * - template => views/page/index.php * - layout => layouts/page.php * * @param string $template * @param string $layout * @param boolean $die * @return boolean * @throws FileDnxError */ function render($template = null, $layout = null, $die = true) { // Set template and layout... if (!is_null($template)) { $this->setTemplate($template); } if (!is_null($layout)) { $this->setLayout($layout); } Hook::fire('override_action_view', $this, $ret); // Get template and layout paths $template_path = $this->getTemplatePath(); $layout_path = $this->getLayoutPath(); // Fetch content... $content = tpl_fetch($template_path); // Assign content and render layout TimeIt::start("Transfer"); $this->renderLayout($layout_path, $content); TimeIt::stop(); // Die! if ($die) { die; } // We are done here... return true; }
/** * Contruct controller and execute specific action * * @access public * @param string $controller_name * @param string $action * @return null */ static function executeAction($controller_name, $action) { $max_users = config_option('max_users'); if ($max_users && Users::count() > $max_users) { echo lang("error") . ": " . lang("maximum number of users exceeded error"); return; } ajx_check_login(); if (isset($_GET['active_project']) && logged_user() instanceof User) { $dont_update = false; if (GlobalCache::isAvailable()) { $option_value = GlobalCache::get('user_config_option_' . logged_user()->getId() . '_lastAccessedWorkspace', $success); if ($success) { $dont_update = $option_value == $_GET['active_project']; } } if (!$dont_update) { set_user_config_option('lastAccessedWorkspace', $_GET['active_project'], logged_user()->getId()); if (GlobalCache::isAvailable()) { GlobalCache::update('user_config_option_' . logged_user()->getId() . '_lastAccessedWorkspace', $_GET['active_project']); } } } Env::useController($controller_name); $controller_class = Env::getControllerClass($controller_name); if (!class_exists($controller_class, false)) { throw new ControllerDnxError($controller_name); } // if $controller = new $controller_class(); if (!instance_of($controller, 'Controller')) { throw new ControllerDnxError($controller_name); } // if if (is_ajax_request()) { // if request is an ajax request return a json response // execute the action $controller->setAutoRender(false); $controller->execute($action); // fill the response $response = AjaxResponse::instance(); if (!$response->hasCurrent()) { // set the current content $response->setCurrentContent("html", $controller->getContent(), page_actions(), ajx_get_panel()); } $response->setEvents(evt_pop()); $error = flash_pop('error'); $success = flash_pop('success'); if (!is_null($error)) { $response->setError(1, clean($error)); } else { if (!is_null($success)) { $response->setError(0, clean($success)); } } // display the object as json tpl_assign("object", $response); $content = tpl_fetch(Env::getTemplatePath("json")); tpl_assign("content_for_layout", $content); TimeIt::start("Transfer"); if (is_iframe_request()) { tpl_display(Env::getLayoutPath("iframe")); } else { tpl_display(Env::getLayoutPath("json")); } TimeIt::stop(); } else { return $controller->execute($action); } }
} // if // Remove injection from url parameters foreach ($_GET as $k => &$v) { $v = remove_css_and_scripts($v); } // Get controller and action and execute... try { if (!defined('CONSOLE_MODE')) { Env::executeAction(request_controller(), request_action()); } } catch (Exception $e) { if (Env::isDebugging()) { Logger::log($e, Logger::FATAL); Env::dumpError($e); } else { Logger::log($e, Logger::FATAL); redirect_to(get_url('error', 'execute_action')); } // if } // try if (Env::isDebuggingTime()) { TimeIt::stop(); if (array_var($_REQUEST, 'a') != 'popup_reminders') { Env::useHelper('format'); $report = TimeIt::getTimeReportByType(); $report .= "\nMemory Usage: " . format_filesize(memory_get_usage(true)); file_put_contents('cache/log.time', "Request: " . print_r($_REQUEST, 1) . "\nTime Report:\n------------\n{$report}\n--------------------------------------\n", FILE_APPEND); } }
function getAssignableCompanies($project = null) { if ($this->isMemberOfOwnerCompany()) { return Companies::getCompaniesWithUsers(); } TimeIt::start('get assignable companies'); if ($project instanceof Project) { $ws = $project->getAllSubWorkspacesQuery(true); } $uid = $this->getId(); $cid = $this->getCompany()->getId(); $tp = TABLE_PREFIX; $gids = "SELECT `group_id` FROM `{$tp}group_users` WHERE `user_id` = {$uid}"; $q1 = "SELECT `project_id` FROM `{$tp}project_users` WHERE (`user_id` = {$uid} OR `user_id` IN ({$gids})) AND `can_assign_to_other` = '1'"; $q2 = "SELECT `project_id` FROM `{$tp}project_users` WHERE (`user_id` = {$uid} OR `user_id` IN ({$gids})) AND `can_assign_to_owners` = '1'"; if (isset($ws)) { $q1 .= " AND `project_id` IN ({$ws})"; $q2 .= " AND `project_id` IN ({$ws})"; } $query1 = "SELECT `user_id` FROM `{$tp}project_users` WHERE `project_id` IN ({$q1})"; $query2 = "SELECT `user_id` FROM `{$tp}project_users` WHERE `project_id` IN ({$q2})"; $query = "SELECT `company_id` FROM `{$tp}users` WHERE `id` IN ({$query1}) AND `company_id` <> 1 AND `company_id` <> {$cid} OR `id` IN ({$query2}) AND `company_id` = 1"; // get companies for assignable users (see getAssignableUsers) $companies = Companies::findAll(array('conditions' => "`id` = {$cid} OR `id` IN ({$query})")); TimeIt::stop(); return $companies; }