Skip to content

Instantly share code, notes, and snippets.

@lordmatt
Last active December 15, 2015 14:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordmatt/5276878 to your computer and use it in GitHub Desktop.
Save lordmatt/5276878 to your computer and use it in GitHub Desktop.
This is a fork of Graeme Cook's 0.1.0 ItemLicense plugin for NucleusCMSwhich can be found here: http://wakka.xiffy.nl/itemlicense_code The purpose of the fork is to make changes including depreciating all support for PHP4 and standardised on PHP5 style coding. Also it means that his code is available on github.
<?php
/**
* Apply a license of your choice to NucleusCMS blogs on an item-by-item basis.
*
* Copyright (C) ???? Graeme Cook, 2013 Matthew Brown aka Lord Matt
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* @author Graeme Cook <www.blog-4-justice.org/>
* @author Matthew Bown aka Lord Matt <http://lordmatt.co.uk/>
*
*/
/* USAGE
TemplateVar:
<%ItemLicense%> displays the license defined in the plugin options
<%ItemLicense(print)%> displays the print-specific version of the license defined in the corresponding plugin option
*/
class NP_ItemLicense extends NucleusPlugin {
public function getName() { return 'ItemLicense'; }
public function getAuthor() { return 'Graeme Cook (www.blog-4-justice.org) | Lord Matt'; }
public function getVersion() { return '0.20'; }
public function getURL() { return 'http://wakka.xiffy.nl/itemlicense'; }
public function getDescription() { return 'Apply a license of your choice to selected blogs on an item-by-item basis. USAGE: The TemplateVar <%ItemLicense%> displays the license defined in the plugin options; <%ItemLicense(print)%> displays the print-specific version of the license defined in the corresponding plugin option (if you have set it).'; }
public function getTableList() {
return array(sql_table('plugin_itemlicense'));
}
public function getEventList() {
return array(
'AddItemFormExtras',
'EditItemFormExtras',
'PostAddItem',
'PreUpdateItem',
'PreDeleteItem'
);
}
public function install() {
$this->createOption('blogs', 'Apply to following blogs only (short names, comma-delimited; if left blank then the License option is available in all blogs):', 'text', '');
$this->createOption('deletetables', 'Delete this plugin\'s table and data when uninstalling?', 'yesno', 'no');
$this->createOption('default', 'Default value for License option when adding/editing a blog item:', 'select', '1', 'no|0|yes|1');
$this->createOption('license', 'License to insert for screen display (may include HTML)', 'textarea', '');
$this->createOption('print', 'License to insert for print display (if different from the above, otherwise leave empty)', 'textarea', '');
sql_query(sprintf("CREATE TABLE IF NOT EXISTS %s (
itemid int(11) auto_increment,
license tinyint(2) NOT NULL,
PRIMARY KEY(itemid)
) TYPE=MyISAM",
sql_table('plugin_itemlicense')
)
);
}
public function unInstall() {
if ($this->getOption('deletetables') == '1') {
sql_query(sprintf("DROP TABLE IF EXISTS %s", sql_table('plugin_itemlicense')));
}
}
public function supportsFeature($feature) {
switch ($feature) {
case 'SqlTablePrefix':
return 1;
default:
return 0;
}
}
public function event_AddItemFormExtras($data) {
$shortname = $data['blog']->getShortName();
if (! $this->isEnabledForBlog($shortname)) { return; }
$this->editItemFormField();
}
public function event_EditItemFormExtras($data) {
$shortname = $data['blog']->getShortName();
if (! $this->isEnabledForBlog($shortname)) { return; }
$itemid = $data['itemid'];
$license = $this->getItemLicense($itemid);
$this->editItemFormField($itemid, $license);
}
public function event_PostAddItem(&$data) {
$itemid = $data['itemid'];
$license = postVar('itemlicense');
$this->setItemLicense($itemid, $license);
}
public function event_PreDeleteItem(&$data) {
$itemid = $data['itemid'];
$query = sprintf("DELETE FROM %s WHERE itemid = %d", sql_table('plugin_itemlicense'), $itemid);
sql_query($query);
}
public function event_PreUpdateItem($data) {
$itemid = $data['itemid'];
$license = postVar('itemlicense');
$this->setItemLicense($itemid, $license);
}
public function doTemplateVar(&$item, $print = '') {
$license = $this->getItemLicense($item->itemid);
if ($license) {
if (!$print) {
echo $this->getOption('license');
} else {
if ($this->getOption('print')){
echo $this->getOption('print');
}else{
echo $this->getOption('license');
}
}
}
}
protected function editItemFormField($itemid = '', $license = '') {
echo '<h3>License</h3>';
$s1 = $s2 = '';
switch ($license) {
case '1':
$s1 = ' checked="checked"';
break;
case '0':
$s2 = ' checked="checked"';
break;
default:
if ($this->getOption('default') == '1'){
$s1 = ' checked="checked"';
}else{
$s2 = ' checked="checked"';
}
}
echo 'Apply license to this item?';
echo '<input name="itemlicense" type="radio" value="1" id="itemlicense1"' . $s1 . ' /><label for="itemlicense1">Yes</label> ';
echo '<input name="itemlicense" type="radio" value="0" id="itemlicense2"' . $s2 . ' /><label for="itemlicense2">No</label>';
}
protected function isEnabledForBlog($blogShortName) {
$bloglist = $this->getOption('blogs');
if (! $bloglist) {return TRUE;}
$permittedblogs = explode(",", $bloglist);
foreach ($permittedblogs as $permittedblog) {
// case-insensitive comparison
if (strcasecmp(trim($permittedblog), $blogShortName) == 0) {
return TRUE;
}
}
}
protected function setItemLicense($itemid, $license) {
// single function handles both INSERT and UPDATE
if (isset($license)) {
$query = sprintf("SELECT license FROM %s WHERE itemid = %d", sql_table('plugin_itemlicense'), $itemid);
$result = sql_query($query);
if ($row = mysql_fetch_assoc($result)){
$query = sprintf("UPDATE %s SET license = '%d' WHERE itemid = %d", sql_table('plugin_itemlicense'), intval($license), $itemid);
}else{
$query = sprintf("INSERT INTO %s (itemid, license) VALUES (%d, %d)", sql_table('plugin_itemlicense'), $itemid, intval($license));
}
sql_query($query);
}
}
protected function getItemLicense($itemid) {
$query = sprintf("SELECT license FROM %s WHERE itemid = %d", sql_table('plugin_itemlicense'), $itemid);
$result = sql_query($query);
if ($row = mysql_fetch_array($result)) {
return $row['license'];
}else{
return '';
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment