How do I create an add-on that allows a user to click a link and call php code?

Before I upload this add-on to Resources, here is the Post.php files with a few more comments. If anyone sees where the comments might be improved, please let me know.

PHP:
<?php

class Andy_ChangeDate_ControllerPublic_Post extends XFCP_Andy_ChangeDate_ControllerPublic_Post
{
   public function actionChangedate()
   {
     // if you are not a super admin, return
     if (!XenForo_Visitor::getInstance()->isSuperAdmin())
     {
       return;
     }
        
     // get post_id from route, example /forums/posts/1859225/changedate
     $postId = $this->_input->filterSingle('post_id', XenForo_Input::UINT);
    
     // get all post data variables
     $post = $this->getModelFromCache('XenForo_Model_Post')->getPostById($postId);
    
     // format date and time for use in template input  
     $userLanguage = XenForo_Visitor::getInstance()->getLanguage();
     $timeFormat = $userLanguage['date_format'] . ' ' . $userLanguage['time_format'];
     $post['formatted_date'] = date($timeFormat, $post['post_date'] + XenForo_Locale::getTimeZoneOffset());
    
     // display andy_changedate template
     $viewParams = array('post' => $post);
     return $this->responseView('Andy_DateChange_ViewPublic_Post','andy_changedate',$viewParams);
   }
  
   public function actionChangedatesave()
   {
     // make sure data comes from $_POST
     $this->_assertPostOnly();
    
     // if you are not a super admin, you will get an error
     if (!XenForo_Visitor::getInstance()->isSuperAdmin())
     {
       return;
     }
    
     // get post_id 
     $post['post_id'] = $this->_input->filterSingle('post_id', XenForo_Input::UINT);
    
     // get new_post_date from input    
     $newPostDate = $this->_input->filterSingle('new_post_date', XenForo_Input::STRING);
    
     // convert $newPostDate to unix timestamp
     $dateline = strtotime($newPostDate ) - XenForo_Locale::getTimeZoneOffset();    
    
     // make sure $dateline has a value
     if (!$dateline)
     {
       return $this->responseError(new XenForo_Phrase('incorrect_date_format_entered'));
     }
    
     //########################################
     // start database operations
     //########################################
    
     // update xf_post table (post_date)
     $dw = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
     $dw->setExistingData($post['post_id']);
     $dw->set('post_date', $dateline);
     $dw->save();
    
     // update xf_post table (position) for each post in the thread
     // also updates other tables with first post and last post information
     $threadId = $dw->get('thread_id');
     $dw = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
     $dw->setExistingData($threadId);
     $dw->rebuildDiscussion();
     $dw->save();
    
     //########################################
     // return with response redirect
     //########################################
        
     return $this->responseRedirect(
     XenForo_ControllerResponse_Redirect::SUCCESS,
     XenForo_Link::buildPublicLink('posts', $post),'Post Date Changed');      
   }
}

?>
 
Last edited:
Hello Andy,

for a litle Addon, i must search an replace 2 lines in a existing php file in a library sub directory - how can i do this within my addon?

The only addon i every made is a simple template addon - so i need a litle bit help. ;)

regards
 
Hello Andy,

for a litle Addon, i must search an replace 2 lines in a existing php file in a library sub directory - how can i do this within my addon?

The only addon i every made is a simple template addon - so i need a litle bit help. ;)

regards

Hi otto,

I suggest you create a new thread and explain what the add-on is going to do and explain what steps in creating this add-on you have already done.
 
Hi I'm learning and I have a question.

@Marcus
In this case why not just extend the edit action? I'm shooting in the dark now: maybe because in XenForo_DataWriter_DiscussionMessage_Post we can't set the Post's Date?
 
Last edited:
Back
Top Bottom