Advertisement
arakuta

Forum System MySQL / SAMP

Apr 24th, 2015
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 5.90 KB | None | 0 0
  1. #include <a_samp>
  2. #include <a_mysql>
  3. #include <zcmd>
  4.  
  5. #define DIALOG_FORUM_CATEGORY   666
  6. #define DIALOG_FORUM_THREAD     667
  7. #define DIALOG_FORUM_POST       668
  8. #define DIALOG_FORUM_SUBMIT     669
  9.  
  10. #undef MAX_PLAYERS
  11. #define MAX_PLAYERS 25
  12.  
  13. new handle;
  14. new SQL[500];
  15. new STR[144];
  16.  
  17. new f_Categorias[1000];
  18. new f_Threads[1000];
  19. new f_Post[2000];
  20.  
  21. new f_categoriasID[10];
  22. new f_threadsID[MAX_PLAYERS][10];
  23.  
  24. new p_Thread[MAX_PLAYERS];
  25. new p_Categoria[MAX_PLAYERS];
  26. new p_Post[MAX_PLAYERS];
  27.  
  28. forward CarregarCategorias();
  29. forward AbreTopicos(playerid,catid);
  30. forward AbrePost(playerid,postid);
  31. forward OnPlayerPost(playerid,threadid);
  32.  
  33. new TemForum;
  34.  
  35. public OnFilterScriptInit()
  36. {
  37.    
  38.     handle = mysql_connect("127.0.0.1","root","foruminterno","ahuehue123");
  39.    
  40.     printf("handle: %d",handle);
  41.    
  42.     CarregarForum();
  43.  
  44.     return 1;
  45. }
  46.  
  47. public OnFilterScriptExit()
  48. {
  49.     mysql_close(handle);
  50. }
  51.  
  52. CMD:forumteste(playerid)
  53. {
  54.     if(!TemForum)
  55.         return 1;
  56.    
  57.     ShowPlayerDialog(playerid,DIALOG_FORUM_CATEGORY,DIALOG_STYLE_LIST,"Categorias",f_Categorias,"Selecionar","Sair");
  58.    
  59.     p_Categoria[playerid] = 0;
  60.     p_Thread[playerid] = 0;
  61.    
  62.     return 1;
  63. }
  64.  
  65. public OnDialogResponse(playerid,dialogid,response,listitem,inputtext[])
  66. {
  67.     switch(dialogid)
  68.     {
  69.         case DIALOG_FORUM_THREAD:
  70.         {
  71.             if(response)
  72.             {
  73.                 format(SQL,200,"SELECT * FROM posts WHERE thread = %d LIMIT 10",f_threadsID[playerid][listitem]);
  74.                 mysql_function_query(handle,SQL,true,"AbrePost","dd",playerid,f_threadsID[playerid][listitem]);
  75.                
  76.                 p_Thread[playerid] = f_threadsID[playerid][listitem];
  77.                
  78.                 printf("Abrindo tópico %d",p_Thread[playerid]);
  79.                
  80.             }
  81.             else
  82.             {
  83.                 cmd_forumteste(playerid);
  84.             }
  85.         }
  86.         case DIALOG_FORUM_CATEGORY:
  87.         {
  88.             if(response)
  89.             {
  90.                 format(SQL,200,"SELECT * FROM threads WHERE category = %d LIMIT 10",f_categoriasID[listitem]);
  91.                 mysql_function_query(handle,SQL,true,"AbreTopicos","dd",playerid,f_categoriasID[listitem]);
  92.                
  93.                 p_Categoria[playerid] = f_categoriasID[listitem];
  94.            
  95.             }
  96.         }
  97.         case DIALOG_FORUM_POST:
  98.         {
  99.             if(response)
  100.             {
  101.                 ShowPlayerDialog(playerid,DIALOG_FORUM_SUBMIT,DIALOG_STYLE_INPUT,"Postar","Digite o texto que você quer acrescentar ao tópico","Postar","Voltar");
  102.             }
  103.             else
  104.             {
  105.                 format(SQL,200,"SELECT * FROM threads WHERE category = %d LIMIT 10",p_Categoria[playerid]);
  106.                 mysql_function_query(handle,SQL,true,"AbreTopicos","dd",playerid,p_Categoria[playerid]);
  107.             }
  108.         }
  109.         case DIALOG_FORUM_SUBMIT:
  110.         {
  111.             if(response)
  112.             {
  113.                 new name[24];
  114.                 GetPlayerName(playerid,name,24);
  115.                
  116.                 format(SQL,500,"INSERT INTO posts (username,content,thread,date) VALUES ('%s','%s','%d','%s');",name,inputtext,p_Thread[playerid],FormatDate());
  117.                 mysql_function_query(handle,SQL,false,"OnPlayerPost","dd",playerid,p_Thread[playerid]);
  118.                
  119.                 printf("Postando %s",inputtext);
  120.             }
  121.             else
  122.             {
  123.                 format(SQL,200,"SELECT * FROM posts WHERE thread = %d LIMIT 10",p_Thread[playerid]);
  124.                 mysql_function_query(handle,SQL,true,"AbrePost","dd",playerid,p_Thread[playerid]);
  125.                
  126.                 printf("Abrindo tópico %d",p_Thread[playerid]);
  127.             }
  128.         }
  129.     }
  130.     return 1;
  131. }
  132.  
  133. stock CarregarForum()
  134. {
  135.     SQL = "SELECT * FROM categorias WHERE 1";
  136.     mysql_function_query(handle,SQL,true,"CarregarCategorias","");
  137. }
  138.  
  139. stock FormatDate()
  140. {
  141.     new m, d, y, h, M, date[25];
  142.    
  143.     getdate(y,m,d);
  144.     gettime(h,M);
  145.    
  146.     format(date,25,"%02d/%02d/%02d as %02d:%02d",m,d,y,h,M);
  147.     return date;
  148. }
  149.  
  150. public CarregarCategorias()
  151. {
  152.     new rows,fields;
  153.     cache_get_data(rows,fields,handle);
  154.    
  155.     if(rows)
  156.     {
  157.         for(new i = 0; i < rows; ++i)
  158.         {
  159.             cache_get_field_content(i,"id",SQL,handle);
  160.             f_categoriasID[i] = strval(SQL);
  161.            
  162.             cache_get_field_content(i,"nome",SQL,handle);
  163.             format(f_Categorias,1000,"%s%s%s",f_Categorias,i ? ("\n") : (""),SQL);
  164.            
  165.         }
  166.        
  167.         printf("%d categorias carregadas.",rows);
  168.        
  169.         TemForum = 1;
  170.     }
  171.     else
  172.         print("Não existem categorias no forum. Será desabilitado.");
  173.    
  174.     return 1;
  175. }
  176.  
  177. public AbreTopicos(playerid,catid)
  178. {
  179.    
  180.     new rows,fields;
  181.     cache_get_data(rows,fields,handle);
  182.    
  183.     if(rows)
  184.     {
  185.        
  186.         strdel(f_Threads,0,1000);
  187.        
  188.         for(new i = 0; i < rows; ++i)
  189.         {
  190.             cache_get_field_content(i,"id",SQL,handle);
  191.             f_threadsID[playerid][i] = strval(SQL);
  192.            
  193.             cache_get_field_content(i,"title",SQL,handle);
  194.             format(f_Threads,1000,"%s%s%s",f_Threads,i ? ("\n") : (""),SQL);
  195.         }
  196.  
  197.        
  198.         ShowPlayerDialog(playerid,DIALOG_FORUM_THREAD,DIALOG_STYLE_LIST,"Tópicos",f_Threads,"Selecionar","Voltar");
  199.        
  200.         printf("%d topicos carregados.",rows);
  201.        
  202.     }
  203.     else
  204.     {
  205.         SendClientMessage(playerid,-1,"[FORUM] Não existem tópicos nessa categoria.");
  206.         cmd_forumteste(playerid);
  207.     }
  208.     return 1;
  209. }
  210.  
  211. public AbrePost(playerid,postid)
  212. {
  213.    
  214.     new rows,fields;
  215.     cache_get_data(rows,fields,handle);
  216.    
  217.     if(rows)
  218.     {
  219.        
  220.         strdel(f_Post,0,2000);
  221.         new by[25];
  222.        
  223.         for(new i = 0; i < rows; ++i)
  224.         {
  225.             cache_get_field_content(i,"id",SQL,handle);
  226.             f_threadsID[playerid][i] = strval(SQL);
  227.            
  228.             cache_get_field_content(i,"username",by,handle);
  229.  
  230.             cache_get_field_content(i,"content",SQL,handle);
  231.            
  232.             // Quebra de linha a cada 50 caracteres
  233.            
  234.             new size = strlen(SQL);
  235.            
  236.             if(size > 100)
  237.                 strins(SQL,"\n",100);
  238.             if(size > 50)
  239.                 strins(SQL,"\n",50);
  240.            
  241.             format(f_Post,1000,"%s%s{FF2222}Postado por %s\n{FFFFFF}%s",f_Post,i ? ("\n\n") : (""),by,SQL);
  242.  
  243.         }
  244.  
  245.         ShowPlayerDialog(playerid,DIALOG_FORUM_POST,DIALOG_STYLE_MSGBOX,"Post",f_Post,"Responder","Voltar");
  246.        
  247.     }
  248.     else
  249.     {
  250.         SendClientMessage(playerid,-1,"[FORUM] O tópico selecionado não existe.");
  251.     }
  252.     return 1;
  253. }
  254.  
  255. public OnPlayerPost(playerid,threadid)
  256. {
  257.     format(SQL,200,"SELECT * FROM posts WHERE thread = %d LIMIT 10",threadid);
  258.     mysql_function_query(handle,SQL,true,"AbrePost","dd",playerid,threadid);
  259.                
  260.     p_Thread[playerid] = threadid;
  261.                
  262.     printf("Abrindo tópico %d após postagem %d.",p_Thread[playerid],cache_insert_id(handle));
  263.  
  264.     return 1;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement