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 require_once "Varien/Pear.php";
00028 require_once "Varien/Pear/Package.php";
00029
00030 abstract class Mage_Adminhtml_Model_Extension_Collection_Abstract extends Varien_Data_Collection
00031 {
00032
00033 public function loadData($printQuery = false, $logQuery = false)
00034 {
00035 if ($this->isLoaded()) {
00036 return $this;
00037 }
00038
00039
00040 $packages = $this->_fetchPackages();
00041
00042
00043 if (!empty($this->_filters)) {
00044 foreach ($packages as $i=>$pkg) {
00045 if (!$this->validateRow($pkg)) {
00046 unset($packages[$i]);
00047 }
00048 }
00049 }
00050
00051
00052 $this->_totalRecords = sizeof($packages);
00053 $this->_setIsLoaded();
00054
00055
00056 if (!empty($this->_orders)) {
00057 usort($packages, array($this, 'sortPackages'));
00058 }
00059
00060
00061 $from = ($this->getCurPage() - 1) * $this->getPageSize();
00062 $to = $from + $this->getPageSize() - 1;
00063
00064 $cnt = 0;
00065 foreach ($packages as $pkg) {
00066 $cnt++;
00067 if ($cnt<$from || $cnt>$to) {
00068 continue;
00069 }
00070 $item = new $this->_itemObjectClass();
00071 $item->addData($pkg);
00072 $this->addItem($item);
00073 }
00074
00075 return $this;
00076 }
00077
00078 abstract protected function _fetchPackages();
00079
00080 public function setOrder($field, $dir)
00081 {
00082 $this->_orders[] = array('field'=>$field, 'dir'=>$dir);
00083 return $this;
00084 }
00085
00086 public function sortPackages($a, $b)
00087 {
00088 $field = $this->_orders[0]['field'];
00089 $dir = $this->_orders[0]['dir'];
00090
00091 $cmp = $a[$field] > $b[$field] ? 1 : ($a[$field] < $b[$field] ? -1 : 0);
00092
00093 return ('asc'===$dir) ? $cmp : -$cmp;
00094 }
00095
00096 public function addFieldToFilter($field, $condition)
00097 {
00098 $this->_filters[$field] = $condition;
00099 return $this;
00100 }
00101
00102 public function validateRow($row)
00103 {
00104 if (empty($this->_filters)) {
00105 return true;
00106 }
00107 foreach ($this->_filters as $field=>$filter) {
00108 if (!isset($row[$field])) {
00109 return false;
00110 }
00111 if (isset($filter['eq'])) {
00112 if ($filter['eq']!=$row[$field]) {
00113 return false;
00114 }
00115 }
00116 if (isset($filter['like'])) {
00117 $query = preg_replace('#(^%|%$)#', '', $filter['like']);
00118 if (strpos(strtolower($row[$field]), strtolower($query))===false) {
00119 return false;
00120 }
00121 }
00122 if ('version'===$field) {
00123 if (isset($filter['from'])) {
00124 if (!version_compare($filter['from'], $row[$field], '<=')) {
00125 return false;
00126 }
00127 }
00128 if (isset($filter['to'])) {
00129 if (!version_compare($filter['to'], $row[$field], '>=')) {
00130 return false;
00131 }
00132 }
00133 }
00134 }
00135 return true;
00136 }
00137
00138 public function getAllIds()
00139 {
00140 $this->load();
00141
00142 $ids = array();
00143 foreach ($this->getIterator() as $item) {
00144 $ids[] = $item->getId();
00145 }
00146 return $ids;
00147 }
00148
00149 }