Documentation

Light-weight, fast, simple and powerful!
 
Translations of this page?:

Writing a plugin (part 2)

So far, we’ve:

  • Created our plugin’s structure.
  • Created the basic models.
  • Created and partially coded the controller and
  • Created the menu_index and sidebar view files.

Today, we’ll finish off our WolfCMS plugin.

Lets start by processing the form displayed by the index() method.

As you may recall from part 1, the default page for a controller is displayed using the index() method. Much like a web directory where index.html or index.php is the default file. Our plugin’s default page does little more than display a menu and its items inside of a form and navigate between menus using a list of links in the sidebar.

On the index page, each menu item has its own row on the form containing two fields – item_delete[] and item_position[id] which are both arrays.

Item_Delete[] will contain a list of ID’s that have been selected to be deleted and Item_Position[] will contain a multi dimensional array of item ids and their positions as found in the input boxes. By creating this kind of form, our process can simply loop through each array and delete any items selected to be deleted, and update the position in the table with the values in the input boxes.

First, we need to add a new method to our MenuItem model – update_position().

The Record class, which is the parent class of all plugin models in WolfCMS already contains a method called delete so theres no need to write one.

Copy and paste the following method into the MenuItem model class.

update_position method

	/**
	 * Update an item's position in the table.
	 */
	public static function updatePosition($id, $new_position)
	{
		if (!is_numeric($id) || !is_numeric($new_position)) {
			return false;
		}
 
		// Select the item in the table.
		$item = self::findById($id);
		if (!isset($item) || !is_object($item)) {
			return false;
		}
 
		$item->position = $new_position;
 
		if (!$item->save()) {
			return false;
		}
 
		return true;
	}

We can now process the index page form by looping through the item_delete & item_position arrays.

	// Process the index form.
	private function update()
	{
		// Get the post data.
		$deleteData = isset($_POST['item_delete']) ? $_POST['item_delete'] : false;
		$positionData = isset($_POST['item_position']) ? $_POST['item_position'] : false;
 
		// Process any items selected for deletion.
		if ($deleteData !== false && is_array($deleteData))
		{
			foreach ($deleteData as $itemId)
			{
				if (is_numeric($itemId)) {
					$item = MenuItem::findById($itemId);
					$item->delete();
 
					unset($positionData[$itemId]);
				}
			}
 
			unset($itemId);
		}
 
		// Set the position data of the menu items.
		if ($positionData !== false && is_array($positionData))
		{
			foreach ($positionData as $itemId => $pos)
			{
				if (is_numeric($itemId) && is_numeric($pos)) {
					MenuItem::updatePosition($itemId, $pos);
				}
			}
		}
 
		Flash::set('success', 'Items updated!');
		redirect(get_url('plugin/menu'));
	}
 
tutorials/writing_a_plugin_2.txt · Last modified: 2012-11-06 14:15 by nowotny
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.2
Copyright 2010 wolfcms.org / design by yello studio / Wolf CMS Inside