Skip to content

Categories:

Joomla Component Ordering, Positioning Rows

When creating a custom component in Joomla admin area it is somewhat frequent that one wants the ability to order items based on an order value associated with each row. Just like how articles have an order in the Joomla “Article Manager”.

The following are the necessary code snippets that allow you to make this possible.

I hope to add a little more explanation to these soon.

controller.php

function saveorder()
{
	global $option;
	// Initialize variables
	$db	=& JFactory::getDBO();
	$cid	= JRequest::getVar( 'cid', array(), 'post', 'array' );
	$total	= count( $cid );
	$order= JRequest::getVar( 'order', array(0), 'post', 'array' );
	JArrayHelper::toInteger($order, array(0));
 
	$row =& JTable::getInstance('article', 'Table');
 
	// update ordering values
	for( $i=0; $i < $total; $i++ ) {
		$row->load( (int) $cid[$i] );
 
		if ($row->ordering != $order[$i]) {
			$row->ordering = $order[$i];
			if (!$row->store()) {
				JError::raiseError(500, 
                                     $db->getErrorMsg() );
			}
		}
	}
 
	$row->reorder();
 
	$msg 	= 'New ordering saved';
	$this->setRedirect('index.php?option='.$option, $msg);
}

views/all/tmp/default.php

<th width="8%">
	<?php echo JHTML::_('grid.sort',   'Position', $row->ordering, @$lists['order_Dir'], @$lists['order'] ); ?>
	<?php echo JHTML::_('grid.order',  $this->rows ); ?>
</th>

views/all/tmp/default.php

<td class="order">
        <span><?php echo $this->pagination->orderUpIcon( $i, ($i > 0), 'orderup', 'Move Up'); ?></span>
	<span><?php echo $this->pagination->orderDownIcon( $i, $n, ($i < $n ), 'orderdown', 'Move Down'); ?></span>
	<input type="text" name="order[]" size="5" value="<?php echo $row->ordering; ?>" class="text_area" style="text-align: center" />
</td>

Posted in General.

Tagged with , .


PHP ForEach Last Item, Last Loop, Last Iteration

Often times I will want to do something different in a foreach loop on the last iteration. The code below is a good standard way of handling this…

$last_item = end($array);
$last_item = each($array);
reset($array);
foreach($array as $key => $value){
	// code executed during standard iteration
	if($value == $last_item['value'] && $key == $last_item['key']){
		// code executed on the 
                // last iteration of the foreach loop
	}
}

Note that in the if clause I check to make sure that both the key AND the value of the last item iterated match. If you were to check simply the value you could end up getting a match before the last iteration.

The above code should help you when trying to do something different on the last element, last value, last iteration, last loop, or last item of a PHP foreach loop.

Posted in General.

Tagged with .


Joomla: Adding Meta Description & Keywords

When using Joomla to run your website it is important to add Meta information to your articles. This information will be used by Search Engine Robots that index your site. The following is a quick guide to adding this information.

  1. Log into the Joomla back-end at http://<yoursite>.<com/org/net>/administrator
  2. Once logged in navigate using the upper navigation bar to Content > Article Manager
  3. Click on any article in the manager to open the article
  4. On the right column click on the bar that says “Meta Information”
  5. The area will expand to show a few input areas. The ones we are concerned with are labeled “Description” and “Keywords” respectively.
  6. In the “Description” input type a one sentence summary of the content in this article. You could to two sentences but one is really best.
  7. Now in the “Keywords” box type in 2-3 keywords for the article. The words should be separated using semi-colons e.g. acts media; web design; web development; Also, notice that you can type key phrases not just words. You should try to keep the keywords at 3 or below.
  8. When you are done inputting this information click on “Save” or “Apply” in the upper-right of the screen.
  9. Great! Search Engines will now be able to better index this page of your site.

Posted in General.

Tagged with .


MySQL Ant Task

I recently wanted to be able to automate a mysqldump statement from Eclipse using Apache Ant. I’m guessing this code could be cleaned up some to better handle the command line arguments, so if somebody can let me know how it can be cleaned up I’m all ears. However, this worked so I went with it.

Also, I created a ZIP archive of the resulting dump statement.

<property name="local_database_host" value="localhost" />
<property name="local_database_username" value="root"/>
<property name="local_database_password" value="password" />
<property name="local_database_name" value="bzftp_beta"/>
<property name="backup_directory" value="C:/Databases/" />
<property name="backup_file" value="bzftp_beta" />
 
<target name="createDBArchive" description="-->> Create database dump archive file">
	<exec dir="" executable="C:/xampp/mysql/bin/mysqldump.exe">
		<arg line='-u "${local_database_username}" -r "${backup_directory}${backup_file}.sql" ${local_database_name} jos_categories jos_sections jos_content jos_content_frontpage jos_content_rating jos_components jos_menu jos_modules jos_templates_menu'  />
	</exec>
 
	<zip destfile="${backup_directory}${backup_file}.sql.zip" update="true">
		<fileset dir="${backup_directory}">
			<include name="${backup_file}.sql"/>
		</fileset>
	</zip>
</target>

Posted in General.

Tagged with , , .


Joomla Loading Plug-Ins in Custom Components

I was having some trouble determining how to access and use a plugin in a custom component in Joomla.

