Location: PHPKode > projects > cutenews aj-fork > 167/plugins/long-to-short.php
<?php

/*
Plugin Name:	Long-2-Short
Plugin URI:		http://www.brandedthoughts.co.uk/cutewiki/index.php/Long_2_Short_Plugin
Description:	Automatically creates an excerpt of news from the "full story" field for use as the "short story".
Version:		1.0
Author:			David Carrington
Author URI:		http://www.brandedthoughts.co.uk
Application:	CuteNews
Required Framework: 1.1.3
*/

define('L2S_DEFAULT_CHARACTERS', 100);
define('L2S_DEFAULT_ELLIPSES', '...');

add_action('new-validate-entry','l2s_run');
add_filter('cutenews-options', 'l2s_AddToOptions');
add_action('plugin-options','l2s_CheckAdminOptions');

function l2s_run($hook) {
	global $full_story, $short_story;
	
	// Load the plugin settings
	$l2s = new PluginSettings('Long-2-Short');
	$characters = (int) $l2s->settings['characters'];
	$ellipses = $l2s->settings['ellipses'];
	if (!$characters) $characters = L2S_DEFAULT_CHARACTERS;
	if (!$ellipses) $ellipses = L2S_DEFAULT_ELLIPSES;
	
	// Don't run this function if there is already a short story caption
	if ($short_story)
		return false;
	
	// If the story is longer than the minimum amount of characters
	if (strlen($full_story) > $characters) {
		// Trim the long story to fit into the short story
		$short_story = substr($full_story, 0, $characters);
		
		// Add the ellipses
		$short_story .= $ellipses;
	} else {
		// Place the "short" long story into the short story field
		$short_story = $full_story;
	
		// Remove the long story
		$full_story = '';
	}
}

function l2s_AddToOptions($options, $hook) {
	global $PHP_SELF;

	// Add a custom screen to the "options" screen
	$options[] = array(
		'name'		=> 'Long-2-Short Configuration',
		'url'		=> $PHP_SELF.'?mod=options&amp;action=l2s',
		'access'	=> '1',
	);

	// return the customized options
	return $options;
}

function l2s_CheckAdminOptions($hook) {
	// check if the user is requesting the L2S options
		if ($_GET['action'] == 'l2s')
			// show the L2S admin screen
		l2s_AdminOptions();
}

function l2s_AdminOptions() {
	// Load the plugin settings
	$l2s = new PluginSettings('Long-2-Short');
	
	if ($_POST['l2s']) {
		$l2s->settings['characters'] = (int) stripslashes($_POST['l2s']['characters']);
		$l2s->settings['ellipses'] = stripslashes($_POST['l2s']['ellipses']);
		$l2s->save();
		msg('info','Long-2-Short settings saved', 'Your settings were saved successfully', '?mod=options&amp;action=l2s');
	}

	// Display header
	echoheader('user','Long-2-Short Configuration');
	
	$characters = (int) $l2s->settings['characters'];
	$ellipses = $l2s->settings['ellipses'];
	
	if (!$characters)
		$characters = L2S_DEFAULT_CHARACTERS;
	
	if (!$ellipses)
		$ellipses = L2S_DEFAULT_ELLIPSES;
	
	$buffer = '
<style type="text/css">
.easyform label { width: 200px; margin-bottom: 10px;}
</style>
<form method="post" action="?mod=options&amp;action=l2s&amp;subaction=save" class="easyform">
	<a href="http://www.brandedthoughts.co.uk/cutewiki/index.php/Long_2_Short_Plugin">Help</a>
	<div>
		<label for="txtCharacters">Number of characters</label>
		<input id="txtCharacters" name="l2s[characters]" value="'.$characters.'" />
	</div>
	<div>
		<label for="txtEllipses">Ellipses characters</label>
		<input id="txtEllipses" name="l2s[ellipses]" value="'.$ellipses.'" />
	</div>
	<div>
		<input type="submit" value="Save Settings" />
	</div>
</form>';
	
	echo $buffer;
	
	// Display footer
	echofooter();
}

?>
Return current item: cutenews aj-fork