public function getFormFieldFromEditableFormField(EditableFormField $fieldRecord)
 {
     $field = $fieldRecord->getFormField();
     // Remove templates added by EditableFormField
     $field->setFieldHolderTemplate(null);
     $field->setTemplate(null);
     // Simplify dropdown to only have options that are used
     if ($field->hasMethod('getSource')) {
         $source = $field->getSource();
         $availableSources = SubmittedFormField::get()->filter(array('ParentID' => $this->controller->data()->PublishedSubmittedFormIDs(), 'Name' => $fieldRecord->Name, 'Value' => array_keys($source)))->column('Value');
         if (!$availableSources) {
             // Don't show the field if there is nothing to search on.
             return null;
         }
         $newSources = array();
         foreach ($availableSources as $value) {
             if (isset($source[$value])) {
                 $newSources[$value] = $source[$value];
             }
         }
         if (!$newSources) {
             // Don't show the field if there is nothing to search on.
             return null;
         }
         $field->setSource($newSources);
     }
     if ($field->hasMethod('setHasEmptyDefault') && ($dropdownEmptyString = $this->config()->dropdown_empty_string)) {
         // Defaults to '- Please select -', configured above.
         $field->setEmptyString($dropdownEmptyString);
     }
     // Attach EditableFormField to differentiate EditableFormField fields from regular ones
     // in the form.
     $field->EditableFormField = $fieldRecord;
     return $field;
 }
