Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ItemManager 2.0
Ok thx.

(2019-01-24, 23:14:52)PGK37 Wrote: I do not know, it looks like they changed when I uploaded to the server.

Nothing here changes on its own... First you have to determine if the item ID's on the hosts are identical or not. I guess that's the problem.

Run some tests:
  1. Make backups.
  2. Delete all items on the OVH host also under: ⁨data/imanager⁩/⁨items⁩/*
  3. Create a new item for a "problem-page" with IM Extra Fields and compare the ID with localhost.
Reply
(2019-01-24, 23:44:29)Bigin Wrote: Ok thx.

(2019-01-24, 23:14:52)PGK37 Wrote: I do not know, it looks like they changed when I uploaded to the server.

Nothing here changes on its own... First you have to determine if the item ID's on the hosts are identical or not. I guess that's the problem.

Run some tests:
  1. Make backups.
  2. Delete all items on the OVH host also under: ⁨data/imanager⁩/⁨items⁩/*
  3. Create a new item for a "problem-page" with IM Extra Fields and compare the ID with localhost.

Thanks for your feedback.
Do I have to delete the data sides OVH and LOCALHOST and add a new element on both sides to make this comparison?
Reply
I deleted the data on both sides and created elements. The IDs seem to be identical.
The problem arises as soon as a photo is added it seems
Reply
Then look at GS log file, there you will find the problem
Reply
Hi Bigin,

I just came across ItemManager and IM Extra Fields.
I want to personally thank you for creating ItemManager and IM Extra Fields for GS.
These are amazing applications and make any sort of website possible.
F.
Reply
Hello Felix, thank you for your interest in ItemManager, enjoy using it! ;-)
Reply
Hi,
I just started to begin to use the ItemManager. I have put this code from an example
in the top of template.php from the Innovation theme:


Code:
<?php
$imanager = imanager();
$item = new Item(1);
$item->name = 'My item name';
$item->setFieldValue('data', 'Hello world');
$item->save();
?>


Then I want to output in the template.php from the Innovation theme like this:

Code:
<?php
$item = imanager()->getItem(1, 'name=My item name');
echo $item->fields->data->value;
?>

But Hello world is not outputted. What am I doing wrong ?
Reply
Hi Felix,

you probably have not created a category yet?

Are you a bit familiar with MySQL databases? So you could see it that way:

1. A category is similar to a table in the database.
2. A category is assigned different fields. Fields are the columns in your table.
3. And the items are the datasets.

To be able to create your datasets (items), you will first create a table (category) with columns (fields).

Try ItemManager v3 the API is much easier to get familiar with it:

https://im.ehret-studio.com/tuts/itemman...formation/
Reply
Hi Bigin,

Thanks for your reply,

Yes I read about ItemManager v3 but I want to learn everything about ItemManager v2 first
because it integrates so nicely with GS.

What I did:

I added your helper function in the Theme file functions.php

function get_page_item($category_id) {
    $imanager = imanager();
    $mapper = $imanager->getItemMapper();
    $mapper->alloc($category_id);
    $pageId = Util::computeUnsignedCRC32(return_page_slug());
    return $mapper->getSimpleItem($pageId);
}


Then I added this test code to the Theme template file template.php

<?php
$item = get_page_item(1);
if($item) {
echo "<h3>$item->name</h3>";
echo $item->text;
}
?>


Now the contents of the fields name and text are published on the page,
this is great, my first successful steps into ItemManager !

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I still have problems getting field contents published on a page
without the helper function when I try to use this way:

$imanager = imanager();

// to get a specific item by "category id" and "item id" use

$item = $imanager->getItem(1, 2155046657);
echo "<h3>$item->name</h3>";
echo $item->text;

What am I missing here ? Do I have to use here also:

$imanager = imanager();
$mapper = $imanager->getItemMapper();
$mapper->alloc($category_id);

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

I have created a category, fields and added contents to the fields
when I edit a page. So far so good.

However following this example still does not work for me:

$imanager = imanager();   

$item = new Item(1);   
$item->name = 'My item name';   
$item->setFieldValue('data', 'Hello world');   
$item->save();   

In order to bring the data into display again do this:

$item = imanager()->getItem(1, 'name=My item name');   
echo $item->fields->data->value; // Outputs: Hello world   


What am I missing here ? Do I have to use here also:

$imanager = imanager();
$mapper = $imanager->getItemMapper();
$mapper->alloc($category_id);

Thanks in advance,
F.
Reply
Hi,

you're almost doing it right. 
This is your mistake:
PHP Code:
echo $item->text

The main difficulty with IM2 is that there are 2 types of item-object. The first one is native "Item" object and the second one is "SimpleItem" object - this can be really confusing...
It's better to use native "Item" object only if you intend to modify an item, e.g. to store a value in an item field, etc. But if you want to just view the items, you should rather use the "SimpleItem" object.

So let's get back to your problem. With this method you select an "Item" object, but access the "text" field as if it were a SimpleItem object:
PHP Code:
$item $imanager->getItem(12155046657); // $item is an "Item" object
echo $item->text// <- There's wrong SimpleItem method 

If you want to access an "Item" object field value, you have to do it this way:
PHP Code:
echo $item->fields->text->value
// or:
echo $item->getFieldValue('text'); 

In the case above, you don't want to have "write" access to the item, you only want to show the value of a field. That' s why it is better to use SimpleItem object in this case. To be able to operate with SimpleItem objects, you should use the ItemMapper class. ItemMapper - is a class that allows you to work with Items e.g. to select, sort or filter them:

PHP Code:
$imanager imanager();
$itemMapper $imanager->getItemMapper(); 

To be able to work with the items of a category with ID 1, you have to load them into the memory first, you can do this as follows:
PHP Code:
$itemMapper->alloc(1); 

Now, the items of your category are loaded in memory. To select your item (SimpleItem) with the ID 2155046657, do the following:
PHP Code:
$SimpleItem $itemMapper->getSimpleItem(2155046657); 

You can access the value of an SimpleItem object as follows:
PHP Code:
echo $SimpleItem->text

Your example here should work. Check again the ID's. Check under "Admin" > "Manager" > "Items", whether an item was created:
PHP Code:
$imanager imanager();
$item = new Item(1);   
$item
->name 'My item name'  
$item
->setFieldValue('data''Your field value');   
$item
->save(); 

Hope that helps you a little.
Reply
Hi Bigin,

Thanks a lot for your reply and clear explanation about the difference between
"Item" object and "SimpleItem" object.

I followed your instruction and replaced my code with:

Code:
echo $item->fields->text->value


And immediately I have my code working here:

Code:
<?php
$imanager = imanager();
$itemMapper = $imanager->getItemMapper();
$itemMapper->init(2);       //  Category: 2 | Item: 2147483647      an Item can have a name number and an id number ?
$items = $itemMapper->items;
foreach($items as $item) {
echo $item->name . "<br>";  // $item->name = item name or id        Category 2 has 1 item with name = 3052675811 and id = 2147483647 ?
echo $item->fields->text->value . "<br>";   // $item->name = category name or id
}
echo "<br>";
$my_item = $itemMapper->getItem('name= 3052675811');  // search for a specific item
echo $my_item->fields->text->value . "<br>";
?>


Thanks again, I start to understand it better now

Although I used $itemMapper->init(2);
instead of
$itemMapper->alloc(2);

Is there a difference between init and alloc ?
Reply
Hi Felix,

yeah there is a difference.
This is the method for loading the SimpleItem objects of a specific category into memory. There is only this one method:
PHP Code:
$itemMapper->alloc($category->id); 

To load the items of type "Item" object, there are a lot of various methods. This allows you to manage the available memory more efficiently, e.g. to avoid loading all items of a category into memory at once. This can be very helpful, when creating pagination for a large number of items.
PHP Code:
$itemMapper->quickInit($category->id[, $fields$start$bulk$pat]);
$itemMapper->limitedInit($category->id$index$limit);
$itemMapper->init($category->id);
$itemMapper->initAll(); // To get all Items as an multidimensional array (Note: it can be very memory sensitive) 
Reply
Hi Bigin,

Thanks for your reply and explaining the various methods how to load the items of type "Item" object

And yes, like you mentioned,  I can confirm that this code is now working also:

Code:
$imanager = imanager();
$item = new Item(1);  
$item->name = 'My item name';  
$item->setFieldValue('data', 'Your field value');  
$item->save();


I believe that the word 'data' in the example above is used as a reference to a field type e.g. a text field
and that data is not a field type by it self. But maybe this is already something obvious to know.
But I am not sure about that because I have seen this somewhere also: $item->fields->color->value
and color is not listed in the list of available field types.

I use the code above like this now with success:


Code:
$imanager = imanager();
$item = new Item(1); // create a new item in Category 1, this new item gets automatically the fields already setup in Category 1
$item->name = 'next item';  // give the new created item a name
$item->setFieldValue('text', 'Hello world');
$item->save();       // Go to the admin => Manager => view all and check if a new item was created
           
$item = imanager()->getItem(1, 'name=next item');
echo $item->fields->text->value;  // check that output is what was saved: Hello world  

When I go to back end admin => Manager => View All => Category1 => on the bottom of the page
I can see that the new item has been created.

F.
Reply
Just FYI:

The Item object method setFieldValue() is used to populate the field value appropriate to its concept.
The method can contain 3 parameters:
Code:
setFieldValue($field_name, $field_value[, $sanitize])

(mixed) $field_name - is the name of the field which value should be filled.
(mixed) $field_value - is the field value.
(boolean) $sanitize (optional) - is a flag that can be used to sanitize the passed $field_value according to the field concept. This value is set to "true" by default.

For example, to set a value of the field of type "text" with the name "content", you can use the method as follows:

PHP Code:
$item->setFieldValue('content''bla-<script>alert("XSS")</script>');
echo 
$item->setFieldValue('content'); 

it echoes: bla-alert("XSS")
Because sanitize flag is set to "true" by default and it removes any HTML.

Another example. If you have set the minimum length of 20 characters for the field content (under IM field details):

PHP Code:
$item->setFieldValue('content''bla-bla'); 

This method returns "false" and the value "bla-bla" will not set because it is shorter than 20 characters. To find out why the method does not have the value set you can check the error code:

PHP Code:
if(!$item->setFieldValue('email''bla-bla')) {
 
   echo MsgReporter::errorCode(); // 2 - The length of field value is less than *minimum*


Here you can get a list of error codes:
https://ehret-studio.com/articles/itemma...ith-items/

If you do not want to sanitize the value, you can set the $sanitize flag to false:
PHP Code:
if(!$item->setFieldValue('content''<script>alert("XSS")</script>'false)) {
 
   echo MsgReporter::errorCode();
}
echo 
$item->getFieldValue('content'); 
Reply
Hi Bigin,

Thanks for explaining more about setFieldValue() and the list with errors.
I found there also the api reference for items and categories.
Also I found scriptor based on ItemManager.

ItemManager is really powerful with a wide range of applications.

I think it is also the solution for a hidden page to manage other pages.

See posts #5 and #6 in this thread:
http://get-simple.info/forums/showthread.php?tid=13857
Reply
What is the minimum PHP version for ItemManager version 2 - GS plugin ?
Reply
I guess it needs to be a minimum of 5.6 ... maybe 5.4 would do, not tested. PHP7 + is recommended.
Reply
Hi,

For some reason I don't manage to echo an image on the front end.

I have only 1 image with the image name laptop.png inside:
Category id=1
Category name = HomeCategory
Item id = 1
Item name = first_home_item
field name = image (File upload field)

I searched the ItemManager thread and this is what I found how to echo an image on the front end:


Code:
echo '<img alt="" src="'. htmlspecialchars($item->fields->image->imagefullurl[0]). '" width="200" >';


But the image laptop.png does not appear on the front.

What am I doing wrong ?
Reply
It should be:
PHP Code:
$item->fields->image->fullurl[0

You may also want to use Sanitizer's url() method:
PHP Code:
$url IM_SITE_URL.$imanager->sanitizer->url($item->fields->image->fullurl[0]);
echo 
"<img alt='' src='$url' width='200'>"
Reply
Wink 
Hi Bigin,

Yes now I have it working, thanks for helping out.
The image is finally published on the front end !

I didn't even know about IM_SITE_URL, are there more of those ?
Reply
Here you will find the IM constants: plugins/imanager/lib/inc/_def.php

By the way, you can also use following function to modify the images. It returns an image url sized/cropped to the specified dimensions:
PHP Code:
/**
 * Returns URL to resized image
 * The first time it is called, a thumbnail image is created and stored temporarily.
 *
 * @param object $image   - An SimpleItem object
 * @param integer $id     - An index of the image field
 * @param integer $width  - Thumb width
 * @param integer $height - Thumb height
 *
 * @return string - The image url indexed by $id
 *
 */
