discussions and comments add and remove
This commit is contained in:
parent
3eeaa208b4
commit
fdc79f2796
7
Readme
7
Readme
|
@ -8,9 +8,10 @@ add this application to your forum, enable it. the following URLs/methods will n
|
||||||
|
|
||||||
/api/discussionapi (GET: id, limit, offset)
|
/api/discussionapi (GET: id, limit, offset)
|
||||||
|
|
||||||
/api/discussionapi/add (POST:CategoryID, Body, Name, TransientKey, UserID)
|
/api/discussionapi/add (POST: CategoryID, Body, Name, TransientKey, UserID)
|
||||||
/api/discussionapi/remove (POST:CategoryID, DiscussionID, TransientKey, UserID)
|
/api/discussionapi/remove (POST: DiscussionID, CategoryID, TransientKey, UserID)
|
||||||
|
|
||||||
/api/commentapi/add (POST: DiscussionID, CategoryID, Body, Name, TransientKey)
|
/api/commentapi/add (POST: DiscussionID, CategoryID, Body, TransientKey, UserID)
|
||||||
|
/api/commentapi/remove (POST: CommentID, CategoryID, TransientKey, UserID)
|
||||||
|
|
||||||
/api/sessionapi
|
/api/sessionapi
|
||||||
|
|
|
@ -2,22 +2,30 @@
|
||||||
|
|
||||||
class CommentAPIController extends APIController
|
class CommentAPIController extends APIController
|
||||||
{
|
{
|
||||||
public $Uses = array('Form', 'Database', 'CategoryModel', 'DiscussionModel', 'CommentModel');
|
public $Uses = array('Form', 'Database', 'CategoryModel', 'DiscussionModel', 'CommentModel');
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
if (isset($_POST['UserID'])){
|
||||||
|
Gdn::Session()->Start($_POST['UserID'], TRUE, TRUE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function Index()
|
public function Index()
|
||||||
{
|
{
|
||||||
$this->Render();
|
$this->Render();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function Add()
|
public function Add()
|
||||||
{
|
{
|
||||||
$Session = Gdn::Session();
|
$Session = Gdn::Session();
|
||||||
$Errors = array();
|
$Errors = array();
|
||||||
|
|
||||||
// Set the model on the form.
|
// Set the model on the form.
|
||||||
$this->Form->SetModel($this->CommentModel);
|
$this->Form->SetModel($this->CommentModel);
|
||||||
|
|
||||||
if($this->Form->AuthenticatedPostBack() === TRUE)
|
if($this->Form->AuthenticatedPostBack() === TRUE)
|
||||||
{
|
{
|
||||||
$FormValues = $this->Form->FormValues();
|
$FormValues = $this->Form->FormValues();
|
||||||
|
|
||||||
|
@ -40,6 +48,40 @@ class CommentAPIController extends APIController
|
||||||
$this->Render();
|
$this->Render();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a comment.
|
||||||
|
* @param int The category id to remove the comment to.
|
||||||
|
*/
|
||||||
|
public function Remove()
|
||||||
|
{
|
||||||
|
$Session = Gdn::Session();
|
||||||
|
$Errors = array();
|
||||||
|
|
||||||
|
// Set the model on the form.
|
||||||
|
$this->Form->SetModel($this->CommentModel);
|
||||||
|
|
||||||
|
if($this->Form->AuthenticatedPostBack() === TRUE)
|
||||||
|
{
|
||||||
|
$FormValues = $this->Form->FormValues();
|
||||||
|
|
||||||
|
// Check category permissions
|
||||||
|
if(!$Session->CheckPermission('Vanilla.Discussions.Add', $FormValues['CategoryID']))
|
||||||
|
$Errors[] = 'You do not have permission to start discussions in this category';
|
||||||
|
else
|
||||||
|
$CommentID = $this->CommentModel->Delete($FormValues['CommentID']);
|
||||||
|
$this->SetJSON("removed", $CommentID);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
$Errors[] = 'You do not have credentials to post as this user';
|
||||||
|
|
||||||
|
// Return the form errors
|
||||||
|
if(count($Errors) > 0)
|
||||||
|
$this->SetJSON("Errors", $Errors);
|
||||||
|
|
||||||
|
$this->Render();
|
||||||
|
Gdn::Session()->End();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|
|
@ -0,0 +1,160 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class MyClass
|
||||||
|
{
|
||||||
|
|
||||||
|
public $Key = false;
|
||||||
|
|
||||||
|
public $TransientKEY = false;
|
||||||
|
public $UserID = false;
|
||||||
|
public $URL = "example.com/api";
|
||||||
|
|
||||||
|
public function __construct($user=false,$pass=false)
|
||||||
|
{
|
||||||
|
|
||||||
|
if ($user && $pass){
|
||||||
|
return $this->login($user,$pass);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getTransientKey() {
|
||||||
|
return $this->TransientKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getUserID(){
|
||||||
|
return $this->UserID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function curl_post($fields = array(), $url =''){
|
||||||
|
$fields_string = '';
|
||||||
|
|
||||||
|
//url-ify the data for the POST
|
||||||
|
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
|
||||||
|
rtrim($fields_string, '&');
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch,CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch,CURLOPT_POST, count($fields));
|
||||||
|
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
$result = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function curl_get($url=''){
|
||||||
|
|
||||||
|
$ch = curl_init();
|
||||||
|
curl_setopt($ch, CURLOPT_URL, $url);
|
||||||
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
||||||
|
$output = curl_exec($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
return $output;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Login function that retrieves TransientKey
|
||||||
|
*
|
||||||
|
* @param (string) (user) username
|
||||||
|
* @param (string) (pass) password
|
||||||
|
* @return (TransientKey)
|
||||||
|
*/
|
||||||
|
public function login($user='',$pass='') {
|
||||||
|
|
||||||
|
|
||||||
|
$url = $this->URL."/sessionapi/login?user=".$user."&pass=".$pass;
|
||||||
|
|
||||||
|
$json = json_decode($this->curl_get($url));
|
||||||
|
|
||||||
|
$this->TransientKey = $json->user->TransientKey;
|
||||||
|
$this->UserID = $json->user->UserID;
|
||||||
|
|
||||||
|
$this->Key = $this->UserID."-".$this->TransientKey;
|
||||||
|
|
||||||
|
return $json->user;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
DISCUSSIONS
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function addDiscussion($CategoryID = false,$Name = false,$Body = false){
|
||||||
|
|
||||||
|
|
||||||
|
//set POST variables
|
||||||
|
$url = $this->URL."/discussionapi/add";
|
||||||
|
|
||||||
|
$fields = array(
|
||||||
|
'TransientKey' => $this->TransientKey,
|
||||||
|
'UserID' => $this->UserID,
|
||||||
|
'CategoryID' => urlencode($CategoryID),
|
||||||
|
'Body' => urlencode($Body),
|
||||||
|
'Name' => urlencode($Name),
|
||||||
|
'Type' => 'Question'
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->curl_post($fields,$url);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeDiscussion($CategoryID = false,$DiscussionID = false){
|
||||||
|
|
||||||
|
//set POST variables
|
||||||
|
$url = $this->URL."/discussionapi/remove";
|
||||||
|
|
||||||
|
$fields = array(
|
||||||
|
'TransientKey' => $this->TransientKey,
|
||||||
|
'UserID' => $this->UserID,
|
||||||
|
'CategoryID' => urlencode($CategoryID),
|
||||||
|
'DiscussionID' => urlencode($DiscussionID)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->curl_post($fields,$url);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
COMMENTS
|
||||||
|
*/
|
||||||
|
|
||||||
|
public function addComment($DiscussionID = false,$CategoryID = false,$Body = false){
|
||||||
|
|
||||||
|
//set POST variables
|
||||||
|
$url = $this->URL."/commentapi/add";
|
||||||
|
|
||||||
|
$fields = array(
|
||||||
|
'TransientKey' => $this->TransientKey,
|
||||||
|
'UserID' => $this->UserID,
|
||||||
|
'CategoryID' => urlencode($CategoryID),
|
||||||
|
'DiscussionID' => urlencode($DiscussionID),
|
||||||
|
'Body' => urlencode($Body)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->curl_post($fields,$url);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function removeComment($CategoryID = false,$CommentID = false){
|
||||||
|
|
||||||
|
//set POST variables
|
||||||
|
$url = $this->URL."/commentapi/remove";
|
||||||
|
|
||||||
|
$fields = array(
|
||||||
|
'TransientKey' => $this->TransientKey,
|
||||||
|
'UserID' => $this->UserID,
|
||||||
|
'CategoryID' => urlencode($CategoryID),
|
||||||
|
'CommentID' => urlencode($CommentID)
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->curl_post($fields,$url);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
|
@ -0,0 +1,53 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
include('qalib.php');
|
||||||
|
|
||||||
|
echo "Testing qalib<br>";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
$api = new MyClass('admin','pass');
|
||||||
|
|
||||||
|
echo "<pre><br>";
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
//example login
|
||||||
|
|
||||||
|
//print_r($api->login('admin','pass'));
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
//example add and remove discussion
|
||||||
|
|
||||||
|
$json = $api->addDiscussion(1,'esto es una prueba','aquí viene el texto de la pregunta');
|
||||||
|
echo $json;
|
||||||
|
$array = json_decode($json);
|
||||||
|
|
||||||
|
echo "<br>";
|
||||||
|
$json = $api->removeDiscussion(1,$array->DiscussionID);
|
||||||
|
echo $json;
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
|
||||||
|
//example add and remove discussion
|
||||||
|
|
||||||
|
$json = $api->addComment(54,1,'comentario de pruebas');
|
||||||
|
echo $json;
|
||||||
|
$array = json_decode($json);
|
||||||
|
|
||||||
|
echo "<br>";
|
||||||
|
$json = $api->removeComment(1,$array->CommentID);
|
||||||
|
echo $json;
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
?>
|
Reference in New Issue