Advertisement
Guest User

class.nillablog.plugin.php with hide category module

a guest
Jul 28th, 2013
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.83 KB | None | 0 0
  1. <?php if (!defined("APPLICATION")) exit();
  2. /*
  3.  *  (C) Copyright 2011, canofsleep.com
  4.  *
  5.  *  Licensed under the Apache License, Version 2.0 (the "License");
  6.  *  you may not use this file except in compliance with the License.
  7.  *  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17.  
  18. $PluginInfo["NillaBlog"] = array(
  19.     "Name" => "NillaBlog",
  20.     "Description" => "A blog plugin for Vanilla 2+ (http://vanillaforums.org)",
  21.     "Version" => "1.8.1",
  22.     "Author" => "Dan Dumont",
  23.     "AuthorEmail" => "ddumont@gmail.com",
  24.     "AuthorUrl" => "https://github.com/ddumont/nillablog",
  25.     "SettingsUrl" => "/dashboard/settings/nillablog",
  26.     "SettingsPermission" => "Garden.Settings.Manage",
  27.     "RequiredApplications" => array("Vanilla" => "2.0.18") // This needs to be bumped when Vanilla releases with my contributed changes
  28. );
  29.  
  30. /**
  31.  * NillaBlog plugin for Vanilla
  32.  * @author ddumont@gmail.com
  33.  */
  34. class NillaBlog extends Gdn_Plugin {
  35.  
  36.     /**
  37.      * Build the setting page.
  38.      * @param $Sender
  39.      */
  40.     public function SettingsController_NillaBlog_Create($Sender) {
  41.         $Sender->Permission('Garden.Settings.Manage');
  42.  
  43.         $Validation = new Gdn_Validation();
  44.         $ConfigurationModel = new Gdn_ConfigurationModel($Validation);
  45.         $ConfigurationModel->SetField(array("Plugins.NillaBlog.CategoryIDs"));
  46.         $ConfigurationModel->SetField("Plugins.NillaBlog.DisableCSS");
  47.         $ConfigurationModel->SetField("Plugins.NillaBlog.PostsPerPage");
  48.         $ConfigurationModel->SetField("Plugins.NillaBlog.GooglePlusOne");
  49.         $Sender->Form->SetModel($ConfigurationModel);
  50.  
  51.         if ($Sender->Form->AuthenticatedPostBack() === FALSE) {
  52.             $Sender->Form->SetData($ConfigurationModel->Data);
  53.         } else {
  54.             $Data = $Sender->Form->FormValues();
  55. //          $ConfigurationModel->Validation->ApplyRule("Plugins.NillaBlog.CategoryIDs", "RequiredArray");  // Not required
  56.             $ConfigurationModel->Validation->ApplyRule("Plugins.NillaBlog.PostsPerPage", "Integer");
  57.             if ($Sender->Form->Save() !== FALSE)
  58.                 $Sender->StatusMessage = T("Your settings have been saved.");
  59.         }
  60.  
  61.         $Sender->AddSideMenu();
  62.         $Sender->SetData("Title", T("NillaBlog Settings"));
  63.  
  64.         $CategoryModel = new CategoryModel();
  65.         $Sender->SetData("CategoryData", $CategoryModel->GetAll(), TRUE);
  66.         array_shift($Sender->CategoryData->Result());
  67.  
  68.         $Sender->Render($this->GetView("settings.php"));
  69.     }
  70.  
  71.     /**
  72.      * Adjusts the number of posts to display in the blog category.
  73.      * @param $Sender
  74.      */
  75.     public function CategoriesController_BeforeGetDiscussions_Handler($Sender) {
  76.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) )
  77.             return;
  78.         $Sender->EventArguments['PerPage'] = C("Plugins.NillaBlog.PostsPerPage");
  79.     }
  80.  
  81.     /**
  82.      * Insert the first comment under the discussion title for the blog category.
  83.      * This turns the blog category into a list of blog posts.
  84.      * @param $Sender
  85.      */
  86.     public function CategoriesController_AfterDiscussionTitle_Handler($Sender) {
  87.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) )
  88.             return;
  89.  
  90.         $Discussion = $Sender->EventArguments['Discussion'];
  91.  
  92.         $Body = $Discussion->Body;
  93.         $end = strrpos($Body, "<hr");
  94.         if ($end)
  95.             $Body = substr($Body, 0, $end);
  96.         $Discussion->FormatBody = Gdn_Format::To($Body, $Discussion->Format);
  97.         ?>
  98.             <ul class="MessageList">
  99.                 <li>
  100.                     <div class="Message">
  101.                         <?php echo $Discussion->FormatBody; ?>
  102.                     </div>
  103.                 </li>
  104.                 <?php if ($end) { ?>
  105.                     <li>
  106.                         <a href="<?php echo Gdn::Request()->Url(ConcatSep("/", "discussion", $Discussion->DiscussionID, Gdn_Format::Url($Discussion->Name)))?>"
  107.                            class="More"><?php echo T("Read more");?></a>
  108.                     </li>
  109.                 <?php } ?>
  110.             </ul>
  111.         <?php
  112.     }
  113.  
  114.     /**
  115.      * Adds the blog subscription link to each post for easier access.
  116.      * @param $Sender
  117.      */
  118.     public function CategoriesController_DiscussionMeta_Handler($Sender) {
  119.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) )
  120.             return;
  121.  
  122.         $Discussion = $Sender->EventArguments['Discussion'];
  123.         ?>
  124.             <span class='RSS'>
  125.                 <a href='<?php echo Gdn::Request()->Url(ConcatSep("/", $Sender->SelfUrl, "feed.rss")); ?>'>
  126.                     <img src="<?php echo Asset("/applications/dashboard/design/images/rss.gif"); ?>"></img>
  127.                     <?php echo T("Subscribe to this blog"); ?>
  128.                 </a>
  129.             </span>
  130.         <?php
  131.  
  132.         if (C("Plugins.NillaBlog.GooglePlusOne")) {
  133.             ?><span class='plusone'>
  134.                 <g:plusone href="<?php echo Gdn::Request()->Url(ConcatSep("/", "discussion", $Discussion->DiscussionID, Gdn_Format::Url($Discussion->Name)), TRUE);
  135.                     ?>" size="medium">
  136.                 </g:plusone>
  137.             </span><?php
  138.         }
  139.     }
  140.         public function Base_Render_Before($Sender) {
  141.     // Remove Categories set in NillaBlog from CategoriesModule
  142.     if(isset($Sender->Assets['Panel']['CategoriesModule'])
  143.     && (count(C('Plugins.NillaBlog.CategoryIDs')) > 0)
  144.     && (version_compare(PHP_VERSION, '5.4.0') >= 0)) {
  145.     $GetPrivateObject = function &($Object, $Item) {
  146.     $Result = &Closure::bind(function &() use ($Item) {
  147.     return $this->$Item;
  148.     }, $Object, $Object)->__invoke();
  149.      
  150.     return $Result;
  151.     };
  152.     $CategoriesModuleData = &$GetPrivateObject($Sender->Assets['Panel']['CategoriesModule']->Data, '_Result');
  153.     foreach(C('Plugins.NillaBlog.CategoryIDs') as $CategoryID)
  154.     unset($CategoriesModuleData[$CategoryID]);
  155.     }
  156.     }
  157.     /**
  158.      * Adds the class 'NillaBlog' to every discussion in the blog category list.
  159.      * Allows for themes to style the blog independently of the plugin.
  160.      * @param $Sender
  161.      */
  162.     public function CategoriesController_BeforeDiscussionName_Handler($Sender) {
  163.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) )
  164.             return;
  165.         $Sender->EventArguments["CssClass"] .= " NillaBlog NillaBlog".$Sender->CategoryID." ";
  166.     }
  167.  
  168.     /**
  169.      * Adds the class 'NillaBlog' to every comment (including the first post) in the blog category list.
  170.      * Allows for themes to style the blog independently of the plugin.
  171.      * @param $Sender
  172.      */
  173.     public function DiscussionController_BeforeCommentDisplay_Handler($Sender) {
  174.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) )
  175.             return;
  176.         $Sender->EventArguments["CssClass"] .= " NillaBlog NillaBlog".$Sender->CategoryID." ";
  177.     }
  178.  
  179.     /**
  180.      * Sorts blog posts by creation time rather than last comment.
  181.      * @param $Sender
  182.      */
  183.     public function DiscussionModel_BeforeGet_Handler($Sender) {
  184.         $Wheres = $Sender->EventArguments["Wheres"];
  185.         if (!array_key_exists("d.CategoryID", $Wheres) || !in_array($Wheres["d.CategoryID"], C("Plugins.NillaBlog.CategoryIDs")))
  186.             return;
  187.  
  188.         $Sender->EventArguments["SortField"] = "d.DateInserted";
  189.         $Sender->EventArguments["SortDirection"] = "desc";
  190.     }
  191.  
  192.     /**
  193.      * Insert default CSS into the discussion list for the blog.
  194.      * @param $Sender
  195.      */
  196.     public function CategoriesController_Render_Before($Sender) {  
  197.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) || C("Plugins.NillaBlog.DisableCSS") )
  198.             return;
  199.  
  200.         if (C("Plugins.NillaBlog.GooglePlusOne"))
  201.             $Sender->AddJsFile('http://apis.google.com/js/plusone.js');
  202.  
  203.         $Sender->AddCssFile($this->GetResource('design/custom.css', FALSE, FALSE));
  204.     }    
  205.  
  206.     /**
  207.      * Insert default CSS into the comment list for the blog discussion.
  208.      * @param $Sender
  209.      */
  210.     public function DiscussionController_Render_Before($Sender) {
  211.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) || C("Plugins.NillaBlog.DisableCSS") )
  212.             return;
  213.  
  214.         if (C("Plugins.NillaBlog.GooglePlusOne"))
  215.             $Sender->AddJsFile('http://apis.google.com/js/plusone.js');
  216.  
  217.         $Sender->AddCssFile($this->GetResource('design/custom.css', FALSE, FALSE));
  218.     }
  219.  
  220.     /**
  221.      * Insert a clickable comments link appropriate for the blog.  We'll hide the other comment count with CSS.
  222.      * @param $Sender
  223.      */
  224.     public function CategoriesController_BeforeDiscussionMeta_Handler($Sender) {
  225.         if ( !in_array($Sender->CategoryID, C("Plugins.NillaBlog.CategoryIDs")) || C("Plugins.NillaBlog.DisableCSS") )
  226.             return;
  227.  
  228.         $Discussion = $Sender->EventArguments['Discussion'];
  229.         $Count = $Discussion->CountComments - 1;
  230.         $Label = sprintf(Plural($Count, '%s comment', '%s comments'), $Count);
  231.         ?>
  232.             <span class="CommentCount NillaBlog NillaBlog<?php echo $Sender->CategoryID;?>>">
  233.                 <a href="<?php
  234.                     echo Gdn::Request()->Url(ConcatSep("/", "discussion", $Discussion->DiscussionID, Gdn_Format::Url($Discussion->Name).($Count > 0 ? "#Item_2" : "")));
  235.                 ?>">
  236.                     <?php echo $Label; ?>
  237.                 </a>
  238.             </span>
  239.         <?php
  240.     }
  241.  
  242.     public function Setup() {}
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement