Overview

Classes

  • Ws_logged_inModel
  • Ws_permissionsModel
  • Ws_role_permModel
  • Ws_rolesModel
  • Ws_user_roleModel
  • Ws_userModel
  • WsAuth
  • WsauthController
  • WsChart
  • WsConfig
  • WsController
  • WsDatabase
  • WsForm
  • WsImage
  • WsLocalize
  • WsModel
  • WsModelForm
  • WsModelGridView
  • WsUrl

Functions

  • __autoload
  • callHook
  • WsErrorHandler
  • Overview
  • Class
  1: <?php
  2: /**
  3:  * WsModelForm generates HTML form for working with WsModel records.
  4:  *
  5:  * @param WsModel $model WsModel instance
  6:  * @param string $dialog HTML ID of element which holds form
  7:  *
  8:  * Example usage:
  9:  *
 10:  * <code>
 11:  * // add a new record to model
 12:  * $m = new MyModel();
 13:  * $form = new WsModelForm($m, 'form-name');
 14:  * // change label for field
 15:  * $form->fieldLabels['name'] = 'First Name';
 16:  * // change text of submit button
 17:  * $form->submitButtonText = "Save Me";
 18:  * // show form
 19:  * $form->show()
 20:  *
 21:  * // edit existing record
 22:  * $m->getOne(1);
 23:  * $form2 = new WsModelForm($m, 'form-name');
 24:  * $form2->show();
 25:  * </code>
 26:  *
 27:  */
 28: class WsModelForm extends WsForm
 29: {
 30:     /**
 31:      * @var array $fieldLabels Form field labels
 32:      *
 33:      */
 34:     public $fieldLabels;
 35:     /**
 36:      * @var WsModel $_model Instance of WsModel class
 37:      */
 38:     private $_model;
 39:     /**
 40:      * @var string $_dialog ID of popup dialog container used for CRUD
 41:      */
 42:     private $_dialog;
 43:     /**
 44:      * @var string $_id ID of form
 45:      */
 46:     private $_id;
 47: 
 48: 
 49:     function __construct($model, $dialog)
 50:     {
 51:         if (! $model instanceof WsModel) {
 52:             return false;
 53:         }
 54: 
 55:         $this->_model = $model;
 56:         // headers
 57:         $this->fieldLabels = $model->columnHeaders;
 58:         // dialog
 59:         $this->_dialog = $dialog;
 60:         $this->_id = 'WsForm_'.uniqid();
 61: 
 62:         // submit button text
 63:         $this->submitButtonText = WsLocalize::msg('Save');
 64: 
 65:         $this->_form = '<div class="row">'
 66:             .'<div class="column column-12 text-left">';
 67:         $this->_form .= '<form id="'.$this->_id.'" '
 68:             .'class="ws_form" '
 69:             .'method="POST" enctype="multipart/form-data">'
 70:             .'<fieldset>';
 71: 
 72:         // model name
 73:         $this->_form .= '<input type="hidden" name="model_name" value="'
 74:             .get_class($this->_model).'"/>';
 75: 
 76:         // prevent CSRF attack
 77:         if (isset($_SESSION['ws_auth_token'])) {
 78:             $this->_form .= '<input type="hidden" name="csrf" value="'
 79:                 .$_SESSION["ws_auth_token"]
 80:                 .'">';
 81:         }
 82: 
 83:         // parameters for form widget
 84:         $params = array();
 85: 
 86:         // form items
 87:         foreach ($this->_model->columns as $column) {
 88:             // widget name
 89:             $params['name'] = $column;
 90:             // widget id
 91:             $params['id'] = $this->_id.'_'.$column;
 92:             // widget label
 93:             isset($this->fieldLabels[$column]) ?
 94:                 $label = $this->fieldLabels[$column] :
 95:                 $label = $column;
 96:             $params['label'] = $label;
 97:             // widget value
 98:             if (isset($this->_model->$column)) {
 99:                 if ($this->_model->columnType[$column] == 'bool_type') {
100:                     if ($this->_model->$column == true
101:                         or $this->_model->$column == 't'
102:                         or $this->_model->$column == 1
103:                     ) {
104:                         $params['checked'] = true;
105:                     } else {
106:                         $params['checked'] = false;
107:                     }
108:                 } else if ($this->_model->columnType[$column] == 'date_type') {
109:                     // get locale settings
110:                     $lang = substr(
111:                         filter_input(
112:                             INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE',
113:                             FILTER_SANITIZE_STRING
114:                         ), 0,2
115:                     );
116:                     setlocale(LC_ALL, $lang,
117:                         $lang.'_'.strtoupper($lang),
118:                         $lang.'_'.strtoupper($lang).'.utf8'
119:                     );
120:                     $date = strftime('%x', strtotime($this->_model->$column));
121:                     $params['value'] = $date;
122:                     unset($lang, $date);
123:                 } else if ($this->_model->columnType[$column] == 'time_type') {
124:                     // get locale settings
125:                     $lang = substr(
126:                         filter_input(
127:                             INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE'
128:                         ), 0,2
129:                     );
130:                     setlocale(LC_ALL, $lang,
131:                         $lang.'_'.strtoupper($lang),
132:                         $lang.'_'.strtoupper($lang).'.utf8'
133:                     );
134:                     $date = strftime('%X', strtotime($this->_model->$column));
135:                     $params['value'] = $date;
136:                     unset($lang, $date);
137:                 } else if ($this->_model->columnType[$column]
138:                     == 'timestamp_type') {
139:                     // get locale settings
140:                     $lang = substr(
141:                         filter_input(
142:                             INPUT_SERVER, 'HTTP_ACCEPT_LANGUAGE'
143:                         ), 0,2
144:                     );
145:                     setlocale(LC_ALL, $lang,
146:                         $lang.'_'.strtoupper($lang),
147:                         $lang.'_'.strtoupper($lang).'.utf8'
148:                     );
149:                     $date = strftime('%x %X',strtotime($this->_model->$column));
150:                     $params['value'] = $date;
151:                     unset($lang, $date);
152:                 } else {
153:                     $params['value'] = $this->_model->$column;
154:                 }
155:             } else {
156:                 $params['value'] = '';
157:             }
158:             // required
159:             if ($this->_model->columnCanBeNull[$column] == false) {
160:                 $params['required'] = true;
161:             }
162:             // if widget is for ID column
163:             if ($column == 'id') {
164:                 $params['readonly'] = true;
165:                 if (intval($params['value']) < 1) {
166:                     $query = 'SELECT CASE WHEN max(id) IS NULL THEN 1'
167:                         .' ELSE max(id)+1 END AS next_id FROM '
168:                         .$this->_model->tableName;
169:                     $db = new WsDatabase();
170:                     $result = $db->query($query);
171: 
172:                     $params['value'] = $result[0]['next_id'];
173: 
174:                     unset($query, $result, $db);
175:                 }
176:             }
177: 
178:             if (array_key_exists($column, $this->_model->foreignKeys)) {
179:                 $foreign_table = $this->_model->foreignKeys[$column]['table'];
180:                 // if self referecing foreign key
181:                 if ($foreign_table === $this->_model->tableName) {
182:                     $query = 'SELECT '
183:                         .$this->_model->foreignKeys[$column]['column']
184:                         .' AS option, '
185:                         .$this->_model->foreignKeys[$column]['display']
186:                         .' AS display FROM '
187:                         .$this->_model->tableName
188:                         .' ORDER BY option';
189:                 } else {
190:                     $query = 'SELECT '
191:                         .$this->_model->foreignKeys[$column]['column']
192:                         .' AS option, '
193:                         .$this->_model->foreignKeys[$column]['display']
194:                         .' AS display FROM '
195:                         .$this->_model->foreignKeys[$column]['table']
196:                         .' ORDER BY option';
197:                 }
198: 
199:                 $db = new WsDatabase();
200:                 $result = $db->query($query);
201: 
202:                 $this->selectInput($result, $params);
203: 
204:                 unset($query, $result, $db);
205:             } else {
206:                 switch ($this->_model->columnType[$column]) {
207:                     case 'bool_type':
208:                         $this->booleanInput($params);
209:                         break;
210:                     case 'textarea_type':
211:                         $this->textareaInput($params);
212:                         break;
213:                     case 'timestamp_type':
214:                         $params['type'] = 'datetime-local';
215:                         $this->textInput($params);
216:                         break;
217:                     case 'date_type':
218:                         $params['type'] = 'date';
219:                         $this->textInput($params);
220:                         break;
221:                     case 'time_type':
222:                         $params['type'] = 'time';
223:                         $this->textInput($params);
224:                         break;
225:                     case 'password_type':
226:                         $params['type'] = 'password';
227:                         $this->textInput($params);
228:                         break;
229:                     case 'int_type':
230:                         $params['type'] = 'number';
231:                         $this->textInput($params);
232:                         break;
233:                     case 'numeric_type':
234:                         $params['type'] = 'number';
235:                         $this->textInput($params);
236:                         break;
237:                     case 'url_type':
238:                         $params['type'] = 'url';
239:                         $this->textInput($params);
240:                         break;
241:                     case 'email_type':
242:                         $params['type'] = 'email';
243:                         $this->textInput($params);
244:                         break;
245:                     case 'phone_type':
246:                         $params['type'] = 'tel';
247:                         $this->textInput($params);
248:                         break;
249:                     case 'file_type':
250:                         $params['type'] = 'file';
251:                         $this->textInput($params);
252:                         break;
253:                     default:
254:                         $params['type'] = 'text';
255:                         $this->textInput($params);
256:                 }
257:             }
258: 
259:             // clear parameters for next widget
260:             $params = array();
261:         }
262: 
263:         $this->_form .= '<br/>';
264:         $this->_form .= '<br/>';
265:         $this->_form .= '<div class="row">';
266:         $this->_form .= '<div class="column column-12 text-center">';
267:         $this->_form .= '<input type="submit" class="button success"'
268:             .' value="'.$this->submitButtonText.'"/>';
269:         $this->_form .= '</div></div>';
270:         $this->_form .= '<div class="row" id="form_status"></div>';
271:         $this->_form .= '</fieldset>';
272:         $this->_form .= '</form>';
273: 
274:         $this->_form .= '</div>';
275:         $this->_form .= '</div>';
276: 
277:         $this->_form .= '<script>';
278:         // for form validation
279:         $this->_form .= '$("#'.$this->_id.'").validate({';
280:         $this->_form .= 'submitHandler: function(form) {';
281:         $this->_form .= 'var form_object = $("#'.$this->_id.'");';
282:         $this->_form .= 'WssaveModel("'.$this->_id.'", "'.$this->_dialog.'"'
283:             .',"'.WsSERVER_ROOT.'/protected/library/ajax/WsSaveToModel.php");';
284:         $this->_form .= '}';
285:         $this->_form .= '});';
286:         $this->_form .= '</script>';
287:     }
288: 
289: 
290:     public function __toString()
291:     {
292:         return $this->_form;
293:     }
294: 
295: 
296:     /**
297:      * Displays generated model form on screen
298:      *
299:      */
300:     public function show()
301:     {
302:         echo $this->_form;
303:     }
304: 
305: 
306:     /*
307:      * Return name of model
308:      *
309:      * @return string Model name.
310:      *
311:      */
312:     public function getModelName()
313:     {
314:         return $this->_model->className;
315:     }
316: }
317: 
API documentation generated by ApiGen