2015-10-18 02:59:43 +00:00
|
|
|
-- eidolon -- A simple gallery in Haskell and Yesod
|
|
|
|
-- Copyright (C) 2015 Amedeo Molnár
|
|
|
|
--
|
|
|
|
-- This program is free software: you can redistribute it and/or modify
|
|
|
|
-- it under the terms of the GNU Affero General Public License as published
|
|
|
|
-- by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
-- (at your option) any later version.
|
|
|
|
--
|
|
|
|
-- This program is distributed in the hope that it will be useful,
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
-- GNU Affero General Public License for more details.
|
|
|
|
--
|
|
|
|
-- You should have received a copy of the GNU Affero General Public License
|
|
|
|
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-10-20 16:49:37 +00:00
|
|
|
module Handler.Search where
|
2015-10-16 13:32:47 +00:00
|
|
|
|
2017-10-20 16:49:37 +00:00
|
|
|
import Import
|
2015-10-16 13:32:47 +00:00
|
|
|
|
2017-10-20 16:49:37 +00:00
|
|
|
import qualified Data.Text as T
|
|
|
|
import Data.Maybe (fromJust)
|
2016-08-30 12:22:00 +00:00
|
|
|
|
2017-10-20 16:49:37 +00:00
|
|
|
import Database.Persist.Sql (rawSql)
|
|
|
|
|
|
|
|
import System.FilePath (splitDirectories)
|
|
|
|
|
|
|
|
getSearchR :: Handler Html
|
|
|
|
getSearchR = do
|
|
|
|
((res, widget), _) <- runFormGet $ renderBootstrap3 BootstrapBasicForm $
|
|
|
|
searchForm
|
|
|
|
case res of
|
|
|
|
FormSuccess query -> do
|
2017-10-21 13:53:43 +00:00
|
|
|
mediumList <- runDB $ (++)
|
|
|
|
rawSql "select ?? from medium where any(string_to_array(title, ' ') % ?"
|
|
|
|
[PersistText query]
|
|
|
|
rawSql "select ?? from medium where any(string_to_array(description, ' ') % ?"
|
|
|
|
[PersistText query]
|
2017-10-20 16:49:37 +00:00
|
|
|
userList <- runDB $
|
|
|
|
rawSql "select ?? from \"user\" where name % ?" [PersistText query]
|
|
|
|
albumList <- runDB $
|
|
|
|
rawSql "select ?? from album where title % ?" [PersistText query]
|
|
|
|
let allEmpty = null mediumList && null userList && null albumList
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle $ toHtml $ "Eidolon :: Search results for " ++ (T.unpack query)
|
|
|
|
$(widgetFile "result")
|
|
|
|
_ ->
|
|
|
|
defaultLayout $ do
|
|
|
|
setTitle "Eidolon :: Search"
|
|
|
|
$(widgetFile "search")
|
|
|
|
|
|
|
|
searchForm :: AForm Handler T.Text
|
|
|
|
searchForm = areq (searchField True) "Search" Nothing
|