Browse Source

initial commit of addon

pull/1/head
eleith 10 years ago
commit
620257c260
  1. 27
      controllers/class.apicontroller.php
  2. 139
      controllers/class.categorycontroller.php
  3. 45
      controllers/class.commentcontroller.php
  4. 63
      controllers/class.discussioncontroller.php
  5. 21
      controllers/class.sessioncontroller.php
  6. 16
      design/README.txt
  7. 12
      js/ReadMe.txt
  8. 9
      locale/en-CA/definitions.php
  9. 0
      models/empty
  10. 15
      settings/about.php
  11. 3
      settings/configuration.php
  12. 22
      settings/hooks.php
  13. 48
      settings/structure.php

27
controllers/class.apicontroller.php

@ -0,0 +1,27 @@
<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2008, 2009 Vanilla Forums Inc.
This file is part of Garden.
Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
*/
class APIController extends Gdn_Controller {
public function __construct()
{
parent::__construct();
}
public function Initialize()
{
parent::Initialize();
$this->_DeliveryMethod = DELIVERY_METHOD_JSON;
//$this->SetHeader("Content-Type", "application/json; charset=utf-8");
$this->SetHeader("Content-Type", "text/plain; charset=utf-8");
$this->MasterView = 'json';
}
}

139
controllers/class.categorycontroller.php

@ -0,0 +1,139 @@
<?php if (!defined('APPLICATION')) exit();
class CategoryController extends APIController
{
public $Uses = array('Gdn_Format', 'Database', 'CategoryModel', 'DiscussionModel');
//TODO should allow for only one catgories to be looked at
public function Index()
{
$Session = Gdn::Session();
$categories = array();
$discussionsPerCategory = 4;
$DiscussionModel = new DiscussionModel();
$this->CategoryData = $this->CategoryModel->GetFull();
$this->CategoryDiscussionData = array();
foreach($this->CategoryData->Result() as $Category)
{
$this->Category = $Category;
if($Session->CheckPermission('Vanilla.Discussions.View', $this->Category->CategoryID))
{
//TODO be nice if options could be passed to filter
// discussions that are closed, sunk, etc etc...
$this->DiscussionData = $DiscussionModel->Get(0, $discussionsPerCategory, array('d.CategoryID' => $Category->CategoryID));
$category = array();
foreach($Category as $key => $value)
$category[$key] = $value;
#$category["CategoryURL"] = Gdn::Config('Garden.Domain')."/categories/".$Category->UrlCode;
$category["CategoryURL"] = Gdn::Request()->Domain()."/categories/".$Category->UrlCode;
if($this->DiscussionData->NumRows() > 0)
{
$count = 0;
$discussion = array();
$category["discussions"] = array();
foreach($this->DiscussionData->Result() as $Discussion)
{
foreach($Discussion as $key => $value)
$discussion[$key] = $value;
//$discussion["DiscussionURL"] = Gdn::Config('Garden.Domain').'/discussion/'.$Discussion->DiscussionID.'/'.Gdn_Format::Url($Discussion->Name);
$discussion["DiscussionURL"] = Gdn::Request()->Domain().'/discussion/'.$Discussion->DiscussionID.'/'.Gdn_Format::Url($Discussion->Name);
if($count++ < $discussionsPerCategory)
$category["discussions"][] = $discussion;
else
break;
}
}
$categories[] = $category;
}
}
$this->SetJSON("categories", $categories);
$this->Render();
}
/**
* Create a discussion.
* @param int The category id to add the discussion to.
*/
public function Discussions()
{
$CategoryID = 1;
$Session = Gdn::Session();
$DiscussionID = isset($this->Discussion) ? $this->Discussion->DiscussionID : '';
$this->CategoryID = isset($this->Discussion) ? $this->Discussion->CategoryID : $CategoryID;
if(Gdn::Config('Vanilla.Categories.Use') === TRUE)
{
$CategoryModel = new CategoryModel();
// Filter to categories that this user can add to
$CategoryModel->SQL->Distinct()
->Join('Permission _p2', '_p2.JunctionID = c.CategoryID', 'inner')
->Join('UserRole _ur2', '_p2.RoleID = _ur2.RoleID', 'inner')
->BeginWhereGroup()
->Where('_ur2.UserID', $Session->UserID)
->Where('_p2.`Vanilla.Discussions.Add`', 1)
->EndWhereGroup();
$this->CategoryData = $CategoryModel->GetFull();
}
if(isset($this->Discussion))
{
if ($this->Discussion->InsertUserID != $Session->UserID)
$this->Permission('Vanilla.Discussions.Edit', $this->Discussion->CategoryID);
}
else
{
$this->Permission('Vanilla.Discussions.Add');
}
// Set the model on the form.
$this->Form->SetModel($this->DiscussionModel);
if($this->Form->AuthenticatedPostBack() === TRUE)
{
$FormValues = $this->Form->FormValues();
// Check category permissions
if($this->Form->GetFormValue('Announce', '') != '' && !$Session->CheckPermission('Vanilla.Discussions.Announce', $this->CategoryID))
$this->Form->AddError('You do not have permission to announce in this category', 'Announce');
if($this->Form->GetFormValue('Close', '') != '' && !$Session->CheckPermission('Vanilla.Discussions.Close', $this->CategoryID))
$this->Form->AddError('You do not have permission to close in this category', 'Close');
if($this->Form->GetFormValue('Sink', '') != '' && !$Session->CheckPermission('Vanilla.Discussions.Sink', $this->CategoryID))
$this->Form->AddError('You do not have permission to sink in this category', 'Sink');
if(!$Session->CheckPermission('Vanilla.Discussions.Add', $this->CategoryID))
$this->Form->AddError('You do not have permission to start discussions in this category', 'CategoryID');
if($this->Form->ErrorCount() == 0)
{
$DiscussionID = $this->DiscussionModel->Save($FormValues, $this->CommentModel);
$this->Form->SetValidationResults($this->DiscussionModel->ValidationResults());
}
}
if($this->Form->ErrorCount() > 0)
{
// Return the form errors
$this->SetJSON("errors", $this->Form->Errors());
}
$this->Render();
}
}
?>

