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 class Mage_Rss_Model_Mysql4_Order
00034 {
00035 protected $_entityTypeIdsToTypes;
00036 protected $_entityIdsToIncrementIds;
00037
00038 public function __construct()
00039 {
00040 $this->_entityTypeIdsToTypes = array();
00041 $this->_entityIdsToIncrementIds = array();
00042 }
00043
00044 public function getEntityTypeIdsToTypes()
00045 {
00046 return $this->_entityTypeIdsToTypes;
00047 }
00048
00049 public function getEntityIdsToIncrementIds()
00050 {
00051 return $this->_entityIdsToIncrementIds;
00052 }
00053
00054 public function getCoreResource()
00055 {
00056 return Mage::getSingleton('core/resource');
00057 }
00058
00059 public function getAllOrderEntityTypeIds()
00060 {
00061 $orderEntityTypes = array();
00062 $etypeIds = array();
00063 $oattrIds = array();
00064 $eav = Mage::getSingleton('eav/config');
00065 $oTable = '';
00066 foreach (array(
00067 'invoice'=>'sales/order_invoice',
00068 'shipment'=>'sales/order_shipment',
00069 'creditmemo'=>'sales/order_creditmemo'
00070 ) as $entityTypeCode=>$entityModel) {
00071 $entityType = $eav->getEntityType($entityTypeCode);
00072 $entity = Mage::getResourceSingleton($entityModel);
00073 $orderAttr = $eav->getAttribute($entityType, 'order_id');
00074 if (!$oTable) {
00075 $orderAttr->setEntity($entity);
00076 $oTable = $orderAttr->getBackend()->getTable();
00077 }
00078 $this->_entityTypeIdsToTypes[$entityType->getId()] = $entityTypeCode;
00079 $etypeIds[$entityType->getId()] = $entityTypeCode;
00080 $oattrIds[] = $orderAttr->getId();
00081 }
00082 $orderEntityTypes = array(
00083 'entityTypeIds' => $etypeIds,
00084 'orderAttrIds' => $oattrIds,
00085 'order_table' => $oTable);
00086 return $orderEntityTypes;
00087 }
00088
00089 public function getAllOrderEntityIds($oid, $orderEntityTypes)
00090 {
00091 $etypeIdsArr = array_keys($orderEntityTypes['entityTypeIds']);
00092 $res = $this->getCoreResource();
00093 $read = $res->getConnection('core_read');
00094 $select = $read->select()
00095 ->from(array('order' => $res->getTableName('sales/order')), array('entity_id'))
00096 ->join($orderEntityTypes['order_table'],"{$orderEntityTypes['order_table']}.entity_id=order.entity_id
00097 and {$orderEntityTypes['order_table']}.attribute_id in (".implode(',',$orderEntityTypes['orderAttrIds']).")
00098 and {$orderEntityTypes['order_table']}.entity_type_id in (".implode(',', $etypeIdsArr).") and {$orderEntityTypes['order_table']}.value={$oid}"
00099 ,array("{$orderEntityTypes['order_table']}.value"));
00100
00101 $results = $read->fetchAll($select);
00102 $eIds = array($oid);
00103 foreach($results as $result){
00104 $eIds[] = $result['entity_id'];
00105 }
00106 return $eIds;
00107 }
00108
00109 public function getAllEntityIds($entityIds = array())
00110 {
00111 $res = $this->getCoreResource();
00112 $read = $res->getConnection('core_read');
00113 $entityIdStr = implode(',', $entityIds);
00114 $select = $read->select()
00115 ->from($res->getTableName('sales/order'), array('entity_id','increment_id'))
00116 ->where('parent_id in (' .$entityIdStr.')')
00117 ->orWhere('entity_id in (' .$entityIdStr.')');
00118 $results = $read->fetchAll($select);
00119 $eIds = array();
00120 foreach($results as $result){
00121 if($result['increment_id']) {
00122 $this->_entityIdsToIncrementIds[$result['entity_id']] = $result['increment_id'];
00123 }
00124 $eIds[] = $result['entity_id'];
00125 }
00126 return $eIds;
00127 }
00128
00129 public function getAllEntityTypeCommentIds()
00130 {
00131 $entityTypes = array();
00132 $eav = Mage::getSingleton('eav/config');
00133 $etypeIds = array();
00134 $cattrIds = array();
00135 $nattrIds = array();
00136 $cTable = '';
00137 $nTable = '';
00138 foreach (array(
00139 'order_status_history'=>array('model' => 'sales/order_status_history', 'type' => 'order'),
00140 'invoice_comment'=>array('model' => 'sales/order_invoice_comment', 'type' => 'invoice'),
00141 'shipment_comment'=>array('model' => 'sales/order_shipment_comment', 'type' => 'shipment'),
00142 'creditmemo_comment'=>array('model' => 'sales/order_creditmemo_comment', 'type' => 'creditmemo')
00143 ) as $entityTypeCode=>$entityArr) {
00144
00145 $entityType = $eav->getEntityType($entityTypeCode);
00146 $entity = Mage::getResourceSingleton($entityArr['model']);
00147 $commentAttr = $eav->getAttribute($entityType, 'comment');
00148 $notifiedAttr = $eav->getAttribute($entityType, 'is_customer_notified');
00149 $statusAttr = $eav->getAttribute($entityType, 'status');
00150 #$statusAttr->setEntity($entity);
00151 #echo "****".$statusAttr->getBackend()->getTable()."****".$statusAttr->getId();
00152 if (!$cTable) {
00153 $commentAttr->setEntity($entity);
00154 $cTable = $commentAttr->getBackend()->getTable();
00155 }
00156 if (!$nTable) {
00157 $notifiedAttr->setEntity($entity);
00158 $nTable = $notifiedAttr->getBackend()->getTable();
00159 }
00160 $etypeIds[] = $entityType->getId();
00161 $cattrIds[] = $commentAttr->getId();
00162 $nattrIds[] = $notifiedAttr->getId();
00163 $this->_entityTypeIdsToTypes[$entityType->getId()] = $entityArr['type'];
00164
00165
00166
00167
00168
00169
00170
00171
00172 }
00173 $entityTypes = array(
00174 'entityTypeIds' => $etypeIds,
00175 'commentAttrIds' => $cattrIds,
00176 'notifiedAttrIds' => $nattrIds,
00177 'comment_table' => $cTable,
00178 'notified_table' => $nTable);
00179 return $entityTypes;
00180 }
00181
00182
00183
00184
00185
00186
00187 public function getAllCommentCollection($oid)
00188 {
00189 $orderEntityTypes = $this->getAllOrderEntityTypeIds();
00190 $entityIds = $this->getAllOrderEntityIds($oid, $orderEntityTypes);
00191 $allEntityIds = $this->getAllEntityIds($entityIds);
00192
00193 $eTypes = $this->getAllEntityTypeCommentIds();
00194 $etypeIds = implode(',',$eTypes['entityTypeIds']);
00195
00196
00197
00198
00199
00200
00201
00202 $res = $this->getCoreResource();
00203 $read = $res->getConnection('core_read');
00204 $select = $read->select()
00205 ->from(array('order' => $res->getTableName('sales/order')), array('entity_id','created_at','entity_type_id','parent_id'))
00206 ->where('order.entity_id in ('.implode(",", $allEntityIds).')')
00207 ->join($eTypes['comment_table'],"{$eTypes['comment_table']}.entity_id=order.entity_id
00208 and {$eTypes['comment_table']}.attribute_id in (".implode(',',$eTypes['commentAttrIds']).")
00209 and {$eTypes['comment_table']}.entity_type_id in (".$etypeIds.")"
00210 ,array('comment' => "{$eTypes['comment_table']}.value"))
00211 ->join($eTypes['notified_table'],"{$eTypes['notified_table']}.entity_id=order.entity_id
00212 and {$eTypes['notified_table']}.attribute_id in (".implode(',',$eTypes['notifiedAttrIds']).")
00213 and {$eTypes['notified_table']}.entity_type_id in (".$etypeIds.") and {$eTypes['notified_table']}.value=1"
00214 ,array('notified' =>"{$eTypes['notified_table']}.value"))
00215 ->order('created_at desc')
00216 ;
00217 return $read->fetchAll($select);
00218
00219 }
00220
00221 }