コード例 #1
0
 protected function processRecord($record, $columnMap, &$results, $preview = false)
 {
     $siteTreeID = null;
     if (!empty($record['NewURL'])) {
         //redirect to an existing page
         if ($page = SiteTree::get_by_link($record['NewURL'])) {
             $siteTreeID = $page->ID;
         } else {
             //custom url redirect
             $redirect = $this->createRedirect(array('OldURL' => $record['OldURL'], 'RedirectType' => 'Custom', 'RedirectTo' => $record['NewURL']));
             $results->addCreated($redirect);
             return $redirect->ID;
         }
     } else {
         if (!empty($record['PageID'])) {
             //pass in page id directly
             $siteTreeID = $record['PageID'];
         } else {
             return false;
         }
     }
     //check for an existing record
     $existing = OldURLRedirect::get()->filter(array('OldURL' => $record['OldURL'], 'PageID' => $siteTreeID))->first();
     if (!$existing) {
         $redirect = $this->createRedirect(array('OldURL' => $record['OldURL'], 'PageID' => $siteTreeID));
         $results->addCreated($redirect);
         return $redirect->ID;
     } else {
         $results->addUpdated($existing);
         return $existing->ID;
     }
 }
コード例 #2
0
    /**
     * Lookup an OldURLRedirect page which matches the url
     *
     * @param $url
     * @return DataObject|null
     */
    public static function get_from_url($url)
    {
        $url = $url ? $url : (!empty($_GET['url']) ? $_GET['url'] : '');
        if ($url) {
            if (strpos($url, '/') !== 0) {
                $url = '/' . $url;
            }
            $SQL_url = Convert::raw2sql($url);
            $filter = <<<SQL
("OldURL" = '{$SQL_url}' AND "RedirectType" = 'Custom')
OR
("OldURL" = '{$SQL_url}' AND "RedirectType" = 'Internal' AND "PageID" <> 0)
SQL;
            $oldPage = OldURLRedirect::get()->where($filter)->first();
            if ($oldPage && $url == strtolower($oldPage->OldURL)) {
                return $oldPage;
            }
        }
        return null;
    }