PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/core/include/shopObject.php

http://phpfor.googlecode.com/
PHP | 1041 lines | 825 code | 102 blank | 114 comment | 152 complexity | 9935bd744d1ea2f16dc033ab7b8efc79 MD5 | raw file
  1. <?php
  2. /**
  3. * shopObject
  4. * ??????
  5. *
  6. * @uses modelFactory
  7. * @package
  8. * @version $Id$
  9. * @copyright 2003-2007 ShopEx
  10. * @author Wanglei <flaboy@zovatech.com>
  11. * @license Commercial
  12. */
  13. class shopObject extends modelFactory{
  14. var $disabledMark = 'normal';
  15. var $typeName = null;
  16. function shopObject(){
  17. parent::modelFactory();
  18. if(!$this->typeName) $this->typeName = substr(strstr(get_class($this),'_'),1);
  19. }
  20. function getFilter($params){;}
  21. function getByFilter($data,$start=0,$limit=null){
  22. parse_str($data,$data);
  23. $return=array();
  24. foreach($this->getList($this->idColumn,$data,$start,$limit,$count) as $row){
  25. $return[] = $row[$finder['id']];
  26. }
  27. return $return;
  28. }
  29. function modifier_tag(&$rows){
  30. foreach($rows as $r){
  31. $rows[$r] = null;
  32. if(is_array($this->tags[$r])){
  33. foreach($this->tags[$r] as $t){
  34. $rows[$r] .= '<b class="tag">'.$t.'</b>';
  35. }
  36. }
  37. }
  38. unset($this->tags);
  39. }
  40. function modifier_region(&$rows){
  41. foreach($rows as $i=>$r){
  42. list($pkg,$regions,$region_id) = explode(':',$r);
  43. if(is_numeric($region_id)){
  44. $rows[$i] = str_replace('/','-',$regions);
  45. }
  46. }
  47. }
  48. function modifier_time(&$rows,$options=array()){
  49. if(count($options)>0){
  50. $formatId = $options[0];
  51. switch($formatId){
  52. case 'FDATE':
  53. $dateFormat = 'Y-m-d';
  54. break;
  55. case 'SDATE':
  56. $dateFormat = 'y-m-d';
  57. break;
  58. case 'DATE':
  59. $dateFormat = 'm-d';
  60. break;
  61. case 'FDATE_FTIME':
  62. $dateFormat = 'Y-m-d H:i:s';
  63. break;
  64. case 'FDATE_STIME':
  65. $dateFormat = 'Y-m-d H:i';
  66. break;
  67. case 'SDATE_FTIME':
  68. $dateFormat = 'y-m-d H:i:s';
  69. break;
  70. case 'SDATE_STIME':
  71. $dateFormat = 'y-m-d H:i';
  72. break;
  73. case 'DATE_FTIME':
  74. $dateFormat = 'm-d H:i:s';
  75. break;
  76. case 'DATE_STIME':
  77. $dateFormat = 'm-d H:i';
  78. break;
  79. default:
  80. if(!$dateFormat = &$this->system->getConf('admin.dateFormat')){
  81. $dateFormat = 'm-d H:i:s';
  82. }
  83. }
  84. }else{
  85. if(!$dateFormat = &$this->system->getConf('admin.dateFormat')){
  86. $dateFormat = 'm-d H:i:s';
  87. }
  88. }
  89. foreach($rows as $i=>$date){
  90. if($date){
  91. //$date+=($GLOBALS['user_timezone']-SERVER_TIMEZONE)*3600;??????????????????????
  92. $rows[$i] = ($date ? date($dateFormat,$date) : '');
  93. }
  94. }
  95. }
  96. function columnValue($column,$value){
  97. if(!isset($this->_columns)){
  98. $this->_columns = $this->getColumns();
  99. }
  100. switch($this->_columns[$column]['type']){
  101. case 'number':
  102. return intval($value);
  103. break;
  104. case 'date':
  105. case 'time':
  106. return strtotime($value);
  107. case 'bool':
  108. if($value === '1' || $value === '0' || $value === 'true' || $value === 'false') return $value;
  109. else{
  110. if($this->_columns[$column]['bool'] == 'number'){
  111. return ($value)?'1':'0';
  112. }else{
  113. return ($value)?'true':'false';
  114. }
  115. }
  116. break;
  117. case 'money':
  118. if($value{0}=='+' || $value{0}=='*' || $value{0}=='/'){
  119. return $column.$value{0}.floatval(substr($value,1));
  120. }
  121. if($value<0){
  122. return $column.'-'.floatval(substr($value,1));
  123. }else{
  124. return $value;
  125. }
  126. break;
  127. default:
  128. return $value;
  129. break;
  130. }
  131. }
  132. function modifier_money(&$rows,$options=array()){
  133. $objCur = $this->system->loadModel('system/cur');
  134. foreach($rows as $i=>$money){
  135. $rows[$i] = $objCur->changer($money);
  136. }
  137. }
  138. function modifier_bool(&$rows,$options=array()){
  139. $aBool = array(
  140. 0=>'?',
  141. 1=>'?',
  142. 'false'=>'?',
  143. 'true'=>'?' );
  144. foreach($rows as $i => $v){
  145. $rows[$i] = $aBool[$v];
  146. }
  147. }
  148. function modifier_enum(&$rows,$options=array()){
  149. $options = $options['options'];
  150. foreach($rows as $i => $v){
  151. $rows[$i] = $options[$v];
  152. }
  153. }
  154. function modifier_object(&$rows,$options=array()){
  155. if(count($options)==0||!is_array($options)){
  156. trigger_error('Undefined object params',E_USER_WARNING);
  157. return;
  158. }
  159. $objects = $this->objects();
  160. if($mod=$objects[$options[0]]){
  161. $o = &$this->system->loadModel($mod);
  162. $col = $options[1]?$options[1]:$o->textColumn;
  163. $aList = $o->getList($o->idColumn.','.$col,array($o->idColumn=>array_keys(array_flip($rows))),0,-1,$c);
  164. foreach($rows as $k => $v){
  165. $rows[$k] = '-'; //?????? Ever? 2008-07-23
  166. }
  167. foreach($aList as $r){
  168. if($r[$col]){
  169. $rows[$r[$o->idColumn]] = $r[$col];
  170. }
  171. }
  172. $aList = null;
  173. }
  174. }
  175. function setFinderCols($options=null,$filter=null){
  176. $allCols = $this->getColumns($filter);
  177. $process = array('GridEdit','ColumnEdit','GridShow');
  178. foreach($process as $item){
  179. $colsAttr = 'cols'.$item;
  180. $enableAttr = 'enable'.$item.'Cols';
  181. $disableAttr = 'disable'.$item.'Cols';
  182. $this->$colsAttr = array_keys($allCols);
  183. $ret_arr=array();
  184. if($options[$enableAttr]){
  185. $arr = explode(',',$options[$enableAttr]);
  186. foreach($arr as $v){
  187. if(in_array($v,$this->$colsAttr)){
  188. $ret_arr[] = $v;
  189. }
  190. }
  191. $this->$colsAttr = $ret_arr;
  192. }elseif($options[$disableAttr]){
  193. $arr = explode(',',$options[$disableAttr]);
  194. $ret_arr = $this->$colsAttr;
  195. foreach($ret_arr as $k=>$v){
  196. if(in_array($v,$arr)){
  197. unset($ret_arr[$k]);
  198. }
  199. }
  200. $this->$colsAttr = $ret_arr;
  201. }else{
  202. if($item=='GridEdit'||$item=='ColumnEdit'){
  203. $this->$colsAttr = array();
  204. }
  205. }
  206. if($item=='GridEdit'||$item=='ColumnEdit'){
  207. foreach($this->$colsAttr as $k=>$v){
  208. if($this->idColumn == $v){
  209. unset($this->$colsAttr[$k]);
  210. }
  211. }
  212. }
  213. }
  214. }
  215. function getFinder($cols,$filter,$start=0,$limit=null,&$count,$orderby=null,$disabledCols=null,$editMode=false){
  216. $allCols = $this->getColumns($filter);
  217. if($this->hasTag){
  218. $allCols = array_merge(array('_tag_'=>array('label'=>'??','colsGridShow'=>true,'type'=>'tag','class'=>'span-4','noOrder'=>true)),$allCols);
  219. }
  220. $useTools = false;
  221. $extraCols = '';
  222. $ActionsFilterFound = false;
  223. $modifiers = false;
  224. if(is_null($this->colsGridEdit)||is_null($this->colsColumnEdit)||is_null($this->colsGridShow)){
  225. $this->setFinderCols(null,$filter);
  226. }
  227. $disabledCols = array_flip(explode(',',$disabledCols));
  228. foreach(explode(',',$cols) as $col){
  229. if(!isset($disabledCols[$col])){
  230. if(isset($allCols[$col])){
  231. $allCols[$col]['used'] = true;
  232. $colArray[$col] = &$allCols[$col];
  233. if(isset($allCols[$col]['sql'])){
  234. $sql[] = $allCols[$col]['sql'].' as '.$col;
  235. }elseif($col=='_tag_'){
  236. $sql[] = $this->idColumn.' as _tag_';
  237. }else{
  238. $sql[] = $col;
  239. }
  240. }else{
  241. trigger_error('Undefined column "'.$col.'"',E_USER_WARNING);
  242. }
  243. }
  244. }
  245. if(!isset($colArray[$this->idColumn])) array_unshift($sql,$this->idColumn);
  246. if($filter===-1){
  247. $list = array();
  248. }else{
  249. $list = $this->getList(implode(',',$sql).$extraCols,$filter,$start,$limit,$count,$orderby);
  250. if($list===false){
  251. return false;
  252. }
  253. foreach($allCols as $k=>$v){
  254. if($k==$this->textColumn){
  255. $allCols[$k]['required'] = 1;
  256. }
  257. $allCols[$k]['colsGridEdit'] = false;
  258. $allCols[$k]['colsColumnEdit'] = false;
  259. $allCols[$k]['colsGridShow'] = false;
  260. if(in_array($k,$this->colsGridEdit)){
  261. $allCols[$k]['colsGridEdit'] = !$allCols[$k]['readonly'];
  262. }
  263. if(in_array($k,$this->colsColumnEdit)){
  264. $allCols[$k]['colsColumnEdit'] = !$allCols[$k]['readonly'];
  265. }
  266. if(in_array($k,$this->colsGridShow)){
  267. $allCols[$k]['colsGridShow'] = true;
  268. }
  269. }
  270. $id = array();
  271. $highlight = method_exists($this,'is_highlight');
  272. foreach($list as $i=>$row){
  273. foreach($row as $k=>$v){
  274. if(!$editMode || !$colArray[$k]['colsGridEdit'] || $colArray[$k]['type']=='time'||substr($colArray[$k]['type'],0,5)=='time:'){
  275. if(isset($colArray[$k]['modifier']) && $colArray[$k]['modifier']=='row'){
  276. $func = 'modifier_'.$k;
  277. $list[$i][$k] = $this->$func($row);
  278. }else{
  279. if($colArray[$k]['type'] && !is_null($v)){
  280. $modifier_key = $colArray[$k]['options']?$colArray[$k]['type'].'|'.serialize($colArray[$k]['options']):$colArray[$k]['type'];
  281. $modifiers[$modifier_key][$v] = $v;
  282. $list[$i]['__'.$k] = $list[$i][$k];
  283. $list[$i][$k] = &$modifiers[$modifier_key][$v];
  284. }
  285. }
  286. }
  287. if($highlight)$list[$i]['highlight'] = $this->is_highlight($row);
  288. }
  289. $id[] = $row[$this->idColumn];
  290. }
  291. if($this->hasTag && count($id)>0){
  292. $allCols['_tag_']['colsGridShow'] = true;
  293. foreach($this->db->select('select t.tag_name,rel_id from sdb_tag_rel r left join sdb_tags t on t.tag_id=r.tag_id where t.tag_type=\''.$this->typeName.'\' and r.rel_id in('.implode(',',$id).')') as $tag){
  294. $this->tags[$tag['rel_id']][] = $tag['tag_name'];
  295. }
  296. }
  297. foreach($list as $i=>$row){
  298. $list[$i]['_tags'] = json_encode($this->tags[$row[$this->idColumn]]);
  299. }
  300. unset($ctlClassObj,$id);
  301. foreach($modifiers as $type=>$rows){
  302. $params = explode('|',$type,2);
  303. $options = explode(':',$params[0]);
  304. $func = 'modifier_'.array_shift($options);
  305. if(count($params)>1){
  306. $options['options'] = unserialize($params[1]);
  307. }
  308. if(method_exists($this,$func)){
  309. $this->$func($modifiers[$type],$options);
  310. }
  311. }
  312. }
  313. $return = array(
  314. 'list'=>&$list,
  315. 'cols'=>&$colArray,
  316. 'allCols'=>&$allCols
  317. );
  318. return $return;
  319. }
  320. function getColumns($filter=null){
  321. trigger_error('Undefined method "getColumns" in '.get_class($this),E_USER_ERROR);
  322. }
  323. /**
  324. * batchEditCols
  325. * ????
  326. *
  327. * @param mixed $filter
  328. * @access public
  329. * @return void
  330. */
  331. function batchEditCols($filter){
  332. $ret = $this->getColumns($filter);
  333. if(is_null($this->colsGridEdit)||is_null($this->colsColumnEdit)||is_null($this->colsGridShow)){
  334. $this->setFinderCols(null,$filter);
  335. }
  336. foreach($ret as $k=>$col){
  337. if(in_array($k,$this->colsColumnEdit)){
  338. $c[] = "count(DISTINCT $k) as $k";
  339. }else{
  340. unset($ret[$k]);
  341. }
  342. }
  343. $r = $this->db->selectrow('select count('.$this->idColumn.') as count from '.$this->tableName.' where '.$this->_filter($filter));
  344. $rowCount = $r['count'];
  345. //??????????1000???????????
  346. if($rowCount<1000){
  347. $sql = 'select '.implode(',',$c).' from '.$this->tableName.' where '.$this->_filter($filter);
  348. $c = array();
  349. if($r = $this->db->selectrow($sql)){
  350. foreach($r as $col=>$count){
  351. if($count<2){
  352. $c[] = $col;
  353. }
  354. }
  355. foreach($this->db->selectrow('select '.implode(',',$c).' from '.$this->tableName.' where '.$this->_filter($filter)) as $k=>$v){
  356. if(substr($ret[$k]['type'],0,5)=='time:'||$ret[$k]['type']=='time'){
  357. $options = explode(':',$ret[$k]['type']);
  358. array_shift($options);
  359. $rows = array($v);
  360. $this->modifier_time($rows,$options);
  361. $v = $rows[0];
  362. }
  363. $ret[$k]['value'] = $v;
  364. }
  365. }
  366. }
  367. return array('cols'=>$ret,'count'=>$rowCount);
  368. }
  369. function finderResult($data,$start=0,$limit=null){
  370. if($data['filter']){
  371. parse_str($data['filter'],$data);
  372. $finder = $data['_finder'];
  373. unset($data['_finder']);
  374. $return=array();
  375. foreach($this->getList($this->idColumn,$data,$start,$limit) as $row){
  376. $return[] = $row[$this->idColumn];
  377. }
  378. return $return;
  379. }else{
  380. return $data['items'];
  381. }
  382. }
  383. function allViews($objType=null){
  384. if(method_exists($this,'getViews')){
  385. $list = $this->getViews();
  386. }
  387. foreach($list as $k=>$v){
  388. $ret[json_encode($v)]=$k;
  389. }
  390. return $ret;
  391. }
  392. function searchOptions(){ return false; }
  393. function tofilter($d){
  394. parse_str($d['filter'],$filter);
  395. return $filter;
  396. }
  397. /**
  398. * inFilter ????
  399. *
  400. * @param mixed $arr
  401. * @param mixed $filter
  402. * @access public
  403. * @return void
  404. */
  405. function inFilter($arr,$filter){
  406. $filter[$this->idColumn] = $arr;
  407. $row = $this->getList($this->idColumn,$filter,0,-1,$c);
  408. $ret = array();
  409. foreach($row as $v){
  410. $ret[] = $v[$this->idColumn];
  411. }
  412. return is_array($arr)?$ret:($ret[0]==$arr);
  413. }
  414. /**
  415. * addEvent ????????
  416. *
  417. * @param mixed $type ??
  418. * @param mixed $filter ????
  419. * @param mixed $event hook???
  420. * @param mixed $func ????
  421. * @access public
  422. * @return void
  423. */
  424. function addEvent($type,$filter,$event,$func){}
  425. /**
  426. * fireEvent ????
  427. *
  428. * @param mixed $event
  429. * @access public
  430. * @return void
  431. */
  432. function fireEvent($action , &$object, $member_id=0){
  433. $type = $this->typeName;
  434. $hooks_dir = BASE_DIR.'/plugins/hooks/';
  435. $this->system->messenger = $this->system->loadModel('system/messenger');
  436. $this->system->_msgList = $this->system->messenger->actions();
  437. if($this->system->_msgList[$type.'-'.$action]){
  438. $this->system->messenger->actionSend($type.'-'.$action,$object,$member_id);
  439. }
  440. ob_start();
  441. if(!isset($GLOBALS['eventHandles'][$type])){
  442. $handles = &$GLOBALS['eventHandles'][$type];
  443. include('handles.php');
  444. if($hooks[$type][$action]){
  445. foreach($hooks[$type][$action] as $i=>$h){
  446. $r[$h['class'].'_'.$h['func']] = $i;
  447. $o[$i]=10;
  448. }
  449. }
  450. foreach($this->db->select('select * from sdb_event_hdls where target="'.$type.'" order by orderby') as $hdls){
  451. $i = $r[$hdls['class'].'_'.$h['func']];
  452. if($hdls['disabled']==1 && $i){
  453. unset($hooks[$type][$action][$i]);
  454. unset($o[$i]);
  455. }else if($i){
  456. $hooks[$type][$action][$i] = $hdls;
  457. $o[$i]=intval($hdls['orderby']);
  458. }else{
  459. $hdls['setting'] = unserialize($hdls['setting']);
  460. $hooks[$type][$action][] = $hdls;
  461. }
  462. }
  463. array_multisort($o,$hooks[$type][$action]);
  464. foreach($hooks[$type][$action] as $hook){
  465. $hookTemp = explode('_',$hook['class'],2);
  466. $hook_file = $hooks_dir.'hook.'.$hookTemp[1].'.php';
  467. if($hook_file){
  468. if(!include_once($hook_file)){
  469. continue;
  470. }
  471. }
  472. if($func = $hook['func']){
  473. if($class = $hook['class']){
  474. if(class_exists($class)){
  475. $o = new $class;
  476. if(is_callable(array($o,$func))) $o->$func($object,$hook['setting']);
  477. }
  478. }else{
  479. if(function_exists($func)) $func($object,$hook['setting']);
  480. }
  481. }
  482. }
  483. }
  484. ob_get_clean();
  485. return 1;
  486. }
  487. /**
  488. * addTag
  489. *
  490. * @param mixed $mix ?????? id,id???,filter
  491. * @param mixed $tagName
  492. * @access public
  493. * @return void
  494. */
  495. function addTag($mix,$tag_id){
  496. $type = '';
  497. $modTag = $this->system->loadModel('system/tag');
  498. if(is_array($mix)){
  499. if($mix['items']){
  500. $modTag->begin();
  501. foreach($mix['items'] as $id){
  502. $modTag->addTag($tag_id,$id,$type);
  503. }
  504. $modTag->end();
  505. }elseif($mix['filter']){
  506. parse_str($mix['filter'],$filter);
  507. }
  508. }else{
  509. $modTag->addTag($tag_id,$mix,$type);
  510. }
  511. }
  512. function newTag($tagName){
  513. $modTag = &$this->system->loadModel('system/tag');
  514. return $modTag->newTag($tagName,$this->typeName);
  515. }
  516. function setTag($data,$tags){
  517. $a = array();
  518. foreach($this->db->select("select {$this->tableName}.{$this->idColumn} as rel_id from {$this->tableName} where ".$this->_filter($data,$this->tableName)) as $r){
  519. $a[] = $r['rel_id'];
  520. }
  521. $tag_id=array();
  522. foreach($this->db->select("SELECT DISTINCT(r.tag_id) FROM sdb_tag_rel r LEFT JOIN sdb_tags t ON r.tag_id = t.tag_id
  523. where tag_type='".$this->typeName."' AND rel_id IN(".implode(',',$a).")") as $rows){
  524. $tag_id[] = $rows['tag_id'];
  525. }
  526. if(count($tag_id) > 0){
  527. $this->db->exec('delete from sdb_tag_rel where tag_id in('.implode(',',$tag_id).') and rel_id in('.implode(',',$a).')');
  528. }else{
  529. $this->db->exec('delete from sdb_tag_rel where rel_id in('.implode(',',$a).')');
  530. }
  531. $modTag = &$this->system->loadModel('system/tag');
  532. foreach($tags as $tag){
  533. $tagId = $modTag->tagId($tag,$this->typeName);
  534. $tag_id[] = $tagId;
  535. if(defined('DB_OLDVERSION') && DB_OLDVERSION){
  536. foreach($this->db->select("select {$tagId} as tag_id,{$this->tableName}.{$this->idColumn} as rel_id from {$this->tableName} where ".$this->_filter($data,$this->tableName)) as $r){
  537. if(!$this->db->exec("insert into sdb_tag_rel (tag_id,rel_id) values({$r['tag_id']},{$r['rel_id']})")){
  538. return false;
  539. }
  540. }
  541. }else{
  542. $sql = "insert into sdb_tag_rel (tag_id,rel_id) select {$tagId} as tag_id,{$this->tableName}.{$this->idColumn} as rel_id from {$this->tableName} where ".$this->_filter($data,$this->tableName);
  543. if(!$this->db->exec($sql)){
  544. return false;
  545. }
  546. }
  547. }
  548. $modTag->recount(array_unique($tag_id));
  549. return true;
  550. }
  551. function &tagList($count=false){
  552. $modTag = &$this->system->loadModel('system/tag');
  553. return $modTag->tagList($this->typeName,$count,$this->tableName,$this->idColumn);
  554. }
  555. //???
  556. function recycle($filter){
  557. $sql = 'update '.$this->tableName.' set disabled=\'true\' where '.$this->_filter($filter);
  558. return $this->db->exec($sql);
  559. }
  560. //??????
  561. function active($filter){
  562. $this->disabledMark = 'recycle';
  563. $sql = 'update '.$this->tableName.' set disabled=\'false\' where '.$this->_filter($filter);
  564. return $this->db->exec($sql);
  565. }
  566. function objects(){
  567. include('objects.php');
  568. return $objects;
  569. }
  570. function count($filter=null){
  571. $row = $this->db->select('SELECT count(*) as _count FROM '.$this->tableName.' WHERE '.$this->_filter($filter));
  572. return intval($row[0]['_count']);
  573. }
  574. /**
  575. * getList
  576. *
  577. * @param mixed $cols ?
  578. * @param mixed $filter ????
  579. * @param $disabled ???? normal|recycle|all
  580. * @access public
  581. * @return void
  582. *
  583. */
  584. function getList($cols,$filter='',$start=0,$limit=20,&$count,$orderType=null){
  585. $ident=md5($cols.print_r($filter,true).$start.$limit);
  586. if(!$this->_dbstorage[$ident]){
  587. if(!$cols){
  588. $cols = $this->defaultCols;
  589. }
  590. if(!empty($this->appendCols)){
  591. $cols.=','.$this->appendCols;
  592. }
  593. $orderType = $orderType?$orderType:$this->defaultOrder;
  594. $sql = 'SELECT '.$cols.' FROM '.$this->tableName.' WHERE '.$this->_filter($filter);
  595. $count = $this->db->_count($sql);
  596. if($orderType)$sql.=' ORDER BY '.implode($orderType,' ');
  597. if($count===false){
  598. return false;
  599. }
  600. $this->_dbstorage[$ident]=$this->db->selectLimit($sql,$limit,$start);
  601. }
  602. return $this->_dbstorage[$ident];
  603. }
  604. function instance($id,$cols='*'){
  605. $addVersionId = array('sdb_orders');
  606. if(in_array($this->tableName,$addVersionId)){
  607. $sql = 'SELECT '.$cols.' FROM '.$this->tableName.' WHERE version_id = 0 and '.$this->idColumn .'= \''.$id.'\'';
  608. }else{
  609. $sql = 'SELECT '.$cols.' FROM '.$this->tableName.' WHERE '.$this->idColumn .'= \''.$id.'\'';
  610. }
  611. return $this->db->selectrow($sql);
  612. }
  613. function wFilter($words,$colum){
  614. $replace = array(",", "+");
  615. $return=str_replace($replace,' ',$words);
  616. $word=explode(" ",$return);
  617. foreach($word as $k=>$v){
  618. foreach($colum as $k=>$v){
  619. $sSql[]=$colum[$k].' LIKE \'%'.$word[$k].'%\'';
  620. }
  621. $sql[]='('.implode('or',$sSql).')';
  622. //$sql[]='($this->textColumn LIKE \'%'.$word[$k].'%\' or bn LIKE \'%'.$word[$k].'%\')';
  623. }
  624. return implode('and',$sql);
  625. }
  626. function _filter($filter,$tableAlias=null,$baseWhere=null){
  627. trim_value($filter);
  628. $tPre = ($tableAlias?$tableAlias:$this->tableName).'.';
  629. $where = $baseWhere?$baseWhere:array(1);
  630. if($this->disabledMark=='normal'){
  631. $where[] =$tPre.'disabled = \'false\'';
  632. }elseif($this->disabledMark=='recycle'){
  633. if(isset($filter['disabled'])){
  634. $where[]=$tPre.'disabled = \''.$filter['disabled'].'\'';
  635. }else{
  636. $where[]=$tPre.'disabled = \'true\'';
  637. }
  638. }
  639. if(isset($filter['keywords']) && $filter['keywords']){
  640. //$where[]=$this->wFilter($filter['keywords']);
  641. if($this->keywordsColumn){
  642. $colum[]=$this->keywordsColumn;
  643. }
  644. if($this->textColumn){
  645. $colum[]=$this->textColumn;
  646. }
  647. $where[]=$this->wFilter($filter['keywords'],$colum);
  648. }
  649. if(isset($filter['tag']) && $tag = $filter['tag']){
  650. unset($filter['tag']);
  651. if(is_array($tag)){
  652. if(count($tag) == 0){
  653. unset($tag);
  654. }
  655. }else{
  656. $tag = array($tag);
  657. }
  658. if($tag){
  659. foreach($this->db->select('select tag_id from sdb_tags where tag_name in (\''.implode('\',\'',$tag).'\')') as $row){
  660. $tag_id[]= $row['tag_id'];
  661. }
  662. if(count($tag_id)>0){
  663. if(defined('DB_OLDVERSION') && DB_OLDVERSION){
  664. $a = array();
  665. foreach($this->db->select("select rel_id from sdb_tag_rel where tag_id in (".implode(',',$tag_id).")") as $r){
  666. $a[] = $r['rel_id'];
  667. }
  668. if(count($a)>0){
  669. $where[] = "{$this->idColumn} in (".implode(',',$a).")";
  670. }
  671. }else{
  672. $where[] = "{$this->idColumn} in (select rel_id from sdb_tag_rel where tag_id in (".implode(',',$tag_id).") )";
  673. }
  674. }
  675. }
  676. }
  677. $cols = $this->getColumns($filter);
  678. if(is_array($filter))
  679. foreach($filter as $k=>$v){
  680. $ac = array();
  681. if(isset($cols[$k])){
  682. if(is_array($v)){
  683. foreach($v as $m){
  684. if($m!=='_ANY_' && $m!==''){
  685. if($_GET['act'] == 'export'){//?????????? by yangbing 2009-11-12 11:58
  686. $ac[] = ($tPre.$k.'=\''.$m.'\'');
  687. }else{
  688. $ac[] = $cols[$k]['fuzzySearch']?($tPre.$k.' like \'%'.$m.'%\''):($tPre.$k.'=\''.$m.'\'');
  689. }
  690. }else{
  691. $ac = array();
  692. break;
  693. }
  694. }
  695. if(count($ac)>0){
  696. $where[] = '('.implode($ac,' or ').')';
  697. }
  698. }elseif($v && $v!=''){
  699. if($_GET['act'] == 'export'){//?????????? by yangbing 2009-11-12 11:58
  700. $where[] = ($tPre.$k.'=\''.$v.'\'');
  701. }else{
  702. $where[] = $cols[$k]['fuzzySearch']?($tPre.$k.' like \'%'.$v.'%\''):($tPre.$k.'=\''.$v.'\'');
  703. }
  704. }
  705. }
  706. }
  707. return implode($where,' AND ');
  708. }
  709. /**
  710. * insert
  711. *
  712. * @param mixed $data
  713. * @access public
  714. * @return void
  715. */
  716. function insert($data){
  717. if(method_exists($this,'pre_insert')){
  718. $this->pre_insert($data);
  719. }
  720. if(method_exists($this,'post_insert')){
  721. $this->post_insert($data);
  722. }
  723. $rs = $this->db->exec('select * from '.$this->tableName.' where 0=1');
  724. foreach($data as $k=>$v){
  725. $data[$k] = trim($data[$k]);
  726. }
  727. $sql = $this->db->getInsertSQL($rs,$data);
  728. $cols = $this->getColumns();
  729. $cols[$this->textColumn]['required'] = true;
  730. foreach($cols as $k=>$p){
  731. if($p['required']){
  732. if(!$data[$k]){
  733. trigger_error('<b>'.$p['label'].'</b> ?????',E_USER_ERROR);
  734. }
  735. }
  736. }
  737. if($sql && $this->db->exec($sql)){
  738. return $this->db->lastInsertId();
  739. }else{
  740. return false;
  741. }
  742. }
  743. function preUpdate(){
  744. }
  745. /**
  746. * update
  747. *
  748. * @param mixed $data
  749. * @param mixed $filter
  750. * @access public
  751. * @return void
  752. */
  753. function update($data,$filter){
  754. if(method_exists($this,'pre_update')){
  755. $this->pre_insert($data);
  756. }
  757. if(count($data)==0){
  758. return true;
  759. }
  760. $columnsList = $this->getColumns();
  761. $result = $this->db->exec('select * from '.$this->tableName.' where 0=1');
  762. for($i=0;$i<$result->FieldCount();$i++){
  763. $column = $result->FetchField($i);
  764. if(isset($data[$column->name])){
  765. if($column->type=='unknown' && $columnsList[$column->name]['type']=='money'){
  766. $column->type = 'real'; //PHP_BUG http://bugs.php.net/bug.php?id=36069
  767. }
  768. if($columnsList[$column->name]['required'] && !$data[$column->name]){
  769. trigger_error($columnsList[$column->name]['label'].'?????',E_USER_WARNING);
  770. $GLOBALS['php_errormsg'] = $php_errormsg;
  771. return false;
  772. }
  773. $UpdateValues[] ='`'.$column->name.'`='.$this->db->_quotevalue(trim($data[$column->name]),$column->type,$this->db->_instance);
  774. }
  775. }
  776. if(count($UpdateValues)>0){
  777. //by shiy ????????
  778. $include_table_lastmodify = array('sdb_product_line','sdb_pline_to_dealer','sdb_brand','sdb_goods_type','sdb_specification','sdb_goods_type_spec','sdb_payment_cfg','sdb_type_brand');
  779. if(in_array($this->tableName,$include_table_lastmodify)){
  780. $UpdateValues[] = 'last_modify='.time();
  781. if($this->tableName != 'sdb_pline_to_dealer' && $this->tableName != 'sdb_payment_cfg'){
  782. $UpdateValues[] = 'version_id='.$this->system->getConf('system.curr_sync_version_id');
  783. }
  784. }
  785. $sql = 'update '.$this->tableName.' set '.implode(',',$UpdateValues).' where '.$this->_filter($filter);
  786. if($this->db->exec($sql)){
  787. if($this->db->affect_row()){
  788. return $this->db->affect_row();
  789. }else{
  790. return true;
  791. }
  792. }else{
  793. return false;
  794. }
  795. }
  796. }
  797. /**
  798. * delete
  799. *
  800. * @param mixed $filter
  801. * @access public
  802. * @return void
  803. */
  804. function delete($filter){
  805. if(method_exists($this,'pre_delete')){
  806. $this->pre_delete($filter);
  807. }
  808. if(method_exists($this,'post_delete')){
  809. $this->post_delete($filter);
  810. }
  811. $this->disabledMark = 'recycle';
  812. $sql = 'delete from '.$this->tableName.' where '.$this->_filter($filter);
  813. if($this->db->exec($sql)){
  814. return $this->db->affect_row();
  815. }else{
  816. return false;
  817. }
  818. }
  819. /**
  820. * enable
  821. *
  822. * @param mixed $filter
  823. * @access public
  824. * @return void
  825. */
  826. function enable($filter){
  827. $sql = 'delete from '.$this->tableName.' where '.$this->_filter($filter);
  828. return $this->db->exec($sql);
  829. }
  830. /**
  831. * disable
  832. *
  833. * @param mixed $filter
  834. * @access public
  835. * @return void
  836. */
  837. function disable($filter){
  838. $sql = 'delete from '.$this->tableName.' where '.$this->_filter($filter);
  839. return $this->db->exec($sql);
  840. }
  841. function inputElement($params){
  842. $ident = md5(print_r($params['filter'],true));
  843. $max = 1000;
  844. if($params['data']){
  845. $this->_input[$ident] = $params['data'];
  846. }
  847. if(!isset($this->_input[$ident])){
  848. $this->_input[$ident] = $this->getList($this->idColumn.','.$this->textColumn,$params['filter'],0,$max,$this->_input[$ident.'_c']);
  849. }
  850. unset($params['filter']);
  851. $html = buildTag($params,'select',true);
  852. if(!$params['value']){
  853. $html.='<option></option>';
  854. }
  855. foreach($this->_input[$ident] as $r){
  856. $html.='<option value="'.$r[$this->idColumn].'"'.($r[$this->idColumn]==$params['value']?' selected="selected"':'').'>'.$r[$this->textColumn].'</option>';
  857. }
  858. if($this->_input[$ident.'_c']>$max){
  859. $html.='<option>??'.$this->_input[$ident.'_c']-$max.'...</option>';
  860. }
  861. return $html.='</select>';
  862. }
  863. function &export($list){
  864. $colarray = $this->getColumns();
  865. foreach($list as $i=>$row){
  866. foreach($row as $k=>$v){
  867. if($colarray[$k]['type'] && !is_null($v) && $colarray[$k]['export'] != 'false'){
  868. $modifier_key = $colarray[$k]['options']?$colarray[$k]['type'].'|'.serialize($colarray[$k]['options']):$colarray[$k]['type'];
  869. $modifiers[$modifier_key][$v] = $v;
  870. $list[$i][$k] = &$modifiers[$modifier_key][$v];
  871. }
  872. }
  873. }
  874. foreach($modifiers as $type=>$rows){
  875. $params = explode('|',$type,2);
  876. $options = explode(':',$params[0]);
  877. if(count($params)>1){
  878. $options['options'] = unserialize($params[1]);
  879. }
  880. $type_part = array_shift($options);;
  881. if(method_exists($this,$func = 'exporter_'.$type_part)){
  882. $this->$func($modifiers[$type],$options);
  883. }elseif(method_exists($this,$func = 'modifier_'.$type_part)){
  884. $this->$func($modifiers[$type],$options);
  885. }
  886. }
  887. return $list;
  888. }
  889. /**
  890. * ??$filter????? 2009-12-10 15:58 wubin ?????????????
  891. *
  892. * @param array $filter // ???model??function _filter()??
  893. * @param boolean $is_recycle // ??????????
  894. * @return array(
  895. * "$this->idColumn"=>array(x,x,x,....)
  896. * )
  897. */
  898. function getPrimaryKeyByFilter($filter,$is_recycle = false){
  899. if(empty($this->idColumn) || empty($this->typeName) ) return array();
  900. if($is_recycle) $this->disabledMark = 'recycle';
  901. $sSql = "SELECT ".$this->idColumn." FROM ".$this->tableName." WHERE ".$this->_filter($filter);
  902. $aTemp = $this->db->select($sSql);
  903. $aResult[$this->idColumn] = array();
  904. foreach($aTemp as $row) {
  905. $aResult[$this->idColumn][] = $row[$this->idColumn];
  906. }
  907. return $aResult;
  908. }
  909. function get_arr_replace($arr,$table,$from,$to){
  910. $limit = 50;
  911. $count = count($arr);
  912. $page = ceil($count/$limit);
  913. $return = array();
  914. $tmp = array();
  915. for($i=0;$i<$page;$i++){
  916. $deal_arr = array_slice($arr,$i*$limit,$limit);
  917. $sql = 'SELECT '.$from.','.$to.' FROM '.$table.' WHERE '.$from.' IN ("'.implode('","',$deal_arr).'")';
  918. $row = $this->db->select($sql);
  919. $tmp = array_merge($tmp,$row);
  920. }
  921. foreach($tmp as $k=>$v){
  922. $return[$v[$from]] = $v;
  923. }
  924. return $return;
  925. }
  926. function getWantedRow($where,$table=null,$cols='*'){
  927. $sql = 'SELECT '.$cols.' FROM '.($table?$table:$this->tableName).' WHERE 1=1 AND '.join(' AND ',$where);
  928. return $this->db->selectrow($sql);
  929. }
  930. function getWanted($where,$table=null,$cols='*'){
  931. $sql = 'SELECT '.$cols.' FROM '.($table?$table:$this->tableName).' WHERE 1=1 AND '.join(' AND ',$where);
  932. return $this->db->select($sql);
  933. }
  934. function getItemsIn($id,$cols){
  935. $sql = 'SELECT '.$cols.' FROM '.$this->tableName.' WHERE '.$this->idColumn .' IN ('.$id.')';
  936. return $this->db->select($sql);
  937. }
  938. }
  939. ?>