<commit_before><?php
/**
 * WController.php
 */

defined('WITYCMS_VERSION') or die('Access denied');

/**
 * WController is the base class that will be inherited by all the applications.
 *
 * @package System\WCore
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @version 0.5.0-11-02-2016
 */
abstract class WController {
	/**
	 * @var array Context of the application describing app's name, app's directory and app's main class
	 */
	private $context = array();

	/**
	 * @var array Manifest of the application
	 */
	private $manifest;

	/**
	 * @var mixed Model class to retrieve application's data from the database
	 */
	protected $model;

	/**
	 * @var WView The view object linked to the application
	 */
	protected $view;

	 /**
	 * @var string Action that was performed in this application
	 */
	protected $action = '';

	/**
	 * @var array List of headers for this view
	 */
	private $headers = array();

	/**
	 * Application initialization
	 *
	 * @param array $context Context of the application describing app's name, app's directory and app's main class
	 */
	public function init(array $context) {
		$this->context = $context;

		// Initialize view if the app's constructor did not do it
		if (is_null($this->view)) {
			$this->setView(new WView());
		}

		// Forward the context to the View
		$this->view->setContext($this->context);

		// Parse the manifest
		$this->manifest = $this->loadManifest($this->getAppName());
		if (empty($this->manifest)) {
			WNote::error('app_no_manifest', WLang::get('error_app_no_manifest', $this->getAppName()));
		}
	}

	/**
	 * Default Launch method
	 *
	 * Launch method determines the method that must be executed in the application.
	 * Most of the time, the given action in URL is used.
	 */
	public function launch($action, array $params = array()) {
		// Execute the corresponding method for the given action in the URL
		return $this->forward($action, $params);
	}

	/**
	 * Calls the application's method which is associated to the $action value
	 *
	 * @param string $action Action to execute
	 * @param array  $params Parameters to forward to the app's controller (given in URL or retriever)
	 * @return array The model generated by the action
	 */
	public final function forward($action, array $params = array()) {
		if (!empty($action)) {
			$tpl = WSystem::getTemplate();

			// Assigns action in template
			$this->action = $action;

			if (!$this->context['parent']) {
				$tpl->assign('wity_action', $this->action, true);
			}

			$access_result = $this->hasAccess(($this->getAdminContext() ? 'admin/' : '').$this->getAppName().'/'.$action);

			// Declare the language directory
			WLang::declareLangDir($this->context['directory'].'lang');

			if ($access_result !== true) {
				if (is_array($access_result)) {
					return $access_result; // $this->hasAccess() returned a note
				}

				if ($this->getAdminContext() && empty($_SESSION['access'])) {
					return WNote::error('not_an_admin', WLang::get('error_not_an_admin'));
				}

				return WNote::error('app_no_access', WLang::get('error_app_no_access', $action, $this->getAppName()));
			}

			// Theme configuration for admin
			if ($this->getAdminContext() && !$this->hasParent()) {
				$manifest = $this->getManifest();

				$tpl->assign(array(
					'wity_admin_has_submenu' => $manifest['admin_has_submenu'],
					'wity_admin_actions'     => $manifest['admin']
				));

				$tpl->assign('wity_page_title', sprintf('Admin &raquo; %s%s',
					ucwords($manifest['name']),
					isset($manifest['admin'][$this->action]) ? ' &raquo; '.WLang::get($manifest['admin'][$this->action]['description']) : ''
				));
			}

			// Execute action
			$executable_action = preg_replace('#[^a-z_]#', '', $action);
			if (method_exists($this, $executable_action)) {
				return $this->$executable_action($params);
			} else {
				WNote::error('app_no_method', WLang::get('error_app_no_method', $executable_action, $this->getAppName()), 'debug');
				return array();
			}
		} else {
			return WNote::error('app_no_suitable_action', WLang::get('error_app_no_suitable_action', $this->getAppName()));
		}
	}