45
controllers/class.commentcontroller.php

@ -0,0 +1,45 @@
<?php if (!defined('APPLICATION')) exit();
class CommentController extends APIController
{
public $Uses = array('Form', 'Database', 'CategoryModel', 'DiscussionModel', 'CommentModel');
public function Index()
{
$this->Render();
}
public function Add()
{
$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.Comments.Add', $FormValues['CategoryID']))
{
$CommentID = $this->CommentModel->Save($FormValues);
$this->SetJSON("CommentID", $CommentID);
}
else
$Errors[] = 'You do not have permission to add comments to this discussion';
}
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();
}
}
?>

63
controllers/class.discussioncontroller.php

@ -0,0 +1,63 @@
<?php if (!defined('APPLICATION')) exit();
class DiscussionController extends APIController
{
public $Uses = array('Form', 'Database', 'CategoryModel', 'DiscussionModel', 'CommentModel');
public function Index()
{
$Limit = GetIncomingValue('limit', 5);
$Offset = GetIncomingValue('offset', 0);
$DiscussionID = GetIncomingValue('id', 0);
$Session = Gdn::Session();
$Discussion = $this->DiscussionModel->GetID($DiscussionID);
if($Discussion != False && $Session->CheckPermission('Vanilla.Discussions.View', $Discussion->CategoryID))
{
$this->SetJSON("discussion", $Discussion);
if($Discussion->CountComments > 0)
{
$Comments = $this->CommentModel->Get($DiscussionID, $Limit, $Offset)->Result();
$this->SetJSON("comments", $Comments);
}
}
$this->Render();
}
/**
* Create a discussion.
* @param int The category id to add the discussion to.
*/
public function Add()
{
$Session = Gdn::Session();
$Errors = array();
// Set the model on the form.
$this->Form->SetModel($this->DiscussionModel);
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
$DiscussionID = $this->DiscussionModel->Save($FormValues, $this->CommentModel);
}
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();
}
}
?>

21
controllers/class.sessioncontroller.php

@ -0,0 +1,21 @@
<?php if (!defined('APPLICATION')) exit();
class SessionController extends APIController
{
//TODO should allow for only one catgories to be looked at
public function Index()
{
$Session = Gdn::Session();
if($Session->User != False)
$this->SetJSON("user", array("TransientKey"=>$Session->TransientKey(), "UserID"=>$Session->UserID, "Name"=>$Session->User->Name, "User"=>True));
else
$this->SetJSON("user", array("TransientKey"=>$Session->TransientKey(), "UserID"=>0, "User"=>False));
$this->Render();
}
}
?>

