<?php
/**
- * Table Definition for core company
+ * Table Definition for core enum - it's used in pulldowns or simple option lists.
*/
require_once 'DB/DataObject.php';
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
- function applyFilters($q, $au)
+
+ function applyFilters($q, $au, $roo)
{
- //DB_DataObject::debugLevel(1);
+// DB_DataObject::debugLevel(1);
if (!empty($q['query']['empty_etype'])) {
- $this->whereAdd("etype = ''");
+ $this->whereAdd("core_enum.etype = ''");
}
+ // this should be handled by roo... using '!name[0]' ....
if(!empty($q['!name'])){
- $names = explode(',', $q['!name']);
- $this->whereAdd("
- core_enum.name NOT IN ({$q['!name']})
+ $names = is_array($q['!name']) ? $q['!name'] : explode(',', $q['!name']);
+ foreach($names as $name){
+ $name = $this->escape($name);
+ $this->whereAdd("
+ core_enum.name NOT IN ('$name')
");
- //$this->whereAddIn('name', $names, 'string', 'NOT');
+ }
+ }
+ if(!empty($q['search']['display_name'])) {
+ $name = $this->escape($q['search']['display_name']);
+ // ilike on postgres?!?
+ $this->whereAdd("
+ core_enum.display_name LIKE '{$name}%'
+ ");
- //print_r(11);
}
+ if(!empty($q['query']['search'])) {
+ $name = $this->escape($q['query']['search']);
+ // ilike on postgres?!?
+ $this->whereAdd("
+ core_enum.name LIKE '%{$name}%'
+ OR
+ core_enum.display_name LIKE '%{$name}%'
+ ");
+ }
+ if(!empty($q['query']['search_begins'])) {
+ $name = $this->escape($q['query']['search_begins']);
+ // ilike on postgres?!?
+ $this->whereAdd("
+ core_enum.name LIKE '{$name}%'
+ OR
+ core_enum.display_name LIKE '{$name}%'
+ ");
+ }
+
+ if (isset($q['_etypes'])) {
+ $this->whereAddIn('core_enum.etype', explode(',', $q['_etypes']), 'string');
+ }
}
- function onUpdate($old, $req)
+ function checkPerm($lvl, $au, $req=null)
{
- $x = $this->factory($this->tableName());
- $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')");
- if ($old->etype != $this->etype) {
- $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
+ if (!$au) {
+ return false;
}
+ return true;
+ }
+
+
+ function autoJoinCmsTranslate($lang)
+ {
+ $l = $this->escape($lang);
+
+ $this->_join .= "
+ LEFT JOIN
+ cms_templatestr
+ ON
+ cms_templatestr.lang = '$l'
+ AND
+ cms_templatestr.on_table = 'core_enum'
+ AND
+ cms_templatestr.on_id = core_enum.id
+ AND
+ cms_templatestr.on_col = 'display_name'
+ ";
+
+ $this->selectAdd("
+ CASE WHEN
+ '$l' = 'en' THEN display_name
+ ELSE
+ CASE WHEN cms_templatestr.txt IS NOT NULL AND cms_templatestr.txt != '' THEN
+ cms_templatestr.txt
+ ELSE
+ display_name
+ END
+ END as display_name_tr
+ ");
+
+ }
+
+ function postListFilter($data, $authUser, $q) {
+
+ if(!empty($q['cmsTab'])){
+ $ret = array();
+ foreach($data as $k=>$v){
+ if($v['name'] == 'element'){
+ continue;
+ }
+ $ary = $v;
+ if($ary['name'] == 'page'){
+ $ary['display_name'] = $v['display_name'].' / Elements';
+ }
+ $ret[] = $ary;
+ }
+ $data = $ret;
+ }
+
+ return $data;
}
+
+
function beforeUpdate($old, $request,$roo)
- {
+ {
+ if(!empty($request['_merge_id'])){
+ $this->merge($request['_merge_id'], $roo);
+ }
+
$tn = $this->tableName();
$x = $this->factory($tn);
- if(!($old->etype == $request['etype'] && $old->name == $request['name'])){
- $x->whereAdd("etype = '{$request['etype']}' AND name = '{$request['name']}'");
+ // check if record exists?
+ if(isset($request['etype']) && !($old->etype == $request['etype'] && $old->name == $request['name'])){
+ $x->whereAdd("etype = '{$this->escape($request['etype'])}' AND name = '{$this->escape($request['name'])}'");
+ $x->whereAdd("id != ".((int) $this->id));
$x->find(true);
if($x->count() > 0){
- $roo->jerr('is exsiting');
+ $roo->jerr('a duplicate record already exists');
}
}
}
$tn = $this->tableName();
$x = $this->factory($tn);
- if(empty($req['etype'])){
+ if(empty($req['etype']) || !strlen(trim($req['etype'])) ){
+
+ if (empty($req['name']) || !strlen(trim($req['name']))) {
+ $roo->jerr('name or etype missing');
+ }
+
if($x->get('name', $req['name'])){
- $roo->jerr('name is exsiting');
+ $roo->jerr("name already exists - '{$req['name']}'" );
+ }
+ } else if (!empty($req['_bulk_names'])) {
+
+ $lines = explode("\n", $req['_bulk_names']);
+ foreach($lines as $l) {
+ $l = trim($l);
+ if (!strlen($l)) {
+ continue;
+ }
+ $bits = explode(',', $l);
+ $rr = array(
+ 'etype' => $req['etype'],
+ 'name' => array_shift($bits)
+ );
+
+ $rr['display_name'] = empty($bits) ? $rr['name'] : $bits[0];
+
+ $x = $this->factory($tn);
+ $x->beforeInsert($rr, $roo);
+ $x->setFrom($rr);
+ $x->insert();
+
}
- }else{
- $x->whereAdd("etype = '{$req['etype']}' AND name = '{$req['name']}'");
+ $roo->jok("inserted");
+
+ } else {
+ if (empty($req['name']) || !strlen(trim($req['name']))) {
+ $roo->jerr('name missing');
+ }
+
+ $x->whereAdd("etype = '{$this->escape($req['etype'])}' AND name = '{$this->escape($req['name'])}'");
$x->find(true);
if($x->count() > 0){
- $roo->jerr('is exsiting');
+ $roo->jerr("name already exists - '{$req['name']}'" );
}
}
}
- function onInsert($req)
+
+ function onInsert($req, $roo)
{
$x = $this->factory($this->tableName());
- $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')");
+ $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
}
+ function onUpdate($old, $req, $roo)
+ {
+ $x = $this->factory($this->tableName());
+ $x->query("SELECT core_enum_seqmax_update('". $this->escape($this->etype) ."')"); // no idea why need to do this!!??
+ if ($old->etype != $this->etype) {
+ $x->query("SELECT core_enum_seqmax_update('". $this->escape($old->etype) ."')");
+ }
+
+ if($this->name != $old->name && !empty($old->name) && empty($old->etype) && empty($this->etype)){
+ $x->query("UPDATE core_enum SET etype = '". $this->escape($this->name)
+ ."' WHERE etype = '". $this->escape($old->name)."'");
+ }
+ }
+
+ /**
+ * lookup by etype/name and return id
+ */
function lookup($etype,$name) {
$ce = DB_DataObject::Factory('core_enum');
$ce->etype = $etype;
return $ce->id;
}
return 0;
+ }
+
+ function lookupCreate($etype,$name, $display_name=false) {
+
+ // check
+ $ce = DB_DataObject::Factory('core_enum');
+ $ce->setFrom(array(
+ 'etype' => '',
+ 'name' => $etype
+ ));
+ if (!$ce->find(true)) {
+ $ce->display_name = $etype;
+ $ce->insert();
+ }
+
+ $ce = DB_DataObject::Factory('core_enum');
+ $ce->etype = $etype;
+ $ce->name = $name;
+ if ($ce->find(true)) {
+ return $ce->id;
+ }
+ $ce->active = 1;
+ $ce->display_name = $display_name === false ? $ce->name : $display_name;
+ return $ce->insert();
}
+ function lookupById($id) {
+ $ce = DB_DataObject::Factory('core_enum');
+ $ce->get($id);
+ return $ce;
+ }
+
+ /**
+ *
+ *
+ *
+ * @param string $etype
+ * @param array $name array of name
+ * @return array ID of core_enum
+ */
+
+ function lookupAllByName($etype,$names) {
+ $ce = DB_DataObject::Factory('core_enum');
+ $ce->etype = $etype;
+ $ce->whereAddIn('name', $names, 'string');
+
+ if ($ce->count() > 0) {
+ return $ce->fetchAll('id');
+ }
+ return array();
+ }
+
+ function fetchAllByType($etype, $fetchArg1=false, $fetchArg2=false, $fetchArg3=false)
+ {
+ $x = DB_DataObject::factory('core_enum');
+ $x->etype = $etype;
+ $x->active = 1;
+ $x->orderBy('seqid ASC');
+ return $x->fetchAll($fetchArg1, $fetchArg2, $fetchArg3);
+ }
+
function lookupObject($etype,$name, $create= false)
{
return false;
}
- /**
- * The base enums that the system always needs,
- * If we need to add modular enums, then this can be extended, and the two
- * base data applied.
- *
- * This might be moved to an external file later? - eg json...
- *
- */
- function baseEnums()
+ // fixme - all calls should be to initDatabase, we need to remove initEnums
+ function initDatabase($roo, $data)
{
- //
- return array(
- array(
- 'etype' => '',
- 'name' => 'COMPTYPE',
- 'display_name' => 'Company Types',
- 'cn' => array(
- array(
- 'name' => 'OWNER',
- 'display_name' => 'Owner',
- 'seqid' => 999, // last...
- )
-
- )
- )
- );
-
-
-
+ $this->initEnums($data);
}
- function initEnums($data = false, $base = array())
+
+ function initEnums($data, $base = array())
{
-
- if ($data === false) {
- $this->initEnums($this->baseEnums());
- return;
- }
+ // base only contains etype...
+ //print_r($data);
$seq_id = 0;
if (!empty($base['etype'])) {
$seq_id = 1;
foreach($data as $row) {
$t = DB_DAtaObject::Factory('core_enum');
- $t->setFrom($row);
- $t->setFrom($base);
+ $t->etype = isset($row['etype']) ? $row['etype'] : '';
+ $t->etype = isset($base['etype']) ? $base['etype'] : $t->etype ;
+
+ $t->name = isset($row['name']) ? $row['name'] : '';
+
+ if (empty($t->name)) {
+ print_R($data);
+ die("ERROR: invalid name used for core_enum\n\n");
+ }
- if (!$t->find(true)) {
- if (!empty($base['etype']) && empty($row['seqid'])) {
+ if (!$t->count()) {
+ // base already gave it the etype..
+ $t->setFrom($row);
+
+
+ //$t->is_system_enum = 1; // this should be on the caller..
+
+ if (!empty($row['seqid']) && !is_numeric($row['seqid'])) {
$t->seqid = $seq_id;
$seq_id++;
}
+
$t->insert();
+ }else{
+ $t->find(true); // fetch it..
+ $o = clone($t);
+
+ if ( isset($row['is_system_enum'])) {
+ $t->is_system_enum = isset($row['is_system_enum']) ? $row['is_system_enum'] : $t->is_system_enum;
+ }
+
+ $t->display_name = isset($row['display_name']) ? $row['display_name'] : '';
+
+ $t->update($o);
+
}
if (!empty($row['cn'])) {
$this->initEnums($row['cn'], array('etype' => $t->name));
}
+ 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');
+
+ }
+
}