	/**
	 * Returns the application's name
	 *
	 * @return string application's name<commit_msg>Translates app name in URL in admin<commit_after><?php
/**
 * WController.php
 */

defined('WITYCMS_VERSION') or die('Access denied');

/**
 * WController is the base class that will be inherited by all the applications.
 *
 * @package System\WCore
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @version 0.5.0-11-02-2016
 */
abstract class WController {
	/**
	 * @var array Context of the application describing app's name, app's directory and app's main class
	 */
	private $context = array();

	/**
	 * @var array Manifest of the application
	 */
	private $manifest;

	/**
	 * @var mixed Model class to retrieve application's data from the database
	 */
	protected $model;

	/**
	 * @var WView The view object linked to the application
	 */
	protected $view;

	 /**
	 * @var string Action that was performed in this application
	 */
	protected $action = '';

	/**
	 * @var array List of headers for this view
	 */
	private $headers = array();

	/**
	 * Application initialization
	 *
	 * @param array $context Context of the application describing app's name, app's directory and app's main class
	 */
	public function init(array $context) {
		$this->context = $context;

		// Initialize view if the app's constructor did not do it
		if (is_null($this->view)) {
			$this->setView(new WView());
		}

		// Forward the context to the View
		$this->view->setContext($this->context);

		// Parse the manifest
		$this->manifest = $this->loadManifest($this->getAppName());
		if (empty($this->manifest)) {
			WNote::error('app_no_manifest', WLang::get('error_app_no_manifest', $this->getAppName()));
		}
	}

	/**
	 * Default Launch method
	 *
	 * Launch method determines the method that must be executed in the application.
	 * Most of the time, the given action in URL is used.
	 */
	public function launch($action, array $params = array()) {
		// Execute the corresponding method for the given action in the URL
		return $this->forward($action, $params);
	}

	/**
	 * Calls the application's method which is associated to the $action value
	 *
	 * @param string $action Action to execute
	 * @param array  $params Parameters to forward to the app's controller (given in URL or retriever)
	 * @return array The model generated by the action
	 */
	public final function forward($action, array $params = array()) {
		if (!empty($action)) {
			$tpl = WSystem::getTemplate();

			// Assigns action in template
			$this->action = $action;

			if (!$this->context['parent']) {
				$tpl->assign('wity_action', $this->action, true);
			}

			$access_result = $this->hasAccess(($this->getAdminContext() ? 'admin/' : '').$this->getAppName().'/'.$action);

			// Declare the language directory
			WLang::declareLangDir($this->context['directory'].'lang');

			if ($access_result !== true) {
				if (is_array($access_result)) {
					return $access_result; // $this->hasAccess() returned a note
				}

				if ($this->getAdminContext() && empty($_SESSION['access'])) {
					return WNote::error('not_an_admin', WLang::get('error_not_an_admin'));
				}

				return WNote::error('app_no_access', WLang::get('error_app_no_access', $action, $this->getAppName()));
			}

			// Theme configuration for admin
			if ($this->getAdminContext() && !$this->hasParent()) {
				$manifest = $this->getManifest();

				$tpl->assign(array(
					'wity_admin_has_submenu' => $manifest['admin_has_submenu'],
					'wity_admin_actions'     => $manifest['admin']
				));

				$tpl->assign('wity_page_title', sprintf('Admin &raquo; %s%s',
					WLang::get($manifest['name']),
					isset($manifest['admin'][$this->action]) ? ' &raquo; '.WLang::get($manifest['admin'][$this->action]['description']) : ''
				));
			}

			// Execute action
			$executable_action = preg_replace('#[^a-z_]#', '', $action);
			if (method_exists($this, $executable_action)) {
				return $this->$executable_action($params);
			} else {
				WNote::error('app_no_method', WLang::get('error_app_no_method', $executable_action, $this->getAppName()), 'debug');
				return array();
			}
		} else {
			return WNote::error('app_no_suitable_action', WLang::get('error_app_no_suitable_action', $this->getAppName()));
		}
	}

	/**
	 * Returns the application's name
	 *
	 * @return string application's name
{'fraction_non_alphanumeric': 0.16227084565345948, 'fraction_numerical': 0.0026020106445890008, 'mean_word_length': 3.9020289855072465, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3600452', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2546, 'n_words': 707}