Open Journal Systems  3.3.0
DAOResultFactory.inc.php
1 <?php
2 
18 import('lib.pkp.classes.core.ItemIterator');
19 import('lib.pkp.classes.db.DAOResultIterator');
20 
23  var $dao;
24 
27 
28  var $functionParams;
29 
34  var $idFields;
35 
37  var $records;
38 
40  var $wasEmpty;
41 
42  var $isFirst;
43  var $isLast;
44  var $page;
45  var $count;
46  var $pageCount;
47 
58  function __construct($records, $dao, $functionName, $idFields = array(), $functionParams = array()) {
59  parent::__construct();
60  $this->functionName = $functionName;
61  $this->functionParams = $functionParams;
62  $this->dao = $dao;
63  $this->idFields = $idFields;
64 
65  if (!$records || $records->EOF) {
66  if ($records) $records->Close();
67  $this->records = null;
68  $this->wasEmpty = true;
69  $this->page = 1;
70  $this->isFirst = true;
71  $this->isLast = true;
72  $this->count = 0;
73  $this->pageCount = 1;
74  } else {
75  $this->records = $records;
76  $this->wasEmpty = false;
77  $this->page = $records->AbsolutePage();
78  $this->isFirst = $records->atFirstPage();
79  $this->isLast = $records->atLastPage();
80  $this->count = $records->MaxRecordCount();
81  $this->pageCount = $records->LastPageNo();
82  }
83  }
84 
90  function move($to) {
91  if ($this->records == null) return false;
92  if ($this->records->Move($to))
93  return true;
94  else
95  return false;
96  }
97 
102  function next() {
103  if ($this->records == null) return $this->records;
104  if (!$this->records->EOF) {
106  $dao = $this->dao;
107  $row = $this->records->getRowAssoc(false);
108  $result = $dao->$functionName($row, $this->functionParams);
109  if (!$this->records->MoveNext()) $this->close();
110  return $result;
111  } else {
112  $this->close();
113  $nullVar = null;
114  return $nullVar;
115  }
116  }
117 
122  function nextWithKey($idField = null) {
123  $result = $this->next();
124  if($idField) {
125  assert(is_a($result, 'DataObject'));
126  $key = $result->getData($idField);
127  } elseif (empty($this->idFields)) {
128  $key = null;
129  } else {
130  assert(is_a($result, 'DataObject') && is_array($this->idFields));
131  $key = '';
132  foreach($this->idFields as $idField) {
133  assert(!is_null($result->getData($idField)));
134  if (!empty($key)) $key .= '-';
135  $key .= (string)$result->getData($idField);
136  }
137  }
138  return array($key, $result);
139  }
140 
145  function atFirstPage() {
146  return $this->isFirst;
147  }
148 
153  function atLastPage() {
154  return $this->isLast;
155  }
156 
161  function getPage() {
162  return $this->page;
163  }
164 
169  function getCount() {
170  return $this->count;
171  }
172 
177  function getPageCount() {
178  return $this->pageCount;
179  }
180 
185  function eof() {
186  if ($this->records == null) return true;
187  if ($this->records->EOF) {
188  $this->close();
189  return true;
190  }
191  return false;
192  }
193 
198  function wasEmpty() {
199  return $this->wasEmpty;
200  }
201 
206  function close() {
207  if ($this->records) {
208  $this->records->close();
209  unset($this->records);
210  $this->records = null;
211  }
212  }
213 
218  function toArray() {
219  $returner = array();
220  while (!$this->eof()) {
221  $returner[] = $this->next();
222  }
223  return $returner;
224  }
225 
230  function toIterator() {
231  return new DAOResultIterator($this);
232  }
233 
238  function toAssociativeArray($idField = 'id') {
239  $returner = array();
240  while (!$this->eof()) {
241  $result = $this->next();
242  $returner[$result->getData($idField)] = $result;
243  unset($result);
244  }
245  return $returner;
246  }
247 }
DAOResultFactory\eof
eof()
Definition: DAOResultFactory.inc.php:200
DAOResultFactory\$functionName
$functionName
Definition: DAOResultFactory.inc.php:32
DAOResultFactory\$isLast
$isLast
Definition: DAOResultFactory.inc.php:58
DAOResultFactory\$count
$count
Definition: DAOResultFactory.inc.php:60
DAOResultFactory
Wrapper around ADORecordSet providing "factory" features for generating objects from DAOs.
Definition: DAOResultFactory.inc.php:21
DAOResultFactory\$page
$page
Definition: DAOResultFactory.inc.php:59
DAOResultFactory\getCount
getCount()
Definition: DAOResultFactory.inc.php:184
DAOResultFactory\$functionParams
$functionParams
Definition: DAOResultFactory.inc.php:34
DAOResultFactory\atFirstPage
atFirstPage()
Definition: DAOResultFactory.inc.php:160
DAOResultFactory\$wasEmpty
$wasEmpty
Definition: DAOResultFactory.inc.php:55
DAOResultFactory\$isFirst
$isFirst
Definition: DAOResultFactory.inc.php:57
DAOResultFactory\wasEmpty
wasEmpty()
Definition: DAOResultFactory.inc.php:213
DAOResultFactory\getPage
getPage()
Definition: DAOResultFactory.inc.php:176
ItemIterator\nextWithKey
nextWithKey()
Definition: ItemIterator.inc.php:37
DAOResultFactory\toArray
toArray()
Definition: DAOResultFactory.inc.php:233
DAOResultFactory\toIterator
toIterator()
Definition: DAOResultFactory.inc.php:245
DAOResultFactory\move
move($to)
Definition: DAOResultFactory.inc.php:105
DAOResultFactory\$pageCount
$pageCount
Definition: DAOResultFactory.inc.php:61
DAOResultFactory\close
close()
Definition: DAOResultFactory.inc.php:221
DAOResultFactory\$idFields
$idFields
Definition: DAOResultFactory.inc.php:43
ItemIterator\__construct
__construct()
Definition: ItemIterator.inc.php:22
DAOResultFactory\getPageCount
getPageCount()
Definition: DAOResultFactory.inc.php:192
DAOResultFactory\__construct
__construct($records, $dao, $functionName, $idFields=array(), $functionParams=array())
Definition: DAOResultFactory.inc.php:73
DAOResultFactory\atLastPage
atLastPage()
Definition: DAOResultFactory.inc.php:168
DAOResultFactory\$records
$records
Definition: DAOResultFactory.inc.php:49
DAOResultFactory\toAssociativeArray
toAssociativeArray($idField='id')
Definition: DAOResultFactory.inc.php:253
DAOResultFactory\next
next()
Definition: DAOResultFactory.inc.php:117
DAOResultIterator
Wrapper around a DAOResultFactory providing a proper PHP Iterator implementation.
Definition: DAOResultIterator.inc.php:17
DAOResultFactory\$dao
$dao
Definition: DAOResultFactory.inc.php:26
ItemIterator
Generic iterator class; needs to be overloaded by subclasses providing specific implementations.
Definition: ItemIterator.inc.php:18
DAOResultFactory\nextWithKey
nextWithKey($idField=null)
Definition: DAOResultFactory.inc.php:137