News:

Bored?  Looking to kill some time?  Want to chat with other SMF users?  Join us in IRC chat or Discord

Main Menu

Publish all the dafts via a cronjob

Started by cuongvttt, November 23, 2024, 03:12:49 AM

Previous topic - Next topic

cuongvttt

Hello everyone,

I would like to publish all the drafts via a cronjob, please take a look and help me to correct the following script. Thank you so much.

<?php

// Database connection details
$host = 'localhost';
$db = 'your_smf_database';
$user = 'your_db_user';
$password = 'your_db_password';

// Create connection
$mysqli = new mysqli($host, $user, $password, $db);

// Check connection
if ($mysqli->connect_error) {
   die(
"Connection failed: " . $mysqli->connect_error);
}

// Query to select drafts
$sql = "SELECT * FROM smf_user_drafts WHERE is_published = 0"; // Assuming 'is_published' = 0 means the draft is unpublished
$result = $mysqli->query($sql);

if (
$result->num_rows > 0) {
   while (
$row = $result->fetch_assoc()) {
       
$id_draft = $row['id'];
       
$id_member = $row['id_member'];
       
$body = $row['body']; // Assuming the draft body is stored in the 'body' column

       // Publish the draft: update status to 'published' (or move content to a messages table)
       // Assuming there's a posts table and you move the draft there
       
$publish_sql = "INSERT INTO smf_messages (id_member, body) VALUES ('id_member', '$body')";
       if (
$mysqli->query($publish_sql) === TRUE) {
           
// After publishing, delete the draft from smf_user_drafts
           
$delete_sql = "DELETE FROM smf_user_drafts WHERE id = $id_draft";
           
$mysqli->query($delete_sql);
       }
   }
}

// Close connection
$mysqli->close();

?>

Aleksi "Lex" Kilpinen

Sounds to me like a scheduler would probably be more what you are after, SMFHacks has one available https://www.smfhacks.com/index.php?action=downloads;sa=view;id=172

Just my opinion, but drafts are drafts, they are not meant to be autopublished, and usually it's better not to try and use a tool for something it wasn't meant for.
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

cuongvttt

Hello Aleksi,

Thank you so much for your quick response but it doesn't meet my needs. By the way, I would like to share this code with you.

<?php
// Load SMF's SSI.php
require_once('SSI.php');

// Ensure the user is logged in
if (!$context['user']['is_logged'])
   die(
'You need to be logged in to post drafts.');

// Load the drafts
$request = $smcFunc['db_query']('', '
   SELECT id_draft, subject, body, id_board, id_topic
   FROM {db_prefix}user_drafts
   WHERE id_member = {int:current_member}'
,
   array(
       
'current_member' => $context['user']['id'],
   )
);

// Post each draft
while ($row = $smcFunc['db_fetch_assoc']($request))
{
   
// Prepare the post data
   
$msgOptions = array(
       
'subject' => $row['subject'],
       
'body' => $row['body'],
       
'id' => 0,
   );
   
$topicOptions = array(
       
'id' => $row['id_topic'],
       
'board' => $row['id_board'],
       
'mark_as_read' => true,
   );
   
$posterOptions = array(
       
'id' => $context['user']['id'],
   );

   
// Create the post
   
require_once($sourcedir . '/Subs-Post.php');
   
createPost($msgOptions, $topicOptions, $posterOptions);

   
// Delete the draft
   
$smcFunc['db_query']('', '
       DELETE FROM {db_prefix}user_drafts
       WHERE id_draft = {int:id_draft}'
,
       array(
           
'id_draft' => $row['id_draft'],
       )
   );
}

// Free the result
$smcFunc['db_free_result']($request);

echo
'All drafts have been posted.';
?>

Kindred

If someone auto published my draft, I would be pissed off. By definition,  drafts are not intended to be published by the author for whatever reason
Слaва
Украинi

Please do not PM, IM or Email me with support questions.  You will get better and faster responses in the support boards.  Thank you.

"Loki is not evil, although he is certainly not a force for good. Loki is... complicated."

mickjav

Drafts are posts members are working on, if a member saves a post as a draft then that implies that post is not ready to be posted.

Drafters are auto removed after xx days.

Glade I'm not a member on your forum as I can assure you I would be removing my account, that's if you haven't messed with that too.



cuongvttt

Hello,

Because there is no post/topic scheduling feature so I have to choose this way.

GL700Wing

Quote from: Kindred on November 23, 2024, 05:36:21 PMIf someone auto published my draft, I would be pissed off. By definition,  drafts are not intended to be published by the author for whatever reason
Absolutely agree!!

Also, a draft could be a WIP (ie, 'work in progress') and contain information that the author has no absolutely intention of posting either at all or in the final message.
Life doesn't have to be perfect to be wonderful ...

Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

Aleksi "Lex" Kilpinen

Quote from: cuongvttt on November 24, 2024, 12:09:16 AMBecause there is no post/topic scheduling feature so I have to choose this way.
Well, I did link you to one solution. You did say it doesn't meet your needs, but you never actually explained what those needs are. Perhaps if you shared that, someone could help you figure that out?
Slava
Ukraini!
"Before you allow people access to your forum, especially in an administrative position, you must be aware that that person can seriously damage your forum. Therefore, you should only allow people that you trust, implicitly, to have such access." -Douglas

How you can help SMF

Advertisement: