Location: PHPKode > projects > ANJEL > com_anjel/bots/bot_anjel_content.php
<?php
/* $Id: bot_anjel_content.php 38 2005-11-23 08:58:35Z schmalls $ */
/**
 * /bots/bot_anjel_content.php
 * 
 * <p>Bot to include content items into mailings.</p>
 * 
 * @package ANJEL
 * @subpackage bots
 * @copyright © 2004-2005 Schmalls / Joshua Thompson / All Rights Reserved
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
 * @author Schmalls / Joshua Thompson <hide@address.com>
 * @version 0.6.1
 * @since 0.6.1
 * @link http://www.schmalls.com
 */
 
 /**
  *  Ensures this file is being included by a parent file
  */
 defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');
 
 // register bot functions
 $_MAMBOTS->registerFunction('bot_anjel_edit_tabs', 'bot_anjel_content_edit_tab');
 $_MAMBOTS->registerFunction('bot_anjel_transform_all', 'bot_anjel_content_transform_all');
 
 /**
  * Creates the edit tab
  * 
  * <p>Show a listing of all of the content items available. It creates the 
  * placeholder text to insert into the edit area but is not able to 
  * automatically insert into the edit area. Returns an array with the tab 
  * title as the first element and the html to display as the second 
  * element.</p>
  * 
  * @version 0.6.1
  * @since 0.6.1
  * @return array the tab title and the html to display
  */
 function bot_anjel_content_edit_tab() {
 	 // get the content items
 	 $content_items = bot_anjel_content_get_content_items();
 	 // write the html
 	 ob_start();
?>
<script type="text/javascript">
/* <[CDATA[ */
function bot_anjel_content_update_output() {
	// get the info
	var form = document.adminForm;
	var content_id = form.content_id.options[form.content_id.selectedIndex].value;
	if (form.content_intro.checked) {
		var content_intro = 1;
	} else {
		var content_intro = 0;
	}
	// output the tag
	form.content_tag.value = "{contentitem:" + content_id + "|" + content_intro + "}";
} // end function
/* ]]> */
</script>
<table class="adminform" width="100%">
	<tr>
		<th>
			<?php echo _CONTENT_ADD; ?>
		</th>
	</tr>
	<tr>
		<td><?php echo mosToolTip(str_replace("'", "\'", _CONTENTITEMSELECT) . '<br />' . str_replace("'", "\'", _COPY_PLACEHOLDER)); ?></td>
	</tr>
	<tr>	
		<td>
			<select name="content_id" class="inputbox" size="10" onchange="bot_anjel_content_update_output();">
<?php
	 if(sizeof($content_items) > 0) {
		 foreach ($content_items AS $content_item) {
			 echo '<option value="' . $content_item->id . '">' . $content_item->section . '/' . $content_item->category . '/'. $content_item->title . '</option>' . "\n";
		 } // end foreach
	 } // end if
?>
			</select>
			<br />
			<?php echo _INTROONLY; ?><input type="checkbox" value="1" name="content_intro" onclick="bot_anjel_content_update_output();" /><br /><br />
			<input type="text" name="content_tag" class="inputbox" size="20" onfocus="this.select();" /> 
		</td>
	</tr>
</table>
<?php
	 // get html
	 $return = ob_get_contents();
	 ob_end_clean();
	 return array(_CONTENTITEM, $return);
 } // end function
  
 /**
  * Gets content items
  * 
  * <p>Gets all of the content items from the database with id, title, 
  * section, and category information.</p>
  * 
  * @version 0.6.1
  * @since 0.6.1
  * @global class access to the database
  * @returns array information about content items from the site
  */
 function bot_anjel_content_get_content_items() {
	 global $database;
	 
	 $query = "SELECT a.id as id, a.title as title, b.title as section, c.title as category FROM #__content as a, #__sections as b, #__categories as c WHERE a.sectionid = b.id AND a.catid = c.id";
	 $database->setQuery($query);
	 $contentitems = $database->loadObjectList();
	 return $contentitems;
 } // end function
 
 /**
  * Replaces the content placeholders with the content items
  * 
  * <p>Checks the mailing for content item placeholders. Then replaces these 
  * with the actual content items. Also replace mosimage tags with the 
  * images.</p>
  * 
  * @version 0.6.1
  * @since 0.6.1
  * @param string $html the original html content
  * @param string $text the original text content
  * @global string the site url
  * @global class access to mailing methods
  * @global class access to mainframe methods
  */
 function bot_anjel_content_transform_all($html, $text) {
	 global $mosConfig_live_site, $mailing, $mainframe;
	 
	 // change contentitem placeholders to contentitems
 	 $content_items = array();
	 preg_match_all('/\{contentitem:(.{1,5})\|(.{1})}/', $html, $content_items, PREG_SET_ORDER);
	 foreach ($content_items as $content_item) {
		 // get Itemid
		 $itemid = $mainframe->getItemId($content_item[1]);
		 $replacement = bot_anjel_content_get_content_item($content_item[1]);
		 if ($content_item[2] == '0') {
			 $html = str_replace($content_item[0], '<strong>' . $replacement->title . '</strong>' . "\r\n" . '<br />' . $replacement->introtext . '<br />' . "\r\n" . $replacement->fulltext . "\r\n", $html);
		 } else {
			 $html = str_replace($content_item[0], '<strong>' . $replacement->title . '</strong>' . "\r\n" . '<br />' . $replacement->introtext . '<br />' . "\r\n" . '<a href="' . anjel_sef_rel_to_abs('index.php?option=com_content&task=view&id=' . $content_item[1] . '&Itemid=' . $itemid) . '">' . _READ_MORE . '</a>' . "\r\n", $html);
		 } // end if
		 $images = bot_anjel_content_get_image_array($replacement->images);
		 foreach($images as $image) {
			 $image_string = '<img src="' . $mosConfig_live_site . '/images/stories/' . $image['image'] . '" align="' . $image['align'] . '" alt="' . $image['alttext'] . '" border="' . $image['border'] . '" />';
			 $html = preg_replace('/{mosimage}/', $image_string, $html, 1);
		 } // end foreach
	 } // end foreach
	 $content_items = array();
	 preg_match_all('/\{contentitem:(.{1,5})\|(.{1})}/', $text, $content_items, PREG_SET_ORDER);
	 foreach ($content_items as $content_item) {
		 // get Itemid
		 $itemid = $mainframe->getItemId($content_item[1]);
		 $replacement = bot_anjel_content_get_content_item($content_item[1]);
		 // html to text
		 $replacement->title = strtoupper($mailing->htmlToText($replacement->title));
		 $replacement->introtext = $mailing->htmlToText($replacement->introtext);
		 $replacement->fulltext = $mailing->htmlToText($replacement->fulltext);
		 if ($content_item[2] == '0') {
			 $text = str_replace($content_item[0], $replacement->title . "\r\n" . $replacement->introtext . "\r\n" . $replacement->fulltext . "\r\n", $text);
		 } else {
			 $text = str_replace($content_item[0], $replacement->title . "\r\n" . $replacement->introtext . "\r\n" . '* ' . _READ_MORE . ' ( ' . anjel_sef_rel_to_abs('index.php?option=com_content&task=view&id=' . $content_item[1] . '&Itemid=' . $itemid) . ' )' . "\r\n", $text);
		 } // end if
		 $text = str_replace('{mosimage}', '', $text);
	 } // end foreach
	 // replace page breaks with line breaks
	 $html = str_replace('{mospagebreak}', '<br style="clear: both;" /><br />', $html);
	 $text = str_replace('{mospagebreak}', "\r\n \r\n", $text);
 } // end function
 
 /**
  * Gets a specific content item
  * 
  * <p>Gets the content item's title, intro, and fulltext from the 
  * database.</p>
  * 
  * @version 0.6.1
  * @since 0.6.1
  * @param int $id the content item's id
  * @global class access to the database
  * @return array information about the content item
  */
 function bot_anjel_content_get_content_item($id) {
	global $database;
	
	$query = "SELECT `title`, `introtext`, `fulltext`, `images` FROM `#__content` WHERE id = $id";
	$database->setQuery($query);
	$database->loadObject($content_item);
	$error = $database->getErrorMsg();
	// quit on error
	if (!empty($error)) {
		echo '<p>Error (bots/bot_anjel_content.php->bot_anjel_content_get_content_item() line ' . __LINE__ . '): Could not get content item information from database. Database error: <br />' . $error . '</p>';
		return false;
	} else {
		// check for slashes
		if(get_magic_quotes_runtime()) {
			$content_item->title = stripslashes($content_item->title);
			$content_item->introtext = stripslashes($content_item->introtext);
			$content_item->fulltext = stripslashes($content_item->fulltext);
			$content_item->images = stripslashes($content_item->images);
		} // end if
		return $content_item;
	} // end if
 } // end function
 
 /**
  * Gets the image array for a content item
  * 
  * <p>Creates the image array for a content item with image location 
  * and attribute information.</p>
  * 
  * @version 0.6.1
  * @since 0.6.1
  * @param string $images string from image part of content item
  * @return array image location and property information
  */
 function bot_anjel_content_get_image_array($images) {
	 
	// split around newline
	$first = explode("\n",$images);
	// then split each line around |'s to get image properties
	for($i=0, $n=count($first); $i < $n; $i++) {
		$second = explode('|',$first[$i] . '|||');
		$third[$i]['image'] = $second[0];
		$third[$i]['align'] = $second[1];
		$third[$i]['alttext'] = $second[2];
		$third[$i]['border'] = $second[3];			
	} // end for
	return $third;
 } // end function
?>
Return current item: ANJEL