Esempio n. 1
0
 /**
  * Edit a template
  */
 public function actionEdit($id)
 {
     if ($template = $this->_application->getTemplateById($id)) {
         $pdfLib = new PDFLib();
         $pdfLib->set_option("errorpolicy=exception");
         $tmpFile = tempnam($this->_config->getVarPath() . '/tmp/', 'pdftemplate');
         $document = $pdfLib->open_pdi_document($template->getTmpFilePath(), "");
         $pagecount = $pdfLib->pcos_get_number($document, "length:pages");
         $blocks = array();
         for ($pageNum = 0; $pageNum < $pagecount; $pageNum++) {
             $blockcount = $pdfLib->pcos_get_number($document, "length:pages[{$pageNum}]/blocks");
             for ($blockNum = 0; $blockNum < $blockcount; $blockNum++) {
                 $blocks[] = $pdfLib->pcos_get_string($document, "pages[{$pageNum}]/blocks[{$blockNum}]/Name");
             }
         }
         $blocks = array_unique($blocks);
         $form = new \Foundation\Form();
         $form->setCSRFToken($this->getCSRFToken());
         $form->setAction($this->path("setup/pdftemplates/edit/" . $template->getId()));
         $field = $form->newField();
         $field->setLegend('Edit Template Blocks');
         $element = $field->newElement('TextInput', 'title');
         $element->setLabel('Title');
         $element->setValue($template->getTitle());
         $element->addValidator(new \Foundation\Form\Validator\NotEmpty($element));
         $blockElements = array('applicant-firstName' => 'Applicant: First Name', 'applicant-lastName' => 'Applicant: Last Name', 'applicant-middleName' => 'Applicant: Middle Name', 'applicant-suffix' => 'Applicant: Suffix', 'applicant-fullName' => 'Applicant: Full Name', 'applicant-email' => 'Applicant: Email', 'applicant-id' => 'Applicant: ID', 'applicant-externalid' => 'Applicant: External ID');
         foreach ($this->_application->getApplicationPages() as $applicationPage) {
             if ($applicationPage->getJazzeePage() instanceof \Jazzee\Interfaces\PdfPage) {
                 foreach ($applicationPage->getJazzeePage()->listPdfTemplateElements() as $key => $title) {
                     $blockElements[$key] = $title;
                 }
             }
         }
         foreach ($blocks as $blockName) {
             $element = $field->newElement('SelectList', 'block_' . $blockName);
             $element->setLabel("Block {$blockName}");
             $element->newItem(null, '');
             foreach ($blockElements as $id => $title) {
                 $element->newItem($id, $title);
             }
             if ($template->hasBlock($blockName)) {
                 $blockData = $template->getBlock($blockName);
                 switch ($blockData['type']) {
                     case 'applicant':
                         $element->setValue('applicant-' . $blockData['element']);
                         break;
                     case 'page':
                         $element->setValue('page-' . $blockData['pageId'] . '-element-' . $blockData['elementId']);
                         break;
                 }
             }
         }
         if ($input = $form->processInput($this->post)) {
             $template->setTitle($input->get('title'));
             $template->clearBlocks();
             $matches = array();
             //initialize for use in preg_match
             foreach ($blocks as $blockName) {
                 if ($blockElement = $input->get('block_' . $blockName)) {
                     if (preg_match('#applicant-([a-z]+)#i', $blockElement, $matches)) {
                         $template->addBlock($blockName, array('type' => 'applicant', 'element' => $matches[1]));
                     } else {
                         if (preg_match('#page-([0-9]+)-element-([0-9]+)#i', $blockElement, $matches)) {
                             $template->addBlock($blockName, array('type' => 'page', 'pageId' => $matches[1], 'elementId' => $matches[2]));
                         }
                     }
                 }
             }
             $this->getEntityManager()->persist($template);
             $this->addMessage('success', 'Template Saved Successfully');
             $this->redirectPath('setup/pdftemplates');
         }
         $form->newButton('submit', 'Save');
         $this->setVar('form', $form);
     } else {
         $this->addMessage('error', 'Unable to edit template.  It is not associated with this application.');
         $this->redirectPath('setup/pdftemplates');
     }
 }