Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class SchoolController extends Zend_Controller_Action {
- /**
- * Model of the currently logged in user
- * @var Application_Model_User
- */
- protected $user;
- /**
- * Reference to authentication session
- * @var Zend_Session_Namespace
- */
- protected $auth;
- /**
- * Model of the vacancy
- * @var Application_Model_Vacancy
- */
- protected $vacancy;
- /**
- * Reference user mapper
- * @var Application_Model_UserMapper
- */
- protected $usermapper;
- /**
- * Reference vacancy mapper
- * @var Application_Model_VacancyMapper
- */
- protected $vacancymapper;
- public function preDispatch() {
- $this->auth = new Zend_Session_Namespace('auth');
- if (empty($this->auth->user) || $this->auth->user->role != Application_Model_User::ROLE_SCHOOL) {
- return $this->_helper->redirector('index', 'login');
- }
- $this->user = $this->auth->user;
- $this->usermapper = new Application_Model_UserMapper();
- }
- public function indexAction() {
- return $this->_helper->redirector('vacancies', 'school');
- }
- /**
- * Function for school to edit it's own data.
- *
- * If no button was pushed populate the form with the data from the models.
- *
- * If user pushed "cancel", redirect to the default page.
- *
- * If user pushed "save" try to put all the data to the User and UserInfo models.
- * If there are no exceptions, use UserMapper to save the data from the models to the database.
- * If there are exceptions, populate the form and show an error message.
- * @author Sami Suuronen
- */
- public function editschoolAction() {
- //Save or cancel button was pushed
- if ($this->getRequest()->isPost()) {
- //Get the post parameters
- $params = $this->getRequest()->getParams();
- //Strip html tags
- foreach($params as $param => $value)
- $params[$param] = strip_tags($params[$param]);
- //If user pushed "cancel" redirect to the default page.
- if (array_key_exists('cancel',$params)) {
- return $this->_helper->redirector('index','school');
- }
- //If user pushed "save" try to put all the data from the form to the User and UserInfo models.
- else if (array_key_exists('save',$params)) {
- //An array which contains the easy form fields.
- $formfields = array( 'contactfirstname',
- 'contactlastname',
- 'address',
- 'postnumber',
- 'postoffice',
- 'phonenumber',
- 'email',
- 'description'
- );
- //An array which contains the exceptions ocurred when trying to put formfields
- //to User and UserInfo models.
- $this->view->errors = array();
- //Try to put first the easy fields to the model and catch any exceptions.
- foreach($formfields as $formfield) {
- try {
- $this->user->{$formfield} = $params[$formfield];
- }
- catch(Application_Exception_UserInfoException $e) {
- $this->view->errors[$formfield] = $e->getMessage();
- }
- }
- //FIXME: Merkit joita ei sallita salasanaan?
- //Then handle the special fields separately
- //If there is something in the password fields.
- if (strlen($params['password1']) > 0 || strlen($params['password2']) > 0){
- //If the fields contain equal data.
- if ($params['password1'] == $params['password2']) {
- //Try to put the new password to the model.
- try{
- $this->user->password = $params['password1'];
- }
- catch (Application_Exception_UserInfoException $e) {
- $this->view->errors['password'] = $e->getMessage();
- }
- }
- else {
- $this->view->errors['password'] = 'Not equals';
- }
- }
- //If there were no exceptions, save the User and UserInfo models to the database
- //and redirect to the default page.
- if (empty($this->view->errors)) {
- $this->usermapper->save($this->user);
- return $this->_helper->redirector('index','school');
- }
- }
- }
- //FIXME: nämä tiedot kannasta ei mallista!!!
- //No button was pushed or there were exceptions when trying to save
- //the form data to the User and UserInfo models. Populate the form.
- $this->view->expires = $this->user->getExpires();
- $this->view->schoolname = $this->user->getSchoolname();
- $this->view->contactfirstname = $this->user->getContactfirstname();
- $this->view->contactlastname = $this->user->getContactlastname();
- $this->view->address = $this->user->getAddress();
- $this->view->postnumber = $this->user->getPostnumber();
- $this->view->postoffice = $this->user->getPostoffice();
- $this->view->phonenumber = $this->user->getPhonenumber();
- $this->view->email = $this->user->getEmail();
- $this->view->description = $this->user->getDescription();
- }
- /**
- * Function for school to add a new vacancy.
- *
- * If the form is "addvacancy" and
- * if the user pushed "savedraf" try to put the form fields to the Vacancy model and save it as a draft using VacancyMapper,
- * if the user pushed "publish" try to put the form fields to the Vacancy model and save it as a new vacancy VacancyMapper,
- * if the user pushed "cancel" redirect to the default page.
- *
- * If the form is "sketch" and
- * if the user pushed "delete" delete the vacancy,
- * if the user pushed "edit" populate the form with the vacancy data to edit,
- * if the user pushed "publish" change the vacancy's "draft" property to "false".
- *
- * addvacancy addvacancy addvacancy addvacancy sketch sketch sketch
- * cancel vacancytoedit savedraft publish
- *
- * @author Sami Suuronen
- */
- public function addvacancyAction() {
- //Set the title
- $this->view->title = $this->view->translate('Uusi sijaisuus');
- //Create a new mapper object to transfer the vacancy data from the vacancy object to db and viceversa
- $this->vacancymapper = new Application_Model_VacancyMapper();
- //Figure out the action to take
- //User has pushed one of the submit buttons
- if ($this->getRequest()->isPost()) {
- //Get the post parameters
- $params = $this->getRequest()->getParams();
- $this->view->p = $params;
- if($params['form'] == 'addvacancy'){
- if (array_key_exists('newtime',$params))
- $action = 'newtime';
- else if (array_key_exists('getsubjects',$params))
- $action = 'getsubjects';
- elseif (array_key_exists('savedraft',$params) || array_key_exists('publish',$params))
- $action = 'save';
- else if (array_key_exists('cancel',$params))
- $action = 'cancel';
- }
- else if($params['form'] == 'sketch'){
- if (array_key_exists('delete',$params))
- $action = 'delete';
- else if (array_key_exists('edit',$params))
- $action = 'edit';
- else if (array_key_exists('publish',$params))
- $action = 'publishsketch';
- }
- }
- else{
- $action = 'newvacancy';
- }
- switch($action){
- case 'newtime':{
- $this->view->unsavedformdata = array();
- for($i=1; $i<=$params['vacancytimes']; $i++) {
- $this->view->unsavedformdata['startdate'.$i] = $params['startdate'.$i];
- $this->view->unsavedformdata['starttime'.$i] = $params['starttime'.$i];
- $this->view->unsavedformdata['enddate'.$i] = $params['enddate'.$i];
- $this->view->unsavedformdata['endtime'.$i] = $params['endtime'.$i];
- }
- $this->view->unsavedformdata['schooltype'] = $params['schooltype'];
- $this->view->unsavedformdata['vacancycontactfirstname'] = $params['vacancycontactfirstname'];
- $this->view->unsavedformdata['vacancycontactlastname'] = $params['vacancycontactlastname'];
- $this->view->unsavedformdata['vacancycontactphonenumber'] = $params['vacancycontactphonenumber'];
- $this->view->unsavedformdata['vacancycontactemail'] = $params['vacancycontactemail'];
- $this->view->unsavedformdata['description'] = $params['description'];
- $this->view->vacancytimes = (int)$params['vacancytimes'];
- $this->view->vacancytimes++;
- break;
- }
- case 'getsubjects':{
- $this->view->unsavedformdata = array();
- for($i=1; $i<=$params['vacancytimes']; $i++) {
- $this->view->unsavedformdata['startdate'.$i] = $params['startdate'.$i];
- $this->view->unsavedformdata['starttime'.$i] = $params['starttime'.$i];
- $this->view->unsavedformdata['enddate'.$i] = $params['enddate'.$i];
- $this->view->unsavedformdata['endtime'.$i] = $params['endtime'.$i];
- }
- $this->view->unsavedformdata['schooltype'] = $params['schooltype'];
- $this->view->unsavedformdata['vacancycontactfirstname'] = $params['vacancycontactfirstname'];
- $this->view->unsavedformdata['vacancycontactlastname'] = $params['vacancycontactlastname'];
- $this->view->unsavedformdata['vacancycontactphonenumber'] = $params['vacancycontactphonenumber'];
- $this->view->unsavedformdata['vacancycontactemail'] = $params['vacancycontactemail'];
- $this->view->unsavedformdata['description'] = $params['description'];
- $this->view->vacancytimes = (int)$params['vacancytimes'];
- break;
- }
- case ('save'):{
- $this->vacancies = array();
- $this->view->vacancieserrors = array();
- for($i=1; $i<=$params['vacancytimes']; $i++) {
- if(array_key_exists('vacancytoeditid',$params))
- $this->vacancies[$i] = $this->vacancymapper->get($params['vacancytoeditid'.$i]);
- else
- $this->vacancies[$i] = new Application_Model_Vacancy();
- //Associate this school with the vacancy
- $this->vacancies[$i]->school = $this->user;
- //Testing
- $this->vacancies[$i]->qualification = true;
- //An array which contains parameter names and values to put to the model.
- $formfields = array(
- 'starttime' => $params['startdate'.$i].' '.$params['starttime'.$i],
- 'endtime' => $params['enddate'.$i].' '.$params['endtime'.$i],
- 'schooltype' => $params['schooltype'],
- //Testing
- 'classyear' => '8',
- // 'contactfirstname' => $params['vacancycontactfirstname'],
- // 'contactlastname' => $params['vacancycontactlastname'],
- // 'contactphonenumber' => $params['vacancycontactphonenumber'],
- // 'contactemail' => $params['vacancycontactemail'],
- 'description' => $params['description']
- );
- //An array which contains the exceptions ocurred when trying to put formfields
- //to User and UserInfo models.
- $this->errors = array();
- //Try to put first the easy fields to the model and catch any exceptions...
- foreach($formfields as $formfield => $value) {
- try {
- $this->vacancies[$i]->{$formfield} = $value;
- }
- catch(Application_Exception_UserInfoException $e) {
- $this->errors[$formfield] = $e->getMessage();
- }
- }
- $this->view->vacancieserrors[$i] = $this->errors;
- // foreach($params['subjects'] as $subject) {
- // $this->vacancy{$i}->addSubject($subject);
- // }
- if($i>1) {
- $this->vacancies[$i]->parent = $this->vacancies['1'];
- $this->vacancies['1']->addChild($this->vacancies[$i]);
- }
- if (array_key_exists('savedraft',$params))
- $this->vacancies[$i]->draft = true;
- else if (array_key_exists('publish',$params))
- $this->vacancies[$i]->draft = false;
- }
- $i--;
- //If there are no errors
- $errors = false;
- foreach($this->view->vacancieserrors as $vacancyerrors)
- if (!empty($vacancyerrors))
- $errors = true;
- if(!$errors){
- //Save the vacancy data as a draft and stay on the same page
- if (array_key_exists('savedraft',$params)) {
- $this->vacancymapper->save($this->vacancies[$params['vacancytimes']]);
- return $this->_helper->redirector('addvacancy','school');
- }
- //Publish the vacancy data and go to the default page
- else if (array_key_exists('publish',$params)) {
- $this->vacancymapper->save($this->vacancies[$params['vacancytimes']]);
- return $this->_helper->redirector('index','school');
- }
- }
- //FIXME:tekemättä
- //There were errors, stay on the page and populate the form
- else
- $this->view->drafts = $this->vacancies;
- break;
- }
- case 'cancel':{
- return $this->_helper->redirector('index','school');
- break;
- }
- case 'delete':{
- $this->vacancymapper->delete($this->vacancymapper->get($params['vacancyid']));
- return $this->_helper->redirector('addvacancy','school');
- break;
- }
- case 'edit':{
- $this->view->sketch = $this->vacancymapper->get($params['vacancyid']);
- break;
- }
- case 'publishsketch':{
- $sketchtopublish = $this->vacancymapper->get($params['vacancyid']);
- $sketchtopublish->draft = false;
- $this->vacancymapper->save($sketchtopublish);
- return $this->_helper->redirector('addvacancy','school');
- break;
- }
- }
- //This is executed allways
- //Set the number of vacancy times
- if(!isset($this->view->vacancytimes))
- $this->view->vacancytimes = 1;
- //Get the schooltypes
- $this->view->schooltypes = $this->user->getSchooltype();
- //Get the sketches
- $this->view->sketches = $this->vacancymapper->fetch(array( 'Vacancies.SchoolID=?' => $this->user->id,
- 'Vacancies.Draft=?' => '1'));
- }
- //-------------------------------------------------------
- public function vacanciesAction(){
- $vacancies = array();
- $this->vacancymapper = new Application_Model_VacancyMapper();
- $vacancymodels = $this->vacancymapper->fetch(array( 'Vacancies.SchoolID=?' => $this->user->id));
- foreach ($vacancymodels as $vacancymodel) {
- $vacancydata = array( 'vacancyid' => $vacancymodel->id,
- 'teacherfirstname' => 'Etu',//$vacancymodel->getFiller()->info->firstname,
- 'teacherlastname' => 'Suku',//$vacancymodel->getFiller()->info->lastname,
- 'starttime' => $vacancymodel->starttime,
- 'endtime' => $vacancymodel->endtime,
- 'schooltype' => (string)$vacancymodel->schooltype,
- 'subjects' => $vacancymodel->subjects,
- 'classyear' => $vacancymodel->classyear,
- 'description' => $vacancymodel->description
- );
- array_push($vacancies,$vacancydata);
- }
- $this->view->vacancies = $vacancies;
- $vacancy = (int)$this->getRequest()->getParam('vacancy', 0);
- if ($vacancy > 0) {
- $vacancy = $this->vacancymapper->get($vacancy);
- //Make sure this is school's own vacancy
- if ($vacancy->school->id == $this->user->id)
- $this->view->vacancy = $vacancy;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement