Advertisement
Guest User

Untitled

a guest
May 16th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 14.07 KB | None | 0 0
  1. <?php
  2.  
  3. class SchoolController extends Zend_Controller_Action {
  4.     /**
  5.      * Model of the currently logged in user
  6.      * @var Application_Model_User
  7.      */
  8.     protected $user;
  9.     /**
  10.      * Reference to authentication session
  11.      * @var Zend_Session_Namespace
  12.      */
  13.     protected $auth;
  14.    
  15.     /**
  16.      * Model of the vacancy
  17.      * @var Application_Model_Vacancy
  18.      */
  19.     protected $vacancy;
  20.    
  21.     /**
  22.      * Reference user mapper
  23.      * @var Application_Model_UserMapper
  24.      */
  25.     protected $usermapper;
  26.    
  27.     /**
  28.      * Reference vacancy mapper
  29.      * @var Application_Model_VacancyMapper
  30.      */
  31.     protected $vacancymapper;
  32.    
  33.     public function preDispatch() {
  34.         $this->auth = new Zend_Session_Namespace('auth');
  35.         if (empty($this->auth->user) || $this->auth->user->role != Application_Model_User::ROLE_SCHOOL) {
  36.             return $this->_helper->redirector('index', 'login');
  37.         }
  38.         $this->user = $this->auth->user;
  39.        
  40.         $this->usermapper = new Application_Model_UserMapper();
  41.     }
  42.    
  43.     public function indexAction() {
  44.         return $this->_helper->redirector('vacancies', 'school');
  45.     }
  46.  
  47.     /**
  48.      * Function for school to edit it's own data.
  49.      *
  50.      * If no button was pushed populate the form with the data from the models.
  51.      *
  52.      * If user pushed "cancel", redirect to the default page.
  53.      *
  54.      * If user pushed "save" try to put all the data to the User and UserInfo models.
  55.      * If there are no exceptions, use UserMapper to save the data from the models to the database.
  56.      * If there are exceptions, populate the form and show an error message.
  57.      * @author Sami Suuronen
  58.      */
  59.     public function editschoolAction() {
  60.         //Save or cancel button was pushed
  61.         if ($this->getRequest()->isPost()) {
  62.            
  63.             //Get the post parameters
  64.             $params = $this->getRequest()->getParams();
  65.            
  66.             //Strip html tags
  67.             foreach($params as $param => $value)
  68.                 $params[$param] = strip_tags($params[$param]);
  69.              
  70.             //If user pushed "cancel" redirect to the default page.
  71.             if (array_key_exists('cancel',$params)) {
  72.                 return $this->_helper->redirector('index','school');
  73.             }
  74.             //If user pushed "save" try to put all the data from the form to the User and UserInfo models.
  75.             else if (array_key_exists('save',$params)) {
  76.                 //An array which contains the easy form fields.
  77.                 $formfields = array(    'contactfirstname',
  78.                                         'contactlastname',
  79.                                         'address',
  80.                                         'postnumber',
  81.                                         'postoffice',
  82.                                         'phonenumber',
  83.                                         'email',
  84.                                         'description'
  85.                 );
  86.                 //An array which contains the exceptions ocurred when trying to put formfields
  87.                 //to User and UserInfo models.
  88.                 $this->view->errors = array();
  89.  
  90.                 //Try to put first the easy fields to the model and catch any exceptions.
  91.                 foreach($formfields as $formfield) {
  92.                     try {
  93.                         $this->user->{$formfield} = $params[$formfield];
  94.                     }
  95.                     catch(Application_Exception_UserInfoException $e) {
  96.                         $this->view->errors[$formfield] = $e->getMessage();
  97.                     }
  98.                 }
  99. //FIXME: Merkit joita ei sallita salasanaan?
  100.                 //Then handle the special fields separately            
  101.                 //If there is something in the password fields.
  102.                 if (strlen($params['password1']) > 0 || strlen($params['password2']) > 0){
  103.                     //If the fields contain equal data.
  104.                     if ($params['password1'] == $params['password2']) {
  105.                         //Try to put the new password to the model.
  106.                         try{
  107.                             $this->user->password = $params['password1'];
  108.                         }
  109.                         catch (Application_Exception_UserInfoException $e) {
  110.                             $this->view->errors['password'] = $e->getMessage();
  111.                         }
  112.                     }
  113.                     else {
  114.                         $this->view->errors['password'] = 'Not equals';
  115.                     }
  116.                 }
  117.                
  118.                 //If there were no exceptions, save the User and UserInfo models to the database
  119.                 //and redirect to the default page.
  120.                 if (empty($this->view->errors)) {
  121.                     $this->usermapper->save($this->user);
  122.                     return $this->_helper->redirector('index','school');
  123.                 }
  124.             }
  125.         }
  126. //FIXME: nämä tiedot kannasta ei mallista!!!
  127.         //No button was pushed or there were exceptions when trying to save
  128.         //the form data to the User and UserInfo models. Populate the form.
  129.         $this->view->expires = $this->user->getExpires();
  130.         $this->view->schoolname = $this->user->getSchoolname();
  131.         $this->view->contactfirstname = $this->user->getContactfirstname();
  132.         $this->view->contactlastname = $this->user->getContactlastname();
  133.         $this->view->address = $this->user->getAddress();
  134.         $this->view->postnumber = $this->user->getPostnumber();
  135.         $this->view->postoffice = $this->user->getPostoffice();
  136.         $this->view->phonenumber = $this->user->getPhonenumber();
  137.         $this->view->email = $this->user->getEmail();
  138.         $this->view->description = $this->user->getDescription();              
  139.     }
  140.    
  141.  
  142.     /**
  143.      * Function for school to add a new vacancy.
  144.      *
  145.      * If the form is "addvacancy" and
  146.      * if the user pushed "savedraf" try to put the form fields to the Vacancy model and save it as a draft using VacancyMapper,
  147.      * if the user pushed "publish" try to put the form fields to the Vacancy model and save it as a new vacancy VacancyMapper,
  148.      * if the user pushed "cancel" redirect to the default page.
  149.      *
  150.      * If the form is "sketch" and
  151.      * if the user pushed "delete" delete the vacancy,
  152.      * if the user pushed "edit" populate the form with the vacancy data to edit,
  153.      * if the user pushed "publish" change the vacancy's "draft" property to "false".
  154.      *
  155.      * addvacancy   addvacancy      addvacancy  addvacancy  sketch  sketch  sketch
  156.      * cancel       vacancytoedit   savedraft   publish
  157.      *
  158.      * @author Sami Suuronen
  159.      */
  160.     public function addvacancyAction() {
  161.         //Set the title
  162.         $this->view->title = $this->view->translate('Uusi sijaisuus');
  163.         //Create a new mapper object to transfer the vacancy data from the vacancy object to db and viceversa
  164.         $this->vacancymapper = new Application_Model_VacancyMapper();
  165.        
  166.         //Figure out the action to take
  167.         //User has pushed one of the submit buttons
  168.         if ($this->getRequest()->isPost()) {
  169.             //Get the post parameters
  170.             $params = $this->getRequest()->getParams();
  171.             $this->view->p = $params;
  172.             if($params['form'] == 'addvacancy'){
  173.                 if (array_key_exists('newtime',$params))
  174.                     $action = 'newtime';
  175.                 else if (array_key_exists('getsubjects',$params))
  176.                     $action = 'getsubjects';
  177.                 elseif (array_key_exists('savedraft',$params) || array_key_exists('publish',$params))
  178.                     $action = 'save';
  179.                 else if (array_key_exists('cancel',$params))
  180.                     $action = 'cancel';
  181.                
  182.             }
  183.             else if($params['form'] == 'sketch'){
  184.                 if (array_key_exists('delete',$params))
  185.                     $action = 'delete';
  186.                 else if (array_key_exists('edit',$params))
  187.                     $action = 'edit';
  188.                 else if (array_key_exists('publish',$params))
  189.                     $action = 'publishsketch';
  190.             }
  191.         }
  192.         else{
  193.             $action = 'newvacancy';
  194.         }
  195.        
  196.         switch($action){
  197.             case 'newtime':{
  198.                 $this->view->unsavedformdata = array();
  199.                 for($i=1; $i<=$params['vacancytimes']; $i++) {
  200.                     $this->view->unsavedformdata['startdate'.$i] = $params['startdate'.$i];
  201.                     $this->view->unsavedformdata['starttime'.$i] = $params['starttime'.$i];
  202.                     $this->view->unsavedformdata['enddate'.$i] = $params['enddate'.$i];
  203.                     $this->view->unsavedformdata['endtime'.$i] = $params['endtime'.$i];
  204.                 }
  205.                 $this->view->unsavedformdata['schooltype'] = $params['schooltype'];
  206.                 $this->view->unsavedformdata['vacancycontactfirstname'] = $params['vacancycontactfirstname'];
  207.                 $this->view->unsavedformdata['vacancycontactlastname'] = $params['vacancycontactlastname'];
  208.                 $this->view->unsavedformdata['vacancycontactphonenumber'] = $params['vacancycontactphonenumber'];
  209.                 $this->view->unsavedformdata['vacancycontactemail'] = $params['vacancycontactemail'];
  210.                 $this->view->unsavedformdata['description'] = $params['description'];
  211.                 $this->view->vacancytimes = (int)$params['vacancytimes'];
  212.                 $this->view->vacancytimes++;   
  213.                 break;
  214.             }
  215.             case 'getsubjects':{
  216.                 $this->view->unsavedformdata = array();
  217.                 for($i=1; $i<=$params['vacancytimes']; $i++) {
  218.                     $this->view->unsavedformdata['startdate'.$i] = $params['startdate'.$i];
  219.                     $this->view->unsavedformdata['starttime'.$i] = $params['starttime'.$i];
  220.                     $this->view->unsavedformdata['enddate'.$i] = $params['enddate'.$i];
  221.                     $this->view->unsavedformdata['endtime'.$i] = $params['endtime'.$i];
  222.                 }
  223.                 $this->view->unsavedformdata['schooltype'] = $params['schooltype'];
  224.                 $this->view->unsavedformdata['vacancycontactfirstname'] = $params['vacancycontactfirstname'];
  225.                 $this->view->unsavedformdata['vacancycontactlastname'] = $params['vacancycontactlastname'];
  226.                 $this->view->unsavedformdata['vacancycontactphonenumber'] = $params['vacancycontactphonenumber'];
  227.                 $this->view->unsavedformdata['vacancycontactemail'] = $params['vacancycontactemail'];
  228.                 $this->view->unsavedformdata['description'] = $params['description'];
  229.                 $this->view->vacancytimes = (int)$params['vacancytimes'];
  230.                 break;
  231.             }
  232.             case ('save'):{
  233. $this->vacancies = array();
  234. $this->view->vacancieserrors = array();
  235. for($i=1; $i<=$params['vacancytimes']; $i++) {
  236.                 if(array_key_exists('vacancytoeditid',$params))
  237.                     $this->vacancies[$i] = $this->vacancymapper->get($params['vacancytoeditid'.$i]);
  238.                 else
  239.                     $this->vacancies[$i] = new Application_Model_Vacancy();
  240.                 //Associate this school with the vacancy
  241.                 $this->vacancies[$i]->school = $this->user;
  242.                
  243.                 //Testing
  244.                 $this->vacancies[$i]->qualification = true;
  245.                
  246.                 //An array which contains parameter names and values to put to the model.
  247.                 $formfields = array(
  248.                     'starttime' => $params['startdate'.$i].' '.$params['starttime'.$i],
  249.                     'endtime' => $params['enddate'.$i].' '.$params['endtime'.$i],
  250.                     'schooltype' => $params['schooltype'],
  251.                
  252.                     //Testing
  253.                     'classyear' => '8',
  254.                
  255. //                  'contactfirstname' => $params['vacancycontactfirstname'],
  256. //                  'contactlastname' => $params['vacancycontactlastname'],
  257. //                  'contactphonenumber' => $params['vacancycontactphonenumber'],
  258. //                  'contactemail' => $params['vacancycontactemail'],
  259.                     'description' => $params['description']
  260.                 );
  261.                 //An array which contains the exceptions ocurred when trying to put formfields
  262.                 //to User and UserInfo models.
  263.                 $this->errors = array();
  264.  
  265.                 //Try to put first the easy fields to the model and catch any exceptions...
  266.                 foreach($formfields as $formfield => $value) {
  267.                     try {
  268.                         $this->vacancies[$i]->{$formfield} = $value;
  269.                     }
  270.                     catch(Application_Exception_UserInfoException $e) {
  271.                         $this->errors[$formfield] = $e->getMessage();
  272.                     }
  273.                 }
  274.                 $this->view->vacancieserrors[$i] = $this->errors;
  275.                
  276. //              foreach($params['subjects'] as $subject) {
  277. //                  $this->vacancy{$i}->addSubject($subject);
  278. //              }
  279.                
  280.                 if($i>1) {
  281.                     $this->vacancies[$i]->parent = $this->vacancies['1'];
  282.                     $this->vacancies['1']->addChild($this->vacancies[$i]);
  283.                 }
  284.  
  285.                
  286.                 if (array_key_exists('savedraft',$params))
  287.                     $this->vacancies[$i]->draft = true;
  288.                 else if (array_key_exists('publish',$params))
  289.                     $this->vacancies[$i]->draft = false;
  290. }
  291. $i--;
  292.                 //If there are no errors
  293.                 $errors = false;
  294.                 foreach($this->view->vacancieserrors as $vacancyerrors)
  295.                     if (!empty($vacancyerrors))
  296.                         $errors = true;
  297.  
  298.                 if(!$errors){
  299.                     //Save the vacancy data as a draft and stay on the same page
  300.                     if (array_key_exists('savedraft',$params)) {
  301.                         $this->vacancymapper->save($this->vacancies[$params['vacancytimes']]);
  302.                         return $this->_helper->redirector('addvacancy','school');
  303.                     }
  304.                     //Publish the vacancy data and go to the default page
  305.                     else if (array_key_exists('publish',$params)) {
  306.                         $this->vacancymapper->save($this->vacancies[$params['vacancytimes']]);
  307.                         return $this->_helper->redirector('index','school');
  308.                     }
  309.                 }
  310. //FIXME:tekemättä
  311.                 //There were errors, stay on the page and populate the form
  312.                 else
  313.                     $this->view->drafts = $this->vacancies;
  314.                 break;
  315.             }
  316.                
  317.             case 'cancel':{
  318.                 return $this->_helper->redirector('index','school');
  319.                 break;
  320.             }
  321.             case 'delete':{
  322.                 $this->vacancymapper->delete($this->vacancymapper->get($params['vacancyid']));
  323.                 return $this->_helper->redirector('addvacancy','school');
  324.                 break;
  325.             }
  326.             case 'edit':{
  327.                 $this->view->sketch = $this->vacancymapper->get($params['vacancyid']);
  328.                 break;
  329.             }
  330.             case 'publishsketch':{
  331.                 $sketchtopublish = $this->vacancymapper->get($params['vacancyid']);
  332.                 $sketchtopublish->draft = false;
  333.                 $this->vacancymapper->save($sketchtopublish);
  334.                 return $this->_helper->redirector('addvacancy','school');
  335.                 break;
  336.             }
  337.         }
  338.        
  339.         //This is executed allways
  340.         //Set the number of vacancy times
  341.         if(!isset($this->view->vacancytimes))
  342.             $this->view->vacancytimes = 1;
  343.         //Get the schooltypes
  344.         $this->view->schooltypes = $this->user->getSchooltype();
  345.         //Get the sketches
  346.         $this->view->sketches = $this->vacancymapper->fetch(array(  'Vacancies.SchoolID=?' => $this->user->id,
  347.                                                                     'Vacancies.Draft=?' => '1'));
  348.     }
  349.    
  350.    
  351.    
  352. //-------------------------------------------------------  
  353.     public function vacanciesAction(){
  354.         $vacancies = array();
  355.         $this->vacancymapper = new Application_Model_VacancyMapper();
  356.         $vacancymodels = $this->vacancymapper->fetch(array( 'Vacancies.SchoolID=?' => $this->user->id));
  357.         foreach ($vacancymodels as $vacancymodel) {
  358.             $vacancydata = array(   'vacancyid' => $vacancymodel->id,
  359.                                         'teacherfirstname' => 'Etu',//$vacancymodel->getFiller()->info->firstname,
  360.                                         'teacherlastname' => 'Suku',//$vacancymodel->getFiller()->info->lastname,
  361.                                         'starttime' => $vacancymodel->starttime,
  362.                                         'endtime' => $vacancymodel->endtime,
  363.                                         'schooltype' => (string)$vacancymodel->schooltype,
  364.                                         'subjects' => $vacancymodel->subjects,
  365.                                         'classyear' => $vacancymodel->classyear,
  366.                                         'description' => $vacancymodel->description
  367.                                     );
  368.             array_push($vacancies,$vacancydata);
  369.         }
  370.         $this->view->vacancies = $vacancies;
  371.         $vacancy = (int)$this->getRequest()->getParam('vacancy', 0);
  372.         if ($vacancy > 0) {
  373.             $vacancy = $this->vacancymapper->get($vacancy);
  374.             //Make sure this is school's own vacancy
  375.             if ($vacancy->school->id == $this->user->id)
  376.                 $this->view->vacancy = $vacancy;
  377.         }
  378.     }
  379. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement