<commit_before>{-# LANGUAGE QuasiQuotes, TemplateHaskell #-} 
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE CPP #-}
module Foundation
    ( BISocie (..)
    , BISocieRoute (..)
    , BISocieMessage (..)
    , resourcesBISocie
    , Handler
    , Widget
    , maybeAuth
    , requireAuth
    , module Yesod
    , module Settings
    , module Model
    , StaticRoute (..)
    , AuthRoute (..)
      --
--    , UserCrud -- FIXME Crud
--    , userCrud -- FIXME Crud
    ) where

import Yesod
import Yesod.Static
import Yesod.Auth
import BISocie.Helpers.Auth.HashDB
import Yesod.Auth.OpenId
-- import Yesod.Helpers.Crud -- FIXME
import Yesod.Default.Config
import Yesod.Default.Util (addStaticContentExternal)
import Yesod.Logger (Logger, logLazyText)
import Yesod.Form.Jquery
import qualified Data.ByteString.Lazy as L
import Database.Persist.GenericSql
import qualified Database.Persist.Base
import Settings (PersistConfig, widgetFile)
import Text.Jasmine (minifym)
import Web.ClientSession (getKey)
import Text.Hamlet (hamletFile)
import Text.Cassius (cassiusFile)
import Text.Julius (juliusFile)
#if PRODUCTION
import Network.Mail.Mime (sendmail)
#else
import qualified Data.Text.Lazy.Encoding
#endif

import Model
import Settings.StaticFiles
import qualified Settings
import BISocie.Helpers.Util

-- | The site argument for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
-- starts running, such as database connections. Every handler will have
-- access to the data present here.
data BISocie = BISocie
    { settings :: AppConfig DefaultEnv
    , getLogger :: Logger
    , getStatic :: Static -- ^ Settings for static file serving.
    , connPool :: Database.Persist.Base.PersistConfigPool Settings.PersistConfig -- ^ Database connection pool.
    }

-- This is where we define all of the routes in our application. For a full
-- explanation of the syntax, please see:
-- http://docs.yesodweb.com/book/web-routes-quasi/
--
-- This function does three things:
--
-- * Creates the route datatype BISocieRoute. Every valid URL in your
--   application can be represented as a value of this type.
-- * Creates the associated type:
--       type instance Route BISocie = BISocieRoute
-- * Creates the value resourcesBISocie which contains information on the
--   resources declared below. This is used in Application.hs by the call to
--   mkYesodDispatch
--
-- What this function does *not* do is create a YesodSite instance for
-- BISocie. Creating that instance requires all of the handler functions
-- for our application to be in scope. However, the handler functions
-- usually require access to the BISocieRoute datatype. Therefore, we
-- split these actions into two functions and place them in separate files.
mkYesodData "BISocie" $(parseRoutesFile "config/routes")

-- S3はアクセス制限する
-- S3は基本公開ベースなので制限をするURIを提供してそこからgetFileRを呼ぶ

-- Please see the documentation for the Yesod typeclass. There are a number
-- of settings which can be configured by overriding methods here.
instance Yesod BISocie where
    approot = appRoot . settings
    
    encryptKey _ = fmap Just $ getKey "config/client_session_key.aes"
    
    defaultLayout widget = do
      mu <- maybeAuth
      mmsg <- getMessage
      y <- getYesod
      (title, parents) <- breadcrumbs
      current <- getCurrentRoute
      tm <- getRouteToMaster
      let header = $(hamletFile "hamlet/header.hamlet")
          footer = $(hamletFile "hamlet/footer.hamlet")
      pc <- widgetToPageContent $ do
        widget
        addScriptEither $ urlJqueryJs y
        addScriptEither $ urlJqueryUiJs y
        addStylesheetEither $ urlJqueryUiCss y
        addScriptEither $ Left $ StaticR plugins_upload_jquery_upload_1_0_2_js
        addScriptEither $ Left $ StaticR plugins_bubbleup_jquery_bubbleup_js
        addScriptEither $ Left $ StaticR plugins_exinplaceeditor_jquery_exinplaceeditor_0_1_3_js
        addStylesheetEither $ Left $ StaticR plugins_exinplaceeditor_exinplaceeditor_css
        addScriptEither $ Left $ StaticR plugins_watermark_jquery_watermark_js
        addScriptEither $ Left $ StaticR plugins_ajaxzip2_ajaxzip2_js
        addScriptEither $ Left $ StaticR plugins_selection_jquery_selection_js
        addCassius $(cassiusFile "cassius/default-layout.cassius")
        addJulius $(juliusFile "julius/default-layout.julius")
      hamletToRepHtml $(hamletFile "hamlet/default-layout.hamlet")

    -- This is done to provide an optimization for serving static files from
    -- a separate domain. Please see the staticroot setting in Settings.hs
    urlRenderOverride y (StaticR s) =
        Just $ uncurry (joinPath y (Settings.staticroot $ settings y)) $ renderRoute s
    urlRenderOverride _ _ = Nothing

    -- The page to be redirected to when authentication is required.
    authRoute _ = Just $ AuthR LoginR

    -- This function creates static content files in the static folder
    -- and names them based on a hash of their content. This allows
    -- expiration dates to be set far in the future without worry of
    -- users receiving stale content.
    addStaticContent = addStaticContentExternal minifym base64md5 Settings.staticdir (StaticR . flip StaticRoute [])
    
    -- Enable Javascript async loading
    yepnopeJs _ = Just $ Right $ StaticR js_modernizr_js

instance YesodBreadcrumbs BISocie where
  breadcrumb RootR = return ("", Nothing)
  breadcrumb HomeR{} = return ("ホーム", Nothing)
  breadcrumb HumanNetworkR = do
    (uid, _) <- requireAuth
    return ("ヒューマンネットワーク", Just $ HomeR uid)
  breadcrumb (ScheduleR y m) = do
    (uid, _) <- requireAuth
    return (showText y +++ "年" +++ showText m +++ "月のスケジュール", Just $ HomeR uid)
  breadcrumb NewProjectR = do
    (uid, _) <- requireAuth
    return ("新規プロジェクト作成", Just $ HomeR uid)
  breadcrumb (ProjectR pid) = return ("設定", Just $ IssueListR pid)
    
  breadcrumb ParticipantsListR{} = return ("", Nothing)
  breadcrumb ParticipantsR{} = return ("", Nothing)
  breadcrumb UserListR = return ("ユーザ一覧", Nothing)
  
  breadcrumb CrossSearchR = do
    (uid, _) <- requireAuth
    return ("クロスサーチ", Just $ HomeR uid)
  breadcrumb (IssueListR pid) = do 
    (uid, _) <- requireAuth<commit_msg>FIX: modernizr.js make humannetwork blank<commit_after>{-# LANGUAGE QuasiQuotes, TemplateHaskell #-} 
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE CPP #-}
module Foundation
    ( BISocie (..)
    , BISocieRoute (..)
    , BISocieMessage (..)
    , resourcesBISocie
    , Handler
    , Widget
    , maybeAuth
    , requireAuth
    , module Yesod
    , module Settings
    , module Model
    , StaticRoute (..)
    , AuthRoute (..)
      --
--    , UserCrud -- FIXME Crud
--    , userCrud -- FIXME Crud
    ) where

import Yesod
import Yesod.Static
import Yesod.Auth
import BISocie.Helpers.Auth.HashDB
import Yesod.Auth.OpenId
-- import Yesod.Helpers.Crud -- FIXME
import Yesod.Default.Config
import Yesod.Default.Util (addStaticContentExternal)
import Yesod.Logger (Logger, logLazyText)
import Yesod.Form.Jquery
import qualified Data.ByteString.Lazy as L
import Database.Persist.GenericSql
import qualified Database.Persist.Base
import Settings (PersistConfig, widgetFile)
import Text.Jasmine (minifym)
import Web.ClientSession (getKey)
import Text.Hamlet (hamletFile)
import Text.Cassius (cassiusFile)
import Text.Julius (juliusFile)
#if PRODUCTION
import Network.Mail.Mime (sendmail)
#else
import qualified Data.Text.Lazy.Encoding
#endif

import Model
import Settings.StaticFiles
import qualified Settings
import BISocie.Helpers.Util

-- | The site argument for your application. This can be a good place to
-- keep settings and values requiring initialization before your application
-- starts running, such as database connections. Every handler will have
-- access to the data present here.
data BISocie = BISocie
    { settings :: AppConfig DefaultEnv
    , getLogger :: Logger
    , getStatic :: Static -- ^ Settings for static file serving.
    , connPool :: Database.Persist.Base.PersistConfigPool Settings.PersistConfig -- ^ Database connection pool.
    }

-- This is where we define all of the routes in our application. For a full
-- explanation of the syntax, please see:
-- http://docs.yesodweb.com/book/web-routes-quasi/
--
-- This function does three things:
--
-- * Creates the route datatype BISocieRoute. Every valid URL in your
--   application can be represented as a value of this type.
-- * Creates the associated type:
--       type instance Route BISocie = BISocieRoute
-- * Creates the value resourcesBISocie which contains information on the
--   resources declared below. This is used in Application.hs by the call to
--   mkYesodDispatch
--
-- What this function does *not* do is create a YesodSite instance for
-- BISocie. Creating that instance requires all of the handler functions
-- for our application to be in scope. However, the handler functions
-- usually require access to the BISocieRoute datatype. Therefore, we
-- split these actions into two functions and place them in separate files.
mkYesodData "BISocie" $(parseRoutesFile "config/routes")

-- S3はアクセス制限する
-- S3は基本公開ベースなので制限をするURIを提供してそこからgetFileRを呼ぶ

-- Please see the documentation for the Yesod typeclass. There are a number
-- of settings which can be configured by overriding methods here.
instance Yesod BISocie where
    approot = appRoot . settings
    
    encryptKey _ = fmap Just $ getKey "config/client_session_key.aes"
    
    defaultLayout widget = do
      mu <- maybeAuth
      mmsg <- getMessage
      y <- getYesod
      (title, parents) <- breadcrumbs
      current <- getCurrentRoute
      tm <- getRouteToMaster
      let header = $(hamletFile "hamlet/header.hamlet")
          footer = $(hamletFile "hamlet/footer.hamlet")
      pc <- widgetToPageContent $ do
        widget
        addScriptEither $ urlJqueryJs y
        addScriptEither $ urlJqueryUiJs y
        addStylesheetEither $ urlJqueryUiCss y
        addScriptEither $ Left $ StaticR plugins_upload_jquery_upload_1_0_2_js
        addScriptEither $ Left $ StaticR plugins_bubbleup_jquery_bubbleup_js
        addScriptEither $ Left $ StaticR plugins_exinplaceeditor_jquery_exinplaceeditor_0_1_3_js
        addStylesheetEither $ Left $ StaticR plugins_exinplaceeditor_exinplaceeditor_css
        addScriptEither $ Left $ StaticR plugins_watermark_jquery_watermark_js
        addScriptEither $ Left $ StaticR plugins_ajaxzip2_ajaxzip2_js
        addScriptEither $ Left $ StaticR plugins_selection_jquery_selection_js
        addCassius $(cassiusFile "cassius/default-layout.cassius")
        addJulius $(juliusFile "julius/default-layout.julius")
      hamletToRepHtml $(hamletFile "hamlet/default-layout.hamlet")

    -- This is done to provide an optimization for serving static files from
    -- a separate domain. Please see the staticroot setting in Settings.hs
    urlRenderOverride y (StaticR s) =
        Just $ uncurry (joinPath y (Settings.staticroot $ settings y)) $ renderRoute s
    urlRenderOverride _ _ = Nothing

    -- The page to be redirected to when authentication is required.
    authRoute _ = Just $ AuthR LoginR

    -- This function creates static content files in the static folder
    -- and names them based on a hash of their content. This allows
    -- expiration dates to be set far in the future without worry of
    -- users receiving stale content.
    addStaticContent = addStaticContentExternal minifym base64md5 Settings.staticdir (StaticR . flip StaticRoute [])
    
    -- Enable Javascript async loading
--    yepnopeJs _ = Just $ Right $ StaticR js_modernizr_js

instance YesodBreadcrumbs BISocie where
  breadcrumb RootR = return ("", Nothing)
  breadcrumb HomeR{} = return ("ホーム", Nothing)
  breadcrumb HumanNetworkR = do
    (uid, _) <- requireAuth
    return ("ヒューマンネットワーク", Just $ HomeR uid)
  breadcrumb (ScheduleR y m) = do
    (uid, _) <- requireAuth
    return (showText y +++ "年" +++ showText m +++ "月のスケジュール", Just $ HomeR uid)
  breadcrumb NewProjectR = do
    (uid, _) <- requireAuth
    return ("新規プロジェクト作成", Just $ HomeR uid)
  breadcrumb (ProjectR pid) = return ("設定", Just $ IssueListR pid)
    
  breadcrumb ParticipantsListR{} = return ("", Nothing)
  breadcrumb ParticipantsR{} = return ("", Nothing)
  breadcrumb UserListR = return ("ユーザ一覧", Nothing)
  
  breadcrumb CrossSearchR = do
    (uid, _) <- requireAuth
    return ("クロスサーチ", Just $ HomeR uid)
  breadcrumb (IssueListR pid) = do 
    (uid, _) <- requireAuth
{'fraction_non_alphanumeric': 0.08285223903885161, 'fraction_numerical': 0.0021844281479169916, 'mean_word_length': 3.444868238557559, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2792463', 'n_tokens_mistral': 3974, 'n_tokens_neox': 3885, 'n_words': 1227}