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
00035 class Mage_Backup_Model_Fs_Collection extends Varien_Data_Collection
00036 {
00037
00038
00039
00040
00041 protected $_isLoaded = false;
00042
00043
00044
00045
00046
00047
00048
00049 public function __construct()
00050 {
00051 parent::__construct();
00052 $this->setItemObjectClass(Mage::getConfig()->getModelClassName('backup/backup'))
00053 ->setOrder('time','desc');
00054
00055 }
00056
00057
00058
00059
00060
00061
00062 public function getSize()
00063 {
00064 $this->_loadFiles();
00065 return $this->_totalRecords;
00066 }
00067
00068 public function load($printQuery = false, $logQuery = false)
00069 {
00070 if (!$this->_isLoaded) {
00071 $this->_loadFiles();
00072 if($this->getPageSize()) {
00073 $this->_items = array_slice($this->_items, ($this->getCurPage()-1)*$this->getPageSize(), $this->getPageSize());
00074 }
00075 }
00076
00077 return $this;
00078 }
00079
00080 protected function _loadFiles()
00081 {
00082 if (!$this->_isLoaded) {
00083
00084 $readPath = Mage::getBaseDir('var') . DS . "backups";
00085
00086 $ioProxy = new Varien_Io_File();
00087
00088 try {
00089 $ioProxy->open(array('path'=>$readPath));
00090 }
00091 catch (Exception $e) {
00092 $ioProxy->mkdir($readPath, 0777);
00093 $ioProxy->chmod($readPath, 0777);
00094 $ioProxy->open(array('path'=>$readPath));
00095 }
00096
00097 if (!is_file($readPath . DS . ".htaccess")) {
00098
00099 $ioProxy->write(".htaccess","deny from all", 0644);
00100 }
00101
00102
00103 $list = $ioProxy->ls(Varien_Io_File::GREP_FILES);
00104
00105 $fileExtension = constant($this->_itemObjectClass . "::BACKUP_EXTENSION");
00106
00107 foreach ($list as $entry) {
00108 if ($entry['filetype'] == $fileExtension) {
00109 $item = new $this->_itemObjectClass();
00110 $item->load($entry['text'], $readPath);
00111 $item->setSize($entry['size']);
00112 if ($this->_checkCondition($item)) {
00113 $this->addItem($item);
00114 }
00115 }
00116 }
00117
00118
00119 $this->_totalRecords = count($this->_items);
00120
00121 if ($this->_totalRecords > 1) {
00122 usort($this->_items, array(&$this, 'compareByTypeOrDate'));
00123 }
00124
00125 $this->_isLoaded = true;
00126 }
00127
00128 return $this;
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138 public function setOrder($field, $direction = 'desc')
00139 {
00140 $direction = (strtoupper($direction)=='ASC') ? 1 : -1;
00141 $this->_orders = array($field, $direction);
00142 return $this;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152 public function compareByTypeOrDate(Varien_Object $item1,Varien_Object $item2)
00153 {
00154 if (is_string($item1->getData($this->_orders[0]))) {
00155 return strcmp($item1->getData($this->_orders[0]),$item2->getData($this->_orders[0]))*(-1*$this->_orders[1]);
00156 } else if ($item1->getData($this->_orders[0]) < $item2->getData($this->_orders[0])) {
00157 return 1*(-1*$this->_orders[1]);
00158 } else if ($item1->getData($this->_orders[0]) > $item2->getData($this->_orders[0])) {
00159 return -1*(-1*$this->_orders[1]);
00160 } else {
00161 return 0;
00162 }
00163 }
00164
00165 public function addFieldToFilter($fieldName, $condition)
00166 {
00167 $this->_filters[$fieldName] = $condition;
00168 return $this;
00169 }
00170
00171 protected function _checkCondition($item)
00172 {
00173 foreach ($this->_filters as $field => $condition) {
00174 if (is_array($condition)) {
00175 if (isset($condition['from']) || isset($condition['to'])) {
00176 if ($field == 'time_formated') {
00177 $format = Mage::app()->getLocale()->getDateTimeFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT);
00178 if (isset($condition['from'])) {
00179 $condition['from'] = Mage::app()->getLocale()->date($condition['from'], $format)->getTimestamp()
00180 + Mage::app()->getLocale()->date($condition['from'], $format)->getGmtOffset();
00181 }
00182 if (isset($condition['to'])) {
00183 $condition['to'] = Mage::app()->getLocale()->date($condition['to'], $format)->getTimestamp()
00184 + Mage::app()->getLocale()->date($condition['to'], $format)->getGmtOffset();
00185 }
00186 $field = 'time';
00187 }
00188
00189 if (isset($condition['from']) && $item->getData($field) < $condition['from']) {
00190 return false;
00191 }
00192 if (isset($condition['to']) && $item->getData($field) > $condition['to']) {
00193 return false;
00194 }
00195 }
00196 elseif (!empty($condition['neq']) && $item->getData($field) == $condition['neq']) {
00197 return false;
00198 }
00199 elseif (!empty($condition['like']) && strpos($item->getData($field), trim($condition['like'], '%')) === false) {
00200 return false;
00201 }
00202 elseif (!empty($condition['nlike']) && strpos($item->getData($field), trim($condition['nlike'], '%')) !== false) {
00203 return false;
00204 }
00205 elseif (!empty($condition['in'])) {
00206 $values = $condition['in'];
00207 if(!is_array($values)) {
00208 $values = array($values);
00209 }
00210 if(!in_array($item->getData($field), $values)) {
00211 return false;
00212 }
00213 }
00214 elseif (!empty($condition['nin'])) {
00215 $values = $condition['in'];
00216 if(!is_array($values)) {
00217 $values = array($values);
00218 }
00219 if(in_array($item->getData($field), $values)) {
00220 return false;
00221 }
00222 }
00223 } else if($item->getData($field) != $condition) {
00224 return false;
00225 }
00226 }
00227
00228 return true;
00229 }
00230 }