16
design/README.txt

@ -0,0 +1,16 @@
By default, each application has it's own "design" folder for css and image
files off the root of that application (ie. /scaffolding/design/). If you wanted
to override them, you could copy them into your custom theme folder and edit
them there.
If you want a view to be used across applications (ie. one master view for
vanilla, scffolding, etc), you could place it in:
/themes/theme_name/views/default.master
/themes/theme_name/design/*.png,*.css
If you wanted a view to be specific to one application (ie. an altered master
view for scaffolding only), you could place it in:
/themes/theme_name/app_name/views/default.master
/themes/theme_name/app_name/design/*.png,*.css

12
js/ReadMe.txt

@ -0,0 +1,12 @@
Any javascript files that are specific to this application can be placed in this
folder. They should be named after the application, controller, or controller
method that they are required for. For example:
entry.js: should be included in every page of the "entry" controller.
entry_apply.js: should be included only on the entry.apply() page.
Note 1: these are simply guidelines - you can name any file whatever you want and
include it anywhere you want.
Note 2: You can add a js file to the controller with:
$this->AddJsFile('filename.js');

9
locale/en-CA/definitions.php

@ -0,0 +1,9 @@
<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2008, 2009 Vanilla Forums Inc.
This file is part of Garden.
Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
*/

0
models/empty

15
settings/about.php

@ -0,0 +1,15 @@
<?php
/**
* An associative array of information about this application.
*/
$ApplicationInfo['APIs'] = array(
'Description' => "enable json output for vanilla",
'Version' => '0.2',
'RegisterPermissions' => FALSE, // Permissions that should be added to the application when it is installed.
'SetupController' => 'setup',
'AllowEnable' => TRUE, // You can remove this when you create your own application (leaving it will make it so the application can't be enabled by Garden)
'Author' => "eleith",
'AuthorEmail' => 'eleith@diffbot.com',
'AuthorUrl' => 'http://www.diffbot.com',
'License' => 'MIT'
);

3
settings/configuration.php

@ -0,0 +1,3 @@
<?php if (!defined('APPLICATION')) exit();
// DO NOT EDIT THIS FILE
// All of the settings defined here can be overridden in the /conf/config.php file.

22
settings/hooks.php

@ -0,0 +1,22 @@
<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2008, 2009 Vanilla Forums Inc.
This file is part of Garden.
Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
*/
/*
class SkeletonHooks implements Gdn_IPlugin {
public function Controller_Event_Handler($Sender) {
// Do something
}
public function Setup() {
// No need for anything here...
}
}
*/

48
settings/structure.php

@ -0,0 +1,48 @@
<?php if (!defined('APPLICATION')) exit();
/*
Copyright 2008, 2009 Vanilla Forums Inc.
This file is part of Garden.
Garden is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Garden is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with Garden. If not, see <http://www.gnu.org/licenses/>.
Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com
*/
// Use this file to construct tables and views necessary for your application.
// There are some examples below to get you started.
if (!isset($Drop))
$Drop = FALSE;
if (!isset($Explicit))
$Explicit = TRUE;
/*
The Column method (defined in /library/database/class.generic.structure.php)
has the following arguments:
Column(
$Name, // The name of the column to add
$Type, // The type of column to add
$Length = '', // The length of the column (if applicable)
$Null = FALSE, // A boolean value indicating if the column allows nulls
$Default = NULL, // The default value of the column
$KeyType = FALSE, // The type of key to make the column (primary or key)
$AutoIncrement = FALSE // Should the field auto_increment?
);
Example table construction:
$Construct->Table('ExampleTable')
->PrimaryKey('ExampleTableID')
->Column('ExampleUserID', 'int', TRUE)
->Column('Field1', 'varchar(50)')
->Set($Explicit, $Drop);
Example view construction:
$SQL = $Database->SQL();
$SQL->Select('e.ExampleTableID, e.ExampleUserID, u.Name as ExampleUser, e.Field1')
->From('ExampleTable e')
->Join('User u', 'e.ExampleUserID = u.UserID');
$Construct->View('vw_Example', $SQL);
*/