95 $this->cfg->submitText = $text;
104 $this->cfg->submitPlus = $text;
115 }
catch (Exception $e) {
129 public function to($type) {
133 $prm = $this->cfg->get($type);
134 if ($this->cfg->check($tmp = $type.ucfirst($this->cfg->mode)))
135 $prm = array_merge($prm, $this->cfg->get($tmp));
137 if (!$this->cfg->showSection) {
138 $prm = array_merge($prm, $this->cfg->get($type.
'NoSection'));
139 if ($this->cfg->check($tmp = $type.
'NoSection'.ucfirst($this->cfg->mode)))
140 $prm = array_merge($prm, $this->cfg->get($tmp));
143 $hiddenGlobal = is_array($prm) && (array_key_exists(
'noHidden', $prm) && $prm[
'noHidden']) || !(strpos($prm[
'global'],
'[hidden]') ===
false);
146 $errorPos = $prm[
'errorPos'];
147 $errorsGlobal = array();
149 foreach($this->section as $kSection=>$sectionName) {
151 $errorsSection = array();
152 foreach($this->elements[$kSection] as $name=>$e) {
153 $des = $e->description? str_replace(
'[des]', $e->getDescription(), $prm[
'des']) : null;
154 $line = $e->isHidden()?
'lineHidden' :
'line';
157 if ($this->
isBound() && !$e->isValid() && !$e->isHidden()) {
159 foreach($e->getErrors() as $err) {
160 $tmp[] = str_replace(
'[error]', $err, $prm[
'lineErrorLine']);
161 $errorsSection[] = str_replace(
'[error]', $err, $prm[
'sectionErrorLine']);
162 $errorsGlobal[] = str_replace(
'[error]', $err, $prm[
'globalErrorLine']);
164 $errors = $errorPos ==
'field' ? str_replace(
'[errors]', implode(
'', $tmp), $prm[
'lineErrorWrap']) : null;
168 $requiredMoreLabel = $e->getValidRule(
'required') ? $this->cfg->requiredMoreLabel : null;
170 ? $e->label.$requiredMoreLabel.$this->cfg->sepLabel
171 : $this->cfg->emptyLabel;
173 array(
'[des]',
'[label]',
'[field]',
'[errors]',
'[id]',
'[classLine]'),
174 array($des, $label, $e->to($type),
$errors, $e->id, $e->classLine),
176 if ($e->isHidden() && $hiddenGlobal)
184 if (!empty($errorsSection) && $errorPos ==
'section') {
185 $errors = implode(
'', $errorsSection);
189 array(
'[errors]',
'[fields]',
'[label]'),
196 if ($type ==
'html') {
197 if (array_key_exists(
'incFiles', $prm))
198 foreach($prm[
'incFiles'] as
$f)
201 $plus =
'action="'.$this->cfg->action.
'" method="'.$this->cfg->method.
'"';
203 $plus.=
' enctype="multipart/form-data"';
205 $plus.= $this->cfg->formPlus;
208 if (!empty($errorsGlobal) && $errorPos ==
'global') {
209 $errors = str_replace(
'[errors]', implode(
'', $errorsGlobal), $prm[
'globalError']);
212 array(
'[hidden]',
'[errors]',
'[content]',
'[plus]',
'[submit]',
'[submitText]',
'[submitPlus]'),
213 array($hiddens,
$errors, $ret, $plus, $prm[
'submit'], $this->cfg->submitText, $this->cfg->submitPlus),
222 $this->cfg->showSection =
true;
224 if ($this->cfg->forceOnlyOneLang || $this->cfg->noForceLang)
225 $requiredFields = array();
228 $groupedFieldsAdded =
false;
229 foreach($this->i18nElements as $e) {
230 $e[
'prm'][
'isI18n'] =
true;
231 $initName = $e[
'prm'][
'name'];
232 $e[
'prm'][
'name'] =
db::getCfg(
'i18n').
'['.$lg.
']['.$initName.
']';
233 if ($this->cfg->forceOnlyOneLang || $this->cfg->noForceLang) {
235 if ($e[
'prm'][
'valid'][
'required'])
236 $requiredFields[] = $initName;
238 if (!$groupedFieldsAdded && $e[
'prm'][
'valid'][
'required'] && count($requiredFields)) {
240 foreach($requiredFields as $v)
241 $fields[] =
db::getCfg(
'i18n').
'['.$lg.
']['.$v.
']';
242 $e[
'prm'][
'valid'][
'groupedFields'] = array(
246 $groupedFieldsAdded =
true;
249 if ($nb > 0 || $this->cfg->noForceLang)
250 $e[
'prm'][
'valid'][
'required'] =
false;
252 $this->
add($e[
'type'], $e[
'prm']);
263 return $this->
to(
'html');
270 return $this->
to(
'xul');
279 $validRet = empty($this->customErrors);
280 $this->errors = array();
281 foreach($this->section as $kSection=>$sectionName) {
282 foreach($this->elements[$kSection] as $name=>$e) {
283 $valid = $e->isValid();
285 $this->errors[$name] = $e->getErrors();
286 $validRet = $validRet && $valid;
298 return !empty($this->i18nElements);
307 return array_merge_recursive($this->errors, $this->customErrors);
316 return !empty($this->errors) || !empty($this->customErrors);
326 if ($elt = $this->
get($field)) {
327 $elt->addCustomError($error);
330 if (!array_key_exists($field, $this->customErrors))
331 $this->customErrors[$field] = array();
332 $this->customErrors[$field][] = $error;
343 public function add($type, array $prm=array(), $isI18n=
false) {
345 $this->i18nElements[] = array(
'type'=>$type,
'prm'=>$prm);
350 if ($type instanceof
form_abstract && !$this->
has($name = $type->getName())) {
353 }
else if (is_string($type) && array_key_exists(
'name', $prm) && !$this->
has($name = $prm[
'name'])) {
354 $inst = $this->
getNew($type, $prm);
374 public function has($name) {
375 return array_key_exists($name, $this->elementsSection);
384 public function get($name) {
385 if ($this->
has($name))
386 return $this->elements[$this->elementsSection[$name]][$name];
395 public function del($name) {
396 if ($this->
has($name)) {
397 unset($this->elements[$this->elementsSection[$name]][$name]);
398 unset($this->elementsSection[$name]);
409 foreach($order as $v) {
410 if (isset($this->elementsSection[$v]) && $this->elementsSection[$v] == $this->curSection) {
416 foreach($this->elements[$this->curSection] as $name=>$elt) {
417 if (!isset($tmp[$name]))
431 foreach($order as $v) {
432 $old = is_int($v) ? $v : array_search($v, $this->section);
433 if (!is_null($old) && $old !==
false && isset($this->section[$old])) {
436 $section[$new] = $this->section[$old];
438 foreach(array_keys(
$elements[$new]) as $k)
443 if (count(
$section) < count($this->section)) {
444 foreach($this->section as $old=>$v) {
445 if (!isset($trans[$old])) {
448 $section[$new] = $this->section[$old];
450 foreach(array_keys(
$elements[$new]) as $k)
470 if ($elm = $this->
get($name)) {
471 return $elm->getValue();
483 public function getValues($onlyFilled=
false, $ignoreWhitePassword=
true) {
486 foreach($this->elementsSection as $name=>
$section) {
487 if (!($ignoreWhitePassword && $this->
get($name) instanceof
form_password && !$this->
getValue($name)))
488 $ret[$name] = $this->
getValue($name);
491 $ret = array_diff_key($ret, $this->cfg->notValue);
494 $ret = array_filter($ret, create_function(
'$v',
'return $v ? true : false;'));
497 foreach($ret as $k=>$v) {
498 $matches = explode(
'|', str_replace(
499 array(
'][',
'[',
']'),
503 if (count($matches) > 1) {
505 for($i = 0; $i < count($matches); $i++) {
506 if (!isset($matches[$i+1]))
507 $t[$matches[$i]] = $v;
509 if (!array_key_exists($matches[$i], $t) || !is_array($t[$matches[$i]]))
510 $t[$matches[$i]] = array();
511 $t = &$t[$matches[$i]];
515 $tmp[$matches[0]] = $v;
526 $this->cfg->setInArray(
'notValue', $name,
true);
535 return array_keys($this->elementsSection);
546 public function setValue($name, $value, $refill=
false) {
547 if ($elm = $this->
get($name)) {
548 $elm->setValue($value, $refill);
564 foreach($data as $name=>$value) {
565 if ($this->
setValue($name, $value, $refill))
595 foreach($this->elementsSection as $name=>
$section) {
596 $val = $htVars->getVar(array(
597 'name'=>str_replace(
'.',
'_', $name),
598 'method'=>$this->cfg->method
611 public function getNew($type, array $prm) {
612 if (!array_key_exists(
'mode', $prm))
613 $prm[
'mode'] = $this->cfg->mode;
624 $this->curSection = count($this->section);
638 if (!is_int($search)) {
639 foreach($this->section as $k=>&$s) {
646 if (is_int($find) && $find < count($this->section))
647 $this->curSection = $find;
661 if (is_null($search)) {
663 }
else if (is_int($search)) {
666 foreach($this->section as $k=>&$s) {
672 if (is_int($find) && $find < count($this->section)) {
673 $this->section[$find] = $name;
687 $f = $this->
get($name);
692 $this->elementsSection[$name] =
$section;
706 $this->cfg->mode = $mode;
708 foreach($this->section as $kSection=>$sectionName)
709 foreach($this->elements[$kSection] as $name=>$e)
718 $this->curSection = 0;
725 $this->curSection = count($this->elements)-1;
741 if (!$this->captchaAdded) {
742 if (($typeCpt = $this->cfg->getInarray(
'captcha',
'type')) && ($nameCpt = $this->cfg->getInarray(
'captcha',
'name'))) {
743 $this->
add($typeCpt, $this->cfg->captcha);
744 $this->captchaAdded =
true;
745 $this->cfg->setInArray(
'notValue', $nameCpt, $nameCpt);
757 public function set($key1, $key2, $val) {
758 $this->cfg->setInArray($key1, $key2, $val);
762 $this->cfg->set($key, $val);
769 $this->cfg =
new config($this->cfg->getAll());
771 foreach($this->section as $kSection=>$sectionName) {
772 foreach($this->elements[$kSection] as $name=>$e) {
773 $this->elements[$kSection][$name] =
factory::get(get_class($e), unserialize(serialize($e->getCfg()->getAll())));
774 if (!is_object($e->getValue()))
775 $this->
setValue($name, $e->getValue());
static htmlOut($val, $key=false)
static avlLang($withName=false)
static get($className, array $cfg=array())
static trace($obj, $printExit=false)