示例#1
0
 public function view()
 {
     $this->requireAsset('redactor');
     $f = \Concrete\Package\Formify\Src\FormifyForm::get($this->fID);
     $this->set('f', $f);
     $this->set('context', $this->context);
     $r = \Concrete\Package\Formify\Src\FormifyRecord::getWithToken($this->rID, $this->token);
     if (is_object($r)) {
         $fields = $f->getFields();
         foreach ($fields as $key => $ff) {
             if ($r->getAnswerValue($ff->ffID) != '') {
                 $fields[$key]->defaultValue = $r->getAnswerValue($ff->ffID);
             }
         }
         $this->set('rID', $this->rID);
         $this->set('token', $this->token);
     }
     $html = Loader::helper('html');
     $this->addHeaderItem($html->css('font-awesome.css'));
 }
示例#2
0
 public function process($id = '')
 {
     $editing = false;
     $rID = $_POST['rID'];
     // Record ID
     $token = $_POST['token'];
     // Record Token
     /*
     			1. Get/Create form
     			2. Create fields (if form is in learn more and fields do not exist)
     			3. Verify permissions
     			4. Validate fields
     			5. Get/Create record
     			6. Save answers
     			7. Send notifications
     			8. Prepare response
     			9. Return response
     	Response JSON Object Format:
     			{
     				errors [
     					{
     						type,
     						ffID,
     						label,
     						message
     					}
     				],
     				amountCharged,
     				rID,
     				hook,
     				action,
     				message,
     				url
     			}
     */
     $response = array();
     // Response array
     $response['errors'] = array();
     /* 1. Setup Form */
     if ($id) {
         $f = \Concrete\Package\Formify\Src\FormifyForm::get($id);
     }
     if (!is_object($f)) {
         $pkg = Package::getByHandle('formify');
         $isMagic = $pkg->getConfig()->get('formify.magic');
         if ($isMagic) {
             $f = \Concrete\Package\Formify\Src\FormifyForm::create(ucwords(strtolower($id)));
             $f->set('handle', $id);
             $f->set('magic', true);
         }
     }
     if (is_object($f)) {
         /* 2. Setup Fields */
         $fields = $f->getFields();
         $answers = array();
         foreach ($fields as $ff) {
             $answers[$ff->ffID] = $_POST[$ff->ffID];
         }
         if ($f->isMagic()) {
             foreach ($_POST as $h => $v) {
                 if ($h != 'rID' && $h != 'token') {
                     $field = $f->getField($h);
                     if (!$field) {
                         $field = $f->addField();
                         $field->set('label', ucfirst($h));
                         $field->set('handle', $h);
                         $fields[] = $field;
                         $answers[$field->ffID] = $v;
                     }
                 }
             }
         }
         /* 3. Verify permissions */
         if ($rID != '' && $token != '') {
             $r = \Concrete\Package\Formify\Src\FormifyRecord::getWithToken($rID, $token);
             if (is_object($r)) {
                 $editing = true;
             }
             if (!$r->userCanEdit()) {
                 $e = array();
                 $e['type'] = 'permissions';
                 $e['message'] = $f->errorPermission;
                 $response['errors'][] = $e;
             }
         } else {
             if ($f->getRecordCount() > $f->maxSubmissions && $f->maxSubmissions > 0) {
                 $e = array();
                 $e['type'] = 'submissions';
                 $e['message'] = $f->errorSubmissions;
                 $response['errors'][] = $e;
             }
             if (!$f->userCanAdd()) {
                 $e = array();
                 $e['type'] = 'permissions';
                 $e['message'] = $f->errorPermission;
                 $response['errors'][] = $e;
             }
         }
         if (count($response['errors']) == 0 && count($fields) > 0) {
             /* 4. Validate Fields */
             foreach ($fields as $ff) {
                 //Loop through fields
                 if ($ff->isRequired && $ff->getType()->hasInput()) {
                     //Determine if it's required
                     if ($answers[$ff->ffID] == '') {
                         //See if the actual value is blank
                         $e = array();
                         $e['type'] = 'validation';
                         $e['ffID'] = $ff->ffID;
                         $e['label'] = $ff->label;
                         $e['message'] = $f->errorValidation;
                         $response['errors'][] = $e;
                     }
                 }
                 if ($ff->regex != '') {
                     if ($answers[$ff->ffID] != '') {
                         if (preg_match($ff->regex, $answers[$ff->ffID]) == 0 && $answers[$ff->ffID] != '') {
                             $e = array();
                             $e['type'] = 'validation';
                             $e['ffID'] = $ff->ffID;
                             $e['label'] = $ff->label;
                             $e['message'] = $f->errorValidation;
                             $response['errors'][] = $e;
                         }
                     }
                 }
                 if ($ff->type == 'email') {
                     if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*@[a-z0-9.-]+\\.[a-z]{2,4}\$/i", $answers[$ff->ffID]) && $answers[$ff->ffID] != '') {
                         $e = array();
                         $e['type'] = 'validation';
                         $e['ffID'] = $ff->ffID;
                         $e['label'] = $ff->label;
                         $e['message'] = $f->errorValidation;
                         $response['errors'][] = $e;
                     }
                 }
             }
             if (count($response['errors']) == 0) {
                 /* 5. Get/Create record */
                 if (is_object($r)) {
                     $r->clear();
                 } else {
                     $r = $f->addRecord();
                     switch ($f->defaultRecordStatus) {
                         case 1:
                             $r->approve(true);
                             break;
                         case -1:
                             $r->reject(true);
                             break;
                     }
                 }
                 /* 6. Save answers */
                 foreach ($fields as $field) {
                     $r->addAnswer($field->ffID, $answers[$field->ffID]);
                 }
                 $r->cache();
                 $r->index();
                 /* 7. Send notifications */
                 if ($editing) {
                     //Send update notifications
                     foreach ($f->getNotifications('update') as $n) {
                         $n->send($r);
                     }
                 } else {
                     //Send add notifications
                     foreach ($f->getNotifications('add') as $n) {
                         $n->send($r);
                     }
                 }
                 /* 8. Prepare response */
                 $rID = $r->rID;
             }
         }
     } else {
         //Form not found
         $e = array();
         $e['type'] = 'unknown';
         $e['message'] = 'Form not found.';
         $response['errors'][] = $e;
     }
     if ($_GET['ajax'] == 1) {
         $response['rID'] = $rID;
         $response['hook'] = $f->hook;
         if ($f->submitAction == 'URL' || $f->submitAction == 'cID') {
             $response['action'] = 'redirect';
             if ($f->submitAction == 'URL') {
                 $response['url'] = $f->submitActionURL;
             } else {
                 $redirectCollection = Page::getByID($f->submitActionCollectionID);
                 $response['url'] = $redirectCollection->getCollectionLink();
             }
             if ($f->submitActionPassRecordID) {
                 $response['url'] .= '?' . $f->submitActionRecordIDParameter . '=' . $rID;
             }
         } else {
             $response['action'] = $f->submitAction;
             $response['message'] = $f->submitActionMessage;
         }
         /* 9. Return response */
         $json = Loader::helper('json');
         $rJSON = $json->encode($response);
         echo $rJSON;
     }
 }