diff --git a/application/controllers/jobs/AmpelMail.php b/application/controllers/jobs/AmpelMail.php index e27bd5f82..e29cac8fd 100644 --- a/application/controllers/jobs/AmpelMail.php +++ b/application/controllers/jobs/AmpelMail.php @@ -11,12 +11,13 @@ * @filesource */ -if (! defined('BASEPATH')) - exit('No direct script access allowed'); +if (!defined('BASEPATH')) exit('No direct script access allowed'); -class AmpelMail extends FHC_Controller +class AmpelMail extends CLI_Controller { - private $CIS_AMPELVERWALTUNG_URL; + const CIS_AMPELVERWALTUNG_URL = CIS_ROOT. "cis/index.php?menu=". + CIS_ROOT. "cis/menu.php?content_id=&content=". + CIS_ROOT. "cis/private/tools/ampelverwaltung.php"; /** * Constructor @@ -25,39 +26,12 @@ class AmpelMail extends FHC_Controller { parent::__construct(); - // Allow script execution only from CLI - if ($this->input->is_cli_request()) - { - $cli = true; - } - else - { - $this->output->set_status_header(403, 'Jobs must be run from the CLI'); - echo "Jobs must be run from the CLI"; - exit; - } - $this->CIS_AMPELVERWALTUNG_URL = CIS_ROOT. "cis/index.php?menu=". - CIS_ROOT. "cis/menu.php?content_id=&content=". - CIS_ROOT. "cis/private/tools/ampelverwaltung.php"; + // Load helpers + $this->load->helper('hlp_sancho'); + // Load models $this->load->model('content/Ampel_model', 'AmpelModel'); $this->load->model('person/Person_model', 'PersonModel'); - - // Load helpers - $this->load->helper('hlp_sancho'); - } - - /** - * Main function index as help - * - * @return void - */ - public function index() - { - $result = "The following are the available command line interface commands\n\n"; - $result .= "php index.ci.php jobs/AmpelMail generateAmpelMail"; - - echo $result. PHP_EOL; } /** @@ -210,7 +184,7 @@ class AmpelMail extends FHC_Controller 'uid' => $uid, 'firstName' => $firstName, 'ampel_list' => $html_text, - 'link' => $this->CIS_AMPELVERWALTUNG_URL + 'link' => self::CIS_AMPELVERWALTUNG_URL ); } return $ampel_data_arr; diff --git a/application/controllers/jobs/LVPlanJob.php b/application/controllers/jobs/LVPlanJob.php index dd368974c..2688e5cc9 100644 --- a/application/controllers/jobs/LVPlanJob.php +++ b/application/controllers/jobs/LVPlanJob.php @@ -17,10 +17,9 @@ * * Authors: Andreas Oesterreicher */ -if (! defined('BASEPATH')) - exit('No direct script access allowed'); +if (!defined('BASEPATH')) exit('No direct script access allowed'); -class LVPlanJob extends FHC_Controller +class LVPlanJob extends CLI_Controller { /** * Initialize LVPlanJob Class @@ -29,32 +28,7 @@ class LVPlanJob extends FHC_Controller */ public function __construct() { - // An empty array as parameter will ensure that this controller is ONLY callable from command line - parent::__construct(array()); - - if ($this->input->is_cli_request()) - { - $cli = true; - } - else - { - $this->output->set_status_header(403, 'Jobs must be run from the CLI'); - echo "Jobs must be run from the CLI"; - exit; - } - } - - /** - * Main function index as help - * - * @return void - */ - public function index() - { - $result = "The following are the available command line interface commands\n\n"; - $result .= "php ".$this->config->item('index_page')." jobs/LVPlanJob AddDirectGroups"; - - echo $result.PHP_EOL; + parent::__construct(); } /** diff --git a/application/controllers/jobs/Prestudentstatus.php b/application/controllers/jobs/Prestudentstatus.php index 3f8a0e903..27cafeb0e 100644 --- a/application/controllers/jobs/Prestudentstatus.php +++ b/application/controllers/jobs/Prestudentstatus.php @@ -7,8 +7,7 @@ if (! defined('BASEPATH')) * Database Class * */ - -class Prestudentstatus extends FHC_Controller +class Prestudentstatus extends CLI_Controller { /** * Initialize Prestudentstatus Class @@ -17,32 +16,7 @@ class Prestudentstatus extends FHC_Controller */ public function __construct() { - // An empty array as parameter will ensure that this controller is ONLY callable from command line - parent::__construct(array()); - - if ($this->input->is_cli_request()) - { - $cli = true; - } - else - { - $this->output->set_status_header(403, 'Jobs must be run from the CLI'); - echo "Jobs must be run from the CLI"; - exit; - } - } - - /** - * Main function index as help - * - * @return void - */ - public function index() - { - $result = "The following are the available command line interface commands\n\n"; - $result .= "php ".$this->config->item('index_page')." jobs/Prestudentstatus CorrectStudienplan"; - - echo $result.PHP_EOL; + parent::__construct(); } /** diff --git a/application/controllers/jobs/ReihungstestJob.php b/application/controllers/jobs/ReihungstestJob.php index 7f952f96f..98932911b 100644 --- a/application/controllers/jobs/ReihungstestJob.php +++ b/application/controllers/jobs/ReihungstestJob.php @@ -1,11 +1,9 @@ input->is_cli_request()) - { - $cli = true; - } - else - { - $this->output->set_status_header(403, 'Jobs must be run from the CLI'); - echo "Jobs must be run from the CLI"; - exit; - } - - $this->VILESCI_RT_VERWALTUNGS_URL = site_url('/organisation/Reihungstest'); - // Load models $this->load->model('crm/Reihungstest_model', 'ReihungstestModel'); $this->load->model('crm/RtStudienplan_model', 'RtStudienplanModel'); @@ -38,18 +22,8 @@ class ReihungstestJob extends FHC_Controller } /** - * Main function index as help - * - * @return void + * runReihungstestJob */ - public function index() - { - $result = "The following are the available command line interface commands\n\n"; - $result .= "php index.ci.php jobs/ReihungstestJob runReihungstestInfo"; - - echo $result. PHP_EOL; - } - public function runReihungstestJob() { // Get study plans that have no assigned placement tests yet @@ -92,6 +66,9 @@ class ReihungstestJob extends FHC_Controller } } + /** + * runZentraleReihungstestAnmeldefristAssistenzJob + */ public function runZentraleReihungstestAnmeldefristAssistenzJob() { // Get placement tests where registration date was yesterday @@ -198,7 +175,6 @@ class ReihungstestJob extends FHC_Controller $mailcontent .= '

Mail an alle schicken

'; } $mailcontent_data_arr['table'] = $mailcontent; - //$mailcontent_data_arr['link'] = $this->VILESCI_RT_VERWALTUNGS_URL; // Send email in Sancho design if (!isEmptyString($mailcontent)) @@ -215,7 +191,9 @@ class ReihungstestJob extends FHC_Controller } } - // Checks, if an applicant was assigned to a test after Anmeldefrist + /** + * Checks, if an applicant was assigned to a test after Anmeldefrist + */ public function runZentraleReihungstestNachtraeglichHinzugefuegtJob() { // Get applicants that have been added to a test after Anmeldefrist @@ -315,6 +293,9 @@ class ReihungstestJob extends FHC_Controller } } + /** + * runRemindApplicantsOfPlacementTestJob + */ public function runRemindApplicantsOfPlacementTestJob() { // Get placement tests with testdate within 3 working days @@ -473,8 +454,7 @@ class ReihungstestJob extends FHC_Controller // Set associative array with the prepared HTML tables and URL be used by the template's variables $content_data_arr['studienplan_list'] = $studienplan_list; $content_data_arr['freie_plaetze_list'] = $freie_plaetze_list; - $content_data_arr['link'] = $this->VILESCI_RT_VERWALTUNGS_URL; - ; + $content_data_arr['link'] = site_url('/organisation/Reihungstest'); return $content_data_arr; } diff --git a/application/core/CLI_Controller.php b/application/core/CLI_Controller.php new file mode 100644 index 000000000..61e3eb4c9 --- /dev/null +++ b/application/core/CLI_Controller.php @@ -0,0 +1,102 @@ +_isAllowed(); + } + + /** + * By default the index is used to print the help message to explain how to call them from command line + */ + public function index() + { + $this->printHelp(); + } + + /** + * Retrieves all the public methods of the called controller and display + * a help message to explain how to call them from command line + */ + protected function printHelp() + { + $this->load->library('EPrintfLib'); // loads the EPrintfLib to format the output + + $this->eprintflib->printInfo('The following are the available commands:'); + $this->eprintflib->printInfo(''); + + // Gets a list of public methods of the called controller + $reflectionClass = new ReflectionClass($this->router->class); + $methods = $reflectionClass->getMethods(ReflectionMethod::IS_PUBLIC); + + // Loops through them + foreach ($methods as $method) + { + // Disascard the black listed methods + if (!in_array($method->name, self::BLACKLIST_METHODS)) + { + // Formatted message + $info = sprintf( + self::INFO_FORMAT, // Message format + PHP_BINARY, // PHP binary name + index_page(), // CI index page name + $this->router->class, // The called controller name + $method->name // The called controller current method name + ); + + // Retrieves the parameter names for the current method + $reflectionMethod = new ReflectionMethod($this->router->class, $method->name); + $parameters = $reflectionMethod->getParameters(); + + // Loops through them + foreach ($parameters as $parameter) + { + $info .= sprintf( + $parameter->isOptional() ? self::OPTIONAL_PARAM_FORMAT : self::REQUIRED_PARAM_FORMAT, // Parameter message format required/optional + $parameter->getName() // Current method parameter name + ); + } + + // Print the info message + $this->eprintflib->printInfo($info); + $this->eprintflib->printInfo(''); + } + } + } + + /** + * Checks if the controller is called from the command line, if NOT the execution is immediately stopped + */ + private function _isAllowed() + { + if (!$this->input->is_cli_request()) + { + $this->output->set_status_header(REST_Controller::HTTP_UNAUTHORIZED); + + $this->load->library('EPrintfLib'); // loads the EPrintfLib to format the output + + // Prints the main error message + $this->eprintflib->printError('You are not authorized to access this content'); + // Prints the called controller name + $this->eprintflib->printInfo('Controller name: '.$this->router->class); + // Prints the called controller method name + $this->eprintflib->printInfo('Method name: '.$this->router->method); + + exit; + } + } +}