Esempio n. 2
0
 /**
  * Returns the value of a relation or, in the case of this form, the value
  * of a given child {@link SubmittedFormField}
  * 
  * @param string
  * 
  * @return mixed
  */
 public function relField($fieldName)
 {
     // default case
     if ($value = parent::relField($fieldName)) {
         return $value;
     }
     // check values for a form field with the matching name.
     $formField = SubmittedFormField::get()->filter(array('ParentID' => $this->ID, 'Name' => $fieldName))->first();
     if ($formField) {
         return $formField->getFormattedValue();
     }
 }
 /**
  * Return an array containing the amount of submissions for each options
  *
  * @return array
  */
 public function data()
 {
     if (!$this->Field()->exists() || !$this->Consultation()->Submissions()->count() > 0) {
         return;
     }
     $results = [];
     $options = $this->Field()->Options();
     foreach ($options as $option) {
         $result = SubmittedFormField::get()->filter(array('ParentID' => $this->Consultation()->Submissions()->column('ID')))->filterAny(array('Value' => $option->Title, 'Value:PartialMatch' => $option->Title));
         $optionResult = [];
         $optionResult['Label'] = $option->Title;
         $optionResult['Value'] = $result->count();
         $results[] = $optionResult;
     }
     return $results;
 }
	/**
	 * Process the form that is submitted through the site
	 * 
	 * @param Array Data
	 * @param Form Form 
	 * @return Redirection
	 */
	function process($data, $form) {
		// submitted form object
		$submittedForm = new SubmittedForm();
		$submittedForm->SubmittedBy = Member::currentUser();
		$submittedForm->ParentID = $this->ID;
		$submittedForm->Recipient = $this->EmailTo;
		$submittedForm->write();
		
		// email values
		$values = array();
		$recipientAddresses = array();
		$sendCopy = false;
        $attachments = array();

		$submittedFields = new DataObjectSet();
		
		foreach($this->Fields() as $field) {
			// don't show fields that shouldn't be shown
			if(!$field->showInReports()) continue;
			
			$submittedField = new SubmittedFormField();
			$submittedField->ParentID = $submittedForm->ID;
			$submittedField->Name = $field->Name;
			$submittedField->Title = $field->Title;
					
			if($field->hasMethod('getValueFromData')) {
				$submittedField->Value = $field->getValueFromData($data);
			}
			else {
				if(isset($data[$field->Name])) $submittedField->Value = $data[$field->Name];
			}
			
			$submittedField->write();
			$submittedFields->push($submittedField);

			if(!empty($data[$field->Name])){
				/**
				 * @todo this should be on the EditableFile class. Just need to sort out
				 * 		attachments array
				 */
				if($field->ClassName == "EditableFileField"){	
					if(isset($_FILES[$field->Name])) {
						
						// create the file from post data
						$upload = new Upload();
						$file = new File();
						$upload->loadIntoFile($_FILES[$field->Name], $file);

						// write file to form field
						$submittedField->UploadedFileID = $file->ID;
						
						// Attach the file if its less than 1MB, provide a link if its over.
						if($file->getAbsoluteSize() < 1024*1024*1){
							$attachments[] = $file;
						}

						// Always provide the link if present.
						if($file->ID) {
							$submittedField->Value = "<a href=\"". $file->getFilename() ."\" title=\"". $file->getFilename() . "\">". $file->Title . "</a>";
						} else {
							$submittedField->Value = "";
						}
					}									
				}
			}
			
			// make sure we save
			$submittedField->write();
		}	
		$emailData = array(
			"Sender" => Member::currentUser(),
			"Fields" => $submittedFields,
		);

		// email users on submit. All have their own custom options. 
		if($this->EmailRecipients()) {
			$email = new UserDefinedForm_SubmittedFormEmail($submittedFields);                     
			$email->populateTemplate($emailData);
			if($attachments){
				foreach($attachments as $file){
					// bug with double decorated fields, valid ones should have an ID.
					if($file->ID != 0) {
						$email->attachFile($file->Filename,$file->Filename, $file->getFileType());
					}
				}
			}

			foreach($this->EmailRecipients() as $recipient) {
				$email->populateTemplate($emailData);
				$email->setFrom($recipient->EmailFrom);
				$email->setBody($recipient->EmailBody);
				$email->setSubject($recipient->EmailSubject);
				$email->setTo($recipient->EmailAddress);
				
				// check to see if they are a dynamic sender. eg based on a email field
				// a user selected
				if($recipient->SendEmailFromField()) {
					$name = Convert::raw2sql($recipient->SendEmailFromField()->Name);
					
					if(defined('Database::USE_ANSI_SQL')) {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "\"Name\" = '$name' AND \"ParentID\" = '$submittedForm->ID'");
					} else {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "Name = '$name' AND ParentID = '$submittedForm->ID'");
					}
					
					if($submittedFormField) {
						$email->setFrom($submittedFormField->Value);	
					}
				}
				// check to see if they are a dynamic reciever eg based on a dropdown field
				// a user selected
				if($recipient->SendEmailToField()) {
					$name = Convert::raw2sql($recipient->SendEmailToField()->Name);
					
					if(defined('Database::USE_ANSI_SQL')) {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "\"Name\" = '$name' AND \"ParentID\" = '$submittedForm->ID'");
					} else {
						$submittedFormField = DataObject::get_one("SubmittedFormField", "Name = '$name' AND ParentID = '$submittedForm->ID'");
					}
					
					if($submittedFormField) {
						$email->setTo($submittedFormField->Value);	
					}
				}
				
				if($recipient->SendPlain) {
					$body = strip_tags($recipient->EmailBody) . "\n ";
					if(isset($emailData['Fields'])) {
						foreach($emailData['Fields'] as $Field) {
							$body .= $Field->Title .' - '. $Field->Value .'\n';
						}
					}
					$email->setBody($body);
					$email->sendPlain();
				}
				else {
					$email->send();	
				}
			}
		}
		
		// Redirect to the finished method on this controller with the referrer data
		Director::redirect($this->Link() . 'finished?referrer=' . urlencode($data['Referrer']));
	}
 public function updateDBFromSubmission()
 {
     if ($this->__HasUpdatedDBFromSubmission) {
         return;
     }
     $this->__HasUpdatedDBFromSubmission = true;
     $submission = $this->owner->Submission();
     if (!$submission || !$submission->exists()) {
         return;
     }
     $holderPage = $submission->Parent();
     if (!$holderPage || !$holderPage->exists()) {
         return;
     }
     // Update content field
     $content_field = $this->owner->stat('content_field');
     if ($content_field && $this->owner->hasDatabaseField($content_field)) {
         $contentValue = $this->owner->{$content_field};
         $templatePageMarkup = $this->owner->TemplatePageMarkup();
         if ($templatePageMarkup instanceof HTMLText) {
             $templatePageMarkup = $templatePageMarkup->getValue();
         }
         if ($contentValue != $templatePageMarkup) {
             $this->owner->{$content_field} = $templatePageMarkup;
         }
     }
     // Update title fields (defaults to $Title/$MenuTitle)
     $title_fields = $this->owner->stat('title_fields');
     if ($title_fields) {
         // Get form field name to use. ie. $EditableTextField_6746f
         $fieldName = $holderPage->SubmissionPageTitleField;
         if ($fieldName) {
             // Only set the value if they aren't equal. This ensures
             // that changed fields isn't updated with an identical value.
             $formFieldValue = SubmittedFormField::get()->filter(array('ParentID' => $submission->ID, 'Name' => $fieldName))->first();
             if ($formFieldValue) {
                 $title = $formFieldValue->Value;
                 if ($title !== null) {
                     $title = (string) $title;
                     foreach ($title_fields as $fieldName) {
                         if ($this->owner->hasDatabaseField($fieldName) && $this->owner->{$fieldName} !== $title) {
                             $this->owner->{$fieldName} = $title;
                         }
                     }
                 }
             }
         }
     }
 }
 /**
  * Adds missing values
  *
  * @return array
  */
 public function addMissingValues()
 {
     if (!$this->owner->ID) {
         throw new Exception('Cannot add missing values for an unsaved SubmittedForm');
     }
     $missingFields = $this->MissingValues();
     foreach ($missingFields as $fieldRecord) {
         $record = SubmittedFormField::create();
         $record->Name = $fieldRecord->Name;
         $record->Title = $fieldRecord->Title;
         $record->ParentID = $this->owner->ID;
         $record->write();
     }
 }
