/*******************************************\
| * Ce programme est sous liscence GNU GPL  * |
| * This software is under GNU/GPL licence  * |
| * * * * * * * * * * * * * * * * * * * * * * |
| * http://www.gnu.org/copyleft/gpl.html    * |
 \*******************************************/

/* Cr er par Laurent Coustet <ed@zehome.com>
 * http://ed.zehome.com/                    
 * Made by Laurent Coustet <ed@zehome.com>
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql/mysql.h>
#include "mysql.h"
#include "debug.h"
#include "news.h"

/* vim: set shiftwidth=2 tabstop=2
 */

void GetNews ()
{
  MYSQL mysql;
  MYSQL_ROW row;
  MYSQL_RES *result;
  MYSQL_FIELD *field;
  ST_NEWS *news;
  unsigned int num_fields;
  unsigned int i,j=-1;
  unsigned long *lengths;
  connect(&mysql);
  query(&mysql,"SELECT id,titre,texte,DATE_FORMAT(date,'%W %%e %M %Y') AS formated_date FROM site_news ORDER by date desc",NULL);
  if ((result = mysql_store_result(&mysql))==NULL)
    {
      printf("Impossible d'executer une requete SQL!\n");
      return;
    }
  num_fields = mysql_num_fields(result);
  news = malloc(sizeof(ST_NEWS)*1000);
  field = mysql_fetch_field(result);

  while((row = mysql_fetch_row(result)))
    {
      lengths = mysql_fetch_lengths(result);
      for(i = 0; i < num_fields && i < 999; i++)
  {
    if (strcmp(field[i].name,"id")==0)
      {
        //j = ;
        news[j++].id = atoi(row[i]);
        //printf("[%u][id: %u]\n",j,atoi(row[i]));
        //printf("[%s: %s] ",field[i].name,row[i]);
      }
    if (strcmp(field[i].name,"titre")==0)
      {
        news[j].titre = calloc(strlen(row[i]) + 1, 1);
        strcpy(news[j].titre, row[i]);
        //printf("[%u][Titre: %s]\n",j,row[i]);
      }
    if (strcmp(field[i].name,"texte")==0)
      {
        news[j].texte = calloc(strlen(row[i]) + 1, 1);
        strcpy(news[j].texte, row[i]);
        //printf("[%u][Texte: %s]\n",j,row[i]);
      }
    if (strcmp(field[i].name,"formated_date")==0)
      {
        news[j].date = calloc(strlen(row[i]) + 1, 1);
        strcpy(news[j].date, row[i]);
        //printf("[%u][Date: %s]\n",j, news[j].date);
      }
  }
    } // while
  /* Je l'ai commentée, parceque ca génerais un peu de poubelle
   * en fin de page, je ne sais pas encore pourquoi.
   */

  print_news_head();

  for(i=0;i< mysql_num_rows(result);i++)
    {
      //printf("",i,news[i].id);
      //printf("ST_NEWS[%u][date] = %s | ",i,news[i].date);
      //printf("ST_NEWS[%u][texte] = %s | ",i,news[i].texte);
      printf("<h1 style=\"text-align: left; font-size: 1em;\">%s :%s</h1>\n<br />\n%s",news[i].date,news[i].titre,news[i].texte);
      print_news_foot();
      free(news[i].date);
      free(news[i].titre);
      free(news[i].texte);
    }
  free(news);
  mysql_free_result(result);
  disconnect(&mysql); // Segfault index.cgi
  return;
}

void print_news_head()
{
  printf("<h1>Les Nouvelles (en MySQL!)</h1>\n\n");
}

void print_news_foot()
{
 // printf ("<hr />\n");
}

void AddNews(char *titre, char *texte)
{
   MYSQL mysql;
   connect(&mysql);
   query(&mysql,"INSERT INTO site_news (titre,texte,date) VALUES (\"%s\",\"%s\",NOW())",titre,texte);
   disconnect(&mysql);
}