00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Varien_Data_Collection implements IteratorAggregate, Countable
00035 {
00036 const SORT_ORDER_ASC = 'ASC';
00037 const SORT_ORDER_DESC = 'DESC';
00038
00039
00040
00041
00042
00043
00044 protected $_items = array();
00045
00046
00047
00048
00049
00050
00051 protected $_itemObjectClass = 'Varien_Object';
00052
00053
00054
00055
00056
00057
00058 protected $_orders = array();
00059
00060
00061
00062
00063
00064
00065 protected $_filters = array();
00066
00067
00068
00069
00070
00071
00072 protected $_isFiltersRendered = false;
00073
00074
00075
00076
00077
00078
00079 protected $_curPage = 1;
00080
00081
00082
00083
00084
00085
00086
00087
00088 protected $_pageSize = false;
00089
00090
00091
00092
00093
00094
00095 protected $_totalRecords;
00096
00097
00098
00099
00100
00101
00102 protected $_isCollectionLoaded;
00103
00104 protected $_cacheKey;
00105
00106 protected $_cacheTags = array();
00107
00108 protected $_cacheLifetime = 86400;
00109
00110
00111
00112
00113
00114
00115 protected $_flags = array();
00116
00117 public function __construct()
00118 {
00119
00120 }
00121
00122
00123
00124
00125
00126
00127
00128
00129 public function addFilter($field, $value, $type = 'and')
00130 {
00131 $filter = array();
00132 $filter['field'] = $field;
00133 $filter['value'] = $value;
00134 $filter['type'] = strtolower($type);
00135
00136 $this->_filters[] = $filter;
00137 $this->_isFiltersRendered = false;
00138 return $this;
00139 }
00140
00141
00142
00143
00144
00145
00146 public function isLoaded()
00147 {
00148 return $this->_isCollectionLoaded;
00149 }
00150
00151
00152
00153
00154
00155
00156
00157 protected function _setIsLoaded($flag = true)
00158 {
00159 $this->_isCollectionLoaded = $flag;
00160 return $this;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169 public function getCurPage($displacement = 0)
00170 {
00171 if ($this->_curPage + $displacement < 1) {
00172 return 1;
00173 }
00174 elseif ($this->_curPage + $displacement > $this->getLastPageNumber()) {
00175 return $this->getLastPageNumber();
00176 } else {
00177 return $this->_curPage + $displacement;
00178 }
00179 }
00180
00181
00182
00183
00184
00185
00186 public function getLastPageNumber()
00187 {
00188 $collectionSize = (int) $this->getSize();
00189 if (0 === $collectionSize) {
00190 return 1;
00191 }
00192 elseif($this->_pageSize) {
00193 return ceil($collectionSize/$this->_pageSize);
00194 }
00195 else{
00196 return 1;
00197 }
00198 }
00199
00200
00201
00202
00203
00204
00205 public function getPageSize()
00206 {
00207 return $this->_pageSize;
00208 }
00209
00210
00211
00212
00213
00214
00215 public function getSize()
00216 {
00217 $this->load();
00218 return $this->_totalRecords;
00219 }
00220
00221
00222
00223
00224
00225
00226 public function getFirstItem()
00227 {
00228 $this->load();
00229
00230 if (count($this->_items)) {
00231 reset($this->_items);
00232 return current($this->_items);
00233 }
00234
00235 return new $this->_itemObjectClass();
00236 }
00237
00238
00239
00240
00241
00242
00243 public function getLastItem()
00244 {
00245 $this->load();
00246
00247 if (count($this->_items)) {
00248 return end($this->_items);
00249 }
00250
00251 return new $this->_itemObjectClass();
00252 }
00253
00254
00255
00256
00257
00258
00259 public function getItems()
00260 {
00261 $this->load();
00262 return $this->_items;
00263 }
00264
00265
00266
00267
00268
00269
00270
00271 public function getColumnValues($colName)
00272 {
00273 $this->load();
00274
00275 $col = array();
00276 foreach ($this->getItems() as $item) {
00277 $col[] = $item->getData($colName);
00278 }
00279 return $col;
00280 }
00281
00282
00283
00284
00285
00286
00287
00288
00289 public function getItemsByColumnValue($column, $value)
00290 {
00291 $this->load();
00292
00293 $res = array();
00294 foreach ($this as $item) {
00295 if ($item->getData($column)==$value) {
00296 $res[] = $item;
00297 }
00298 }
00299 return $res;
00300 }
00301
00302
00303
00304
00305
00306
00307
00308
00309 public function getItemByColumnValue($column, $value)
00310 {
00311 $this->load();
00312
00313 foreach ($this as $item) {
00314 if ($item->getData($column)==$value) {
00315 return $item;
00316 }
00317 }
00318 return null;
00319 }
00320
00321
00322
00323
00324
00325
00326
00327 public function addItem(Varien_Object $item)
00328 {
00329 $itemId = $this->_getItemId($item);
00330 if (!is_null($itemId)) {
00331 if (isset($this->_items[$itemId])) {
00332 throw new Exception('Item ('.get_class($item).') with the same id "'.$item->getId().'" already exist');
00333 }
00334 $this->_items[$itemId] = $item;
00335 }
00336 else {
00337 $this->_items[] = $item;
00338 }
00339 return $this;
00340 }
00341
00342 protected function _getItemId(Varien_Object $item)
00343 {
00344 return $item->getId();
00345 }
00346
00347
00348
00349
00350
00351
00352
00353 public function removeItemByKey($key)
00354 {
00355 if (isset($this->_items[$key])) {
00356 unset($this->_items[$key]);
00357 }
00358 return $this;
00359 }
00360
00361
00362
00363
00364
00365
00366 public function clear()
00367 {
00368 $this->_setIsLoaded(false);
00369 $this->_items = array();
00370 return $this;
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 public function walk($callback, array $args=array())
00384 {
00385 $results = array();
00386 $useItemCallback = is_string($callback) && strpos($callback, '::')===false;
00387 foreach ($this->getItems() as $id=>$item) {
00388 if ($useItemCallback) {
00389 $cb = array($item, $callback);
00390 } else {
00391 $cb = $callback;
00392 array_unshift($args, $item);
00393 }
00394 $results[$id] = call_user_func_array($cb, $args);
00395 }
00396 return $results;
00397 }
00398
00399 public function each($obj_method, $args=array())
00400 {
00401 foreach ($args->_items as $k => $item) {
00402 $args->_items[$k] = call_user_func($obj_method, $item);
00403 }
00404 }
00405
00406
00407
00408
00409
00410
00411
00412
00413 public function setDataToAll($key, $value=null)
00414 {
00415 if (is_array($key)) {
00416 foreach ($key as $k=>$v) {
00417 $this->setDataToAll($k, $v);
00418 }
00419 return $this;
00420 }
00421 foreach ($this->getItems() as $item) {
00422 $item->setData($key, $value);
00423 }
00424 return $this;
00425 }
00426
00427
00428
00429
00430
00431
00432
00433 public function setCurPage($page)
00434 {
00435 $this->_curPage = $page;
00436 return $this;
00437 }
00438
00439
00440
00441
00442
00443
00444
00445 public function setPageSize($size)
00446 {
00447 $this->_pageSize = $size;
00448 return $this;
00449 }
00450
00451
00452
00453
00454
00455
00456
00457
00458 public function setOrder($field, $direction = 'desc')
00459 {
00460 $this->_orders[$field] = $direction;
00461 return $this;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470 function setItemObjectClass($className)
00471 {
00472 $className = Mage::getConfig()->getModelClassName($className);
00473
00474
00475
00476
00477
00478
00479 $this->_itemObjectClass = $className;
00480 return $this;
00481 }
00482
00483
00484
00485
00486
00487
00488 public function getNewEmptyItem()
00489 {
00490 return new $this->_itemObjectClass();
00491 }
00492
00493
00494
00495
00496
00497
00498 protected function _renderFilters()
00499 {
00500 return $this;
00501 }
00502
00503
00504
00505
00506
00507
00508 protected function _renderOrders()
00509 {
00510 return $this;
00511 }
00512
00513
00514
00515
00516
00517
00518 protected function _renderLimit()
00519 {
00520 return $this;
00521 }
00522
00523
00524
00525
00526
00527
00528 public function distinct($flag)
00529 {
00530 return $this;
00531 }
00532
00533
00534
00535
00536
00537
00538 public function loadData($printQuery = false, $logQuery = false)
00539 {
00540 return $this;
00541 }
00542
00543
00544
00545
00546
00547
00548 public function load($printQuery = false, $logQuery = false)
00549 {
00550 return $this->loadData($printQuery, $logQuery);
00551 }
00552
00553
00554
00555
00556
00557
00558 public function toXml()
00559 {
00560 $xml = '<?xml version="1.0" encoding="UTF-8"?>
00561 <collection>
00562 <totalRecords>'.$this->_totalRecords.'</totalRecords>
00563 <items>';
00564
00565 foreach ($this as $item) {
00566 $xml.=$item->toXml();
00567 }
00568 $xml.= '</items>
00569 </collection>';
00570 return $xml;
00571 }
00572
00573
00574
00575
00576
00577
00578 public function toArray($arrRequiredFields = array())
00579 {
00580 $arrItems = array();
00581 $arrItems['totalRecords'] = $this->getSize();
00582
00583 $arrItems['items'] = array();
00584 foreach ($this as $item) {
00585 $arrItems['items'][] = $item->toArray($arrRequiredFields);
00586 }
00587 return $arrItems;
00588 }
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605 protected function _toOptionArray($valueField='id', $labelField='name', $additional=array())
00606 {
00607 $res = array();
00608 $additional['value'] = $valueField;
00609 $additional['label'] = $labelField;
00610
00611 foreach ($this as $item) {
00612 foreach ($additional as $code => $field) {
00613 $data[$code] = $item->getData($field);
00614 }
00615 $res[] = $data;
00616 }
00617 return $res;
00618 }
00619
00620 public function toOptionArray()
00621 {
00622 return $this->_toOptionArray();
00623 }
00624
00625 public function toOptionHash()
00626 {
00627 return $this->_toOptionHash();
00628 }
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640 protected function _toOptionHash($valueField='id', $labelField='name')
00641 {
00642 $res = array();
00643 foreach ($this as $item) {
00644 $res[$item->getData($valueField)] = $item->getData($labelField);
00645 }
00646 return $res;
00647 }
00648
00649
00650
00651
00652
00653
00654
00655 public function getItemById($idValue)
00656 {
00657 $this->load();
00658 if (isset($this->_items[$idValue])) {
00659 return $this->_items[$idValue];
00660 }
00661 return null;
00662 }
00663
00664
00665
00666
00667 public function getIterator()
00668 {
00669 $this->load();
00670 return new ArrayIterator($this->_items);
00671 }
00672
00673
00674
00675
00676
00677
00678 public function count()
00679 {
00680 $this->load();
00681 return count($this->_items);
00682 }
00683
00684 public function setCacheKey($key)
00685 {
00686 $this->_cacheKey = $key;
00687 return $this;
00688 }
00689
00690 public function getCacheKey()
00691 {
00692 return $this->_cacheKey;
00693 }
00694
00695 public function setCacheTags($tags)
00696 {
00697 $this->_cacheTags = $tags;
00698 return $this;
00699 }
00700
00701 public function getCacheTags()
00702 {
00703 return $this->_cacheTags;
00704 }
00705
00706 public function getCacheLifetime()
00707 {
00708 return $this->_cacheLifetime;
00709 }
00710
00711
00712
00713
00714
00715
00716
00717 public function getFlag($flag)
00718 {
00719 return isset($this->_flags[$flag]) ? $this->_flags[$flag] : null;
00720 }
00721
00722
00723
00724
00725
00726
00727
00728
00729 public function setFlag($flag, $value = null)
00730 {
00731 $this->_flags[$flag] = $value;
00732 return $this;
00733 }
00734
00735
00736
00737
00738
00739
00740
00741 public function hasFlag($flag)
00742 {
00743 return array_key_exists($flag, $this->_flags);
00744 }
00745 }