/**
  * @Override FC_Document_Manager::edit()
  *
  * This is a copy and paste of the overridden function 
  * except that the module for the form is set to intern.
  * Also, check if the folder has been set. If not create
  * one for the user and load it.
  */
 public function edit()
 {
     if (empty($this->document)) {
         $this->loadDocument();
     }
     // If the folder ID is zero then it was not found
     // when Intern_Folder::documentUpload() was called.
     // Create one and load it.
     if ($this->folder->id == 0) {
         PHPWS_Core::initModClass('intern', 'Intern_Folder.php');
         PHPWS_Core::requireInc('filecabinet', 'defines.php');
         $folder = new Intern_Folder();
         $folder->module_created = 'intern';
         $folder->title = 'intern documents';
         $folder->public_folder = FALSE;
         $folder->ftype = DOCUMENT_FOLDER;
         $folder->loadDirectory();
         $folder->save();
         $this->folder = $folder;
     }
     PHPWS_Core::initCoreClass('File.php');
     $form = new PHPWS_FORM();
     $form->addHidden('module', 'intern');
     $form->addHidden('internship', $_REQUEST['internship']);
     $form->addHidden('action', 'post_document_upload');
     $form->addHidden('ms', $this->max_size);
     $form->addHidden('folder_id', $this->folder->id);
     $form->addFile('file_name');
     $form->setSize('file_name', 30);
     $form->setLabel('file_name', dgettext('filecabinet', 'Document location'));
     $form->addText('title', $this->document->title);
     $form->setSize('title', 40);
     $form->setLabel('title', dgettext('filecabinet', 'Title'));
     $form->addTextArea('description', $this->document->description);
     $form->setLabel('description', dgettext('filecabinet', 'Description'));
     if ($this->document->id) {
         $form->addTplTag('FORM_TITLE', dgettext('filecabinet', 'Update file'));
         $form->addHidden('document_id', $this->document->id);
         $form->addSubmit('submit', dgettext('filecabinet', 'Update'));
     } else {
         $form->addTplTag('FORM_TITLE', dgettext('filecabinet', 'Upload new file'));
         $form->addSubmit('submit', dgettext('filecabinet', 'Upload'));
     }
     $form->addButton('cancel', dgettext('filecabinet', 'Cancel'));
     $form->setExtra('cancel', 'onclick="window.close()"');
     $form->setExtra('submit', 'onclick="this.style.display=\'none\'"');
     if ($this->document->id && Current_User::allow('filecabinet', 'edit_folders', $this->folder->id, 'folder', true)) {
         Cabinet::moveToForm($form, $this->folder);
     }
     $template = $form->getTemplate();
     if ($this->document->id) {
         $template['CURRENT_DOCUMENT_LABEL'] = dgettext('filecabinet', 'Current document');
         $template['CURRENT_DOCUMENT_ICON'] = $this->document->getIconView();
         $template['CURRENT_DOCUMENT_FILE'] = $this->document->file_name;
     }
     $template['MAX_SIZE_LABEL'] = dgettext('filecabinet', 'Maximum file size');
     $sys_size = str_replace('M', '', ini_get('upload_max_filesize'));
     $sys_size = $sys_size * 1000000;
     if ((int) $sys_size < (int) $this->max_size) {
         $template['MAX_SIZE'] = sprintf(dgettext('filecabinet', '%d bytes (system wide)'), $sys_size);
     } else {
         $template['MAX_SIZE'] = sprintf(dgettext('filecabinet', '%d bytes'), $this->max_size);
     }
     if ($this->document->_errors) {
         $template['ERROR'] = $this->document->printErrors();
     }
     return PHPWS_Template::process($template, 'filecabinet', 'document_edit.tpl');
     //        Layout::add(PHPWS_Template::process($template, 'filecabinet', 'document_edit.tpl'));
 }
 public static function display()
 {
     PHPWS_Core::initModClass('intern', 'Internship.php');
     PHPWS_Core::initModClass('intern', 'InternshipFactory.php');
     PHPWS_Core::initModClass('intern', 'Intern_Document.php');
     PHPWS_Core::initModClass('intern', 'Intern_Folder.php');
     PHPWS_Core::initModClass('intern', 'Agency.php');
     PHPWS_Core::initModClass('intern', 'InternshipFormView.php');
     PHPWS_Core::initModClass('intern', 'EditInternshipFormView.php');
     PHPWS_Core::initModClass('intern', 'Term.php');
     PHPWS_Core::initModClass('intern', 'Department.php');
     PHPWS_Core::initModClass('intern', 'Major.php');
     PHPWS_Core::initModClass('intern', 'GradProgram.php');
     PHPWS_Core::initModClass('intern', 'Subject.php');
     $tpl = array();
     if (isset($_REQUEST['internship_id'])) {
         /* Attempting to edit internship */
         try {
             $i = InternshipFactory::getInternshipById($_REQUEST['internship_id']);
         } catch (InternshipNotFoundException $e) {
             NQ::simple('intern', INTERN_ERROR, 'Could not locate an internship with the given ID.');
             return;
         }
         $internshipForm = new EditInternshipFormView('Edit Internship', $i);
         $internshipForm->buildInternshipForm();
         $internshipForm->plugInternship();
         $tpl['TITLE'] = 'Edit Internship';
         $form = $internshipForm->getForm();
         /*** 'Generate Contract' Button ***/
         $tpl['PDF'] = PHPWS_Text::linkAddress('intern', array('action' => 'pdf', 'id' => $i->id));
         /*** Document List ***/
         $docs = $i->getDocuments();
         if (!is_null($docs)) {
             foreach ($docs as $doc) {
                 $tpl['docs'][] = array('DOWNLOAD' => $doc->getDownloadLink('blah'), 'DELETE' => $doc->getDeleteLink());
             }
         }
         $folder = new Intern_Folder(Intern_Document::getFolderId());
         $tpl['UPLOAD_DOC'] = $folder->documentUpload($i->id);
         $wfState = $i->getWorkflowState();
         if (($wfState instanceof SigAuthReadyState || $wfState instanceof SigAuthApprovedState || $wfState instanceof DeanApprovedState || $wfState instanceof RegisteredState) && $docs < 1) {
             NQ::simple('intern', INTERN_WARNING, "No documents have been uploaded yet. Usually a copy of the signed contract document should be uploaded.");
         }
         /******************
          * Change History *
          */
         if (!is_null($i->id)) {
             PHPWS_Core::initModClass('intern', 'ChangeHistoryView.php');
             $historyView = new ChangeHistoryView($i);
             $tpl['CHANGE_LOG'] = $historyView->show();
         }
         // Show a warning if in SigAuthReadyState, is international, and not OIED approved
         if ($i->getWorkflowState() instanceof SigAuthReadyState && $i->isInternational() && !$i->isOiedCertified()) {
             NQ::simple('intern', INTERN_WARNING, 'This internship can not be approved by the Signature Authority bearer until the internship is certified by the Office of International Education and Development.');
         }
         // Show a warning if in DeanApproved state and is distance_ed campus
         if ($i->getWorkflowState() == 'DeanApprovedState' && $i->isDistanceEd()) {
             NQ::simple('intern', INTERN_WARNING, 'This internship must be registered by Distance Education.');
         }
         // Sanity check cource section #
         if ($i->isDistanceEd() && ($i->getCourseSection() < 300 || $i->getCourseSection() > 399)) {
             NQ::simple('intern', INTERN_WARNING, "This is a distance ed internship, so the course section number should be between 300 and 399.");
         }
         // Sanity check distance ed radio
         if (!$i->isDistanceEd() && ($i->getCourseSection() > 300 && $i->getCourseSection() < 400)) {
             NQ::simple('intern', INTERN_WARNING, "The course section number you entered looks like a distance ed course. Be sure to check the Distance Ed option, or double check the section number.");
         }
         PHPWS_Core::initModClass('intern', 'EmergencyContactFormView.php');
         $emgContactDialog = new EmergencyContactFormView($i);
         $tpl['ADD_EMERGENCY_CONTACT'] = '<button type="button" class="btn btn-default btn-sm" id="add-ec-button"><i class="fa fa-plus"></i> Add Contact</button>';
         $tpl['EMERGENCY_CONTACT_DIALOG'] = $emgContactDialog->getHtml();
     } else {
         // Attempting to create a new internship
         // Check permissions
         if (!Current_User::allow('intern', 'create_internship')) {
             NQ::simple('intern', INTERN_ERROR, 'You do not have permission to create new internships.');
             NQ::close();
             PHPWS_Core::home();
         }
         $tpl['TITLE'] = 'Add Internship';
         $internshipForm = new InternshipFormView('Add Internship');
         $internshipForm->buildInternshipForm();
         $tpl['AUTOFOCUS'] = 'autofocus';
         /* Show form with empty fields. */
         $form = $internshipForm->getForm();
         // Show a disabled button in document list if we are adding an internship.
         $tpl['UPLOAD_DOC'] = '<div title="Please save this internship first."><button id="doc-upload-btn" class="btn btn-default btn-sm" title="Please save this internship first." disabled="disabled"><i class="fa fa-upload"></i> Add document</button></div>';
         // Show a disabled emergency contact button
         $tpl['ADD_EMERGENCY_CONTACT'] = '<div title="Please save this internship first."><button class="btn btn-default btn-sm" id="add-ec-button" disabled="disabled" data-toggle="tooltip" title="first tooltip"><i class="fa fa-plus"></i> Add Contact</button></div>';
     }
     /*
      * If 'missing' is set then we have been redirected
      * back to the form because the user didn't type in something and
      * somehow got past the javascript.
      */
     if (isset($_REQUEST['missing'])) {
         $missing = explode(' ', $_REQUEST['missing']);
         //javascriptMod('intern', 'missing');
         /*
          * Set classes on field we are missing.
          */
         foreach ($missing as $m) {
             //$form->addCssClass($m, 'has-error');
             $form->addExtraTag($m, 'data-has-error="true"');
         }
         /* Plug old values back into form fields. */
         $form->plugIn($_GET);
         // If internship is being edited...
         if (isset($_REQUEST['internship_id'])) {
             /* Re-add hidden fields with object ID's */
             $i = InternshipFactory::getInternshipById($_GET['internship_id']);
             $a = $i->getAgency();
             //$f = $i->getFacultySupervisor();
             $form->addHidden('agency_id', $a->id);
             //$form->addHidden('supervisor_id', $f->id);
             $form->addHidden('id', $i->id);
         }
     }
     $form->mergeTemplate($tpl);
     //test($form->getTemplate(),1);
     return PHPWS_Template::process($form->getTemplate(), 'intern', 'add_internship.tpl');
 }