function templateAction()
 {
     $clinicalNoteId = $this->_getParam('clinicalNoteId', 0);
     $revisionId = (int) $this->_getParam('revisionId');
     $cn = new ClinicalNote();
     $cn->clinicalNoteId = (int) $clinicalNoteId;
     $cn->populate();
     if ($revisionId > 0) {
         $cn->revisionId = $revisionId;
     }
     $this->_cn = $cn;
     $templateId = $cn->clinicalNoteTemplateId;
     assert("{$templateId} > 0");
     $cnTemplate = $cn->clinicalNoteDefinition->clinicalNoteTemplate;
     $this->_form = new WebVista_Form(array('name' => 'cn-template-form'));
     $this->_form->setWindow('dummyWindowId');
     $this->_form->setAction(Zend_Registry::get('baseUrl') . "clinical-notes-form.raw/process");
     $cnXML = simplexml_load_string($cnTemplate->template);
     // pre-register print element to view so that other elements can override particularly the onclick event
     $this->_buildForm($cnXML);
     $this->_form->addElement($this->_form->createElement('hidden', 'clinicalNoteId', array('value' => (int) $cn->clinicalNoteId)));
     $formData = array();
     $this->_form->removeElement('ok');
     $element = $this->_form->createElement('hidden', 'clinicalNoteOKId', array('value' => 'OK'));
     $this->_form->addElement($element);
     if ($revisionId > 0) {
         //$this->_form->removeElement('ok');
         $this->_form->removeElement('clinicalNoteOKId');
     } else {
         $revisionId = GenericData::getUnsignedRevisionId(get_class($cn), $cn->clinicalNoteId);
         //$revisionId = $cn->clinicalNoteId;
     }
     $this->_form->addElement($this->_form->createElement('hidden', 'revisionId', array('value' => (int) $revisionId)));
     $db = Zend_Registry::get('dbAdapter');
     $cndSelect = $db->select()->from('genericData')->where("objectClass = 'ClinicalNote'")->where('objectId = ?', (int) $cn->clinicalNoteId);
     if ($revisionId > 0) {
         $cndSelect->where('revisionId = ?', $revisionId);
     }
     trigger_error($cndSelect->__toString(), E_USER_NOTICE);
     foreach ($db->query($cndSelect)->fetchAll() as $row) {
         $formData[$row['name']] = $row['value'];
     }
     $eSignatureId = ESignature::retrieveSignatureId(get_class($cn), $revisionId);
     if ($eSignatureId > 0) {
         // On signed notes generic data is shown
         //$this->_form->removeElement('ok');
         $this->_form->removeElement('clinicalNoteOKId');
         $esig = new ESignature();
         $esig->eSignatureId = $eSignatureId;
         $esig->populate();
         $signPerson = new Person();
         $signPerson->personId = $esig->signingUserId;
         $signPerson->populate();
         $person = new Person();
         $person->personId = $esig->signingUserId;
         $person->populate();
         $this->view->signatureInfo = "Signed on: " . $esig->signedDateTime . " by: " . $person->firstName . ' ' . $person->lastName . ' ' . $person->suffix;
         $element = $this->_form->createElement('hidden', 'clinicalNoteSignatureId', array('value' => $this->view->signatureInfo));
         $this->_form->addElement($element);
     } else {
         // on unsigned notes NSDR is shown but a warning also needs to appear that says data has changed since last save if generic data != NSDR data
         if ((string) $cnXML->attributes()->useNSDR && (string) $cnXML->attributes()->useNSDR == 'true') {
             $nsdrData = ClinicalNote::getNSDRData($cnXML, $cn, $revisionId);
             if ($formData != $nsdrData) {
                 $msg = __('Data has been changed since last save');
                 $this->_form->addElement($this->_form->createElement('hidden', 'dataChangedId', array('value' => $msg)));
             }
             $formData = $nsdrData;
         }
     }
     $this->_form->populate($formData);
     $this->view->form = $this->_form;
     $templateRevisions = array();
     $gdIterator = GenericData::getAllRevisions(get_class($cn), $cn->clinicalNoteId);
     foreach ($gdIterator as $gd) {
         $templateRevisions[$gd->revisionId] = $gd->dateTime;
     }
     $this->view->templateRevisions = $templateRevisions;
 }
 public function buildDefaultGenericData(SimpleXMLElement $xml = null)
 {
     if ($xml === null) {
         if (!strlen($this->clinicalNoteDefinition->clinicalNoteTemplate->template) > 0) {
             $this->clinicalNoteDefinition->populate();
         }
         $xml = new SimpleXMLElement($this->clinicalNoteDefinition->clinicalNoteTemplate->template);
     }
     $revisionId = WebVista_Model_ORM::nextSequenceId();
     $nsdrData = array();
     if ((string) $xml->attributes()->useNSDR && (string) $xml->attributes()->useNSDR == 'true') {
         $nsdrData = ClinicalNote::getNSDRData($xml, $this, $revisionId);
     }
     $dateTime = date('Y-m-d H:i:s');
     foreach ($xml as $question) {
         foreach ($question as $key => $item) {
             if ($key != 'dataPoint') {
                 continue;
             }
             $namespace = NSDR2::extractNamespace((string) $item->attributes()->namespace);
             $name = preg_replace('/[-\\.]/', '_', $namespace);
             $value = '';
             if (strlen((string) $item->attributes()->templateText) > 0) {
                 $templateName = (string) $item->attributes()->templateText;
                 $view = Zend_Layout::getMvcInstance()->getView();
                 $value = $view->action('templated-text', 'template-text', null, array('personId' => $this->personId, 'templateName' => $templateName));
             }
             if ((string) $item->attributes()->default == true) {
                 $value = (string) $item->attributes()->value;
             }
             if (isset($nsdrData[$name])) {
                 $value = $nsdrData[$name];
             }
             $gd = new GenericData();
             $gd->objectClass = get_class($this);
             $gd->objectId = $this->clinicalNoteId;
             $gd->dateTime = $dateTime;
             $gd->name = $name;
             $gd->value = $value;
             $gd->revisionId = $revisionId;
             $gd->persist();
             //trigger_error('PERSISTED:'.print_r($gd->toArray(),true),E_USER_NOTICE);
         }
     }
     return $revisionId;
 }