Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
FormController.php
1 <?php
2 
4 {
5  /**
6  * @desc process submitted fg form
7  */
8  public function actionSubmit()
9  {
10  Yii::import('fg.widgets.form.helpers.FGFormWidgetHelper');
11  $isAjaxRequest = Yii::app()->getRequest()->isAjaxRequest;
12  if($isAjaxRequest) {
13  $response = array(
14  'status'=>true
15  );
16  }
17 
18  $form_id = (int)$_POST['form_id'];
19 
20  $action = $_POST['submitAction'];
21 
22  if(!isset($_SESSION['formdatas'])):
23  $_SESSION['formdatas'] = array();
24  endif;
25  if(!isset($_SESSION['formdatalists'])):
26  $_SESSION['formdatalists'] = array();
27  endif;
28 
29  $model = $this->loadModel($form_id);
30  if($action == "prev" || ($action != "prev" && $model->validateInput()))
31  {
32  // generate formdata arrays
33  $formObject = $model->JSONToArray();
34  $formdataForJSON = array();
35  $formdataList = array();
36 
37  if($action != "prev"):
38  foreach($formObject['elements'] as $element) {
39  if(isset($element['name'])) {
40  $elementObject = FGElement::getInstance($element['type']);
41  $val = @$_POST[$element['name']];
42  $val = $elementObject->serializeFormData($val, $element);
43  $formdataForJSON[$element['name']] = $val;
44  $formdataList[] = array(
45  'label'=>isset($element['label'])? $element['label'] : $element['name'],
46  'value'=>$val
47  );
48  }
49  }
50  // store current input to session
51  $_SESSION['formdatas'][$form_id] = $formdataForJSON;
52  $_SESSION['formdatalists'][$form_id] = $formdataList;
53  endif;
54 
55  // save formdata
56  if($action == "submit"):
57 
58  // create JSON of all previous inputs from session
59  $finalFormData = array();
60 // $finalFormdataList = array();
61 
62  foreach($_SESSION['formdatas'] as $formdatas):
63  $keys = array_keys($formdatas);
64  foreach($keys as $key):
65  $finalFormData[$key] = $formdatas[$key];
66  endforeach;
67  endforeach;
68 
69  $formdata = new FGFormdata();
70  $formdata->form_id = $model->id;
71  $formdata->referer = $_POST['referer'];
72  $formdata->ip = Yii::app()->request->userHostAddress;
73  //$formdata->json = CJSON::encode($formdataForJSON);
74  $formdata->user_id = Yii::app()->session->sessionID;
75  $formdata->json = CJSON::encode($finalFormData);
76  $formdata->save();
77  unset($_SESSION['formdatas']);
78 
79 
80  try {
81  // send submit to form owner
82  //$formObject = $model->JSONToArray();
83  $subject = 'Form submitted on '.Yii::app()->name;
84  $notification = new Notification($subject,
85  Yii::app()->controller->renderMail('fg.views.form.email_form',
86  $subject,
87  array(
88  'model' => $model,
89  'formdata'=>$formdata,
90  'formdataList'=>$_SESSION['formdatalists'])
91  ,true));
92  $notification->recipients[] = new EmailRecipient($formObject['email'],Yii::app()->params['adminEmail']);
93  Yii::app()->notificationManager->notifyAbout($notification);
94  // old mail function
95  /*
96  $mailer = Yii::createComponent('fg.extensions.mailer.EMailer');
97  $mailer->From = Yii::app()->params['adminEmail'];
98  $mailer->FromName = Yii::app()->name;
99  $mailer->CharSet = 'utf-8';
100  $mailer->ContentType = 'text/html';
101  $mailer->AddAddress($formObject['email']);
102  $mailer->Subject = 'Form submitted on '.Yii::app()->name;
103  $mailer->Body = $this->renderPartial('email_form', array(
104  'model'=>$model,
105  'formdata'=>$formdata,
106  'formdataList'=>$_SESSION['formdatalists']//$formdataList
107  ), true);
108  $mailer->Send();
109  */
110 
111  }
112  catch(Exception $e) {}
113 
114 
115  // clear session errors for this form
116  FGFormWidgetHelper::clearErrors($model->id);
117 
118 
119  $redirectUrl = $model->thankyoupage;
120  endif;
121 
122  if($action == "next"):
123  $redirectUrl = $model->nextpage;
124  endif;
125  if($action == "prev"):
126  $redirectUrl = $model->prevpage;
127  endif;
128 
129  //$my = Yii::app()->getModules(false);
130 
131  if($isAjaxRequest)
132  {
133  $response['redirectUrl'] = $redirectUrl;
134  //$response['redirectUrl'] = $my['fg']['thankYouPage'];
135  }
136  else
137  {
138  // go to thank you page
139  $this->redirect($redirectUrl);
140  //$this->redirect($my['fg']['thankYouPage']);
141  }
142  }
143  else
144  {
145  if($isAjaxRequest)
146  {
147  $response['status'] = false;
148  $response['errors'] = array();
149  foreach($model->getErrors() as $attribute=>$errors) {
150  $response['errors'][$attribute]=$errors;
151  }
152  }
153  else
154  {
155  // set session erros for this form
156  FGFormWidgetHelper::setErrors($form_id, $model->getErrors());
157  // return to current form
158  $this->redirect($_POST['return_url']);
159  }
160  }
161  if($isAjaxRequest) {
162  FGHelper::sendJSON($response);
163  }
164  }
165 
166  /**
167  * @desc validate form via ajax request
168  * @return JSON array of errors
169  */
170  /*public function actionValidate()
171  {
172  $model = $this->loadModel((int)$_POST['form_id']);
173  $model->validateInput();
174  $result = array();
175  foreach($model->getErrors() as $attribute=>$errors)
176  $result[$attribute]=$errors;
177  echo CJSON::encode($result);
178  }*/
179 
180  /**
181  * @desc form thank you page
182  */
183  public function actionThankyou()
184  {
185  $this->render('thankyou');
186  //$my = Yii::app()->getModules(false);
187  //$this->redirect($my['fg']['thankYouPage']);
188  }
189 
190  /**
191  * @param int $id
192  * @return FGForms fg form model
193  * @desc load fg form model
194  */
195  public function loadModel($id)
196  {
197  $model=FGForms::model()->findByPk($id);
198  if($model===null)
199  throw new CHttpException(404, 'The requested form does not exist');
200  return $model;
201  }
202  /**
203  * Returns a list of external action classes.
204  *
205  * @return array
206  */
207  public function actions()
208  {
209  return array(
210  'captcha' => array(
211  'class' => 'CCaptchaAction',
212  'backColor' => 0xFFFFFF,
213  'fixedVerifyCode' => YII_DEBUG ? 'polomo' : null
214  ),
215  );
216  }
217 }