From 620257c2607a3f8e63856fdad3bd1ca331d73aac Mon Sep 17 00:00:00 2001 From: eleith Date: Thu, 24 Feb 2011 16:18:29 -0800 Subject: [PATCH] initial commit of addon --- controllers/class.apicontroller.php | 27 ++++ controllers/class.categorycontroller.php | 139 +++++++++++++++++++++ controllers/class.commentcontroller.php | 45 +++++++ controllers/class.discussioncontroller.php | 63 ++++++++++ controllers/class.sessioncontroller.php | 21 ++++ design/README.txt | 16 +++ js/ReadMe.txt | 12 ++ locale/en-CA/definitions.php | 9 ++ models/empty | 0 settings/about.php | 15 +++ settings/configuration.php | 3 + settings/hooks.php | 22 ++++ settings/structure.php | 48 +++++++ 13 files changed, 420 insertions(+) create mode 100755 controllers/class.apicontroller.php create mode 100644 controllers/class.categorycontroller.php create mode 100644 controllers/class.commentcontroller.php create mode 100644 controllers/class.discussioncontroller.php create mode 100644 controllers/class.sessioncontroller.php create mode 100755 design/README.txt create mode 100755 js/ReadMe.txt create mode 100755 locale/en-CA/definitions.php create mode 100755 models/empty create mode 100755 settings/about.php create mode 100755 settings/configuration.php create mode 100755 settings/hooks.php create mode 100755 settings/structure.php diff --git a/controllers/class.apicontroller.php b/controllers/class.apicontroller.php new file mode 100755 index 0000000..0ce8b44 --- /dev/null +++ b/controllers/class.apicontroller.php @@ -0,0 +1,27 @@ +. +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'; + } +} diff --git a/controllers/class.categorycontroller.php b/controllers/class.categorycontroller.php new file mode 100644 index 0000000..e9ddf45 --- /dev/null +++ b/controllers/class.categorycontroller.php @@ -0,0 +1,139 @@ +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(); + } + +} + +?> diff --git a/controllers/class.commentcontroller.php b/controllers/class.commentcontroller.php new file mode 100644 index 0000000..feb6607 --- /dev/null +++ b/controllers/class.commentcontroller.php @@ -0,0 +1,45 @@ +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(); + } + +} + +?> diff --git a/controllers/class.discussioncontroller.php b/controllers/class.discussioncontroller.php new file mode 100644 index 0000000..2d443ec --- /dev/null +++ b/controllers/class.discussioncontroller.php @@ -0,0 +1,63 @@ +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(); + } + +} + +?> diff --git a/controllers/class.sessioncontroller.php b/controllers/class.sessioncontroller.php new file mode 100644 index 0000000..71459d8 --- /dev/null +++ b/controllers/class.sessioncontroller.php @@ -0,0 +1,21 @@ +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(); + } + +} + +?> diff --git a/design/README.txt b/design/README.txt new file mode 100755 index 0000000..73b3387 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/js/ReadMe.txt b/js/ReadMe.txt new file mode 100755 index 0000000..16659fe --- /dev/null +++ b/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'); \ No newline at end of file diff --git a/locale/en-CA/definitions.php b/locale/en-CA/definitions.php new file mode 100755 index 0000000..731ff2d --- /dev/null +++ b/locale/en-CA/definitions.php @@ -0,0 +1,9 @@ +. +Contact Vanilla Forums Inc. at support [at] vanillaforums [dot] com +*/ diff --git a/models/empty b/models/empty new file mode 100755 index 0000000..e69de29 diff --git a/settings/about.php b/settings/about.php new file mode 100755 index 0000000..dbcbdd1 --- /dev/null +++ b/settings/about.php @@ -0,0 +1,15 @@ + "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' +); diff --git a/settings/configuration.php b/settings/configuration.php new file mode 100755 index 0000000..9c5fd30 --- /dev/null +++ b/settings/configuration.php @@ -0,0 +1,3 @@ +. +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... + } +} +*/ \ No newline at end of file diff --git a/settings/structure.php b/settings/structure.php new file mode 100755 index 0000000..92e6a85 --- /dev/null +++ b/settings/structure.php @@ -0,0 +1,48 @@ +. +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); +*/ \ No newline at end of file