18 include_once (
"FDL/Lib.Dir.php");
91 private $debug =
false;
92 private $debuginfo =
"";
98 private $excludeFilter =
"";
104 private $iDoc = null;
110 private $cacheDocuments = array();
115 private $mode =
"TABLE";
116 private $count = - 1;
141 if (!$this->result) {
142 $fld =
new_Doc($this->dbaccess, $this->dirid);
145 $this->mode =
"ITEM";
146 if ($this->
debug) $debuginfo = array();
147 else $debuginfo = null;
148 $tqsql =
getSqlSearchDoc($this->dbaccess, $this->dirid, $this->fromid, $this->
getFilters() , $this->distinct, $this->latest, $this->trash,
false, $this->folderRecursiveLevel, $this->
join);
149 $this->debuginfo[
"query"] = $tqsql[0];
151 if (!is_array($tqsql)) {
152 $this->debuginfo[
"err"] = _(
"cannot produce sql request");
155 foreach ($tqsql as
$sql) {
157 if (preg_match(
'/from\s+([a-z0-9_\-]*)/', $sql, $reg)) $maintable = $reg[1];
158 else $maintable =
'';
159 $maintabledot = ($maintable) ? $maintable .
'.' :
'';
161 $mainid = ($maintable) ?
"$maintable.id" :
"id";
162 $sql = preg_replace(
'/select\s+(.*)\s+from\s/',
"select count($mainid) from ", $sql);
164 if (
$userid != 1) $sql.=
" and (${maintabledot}profid <= 0 or hasviewprivilege($userid, ${maintabledot}profid))";
166 $mb = microtime(
true);
169 $this->debuginfo[
"query"] =
$sql;
170 $this->debuginfo[
"error"] = pg_last_error(
$dbid);
172 $result = pg_fetch_array(
$q, 0, PGSQL_ASSOC);
173 $count+= $result[
"count"];
174 $this->debuginfo[
"delay"] = sprintf(
"%.03fs", microtime(
true) - $mb);
178 $this->
count = $count;
183 }
else $this->
count();
193 $tqsql =
getSqlSearchDoc($this->dbaccess, $this->dirid, $this->fromid, $this->
getFilters() , $this->distinct, $this->latest, $this->trash,
false, $this->folderRecursiveLevel, $this->
join);
197 public function join($jointure)
199 $this->
join = $jointure;
210 if ($this->
count == - 1) {
211 if ($this->searchmode ==
"ITEM") {
225 $this->result =
false;
226 $this->debuginfo =
"";
234 return ($this->result !=
false);
242 if (!$this->excludeFilter) {
245 return array_merge(array(
258 if ($this->
getError())
return array();
260 if (!is_numeric($this->fromid)) {
263 if ($this->fromid != - 1) {
265 if ($this->fromid < - 1) {
268 $err =
simpleQuery($this->dbaccess, sprintf(
"select doctype from docfam where id=%d", abs($this->fromid)) , $doctype,
true,
true);
269 if ($doctype !=
'C')
$fromid = 0;
274 $error = sprintf(_(
"%s is not a family") , $this->fromid);
275 $this->debuginfo[
"error"] = $error;
276 error_log(
"ERROR SearchDoc: " . $error);
277 if ($this->mode ==
"ITEM")
return null;
280 if ($this->only) $this->fromid = - (abs(
$fromid));
283 if ($this->recursiveSearch && $this->dirid) {
285 $tmps->setValue(
"se_idfld", $this->dirid);
286 $tmps->setValue(
"se_latest",
"yes");
289 $tmps->addQuery($tmps->getQuery());
290 $this->dirid = $tmps->id;
294 $this->searchmode = $this->mode;
295 if ($this->mode ==
"ITEM") {
297 $fld =
new_Doc($this->dbaccess, $this->dirid);
298 if ($fld->fromid ==
getFamIdFromName($this->dbaccess,
"SSEARCH")) $this->searchmode =
"TABLE";
300 $debuginfo = array();
302 $this->result =
getChildDoc($this->dbaccess, $this->dirid, $this->
start, $this->
slice, $this->
getFilters() , $this->userid, $this->searchmode, $this->fromid, $this->distinct, $this->
orderby, $this->latest, $this->trash, $debuginfo, $this->folderRecursiveLevel, $this->
join);
303 if ($this->searchmode ==
"TABLE") $this->
count =
count($this->result);
304 $this->debuginfo = $debuginfo;
305 if (($this->searchmode ==
"TABLE") && ($this->mode ==
"ITEM")) $this->mode =
"TABLEITEM";
307 if ($this->mode ==
"ITEM")
return $this;
309 return $this->result;
314 include_once (
"FDL/Class.DocumentList.php");
322 return ($this->debuginfo[
"error"]);
330 if ($this->debuginfo)
return $this->debuginfo[
"error"];
342 $this->
debug = $debug;
351 $this->recursiveSearch = $b;
362 return $this->debuginfo;
371 return $this->debuginfo;
381 if ((!is_numeric(
$slice)) && (
$slice !=
'ALL'))
return false;
405 if (
$dir->isAlive()) {
406 $this->dirid =
$dir->initid;
409 $this->debuginfo[
"error"] = sprintf(_(
"collection %s not exists") ,
$dirid);
421 if (!(is_numeric(
$start)))
return false;
433 if ($this->mode ==
"ITEM") {
434 $n = current($this->result);
435 if ($n ===
false)
return false;
436 $tdoc = pg_fetch_array($n, NULL, PGSQL_ASSOC);
437 if (
$tdoc ===
false) {
438 $n = next($this->result);
439 if ($n ===
false)
return false;
440 $tdoc = pg_fetch_array($n, NULL, PGSQL_ASSOC);
441 if (
$tdoc ===
false)
return false;
444 } elseif ($this->mode ==
"TABLEITEM") {
445 $tdoc = array_shift($this->result);
446 if (!is_array(
$tdoc))
return false;
448 }
else return array_shift($this->result);
459 if ($v[
"doctype"] ==
"C") {
460 if (!isset($this->cacheDocuments[
"family"])) $this->cacheDocuments[
"family"] =
new DocFam($this->dbaccess);
461 $this->cacheDocuments[
"family"]->Affect($v,
true);
464 if (!isset($this->cacheDocuments[
$fromid])) {
465 $this->cacheDocuments[
$fromid] =
createDoc($this->dbaccess, $fromid,
false,
false);
468 $this->cacheDocuments[
$fromid]->Affect($v,
true);
469 $this->cacheDocuments[
$fromid]->nocache =
true;
470 return $this->cacheDocuments[
$fromid];
482 $args = func_get_args();
483 if (
count($args) > 1) {
485 for ($i = 1; $i <
count($args); $i++) {
486 $fs[] = pg_escape_string($args[$i]);
488 $filter = call_user_func_array(
"sprintf", $fs);
490 if (preg_match(
'/^([a-z0-9_\-]+)\./',
$filter, $reg)) {
499 static function sqlcond($values, $column, $integer =
false)
502 if (
count($values) > 0) {
504 $sql_cond =
"$column in (";
505 $sql_cond.= implode(
",", $values);
508 foreach ($values as & $v) $v = pg_escape_string($v);
509 $sql_cond =
"$column in ('";
510 $sql_cond.= implode(
"','", $values);
533 if ($returnobject) $this->mode =
"ITEM";
534 else $this->mode =
"TABLE";
544 $this->mode =
"TABLE";
554 if ($this->userid != 1) $this->excludeFilter = sprintf(
"confidential is null or hasdocprivilege(%d, profid,%d)", $this->userid, 1 <<
POS_CONF);
556 $this->excludeFilter =
'';