Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
FGForms.php
1 <?php
2 
3 class FGForms extends CActiveRecord
4 {
5  /**
6  * Returns the static model of the specified AR class.
7  * @param string $className active record class name.
8  * @return Forms the static model class
9  */
10  public static function model($className=__CLASS__)
11  {
12  return parent::model($className);
13  }
14 
15  /**
16  * @return string the associated database table name
17  */
18  public function tableName()
19  {
20  return 'forms';
21  }
22 
23  /**
24  * @return array validation rules for model attributes.
25  */
26  public function rules()
27  {
28  // NOTE: you should only define rules for those attributes that
29  // will receive user inputs.
30  return array(
31  array('json, html', 'required'),
32  array('name, user thankyoupage', 'length', 'max'=>255),
33  array('salt', 'length', 'max'=>32),
34  // The following rule is used by search().
35  // Please remove those attributes that should not be searched.
36  array('id, name, json, html, user, salt, creator_email thankyoupage nextpage prevpage', 'safe', 'on'=>'search'),
37  );
38  }
39 
40  /**
41  * @return array relational rules.
42  */
43  public function relations()
44  {
45  // NOTE: you may need to adjust the relation name and the related
46  // class name for the relations automatically generated below.
47  return array(
48  'formdata' => array(self::HAS_MANY, 'FGFormdata', 'form_id')
49  );
50  }
51 
52  public function defaultScope()
53  {
54  return array('order'=>'id DESC');
55  }
56 
57  /**
58  * @return array customized attribute labels (name=>label)
59  */
60  public function attributeLabels()
61  {
62  return array(
63  'id' => 'ID',
64  'name' => 'Name',
65  'json' => 'Json',
66  'html' => 'Html',
67  'user' => 'User',
68  'creator_email' => 'Creator Email',
69  'salt' => 'Salt',
70  'thankyoupage' => 'Thankyoupage',
71  );
72  }
73 
74  public function validateInput($inputData = null)
75  {
76  if($inputData === null)
77  $inputData = $_POST;
78  $formObject = $this->JSONToArray();
79  $validator = new FGValidator();
80  foreach($formObject['elements'] as $element)
81  {
82  if(isset($element['name']))
83  {
84  $value = isset($_POST[$element['name']])? strval($_POST[$element['name']]) : '';
85  if(isset($element['mandatory']) && $element['mandatory'] == true && trim($value) === '')
86  {
87  $message = Yii::t('yii','{attribute} cannot be blank.');
88  $validator->addError($this, $element['name'], $message);
89  }
90  elseif(isset($element['validation']) && $element['validation'] != '' && $value !== '')
91  {
92  $validator->validate($element['validation'], $this, $element['name'], $value);
93  }
94  else
95  {
96  $validator->validateElement($element['type'], $this, $element['name'], $value);
97  }
98  }
99  }
100  return !$this->hasErrors();
101  }
102 
103  public function JSONToArray()
104  {
105  return CJSON::decode($this->json);
106  }
107 
108  public function getElementFromJSONByName($name)
109  {
110  $object = $this->JSONToArray();
111  foreach($object['elements'] as $element) {
112  if(isset($element['name']) && $element['name'] == $name) {
113  return $element;
114  }
115  }
116  return false;
117  }
118 
119  public function getElementLabel($name)
120  {
121  $element = $this->getElementFromJSONByName($name);
122  if($element && isset($element['label'])) {
123  return $element['label'];
124  }
125  return $name;
126  }
127 
128  /**
129  * Retrieves a list of models based on the current search/filter conditions.
130  * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
131  */
132  public function search()
133  {
134  // Warning: Please modify the following code to remove attributes that
135  // should not be searched.
136 
137  $criteria=new CDbCriteria;
138 
139  $criteria->compare('id',$this->id,true);
140  $criteria->compare('name',$this->name,true);
141  $criteria->compare('json',$this->json,true);
142  $criteria->compare('html',$this->html,true);
143  $criteria->compare('user',$this->user,true);
144  $criteria->compare('salt',$this->salt,true);
145  // $criteria->compare('thankyoupage',$this->thankyoupage,true);
146  // $criteria->compare('nextpage',$this->nextpage,true);
147  // $criteria->compare('prevpage',$this->prevpage,true);
148 
149  //if(FGModule::isUserAdmin() == false) {
150  // get only user records
151  //$criteria->compare('user', Yii::app()->user->id);
152  //}
153 
154  return new CActiveDataProvider($this, array(
155  'criteria'=>$criteria,
156  'Pagination'=>array(
157  'PageSize'=>40
158  )
159  ));
160  }
161 
162  /*
163  Returns all generated Forms for a user
164  @return CActiveDataProvider the data provider that can return the models of all forms
165  */
166  public function searchAllForUser($user_email = null)
167  {
168  $criteria = new CDbCriteria();
169  if($user_email != null){
170  $criteria->compare('creator_email', $user_email,true);
171 
172  }
173  return new CActiveDataProvider($this,array(
174  'criteria' => $criteria,
175  'Pagination' => false
176  ));
177  }
178 }