/**
* Table Definition for Companies
*/
-require_once 'DB/DataObject.php';
+class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
-class Pman_Core_DataObjects_Companies extends DB_DataObject
+class Pman_Core_DataObjects_Core_Company extends DB_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
- public $__table = 'Companies'; // table name
+ public $__table = 'core_company'; // table name
public $code; // string(32) not_null
public $name; // string(128)
public $remarks; // blob(65535) blob
function applyFilters($q, $au)
{
- $tn = $this->tableName();
+
+ $tn = $this->tableName();
$this->selectAdd("i18n_translate('c' , {$tn}.country, 'en') as country_display_name ");
$tn = $this->tableName();
//DB_DataObject::debugLevel(1);
- $x = DB_DataObject::factory('Companies');
+ $x = DB_DataObject::factory('core_company');
$x->comptype= 'OWNER';
$x->find(true);
+ var_dump('fetch here??');exit;
+
if (!empty($q['query']['company_project_id'])) {
$add = '';
if (!empty($q['query']['company_include_self'])) {
if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
// in session...
$a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
- $u = DB_DataObject::factory('Companies');
+ $u = DB_DataObject::factory('core_company');
if ($u->get($a->id)) { //&& strlen($u->passwd)) {
return true;
}
if (!empty($_SESSION[__CLASS__][$sesPrefix .'-auth'])) {
$a = unserialize($_SESSION[__CLASS__][$sesPrefix .'-auth']);
- $u = DB_DataObject::factory('Companies');
+ $u = DB_DataObject::factory('core_company');
if ($u->get($a->id)) { /// && strlen($u->passwd)) {
return clone($u);
}
$img = DB_DataObject::factory('Images');
$img->onid= 0;
- $img->ontable = 'Companies';
+ $img->ontable = $this->tableName();
$img->imgtype = 'LOGO';
// should check uploader!!!
if ($img->find()) {
$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..
){
$roo->jerr('This company is not allow to delete');
}
$img = DB_DataObject::factory('Images');
- $img->ontable = 'Companies';
+ $img->ontable = $this->tableName();
$img->onid = $this->id;
$img->find();
while ($img->fetch()) {
function initCompanies($roo, $opts)
{
- $companies = DB_DataObject::factory('companies');
+ $companies = DB_DataObject::factory('core_company');
$ctype = empty($opts['add-company-with-type']) ? 'OWNER' : $opts['add-company-with-type'];
$roo->jerr("invalid company type '$ctype'");
}
if ($ctype =='OWNER') {
- $companies = DB_DataObject::factory('companies');
+ $companies = DB_DataObject::factory('core_company');
$companies->comptype_id = $enum;
if ($companies->count()) {
$roo->jerr("Owner company already exists");
}
}
- $companies = DB_DataObject::factory('companies');
+ $companies = DB_DataObject::factory('core_company');
// check that
$companies->setFrom(array(
$companies->insert();
$companies->onInsert(array(), $roo);
}
- function lookupOwner()
+ static function lookupOwner()
{
$enum = DB_DataObject::Factory('core_enum')->lookup('COMPTYPE', 'OWNER' );
- $companies = DB_DataObject::factory('companies');
+ $companies = DB_DataObject::factory('core_company');
$companies->comptype_id = $enum;
if ($companies->find(true)) {
return $companies;
}
return false;
}
+
+ function merge($merge_to, $roo)
+ {
+ $affects = array();
+
+ $all_links = $x->databaseLinks();
+
+ 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;
+ }
}