Esempio n. 7
0
 function process($data, $form)
 {
     $submittedForm = new SubmittedForm();
     $submittedForm->SubmittedBy = Member::currentUser();
     $submittedForm->ParentID = $this->ID;
     $submittedForm->Recipient = $this->EmailTo;
     $submittedForm->write();
     $values = array();
     $recipientAddresses = array();
     $sendCopy = false;
     $attachments = array();
     $submittedFields = new DataObjectSet();
     foreach ($this->Fields() as $field) {
         $submittedField = new SubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->Title;
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         $submittedField->write();
         $submittedFields->push($submittedField);
         if (!empty($data[$field->Name])) {
             // execute the appropriate functionality based on the form field.
             switch ($field->ClassName) {
                 case "EditableEmailField":
                     if ($field->SendCopy) {
                         $recipientAddresses[] = $data[$field->Name];
                         $sendCopy = true;
                         $values[$field->Title] = '<a style="white-space: nowrap" href="mailto:' . $data[$field->Name] . '">' . $data[$field->Name] . '</a>';
                     }
                     break;
                 case "EditableFileField":
                     // Returns a file type which we attach to the email.
                     $submittedfile = $field->createSubmittedField($data[$field->Name], $submittedForm);
                     $file = $submittedfile->UploadedFile();
                     $filename = $file->getFilename();
                     // Attach the file if its less than 1MB, provide a link if its over.
                     if ($file->getAbsoluteSize() < 1024 * 1024 * 1) {
                         $attachments[] = $file;
                     }
                     // Always provide the link if present.
                     if ($file->ID) {
                         $submittedField->Value = $values[$field->Title] = "<a href=\"" . $filename . "\" title=\"" . Director::absoluteBaseURL() . $filename . "\">Uploaded to: " . Director::absoluteBaseURL() . $filename . "</a>";
                     } else {
                         $submittedField->Value = $values[$field->Title] = "";
                     }
                     break;
             }
         } elseif ($field->hasMethod('getValueFromData')) {
             $values[$field->Title] = Convert::linkIfMatch($field->getValueFromData($data));
         } else {
             if (isset($data[$field->Name])) {
                 $values[$field->Title] = Convert::linkIfMatch($data[$field->Name]);
             }
         }
     }
     if ($this->EmailOnSubmit || $sendCopy) {
         $emailData = array("Recipient" => $this->EmailTo, "Sender" => Member::currentUser(), "Fields" => $submittedFields);
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $email->populateTemplate($emailData);
         $email->setTo($this->EmailTo);
         $email->setSubject($this->Title);
         // add attachments to email (<1MB)
         if ($attachments) {
             foreach ($attachments as $file) {
                 $email->attachFile($filename, $filename);
             }
         }
         $email->send();
         // send to each of email fields
         foreach ($recipientAddresses as $addr) {
             $email->setTo($addr);
             $email->send();
         }
     }
     $custom = $this->customise(array("Content" => $this->customise(array('Link' => $data['Referrer']))->renderWith('ReceivedFormSubmission'), "Form" => " "));
     return $custom->renderWith('Page');
 }