function actionAddNote() { // Create the add form $form = new YDForm('addEntryForm'); // Add the elements $form->addElement('text', 'title', 'Title:'); $form->addElement('textarea', 'body', 'Contents:'); $form->addElement('submit', 'cmdSubmit', 'Save'); // Apply filters $form->addFilter('title', 'trim'); $form->addFilter('body', 'trim'); // Add a rule $form->addRule('title', 'required', 'Title is required'); $form->addRule('body', 'required', 'Contents is required'); // Process the form if ($form->validate()) { // Save the entries in an array $entry = array('id' => md5($form->getValue('title') . $form->getValue('body')), 'title' => $form->getValue('title'), 'body' => $form->getValue('body')); // Save the serialized entry to a file $this->dataDir->createFile($entry['id'] . '.dat', YDObjectUtil::serialize($entry)); // Forward to the list view $this->forward('default'); // Return return; } // Add the form to the template $this->template->assignForm('form', $form); // Output the template $this->template->display(); }
/** * This function will render the form. * * @returns The rendered form. */ function render() { // Form $xml['form'][0]['#'] = array(); $xml['form'][0]['@']['name'] = $this->_form->_name; $xml['form'][0]['@']['method'] = $this->_form->_method; $xml['form'][0]['@']['action'] = $this->_form->_action; $xml['form'][0]['@']['target'] = $this->_form->_target; $xml['form'][0]['@']['legend'] = $this->_form->_legend; $form =& $xml['form'][0]['#']; $attr =& $form['attributes'][0]['#']; $elem =& $form['elements'][0]['#']; $rule =& $form['rules'][0]['#']; $comp =& $form['comparerules'][0]['#']; $frul =& $form['formrules'][0]['#']; $filt =& $form['filters'][0]['#']; $r_elem =& $form['registered'][0]['#']['elements'][0]['#']; $r_filt =& $form['registered'][0]['#']['filters'][0]['#']; $r_rule =& $form['registered'][0]['#']['rules'][0]['#']; $r_rend =& $form['registered'][0]['#']['renderers'][0]['#']; // Attributes if (sizeof($this->_form->_attributes)) { $attr =& $attr['attribute']; $i = 0; foreach ($this->_form->_attributes as $name => $value) { $attr[$i]['@']['name'] = $name; $attr[$i]['@']['value'] = $value; $i++; } } // Elements if (sizeof($this->_form->_elements)) { $i = 0; $elem =& $elem['element']; foreach ($this->_form->_elements as $name => $obj) { $arr = $obj->toArray(); // Name and Type $elem[$i]['@']['name'] = $arr['name']; $elem[$i]['@']['type'] = $arr['type']; // Labels (support only default values) $e_lab =& $elem[$i]['#']['labels'][0]['#']['label']; $e_lab[0]['@']['id'] = 'default'; $e_lab[0]['@']['value'] = $arr['label']; // Values (support only default values) $e_val =& $elem[$i]['#']['values'][0]['#']['value']; $e_val[0]['@']['id'] = 'default'; $e_val[0]['@']['value'] = $arr['value']; // Attributes $e_attr =& $elem[$i]['#']['attributes'][0]['#']; if (sizeof($arr['attributes'])) { $e_attr =& $e_attr['attribute']; $j = 0; foreach ($arr['attributes'] as $name => $value) { $e_attr[$j]['@']['name'] = $name; $e_attr[$j]['@']['value'] = $value; $j++; } } // Options $e_opts =& $elem[$i]['#']['options'][0]['#']; if (sizeof($arr['options'])) { $e_opts =& $e_opts['option']; $j = 0; foreach ($arr['options'] as $name => $value) { $e_opts[$j]['@']['name'] = $name; $e_opts[$j]['@']['value'] = $value; $j++; } } $i++; } } // Rules if (sizeof($this->_form->_rules)) { $i = 0; $rule =& $rule['rule']; foreach ($this->_form->_rules as $e => $rules) { foreach ($rules as $r) { // Name and Type $rule[$i]['@']['element'] = $e; $rule[$i]['@']['type'] = $r['rule']; // Errors (support only default values) $r_err =& $rule[$i]['#']['errors'][0]['#']['error']; $r_err[0]['@']['id'] = 'default'; $r_err[0]['@']['value'] = $r['error']; // Options $r_opt =& $rule[$i]['#']['options'][0]['#']; if (!is_null($r['options'])) { // If Integer, Float, String create just one option with id = "" if (is_scalar($r['options'])) { $r_opt =& $r_opt['option']; $r_opt[0]['@']['id'] = ""; $r_opt[0]['@']['value'] = $r['options']; $r_opt[0]['@']['serialized'] = 'false'; // If Array, create all options } else { if (is_array($r['options']) && !empty($r['options'])) { $r_opt =& $r_opt['option']; $j = 0; foreach ($r['options'] as $id => $value) { $r_opt[$j]['@']['id'] = $id; $r_opt[$j]['@']['serialized'] = 'false'; if (is_object($value) || is_array($value)) { $value = YDObjectUtil::serialize($value); $r_opt[$j]['@']['serialized'] = 'true'; } $r_opt[$j]['@']['value'] = $value; $j++; } // If Object, serialize it } else { if (is_object($r['options'])) { $r_opt =& $r_opt['option']; $r_opt[0]['@']['id'] = ""; $r_opt[0]['@']['value'] = YDObjectUtil::serialize($r['options']); $r_opt[0]['@']['serialized'] = 'true'; } } } } $i++; } } } // Compare Rules if (sizeof($this->_form->_comparerules)) { $i = 0; $comp =& $comp['rule']; foreach ($this->_form->_comparerules as $r) { // Type $comp[$i]['@']['type'] = $r['rule']; // Elements $c_elem =& $comp[$i]['#']['elements'][0]['#']['element']; $j = 0; foreach ($r['elements'] as $e) { $c_elem[$j]['@']['name'] = $e; $j++; } // Errors (support only default values) $c_err =& $comp[$i]['#']['errors'][0]['#']['error']; $c_err[0]['@']['id'] = 'default'; $c_err[0]['@']['value'] = $r['error']; $i++; } } // Form Rules if (sizeof($this->_form->_formrules)) { $i = 0; $frul =& $frul['rule']; foreach ($this->_form->_formrules as $r) { if (is_array($r)) { $r = $r[0] . '::' . $r[1]; } // Type $frul[$i]['@']['callback'] = $r; $i++; } } // Filters if (sizeof($this->_form->_filters)) { $i = 0; $filt =& $filt['element']; foreach ($this->_form->_filters as $e => $filters) { foreach ($filters as $f) { // Name and Filter $filt[$i]['@']['name'] = $e; $filt[$i]['@']['filter'] = $f; $i++; } } } // Registered filters if (sizeof($this->_form->_regFilters)) { $i = 0; $r_filt =& $r_filt['filter']; foreach ($this->_form->_regFilters as $name => $r) { if (is_array($r['callback'])) { $r['callback'] = $r['callback'][0] . '::' . $r['callback'][1]; } $r_filt[$i]['@']['name'] = $name; $r_filt[$i]['@']['callback'] = $r['callback']; $r_filt[$i]['@']['file'] = $r['file']; $i++; } } // Registered rules if (sizeof($this->_form->_regRules)) { $i = 0; $r_rule =& $r_rule['rule']; foreach ($this->_form->_regRules as $name => $r) { if (is_array($r['callback'])) { $r['callback'] = $r['callback'][0] . '::' . $r['callback'][1]; } $r_rule[$i]['@']['name'] = $name; $r_rule[$i]['@']['callback'] = $r['callback']; $r_rule[$i]['@']['file'] = $r['file']; $i++; } } // Registered renderers if (sizeof($this->_form->_regRenderers)) { $i = 0; $r_rend =& $r_rend['renderer']; foreach ($this->_form->_regRenderers as $name => $r) { $r_rend[$i]['@']['name'] = $name; $r_rend[$i]['@']['class'] = $r['class']; $r_rend[$i]['@']['file'] = $r['file']; $i++; } } // Registered elements if (sizeof($this->_form->_regElements)) { $i = 0; $r_elem =& $r_elem['element']; foreach ($this->_form->_regElements as $name => $r) { $r_elem[$i]['@']['name'] = $name; $r_elem[$i]['@']['class'] = $r['class']; $r_elem[$i]['@']['file'] = $r['file']; $i++; } } $xml = new YDXml($xml, YD_XML_ARRAY); return $xml->toString(); }
function actionSuscribe() { if (!$this->user->authentificated) { // Create the profile form $form = new YDForm('suscribeForm'); $form->addElement('text', 'suscribeLogin', t('user.login')); $form->addElement('textarea', 'suscribeDescription', t('user.desc')); $form->addElement('password', 'suscribePassUn', t('user.pwd')); $form->addElement('password', 'suscribePassDeux', t('user.confirmpwd')); $form->addElement('submit', 'cmdSubmit', t('user.valid')); // Add rules $form->addFormRule(array(&$this, 'checkSuscribe')); // Process the form if ($form->validate()) { // Insert profile $this->user->id = ''; $this->user->login = $form->getValue('suscribeLogin'); $this->user->description = $form->getValue('suscribeDescription'); $this->user->password = md5($form->getValue('suscribePassUn')); $this->user->insert(); $this->user->auth(); $_GET['idUser'] = $this->user->id; $_SESSION['s_user'] = YDObjectUtil::serialize($this->user); // Redirect sur les forums header('location: forums.php'); return; } // Assign variables to the template $this->actionTpl->assign('form', $form->toArray()); $content = new page($this->actionTpl->fetch('templates/user.suscribe.tpl'), 'Inscription'); // Display the action template into the master template $this->display($content); } else { $this->actionMonProfil(); return; } }
/** * This function will serialize the object. */ function serialize($obj) { return YDObjectUtil::serialize($this); }
function checkLogin($fields) { $user = new userObject($fields['loginName'], md5($fields['loginPass'])); $temp = $user->auth(); // errors during authentification ? if (is_array($temp)) { return $temp; } else { // else log the user $this->user = $user; $_SESSION['s_user'] = YDObjectUtil::serialize($user); return true; } }