ItemManager API Reference - ItemManager class

Erstellt am: Tuesday 27 December 2016  |  Letzte Änderung am: Tuesday 28 August 2018

This article describes basic functionality, methods and their parameters of the ItemManager class.

API Elements

ItemManager's API is composed of the following main parts:

  • ItemManager - The ItemManager base class
    • Primary:
      • CategoryMapper - Enables loading and manipulation of Category objects.
      • ItemMapper - Enables loading and manipulation of Item objects.
      • FieldMapper - Enables loading and manipulation of Field objects.
    • Input:
      • Sanitizer - Sanitizing and validating user input and preparing data for the output.
    • Utilities & Helpers:
      • SectionCache - Enables caching of markup, strings, arrays or objects.
      • TemplateEngine - A simple ItemManager class to separate HTML and PHP.
      • Util - Enables creation of logs, logging of events, formatting output etc.

Bird’s-eye view

Here’s a bird’s-eye view of ItemManager's primary part:

ItemManager Class

The ItemManager class is the base class for most ItemManager's classes and modules and it's the core of ItemManager flat-file framework.

Note, in order to save server resources, ItemManager variable ist not automatically available for use, you must always initialize ItemManager base class before you can use it.

The easiest way to initialize an instance of ItemManger class is to use imanager() function. The ItemManager class can be accessed from templates and/or plugins of GetSimple-CMS. So in order to create an API variable just call:

// Get instance of the ItemManager class
$imanager = imanager();

You can also use ItemManager class within your function scope:

// Get instance of the ItemManager class inside local function scope
function test() { 
    $imanager = imanager(); /* reference to ItemManager API variable inside local scope */ 

Categories and Items

After that, you're now able to work with categories and items. I would like to add at this point, that I have always tried to make ItemManager as efficient as possible and to find the most effective way of memory management. Memory isn't an unlimited resource, there is always a limit as how many items and categories you can keep in memory at once. That's why ItemManager offers a several options for accessing the items or categories, these methods can be used depending on the situation and your requirements.

Your choice which of the available methods you should use depends, for example, on two factors such as:

  • How many items do you want to load into memory?

  • Do you want a write access or read-only access to the items?

As from Version 2.3.8 ItemManager provides new item object type called SimpleItem. If you do't need a write access it's recommended to use the SimpleItem objects to deal with items. However, since all items belong to a certain category, you must specify the category ID before you can fetch the items. If you do not know the category ID, you can refer to it by name or other unique attribute. If you want to work with categories, you should always use the CategoryMapper. CategoryMapper is used to retrieve one or more categories using specific selectors:

$categoryMapper = $imanager->getCategoryMapper();

Now, you can use $categoryMapper to retrieve a category with ID 3:

$category = $categoryMapper->getCategory(3);
echo $category->name; // Outputs: Products

You can also refer to a category by its name:

$category = $categoryMapper->getCategory('name=Products');
echo $category->id; // Outputs: 3

If you want to work with items, you should use the ItemMapper class, it's used to retrieve one or more items using specific selectors. Since we do not work with a relational database, the items of a certain category must first be loaded into the memory, for SimpleItem objects this is done with the method alloc().

$itemMapper = $imanager->getItemMapper();
$itemMapper->alloc($category->id); /* Our selected category id */

Now that we have selected our product category and loaded all items belonging to this category into the memory. Let's try to find an item called Shoes:

$item = $itemMapper->getSimpleItem('name=Shoes');
if($item) {
	echo $item->name; // Outputs: Shoes

Here is the complete script for the previous work and your reference:

// Get ItemManager instance
$imanager = imanager();

// Get the CategoryMapper
$categoryMapper = $imanager->getCategoryMapper();
// Select category with ID 3
$category = $categoryMapper->getCategory(3);

// Get ItemMapper
$itemMapper = $imanager->getItemMapper();
// Load all SimpleItem objects from category id 3 into memory 

// Select item called Shoes
$item = $itemMapper->getSimpleItem('name=Shoes');
// Output the item name if item is found
if($item) {
	echo $item->name;

More infos about ItemManager's categories: Working with Categories

More infos about ItemManager's items: Working with Items

Lots of the API objects in ItemManager are chainable, using a fluent interface. This enables you to accomplish multiple operations in only one or a few lines code. For example, previous script in just 3 lines code:

$item = imanager()->getItemMapper()->getSimpleItem('name=Shoes');
echo $item->name;

ItemManager:: Methods

  • getTemplateEngine()
  • getSectionCache()
  • getCategoryMapper()
  • getItemMapper()
  • getCategory()
  • getItem()
  • getItems()
  • filter()
  • getCategoryClass()
  • getFieldsClass()
  • getItemClass()
  • setAdmin()
  • setActions()
  • ProcessCategory()
  • deleteCategory()
  • createCategoryByName()
  • updateCategory()
  • createFields()
  • saveFieldDetails()
  • deleteSearchIndex()
  • buildPagination()
  • saveItem()
  • deleteItem()
  • renameTmpDir()
  • cleanUpCachedFiles()
  • cleanUpTempContainers()

Autor: Bigin  |  Tags:  GetSimpleItemManagerFrameworkPHPScriptsDevelopment