I finally figured it out and the rather simple code snippet is below…

 
//Gives access to all plugins in the content folder
JPluginHelper::importPlugin('content');
// This variable is passed by reference so we need it later
$campinfo = "";
//I created my own trigger below, but "onContentPrepare" would work fine here
$dispatcher->trigger('onCampPrepare', array ($id, &$campinfo));
//assign are variable to be acessed in the template
$this->assignRef('campinfo', $campinfo);

Posted in General.

Tagged with , .


Joomla Component Global Models

One thing that I often find necessary when creating a custom component in Joomla is the need to have a “Global” model that I can use in some or all of my views. It took me awhile to find documentation on this and what I did find was outdated documentation, so I think it is worth providing some here…

I will use some simple code examples so you can get an idea of how it works.

Prerequisite knowledge

1. Create your global model

Create a file called “global.php” and place it in the “models” folder of you component.

Insert the following code…[global.php]

<?php
defined('_JEXEC') or die ('Resctricted access');
jimport('joomla.application.component.model');
JModel::addIncludePath(JPATH_COMPONENT.DS.'models');
class CustomComponentModelGlobal extends JModel
{
	var $data = null;
	function getData()
	{
		if(empty($this->data))
		{
			$this->data = "My Global Data";		
		}
		return $this->data;
	}
}

2. Modify Controller Code

We will modify the code of the display method in the controller to make our global model available in all of the views.

controller.php

function display()
{
	$view = JRequest::getVar('view');
	if(!$view){
		JRequest::setVar('view', 'default');
	}
 
	$model = $this->getModel('Global');
 
	$document =& JFactory::getDocument();
	$viewType	= $document->getType();
	$viewName	= $view;
	$view = & $this->getView( 
                             $viewName,
                             $viewType, 
                             '',
                             array( 'base_path'=>$this->_basePath));
	$view->setModel($model);
 
	parent::display();
}

3. Access the Global model in the view

view.html.php

function display($tpl=null)
{
	/*
	 * The model can now be accessed in the view
	 * in either way below
	 */
 
	$data =& $this->get('data', 'Global');
        $globalModel = $this->getModel('Global');
        $data =& $globalModel->getData();
 
        //assign the data to a ref for output in the html
        $this->assignRef('myGobalData', $data);
}

Posted in General.

Tagged with , , .


Hide Page Title in WordPress

On my site I am using the Carrington Theme by Crowd Favorite (I’m also using the Carrington Mobile Theme). It’s a great theme!

One problem I had was that on the homepage of my site I wanted to hide the title, so that it would not say “Home” in big black letters. The PHP file that needed to be edited in order to make this happen could not be accessed in the regular Appearance->Editor area. This is because the post on my homepage is a “Page” and not a regular “Post”. So the following is how to edit the default Page template for the Carrington Theme.

  1. Using an FTP client grab wp-content/themes/carrington-blog/content/page.php from your site’s directory.
    Note if you are using the default WordPress theme this equivalent file can be found under wp-content/themes/default/page.php.
  2. Make necessary edits to file and then upload it back to your site.

On my site I wanted to hide the title (<h1>) tag on my homepage. The homepage had an ID of 2 so I used the following PHP code to achieve my desired result.

Note: to find the id of a post or page in the WordPress admin look at the URL string in your browser when editing a post or a page. It will have somewhere in there the word post=<a #> “a #” is the id of that post or page.

<?php if(get_the_ID() != 2){ ?>
         <h1 class="entry-title full-title"><?php the_title() ?></h1>
<?php } ?>

If you didn’t want page titles to ever appear you could just delete everything between the <h1> tags.

Posted in General.

Tagged with , , .


traderdomains.com is a scam

I received an email this morning saying that [InsertDomainHere].com was available. I had been interested in acquiring this domain for sometime. I was not sure how traderdomains.com knew this, but somehow they did. They wanted $49.99 to secure the domain. I quickly did a search to see if traderdomains.com was actually real domain broker and came up with this post. Traderdomains.com is a total scam.

It turns out that the domain name I was interested in was indeed available, and owned by nobody. I was thus able to scoop it up for a normal domain registration fee of $10.00 or less. So be warned traderdomains.com, traderdomains.org, traderdomains.net, etc. is a complete scam.

Posted in General.

Tagged with , .


Microsoft Outlook 2010

I have been playing around recently with the Technical Preview of Microsoft Office and most specifically Outlook 2010. So far I have been quite happy with it.

The main thing that is different design wise is that it has the ribbon like other 2007 Office programs. They must have done some significant work to make it snappier because it definitely is. I’m guessing using the 64bit version helps as well.

Here are some of the great features that it has.

  1. It is 64 bit.
  2. Does not crash as much as my 2007.
  3. Organizes emails into conversations. This is similar to gmail. (at first I was annoyed but now I like it)

The quickness of it vs. 2007 and that it has a 64 bit version is a huge improvement, but beyond that it doesn’t seem to me there are too many other reasons people will want to upgrade. For me these two are enough, but others may think about holding off.

Posted in General.

Tagged with , , .


MooTools target in IE

When using MooTools 1.11 the event object target property can not be read by IE. The way to correct this is to use the following…

        var target = $(e.target || e.srcElement);

Here it is with surrounding code…

 
$('elementId').addEvent('click', function(e) {
	var target = $(e.target || e.srcElement);
	if(target.checked){
		//do something here
	} else {
		//do something here
	}
});

Posted in General.

Tagged with , .