X-Git-Url: http://git.roojs.org/?a=blobdiff_plain;f=DataObjects%2FCore_company.php;h=aaf08e29908ecd8d48b2d3cdf8239aa9764ffccb;hb=b02a820041af130c0bc572177baa9b52d5526188;hp=c709f3ab6bee6c283796ba1971de5602e3f9494e;hpb=4c1f9867b196ebcaed870af39c4ca859abe30d47;p=Pman.Core diff --git a/DataObjects/Core_company.php b/DataObjects/Core_company.php index c709f3ab..aaf08e29 100644 --- a/DataObjects/Core_company.php +++ b/DataObjects/Core_company.php @@ -240,13 +240,33 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject $this->update(); } - - - + } + + function beforeInsert($q, $roo) + { + if(!empty($q['_check_name'])){ + if($this->checkName()){ + $roo->jok('OK'); + } + + $roo->jerr('EXIST'); + } } function beforeUpdate($old, $q,$roo) { + if(!empty($q['_check_name'])){ + if($this->checkName()){ + $roo->jok('OK'); + } + + $roo->jerr('EXIST'); + } + + if(!empty($q['_merge_id'])){ + $this->merge($q['_merge_id'], $roo); + } + if(!empty($this->is_system) && ($old->code != $this->code ) // used to be not allowed to change name.. ){ @@ -421,4 +441,63 @@ class Pman_Core_DataObjects_Core_Company extends DB_DataObject } return false; } + + function merge($merge_to, $roo) + { + $affects = array(); + + $all_links = $GLOBALS['_DB_DATAOBJECT']['LINKS'][$this->_database]; + + foreach($all_links as $tbl => $links) { + foreach($links as $col => $totbl_col) { + $to = explode(':', $totbl_col); + if ($to[0] != $this->tableName()) { + continue; + } + + $affects[$tbl .'.' . $col] = true; + } + } + + foreach($affects as $k => $true) { + $ka = explode('.', $k); + + $chk = DB_DataObject::factory($ka[0]); + + if (!is_a($chk,'DB_DataObject')) { + $roo->jerr('Unable to load referenced table, check the links config: ' .$ka[0]); + } + + $chk->{$ka[1]} = $this->id; + + foreach ($chk->fetchAll() as $c){ + $cc = clone ($c); + $c->{$ka[1]} = $merge_to; + $c->update($cc); + } + } + + $this->delete(); + + $roo->jok('Merged'); + + } + + function checkName() + { + $company = DB_DataObject::factory('core_company'); + $company->setFrom(array( + 'name' => $this->name + )); + + if(!empty($this->id)){ + $company->whereAdd("id != {$this->id}"); + } + + if(!$company->find(true)){ + return true; + } + + return false; + } }