Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
Class.DocumentList.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 /**
7  * Document list class
8  *
9  * @author Anakeen
10  * @version $Id: $
11  * @package FDL
12  */
13 /**
14  */
15 class DocumentList implements Iterator, Countable
16 {
17  /**
18  * @var null|SearchDoc
19  */
20  private $search = null;
21  /**
22  * @var null|Doc
23  */
24  private $currentDoc = null;
25  /**
26  * anonymous function
27  * @var Closure
28  */
29  private $hookFunction = null;
30 
31  private $init = false;
32  public $length = 0;
33 
34  public function __construct(SearchDoc & $s = null)
35  {
36  $this->search = & $s;
37  }
38  /**
39  * get number of returned documents
40  * can be upper of real length due to callback map
41  *
42  * @api Get number of returned documents
43  * @return int
44  */
45  public function count()
46  {
47  $this->initSearch();
48  return $this->length;
49  }
50  private function initSearch()
51  {
52  if ($this->search) {
53  if (!$this->init) {
54  if (!$this->search->isExecuted()) $this->search->search();
55  if ($this->search->getError()) {
56  throw new Dcp\Exception($this->search->getError());
57  }
58  $this->length = $this->search->count();
59  $this->init = true;
60  } else {
61  $this->search->rewind();
62  }
63  }
64  }
65 
66  private function getCurrentDoc()
67  {
68  $this->currentDoc = $this->search->getNextDoc();
69  $good = ($this->callHook() !== false);
70  if (!$good) {
71  while ($this->currentDoc = $this->search->getNextDoc()) {
72  $good = ($this->callHook() !== false);
73  if ($good) break;
74  }
75  }
76  }
77 
78  public function rewind()
79  {
80  $this->initSearch();
81  $this->getCurrentDoc();
82  }
83  /**
84  * @return Doc|null
85  */
86  public function next()
87  {
88  $this->getCurrentDoc();
89  }
90 
91  private function callHook()
92  {
93  if ($this->currentDoc && $this->hookFunction) {
94  // call_user_func($function, $this->currentDoc);
95  $h = $this->hookFunction;
96  return $h($this->currentDoc);
97  }
98  return true;
99  }
100  public function key()
101  {
102  return is_array($this->currentDoc) ? $this->currentDoc["id"] : $this->currentDoc->id;
103  }
104  /**
105  * @return Doc
106  */
107  public function current()
108  {
109  return $this->currentDoc;
110  }
111  /**
112  * @return bool
113  */
114  public function valid()
115  {
116  return $this->currentDoc != false;
117  }
118  /**
119  * @return null|SearchDoc
120  */
121  public function &getSearchDocument()
122  {
123  return $this->search;
124  }
125  /**
126  * set document identifiers to be used in iterator
127  * @param int[] $ids document identifiers
128  * @param bool $useInitid if true identifier must ne initid else must be latest ids
129  * @deprecated use addDocumentIdentifiers instead
130  */
131  public function addDocumentIdentificators(array $ids, $useInitid = true)
132  {
133  $this->addDocumentIdentifiers($ids, $useInitid);
134  }
135  /**
136  * set document identifiers to be used in iterator
137  *
138  * @api Set document identifiers to be used in iterator
139  * @param int[] $ids document identifiers
140  * @param bool $useInitid if true identifier must ne initid else must be latest ids
141  */
142  public function addDocumentIdentifiers(array $ids, $useInitid = true)
143  {
144  $this->search = new SearchDoc(getDbAccess());
145  $this->search->setObjectReturn();
146  $this->search->excludeConfidential();
147  foreach ($ids as $k => $v) {
148  if ((!$v) || (!is_numeric($v))) unset($ids[$k]);
149  }
150  $ids = array_unique($ids);
151  $sid = $useInitid ? "initid" : "id";
152  if (count($ids) == 0) {
153  $this->search->addFilter("false");
154  } else {
155  $this->search->addFilter($this->search->sqlCond($ids, $sid, true));
156  }
157  }
158  /**
159  * apply a callback on each document
160  * if callback return false, the document is skipped from list
161  * @param Closure $hookFunction
162  * @return void
163  */
164  public function listMap($hookFunction)
165  {
166  $this->hookFunction = $hookFunction;
167  }
168 }
Exception class use exceptionCode to identifiy correctly exception.
Definition: exceptions.php:19
search(Action &$action)
Definition: search.php:30
if($famId) $s
listMap($hookFunction)
addDocumentIdentifiers(array $ids, $useInitid=true)
__construct(SearchDoc &$s=null)
getDbAccess()
Definition: Lib.Common.php:368
addDocumentIdentificators(array $ids, $useInitid=true)
← centre documentaire © anakeen