Ejemplo n.º 1
0
 /**
  * Load rewrite information for request.
  *
  * @param Rootd_Link_Model_Node $object The link object.
  * @param array|string          $path   The path(s) to load.
  * 
  * @return Rootd_Link_Model_Resource_Node
  */
 public function loadByRequestPath(Rootd_Link_Model_Node $object, $path)
 {
     $today = date(Varien_Db_Adapter_Pdo_Mysql::DATE_FORMAT, Mage::getSingleton('core/date')->timestamp(time()));
     if (!is_array($path)) {
         $path = array($path);
     }
     $pathBind = array();
     foreach ($path as $key => $url) {
         $pathBind['path' . $key] = $path[$key] = ltrim($url, Mage::helper('link')->getFrontName() . '/');
     }
     // Determine column by which to load
     $parts = explode('/', $path[0]);
     if ($parts[0] == 'node') {
         $loadByName = 'link_id';
         $loadBy = "`main_table`.`{$loadByName}`";
         $pathBind = array('id0' => $parts[1]);
     } else {
         $loadByName = 'request_path';
         $loadBy = "`main_table`.`{$loadByName}`";
     }
     $adapter = $this->_getReadAdapter();
     $select = $adapter->select()->from(array('main_table' => $this->getMainTable()), array('main_table.*', 'options' => new Zend_Db_Expr("'RP'")))->where($loadBy . ' IN (:' . implode(', :', array_flip($pathBind)) . ')')->where('`main_table`.`store_id` IN(?)', array(Mage_Core_Model_App::ADMIN_STORE_ID, (int) $object->getStoreId()))->where(new Zend_Db_Expr('
                 (`main_table`.`is_active` = 1 AND `main_table`.`active_from` IS NULL AND `main_table`.`active_to` IS NULL) OR 
                 (`main_table`.`is_active` = 1 AND `main_table`.`active_from` <= ? AND `main_table`.`active_to` >= ?)'), array($today))->joinLeft(array('a' => Mage::getResourceSingleton('link/node_attachment')->getMainTable()), "`main_table`.`object_id` = `a`.`attachment_id`", array());
     //Zend_Debug::dump($select->assemble());exit;
     $items = $adapter->fetchAll($select, $pathBind);
     //Zend_Debug::dump($items);exit;
     // Determine path to use by lowest penalty
     if ($loadByName == 'link_id') {
         $foundItem = array_shift($items);
     } else {
         $mapPenalty = array_flip(array_values($path));
         // Mapping array: lower index = better
         $currentPenalty = null;
         $foundItem = null;
         foreach ($items as $item) {
             if (!array_key_exists($item['request_path'], $mapPenalty)) {
                 continue;
             }
             $penalty = $mapPenalty[$item['request_path']] << 1 + ($item['store_id'] ? 0 : 1);
             if (!$foundItem || $currentPenalty > $penalty) {
                 $foundItem = $item;
                 $currentPenalty = $penalty;
                 if (!$currentPenalty) {
                     break;
                     // Found best matching item with zero penalty, no reason to continue
                 }
             }
         }
     }
     //Zend_Debug::dump($foundItem);exit;
     // Set data and finish loading
     if ($foundItem) {
         $object->setData($foundItem);
     }
     $this->unserializeFields($object);
     $this->_afterLoad($object);
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * Save the link attachment
  *
  * @param Rootd_Link_Model_Node The link model.
  * 
  * @return Rootd_Link_Adminhtml_LinkController
  */
 protected function _saveAttachment(Rootd_Link_Model_Node $model)
 {
     $helper = Mage::helper('link');
     try {
         if (isset($_FILES['target_file']) && !empty($_FILES['target_file']['name'])) {
             $file = $_FILES['target_file']['name'];
             $path = $helper->generateAttachmentPath($file, true);
             $uploader = new Varien_File_Uploader('target_file');
             $uploader->setAllowCreateFolders(true);
             $uploader->setAllowRenameFiles(false);
             $uploader->setFilesDispersion(false);
             $uploader->save(dirname($path), $file);
             $model->setTargetFile($helper->generateAttachmentPath($file, false))->setSaveAttachmentFlag(true);
         }
     } catch (Exception $error) {
         Mage::getSingleton('adminhtml/session')->addNotice($helper->__("Failed to upload attachment: {$error->getMessage()}"));
     }
     return $this;
 }