function getResizedUrl(\SimpleItem $item$id 0$width 200$height 0$type 'adaptiveResize')
{
 
   global $SITEURL;
 
   if(empty($item->image[$id])) return;
 
   if(!file_exists(GSROOTPATH.$item->image[$id])) return;
 
   $relpath dirname(@$item->image[$id]);
 
   $path dirname(GSROOTPATH.$item->image[$id]);
 
   $base basename(GSROOTPATH.$item->image[$id]);
 
   if(!file_exists($path.'/thumbnail/'.$width.'x'.$height.'_'.$base)) {
 
       $thumb = \PhpThumbFactory::create(GSROOTPATH.$item->image[$id]);
 
       $path_parts pathinfo(GSROOTPATH.$item->image[$id]);
 
       $thumb->{$type}($width$height);
 
       $thumb->save($path.'/thumbnail/'.$width.'x'.$height.'_'.$base$path_parts['extension']);
 
   }
 
   return $SITEURL.$relpath.'/thumbnail/'.$width.'x'.$height.'_'.$base;


Note that you must pass a SimpleItem as the first parameter.
Reply
Hi,

I found following code in the thread what comes handy for debugging purposes:


Code:
foreach($your_item->fields->image->imagename as $imagename) {
   if($imagename == 'your_image.jpg')
   echo $imagename;
}


However, this part does not seem to output anything:

$your_item->fields->image->imagename

Is imagename still a valid part of ItemManager version 2 ?
Reply
Hello,

image_name is an image field property, this is deprecated and no longer available.
There are available properties for the FileUpload field (excluding of the default field properties):

1. file_name
2. path
3. fullpath
4. url
5. fullurl
6. title

You can check this with:
PHP Code:
Util::preformat($item->fields->image); 
Reply
Hi,

Thanks for showing the IM constants and the available FileUpload field properties.

Util::preformat($item->fields->image); is also super cool for debugging.

With this info I could debug my faulty code and now have everything working.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Fun fact is that echo '<img alt="" src="'. $my_item->fields->image->fullurl[0] .'" width="100" >'
is working on the home page but not on the about page

On the about page I have to add IM_SITE_URL to make the image show on the front
echo '<img alt="" src="'. IM_SITE_URL . $my_item->fields->image->fullurl[0] .'" width="100" >'; // this is working !

Maybe this has something to do with the Innovation theme or maybe Laragon local server
that I am using.

Anyway, IM_SITE_URL is really very handy to use.

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

I had a look at ItemManager v3 but it does not seem to have a visual GUI.
I guess it was designed to use it programatically. Advantage is that it
makes it independent of a CMS and can use it anywhere as a php include.

What I like about ItemManager v2 as a plugin for GS is the visual GUI
under the Manager Tab in the GS backend. The visual GUI makes it easy.
Reply
Hello Felix,

yes you are right, it has no GUI by default. But it has a much more powerful and simple API, it scales well and has 10x better performance than IM2.

It would also be a great challenge for someone who wants to discover the new API to write a plugin for GetSimple that provides a simple GUI for ItemManager3? ;-)

Anyway, I hope you enjoy IM whether 2 or 3 :-)
Reply




Users browsing this thread: 3 Guest(s)