<commit_before><?php

require_once('lib/limonade');

# Setting global options of our application
function configure()
{
  $localhost = preg_match('/^localhost(\:\d+)?/', $_SERVER['HTTP_HOST']);
  $env =  $localhost ? ENV_DEVELOPMENT : ENV_PRODUCTION;
	$dsn = $env == ENV_PRODUCTION ? 'sqlite:db/prod.db' : 'sqlite:db/dev.db';
	$db = new PDO($slcted_db);
	$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  option('env', $env);
	option('db_conn', $db);
	setlocale(LC_TIME, "fr_FR");
}


# will be executed bfore each controller function
function before()
{
  html_layout('layouts/default.html.php');
}

# Defining routes and controllers
# ----------------------------------------------------------------------------
# RESTFul map:
#
# GET    /posts             blog_posts_index
# GET    /posts/:id         blog_posts_show 
# GET    /posts/new         blog_posts_new 
# POST   /posts             blog_posts_create
# GET    /posts/:id/edit    blog_posts_edit 
# PUT    /posts/:id         blog_posts_update
# DELETE /posts/:id         blog_posts_destroy
# GET    /                  blog_posts_home (redirect to /posts)

# matches GET /
dispatch('/', 'blog_posts_home');
  function blog_posts_home()
  {
    redirect(url_for('posts'));
  }

# matches GET /posts  
dispatch('/posts', 'blog_posts_index')
  function blog_posts_index()
  {
    $posts = post_find_all();
    set('posts', $posts);
    html('posts/index.html.php');
  }
  
# matches GET /posts/1  
dispatch('/posts/:id', 'blog_posts_show')
  function blog_posts_show()
  { 
    if( $post = post_find(params('id')) )
    {
      set('post', $post);
      html('posts/show.html.php');
    }
    else
    {
      halt(NOT_FOUND, "This post doesn't exists");
    }
    
  }
  
# matches GET /posts/new  
dispatch('/posts/new', 'blog_posts_new')
  function blog_posts_new()
  { 
    html('posts/new.html.php');
  }
  
# matches POST /posts
dispatch_post('/posts', 'blog_posts_create')
  function blog_posts_create()
  { 
    if($post_id = post_create($_POST['post']))
    {
      redirect(url_for('posts', $post_id));
    }
    else
    {
      halt(SERVER_ERROR, "AN error occurd while trying to create a new post")
    }
  }
  
# matches GET /posts/1/edit  
dispatch('/posts/:id/edit', 'blog_posts_edit')
  function blog_posts_edit()
  {
    if($post = post_find(params('id')))
    {
      html('posts/edit.html.php');
    }
    else<commit_msg>In blog_posts_new, passing an empty post<commit_after><?php

require_once('lib/limonade');

# Setting global options of our application
function configure()
{
  $localhost = preg_match('/^localhost(\:\d+)?/', $_SERVER['HTTP_HOST']);
  $env =  $localhost ? ENV_DEVELOPMENT : ENV_PRODUCTION;
	$dsn = $env == ENV_PRODUCTION ? 'sqlite:db/prod.db' : 'sqlite:db/dev.db';
	$db = new PDO($slcted_db);
	$db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING );
  option('env', $env);
	option('db_conn', $db);
	setlocale(LC_TIME, "fr_FR");
}


# will be executed bfore each controller function
function before()
{
  html_layout('layouts/default.html.php');
}

# Defining routes and controllers
# ----------------------------------------------------------------------------
# RESTFul map:
#
# GET    /posts             blog_posts_index
# GET    /posts/:id         blog_posts_show 
# GET    /posts/new         blog_posts_new 
# POST   /posts             blog_posts_create
# GET    /posts/:id/edit    blog_posts_edit 
# PUT    /posts/:id         blog_posts_update
# DELETE /posts/:id         blog_posts_destroy
# GET    /                  blog_posts_home (redirect to /posts)

# matches GET /
dispatch('/', 'blog_posts_home');
  function blog_posts_home()
  {
    redirect(url_for('posts'));
  }

# matches GET /posts  
dispatch('/posts', 'blog_posts_index')
  function blog_posts_index()
  {
    $posts = post_find_all();
    set('posts', $posts);
    html('posts/index.html.php');
  }
  
# matches GET /posts/1  
dispatch('/posts/:id', 'blog_posts_show')
  function blog_posts_show()
  { 
    if( $post = post_find(params('id')) )
    {
      set('post', $post);
      html('posts/show.html.php');
    }
    else
    {
      halt(NOT_FOUND, "This post doesn't exists");
    }
    
  }
  
# matches GET /posts/new  
dispatch('/posts/new', 'blog_posts_new')
  function blog_posts_new()
  { 
    # passing an empty post to the view
    set('post', array('id'=>'', 'title'=>'Your title here...', 'body'=>'Your content...'));
    html('posts/new.html.php');
  }
  
# matches POST /posts
dispatch_post('/posts', 'blog_posts_create')
  function blog_posts_create()
  { 
    if($post_id = post_create($_POST['post']))
    {
      redirect(url_for('posts', $post_id));
    }
    else
    {
      halt(SERVER_ERROR, "AN error occurd while trying to create a new post")
    }
  }
  
# matches GET /posts/1/edit  
dispatch('/posts/:id/edit', 'blog_posts_edit')
  function blog_posts_edit()
  {
    if($post = post_find(params('id')))
    {
      set('post', $post);
      html('posts/edit.html.php');
    }
    else
{'fraction_non_alphanumeric': 0.18314359896145396, 'fraction_numerical': 0.000798881565807869, 'mean_word_length': 2.695940959409594, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '2420243', 'n_tokens_mistral': 1714, 'n_tokens_neox': 1706, 'n_words': 332}