55 $defaultClass =
'db_row_'.$this->cfg->table->getName();
56 if (get_class($this) != $defaultClass)
57 $this->cfg->overload($defaultClass);
59 $this->table = $this->cfg->table;
61 if (!empty($this->cfg->data)) {
63 }
else if (!empty($this->cfg->findId)) {
64 $tmp = $this->
getTable()->find($this->cfg->findId);
66 $this->cfg->data = $tmp->getValues(
'data');
79 $this->cfg->data = $data;
80 if (array_key_exists($this->
getTable()->getIdent(), $data) && $data[$this->
getTable()->getIdent()])
83 $primary = $this->
getTable()->getPrimary();
85 foreach($primary as $pp) {
86 if (array_key_exists($pp, $data) && $data[$pp])
89 if ($p == count($primary))
94 if (array_key_exists($linkedKey, $data)) {
99 if (array_key_exists($relatedKey, $data)) {
110 if (!$this->
isNew() && $this->
getId() && ($force || $this->cfg->needReload)) {
112 $this->cfg->needReload =
false;
122 return $this->cfg->db;
141 return $this->
getDb()->getWhere($prm);
152 public function getForm($showFields = null, array $formParam = array(), $passConfirm =
true) {
153 $form =
factory::get(
'form_db', array_merge($formParam, array(
157 foreach($this->
getTable()->getField() as
$f) {
158 if ($f[
'name'] != $this->
getTable()->getIdent() && !$f[
'auto'] &&
159 (empty($showFields) || in_array($f[
'name'], $showFields))) {
160 $f[
'label'] = $this->
getTable()->getLabel($f[
'name']);
161 $f[
'link'] = $this->
getTable()->getLinked($f[
'name']);
162 $f[
'value'] = $this->
get($f[
'name']);
163 $obj = $form->addFromField($f);
166 $f[
'name'].=
'Confirm';
167 $f[
'label'] = $this->
getTable()->getLabel($f[
'name']);
168 $moreConf = $this->
getTable()->getCfg()->getInArray(
'fields', $f[
'name']);
169 if (is_array($moreConf) && array_key_exists(
'comment', $moreConf)) {
172 $form->addFromField($f)->addRule(
'equal', $obj);
173 $form->addNotValue($f[
'name']);
174 if (!$this->
isNew()) {
176 $form->get($name)->getValid()->delRule(
'required');
177 $form->get($f[
'name'])->getValid()->delRule(
'required');
179 if (!empty($showFields) && !in_array($f[
'name'], $showFields)) {
180 $key = array_search($name, $showFields);
181 array_splice($showFields, $key+1, 0, array($f[
'name']));
187 $relatedUpdated =
false;
188 $related = $this->cfg->getInArray(
'data',
'related');
190 if (empty($showFields) || in_array($r[
'tableLink'], $showFields)) {
191 $r[
'name'] = $r[
'tableLink'];
192 $r[
'label'] = $this->
getTable()->getLabel($r[
'table']);
194 $form->addFromRelated($r);
197 $relatedUpdated =
true;
205 $this->cfg->setInArray(
'data',
'related',
$related);
207 $form->setValues($this->
getValues(
'flat'));
209 $i18nFields = $this->
getTable()->getI18nFields();
210 $i18nFieldsT = array();
212 foreach($i18nFields as $f) {
213 if ((empty($showFields) || in_array(
db::getCfg(
'i18n').$f[
'name'], $showFields))) {
214 $i18nFieldsT[] = $f[
'name'];
215 $f[
'label'] = $this->
getTable()->getI18nTable()->getLabel($f[
'name']);
216 $form->addFromField($f,
true);
224 $primary = $this->
getTable()->getI18nTable()->getPrimary();
226 $lang = $r->get($primary[1]);
227 foreach($r->getValues() as $k=>$v) {
228 $tmp[
db::getCfg(
'i18n').
'['.$lang.
']['.$k.
']'] = $v;
231 $form->setValues($tmp);
233 $form->setBound(
false);
234 if (is_array($showFields) && !empty($showFields))
235 $form->reOrder($showFields);
255 $this->
new = (bool)
$new;
265 unset($values[$this->
getTable()->getIdent()]);
266 $id = $this->
getTable()->insert($values);
267 $this->
set($this->
getTable()->getIdent(), $id);
304 throw new nException(
'db_row::saveRelated: try to save related for a new row');
308 if (array_key_exists($r[
'tableLink'],
$changes)) {
310 $r[
'fk1'][
'name'] => $this->
getId()
312 $r[
'tableObj']->delete($values);
313 $hasFields = isset($r[
'fields']) && count($r[
'fields']);
314 if (($tmp =
$changes[$r[
'tableLink']]) && is_array($tmp)) {
315 foreach($tmp as $t) {
316 $curValues = $values;
318 foreach($t as $k=>$v) {
320 $curValues[$r[
'fk2'][
'name']] = $v;
325 $curValues = array_merge($values, array($r[
'fk2'][
'name'] => $t));
326 $r[
'tableObj']->insert($curValues);
340 throw new nException(
'db_row::saveI18n: try to save i18n for a new row');
342 if (!empty($this->i18nRows)) {
343 list($fkId, $lang) = ($this->
getTable()->getI18nTable()->getPrimary());
344 foreach($this->i18nRows as $r) {
346 $r->set($fkId, $this->getId());
357 public function delete() {
373 return $this->
get($this->
getTable()->getIdent());
380 $this->changes = array();
389 return in_array($key, $this->
getTable()->getCols());
399 public function get($key, $mode =
'flat') {
404 if ($mode ==
'flat' && $this->
hasChange($key))
405 $val = $this->changes[$key];
407 $val = $this->cfg->getInArray(
'data', $key);
409 }
else if ($val = $this->cfg->getInArray(
'data', $key)) {
411 }
else if ($this->
getTable()->isRelated($key)) {
412 $key = $this->
getTable()->getRelatedTableName($key);
414 if (isset($values[$key]))
415 return $values[$key];
417 if (!$this->
getTable()->
getCfg()->autoJoin && ($mode ==
'flat' || $mode ==
'flatReal')) {
420 $v = $this->
getTable()->getRelated($key);
422 $fields = explode(
',', $v[
'fk2'][
'link'][
'fields']);
423 $i18nFields = explode(
',', $v[
'fk2'][
'link'][
'i18nFields']);
424 array_walk($i18nFields, create_function(
'&$v',
'$v = "'.
db::getCfg(
'i18n').
'".$v;'));
425 $fields = array_filter(array_merge($fields, $i18nFields));
427 $hasFields = isset($v[
'fields']) && count($v[
'fields']);
429 foreach($tmp as $vv) {
430 if ($mode ==
'flat') {
433 db::getCfg(
'relatedValue')=>$vv->get($v[
'fk2'][
'link'][
'ident'])
435 foreach($v[
'fields'] as $kF=>$vF) {
436 $curVal[$kF] = $vv->get($kF);
440 $ret[] = $vv->get($v[
'fk2'][
'link'][
'ident']);
444 foreach($fields as
$f) {
446 $tmp2[] = $vv->get($f);
448 $ret[] =
utils::htmlOut(implode($v[
'fk2'][
'link'][
'sep'], $tmp2));
466 public function getI18n($key, $mode=
'flat', $lang=null) {
467 return $this->
getI18nRow($lang)->get($key, $mode);
477 if (is_null($lang) || !$lang)
479 if (!array_key_exists($lang, $this->i18nRows)) {
482 $primary = $this->
getTable()->getI18nTable()->getPrimary();
483 $this->i18nRows[$lang] = $this->
getTable()->getI18nTable()->getRow();
484 $this->i18nRows[$lang]->setValues(array(
485 $primary[0]=>$this->
getId(),
489 return $this->i18nRows[$lang];
510 case 'flatNoRelated':
511 $data = array_merge($this->cfg->data, $this->getChanges());
512 $tmp = $this->
getTable()->getCols();
514 if ($mode ==
'flat') {
519 if (array_key_exists($key = $k.
'_'.$v[
'ident'], $data))
520 $data[$k] = $data[$k.
'_'.$v[
'ident']];
523 if (array_key_exists(
'related', $data)) {
527 $hasFields = isset($v[
'fields']) && count($v[
'fields']);
528 foreach($data[
'related'][$v[
'fk2'][
'link'][
'table']] as $vv) {
531 db::getCfg(
'relatedValue')=>$vv[$v[
'fk2'][
'link'][
'ident']]
533 foreach($v[
'fields'] as $kF=>$vF) {
534 $curVal[$kF] = $vv[$kF];
536 $data[$k][] = $curVal;
538 $data[$k][] = $vv[$v[
'fk2'][
'link'][
'ident']];
544 return array_intersect_key($data, array_flip($tmp));
547 case 'flatRealNoRelated':
548 $data = array_merge($this->cfg->data, $this->getChanges());
549 $tmp = $this->
getTable()->getCols();
551 if ($mode ==
'flatReal' && array_key_exists(
'related', $data)) {
555 $fields = explode(
',', $v[
'fk2'][
'link'][
'fields']);
556 $i18nFields = explode(
',', $v[
'fk2'][
'link'][
'i18nFields']);
557 array_walk($fields, create_function(
'&$v',
'$v = "'.$v[
'fk2'][
'link'][
'table'].
'_".$v;'));
558 array_walk($i18nFields, create_function(
'&$v',
'$v = "'.$v[
'fk2'][
'link'][
'table'].
'_'.
db::getCfg(
'i18n').
'".$v;'));
559 $fields = array_merge($fields, $i18nFields);
560 foreach($data[
'related'][$v[
'fk2'][
'link'][
'table']] as $vv) {
562 foreach($fields as
$f)
563 if (array_key_exists($f, $vv))
565 $data[$k][] = implode($v[
'fk2'][
'link'][
'sep'], $tmp2);
571 return array_intersect_key($data, array_flip($tmp));
575 return $this->cfg->data;
590 if(array_key_exists($name, $tmp))
603 public function set($key, $value, $force =
false) {
605 return $this->
setI18n($value, $force);
607 $field = $this->
getTable()->getField($key);
610 if (isset($field[
'comment']) && is_array($field[
'comment'])) {
611 foreach($field[
'comment'] as $k=>$v) {
612 if (!is_array($v) && strpos($v,
'fct:') === 0) {
613 $fct = substr($v, 4);
618 if (!is_null($fct) && function_exists($fct))
619 $value = $fct($value);
620 if ($force || $this->
get($key) != $value)
621 $this->changes[$key] = $value;
631 public function setI18n(array $values, $force =
false, $lg = null) {
632 if (!is_null($lg) && $lg) {
634 $this->
getI18nRow($lg)->setValues($values, $force);
636 foreach($values as $lg=>$val)
637 $this->
setI18n($val, $force, $lg);
647 public function setValues(array $values, $force =
false) {
648 foreach($values as $k=>$v)
649 $this->
set($k, $v, $force);
663 return $this->changes[$name];
692 if (array_key_exists(
db::getCfg(
'i18n'), $tmp))
708 foreach($this->i18nRows as $r)
709 $hasChange = $hasChange || $r->hasChange();
712 return array_key_exists($name, $this->changes);
722 public function getLinked($field = null, $reload =
false) {
723 if ($this->
getTable()->isLinked($field)) {
724 if (!array_key_exists($field, $this->linked)) {
726 if ($val = $this->
get($field,
'flatReal')) {
727 $tmp = $this->
getTable()->getLinked($field);
728 $data[$tmp[
'ident']] = $val;
729 }
else if ($val = $this->
get($field,
'flat')) {
730 $tmp = $this->
getTable()->getLinked($field);
731 $data[$tmp[
'ident']] = $val;
733 $this->linked[$field] = $this->
getTable()->getLinkedTableRow($field, $data);
735 if ($reload && array_key_exists($field, $this->linked) && !is_null($this->linked[$field]))
736 $this->linked[$field]->reload(
false);
737 return $this->linked[$field];
749 if (!is_null($field)) {
750 if ($this->
getTable()->isLinked($field)) {
752 $this->linked[$field] =
$linked;
754 $this->linked[$field] = $this->
getTable()->getLinkedTableRow($field,
$linked);
770 $id = $this->
getId();
773 return $related[
'tableObj']->getLinkedTable(
$related[
'fk2'][
'name'])->select(array(
776 'name'=>
$related[
'fk2'][
'link'][
'table'].
'.'.
$related[
'fk2'][
'link'][
'ident'],
777 'in'=>$this->
getDb()->selectQuery(array(
778 'fields'=>$related[
'fk2'][
'name'],
779 'table'=>$related[
'tableLink'],
780 'where'=>$related[
'fk1'][
'name'].
'='.$id
794 if (!is_null($name)) {
795 if ($this->
getTable()->getI18nTable() && $name == $this->
getTable()->getI18nTable()->getName()) {
796 $primary = $this->
getTable()->getI18nTable()->getPrimary();
798 $this->i18nRows[$r[$primary[1]]] = $this->
getTable()->getI18nTable()->getRow(
799 array_merge($r, array($primary[0]=>$this->
getId())));
802 $name = $this->
getTable()->getRelatedTableName($name);
803 if ($this->
getTable()->isRelated($name)) {
804 if (!array_key_exists($name, $this->related))
805 $this->related[$name] = array();
811 if ($v instanceof db_row)
812 $this->related[$name][] = $v;
814 $this->related[$name][] = $this->
getTable()->getRelatedTableRow($name, $v);
831 $primary = $this->
getTable()->getPrimary();
834 foreach($primary as $p) {
835 $where[] = $this->
getTable()->getRawName().
'.'.$p.=
'="'.$values[$p].
'"';
837 return implode(
' AND ', $where);
859 $prm[
'returnId'] =
true;
861 $field = $prm[
'field'];
862 $where = $this->
getDb()->makeWhere($prm[
'where']);
866 if (!is_null($where))
869 $field = $this->
getTable()->getIdent();
870 $val = $this->
get($field);
872 $query =
'(SELECT '.$field.
','.$this->
getTable()->getIdent().
' FROM '.$this->
getTable()->getRawName().
' '.$where.$field.
' < ? ORDER BY '.$field.
' DESC LIMIT 1) 874 (SELECT '.$field.
','.$this->
getTable()->getIdent().
' FROM '.$this->
getTable()->getRawName().
' '.$where.$field.
' > ? ORDER BY '.$field.
' ASC LIMIT 1)';
875 $vals = $this->
getDb()->query($query,
utils::htmlDeOut(array($val, $val)))->fetchAll(PDO::FETCH_NUM);
876 $ret = array(null, null);
877 $useIndex = $prm[
'returnId'] ? 1 : 0;
878 if (array_key_exists(1, $vals)) {
879 $ret[0] = $vals[0][$useIndex];
880 $ret[1] = $vals[1][$useIndex];
881 }
else if (array_key_exists(0, $vals)) {
884 $ret[1] = $vals[0][$useIndex];
886 $ret[0] = $vals[0][$useIndex];
890 $ret[0] = $this->
getTable()->find($ret[0]);
892 $ret[1] = $this->
getTable()->find($ret[1]);
898 if (strpos($name,
'get') === 0 || strpos($name,
'set') === 0) {
899 $tblName = strtolower(substr($name, 3, 1)).substr($name, 4);
903 foreach(array_keys(
$linked) as $v) {
904 if (strpos($v, $tblName.
'_') === 0) {
912 $tblName = $this->
getTable()->getName().
'_'.substr($tblName, 0, -1);
915 foreach(array_keys(
$related) as $v) {
916 if ($v == $tblName) {
925 if ($this->
getTable()->isLinked($name)) {
926 if (empty($prm) || is_bool($prm[0])) {
927 return $this->
getLinked($name, isset($prm[0]) ? $prm[0] :
false);
931 }
else if ($this->
getTable()->isRelated($name)) {
948 return !is_null($this->
getTable()->getField($offset));
959 return $this->
get($offset);
970 $this->
set($offset, $value);
980 $this->
set($offset, null);
984 return $this->
get($name);
987 public function __set($name, $val) {
988 return $this->
set($name, $val);
static htmlOut($val, $key=false)
static htmlDeOut($val, $key=false)
getWhere(array $prm=array())
getForm($showFields=null, array $formParam=array(), $passConfirm=true)
getInValues($name=null, $mode='flatReal')
static initTab(array &$vars, array $init)
setLinked($linked, $field=null)
setValues(array $values, $force=false)
getLinked($field=null, $reload=false)
setRelated($related, $name=null)
getI18n($key, $mode='flat', $lang=null)
static unI18nName($field)
offsetSet($offset, $value)
static isI18nName($field)
static mergeCfg(array &$prm, array $cfg)
getAround(array $prm=array())
static get($className, array $cfg=array())
setI18n(array $values, $force=false, $lg=null)