bigin / ItemManager_2.0

ItemManager (IM) is a simple flat-file framework for GetSimple-CMS that allows you to develop completely customizable PHP applications bundled with GetSimple-CMS.
MIT License
5 stars 3 forks source link

Has bug with itemMapper #7

Closed mittus closed 7 years ago

mittus commented 7 years ago

When I create Item from function, I can't see them on page. It's appears on ItemManager page, but not dysplayed on site page.

Code for display Items:

<?php
    $catid = 1;
    $imanager = imanager();
    $itemMapper = $imanager->getItemMapper();
    $limit = 20;
    $start = !empty($_GET['page']) ? (((int)$_GET['page'] -1) * $limit +1) : 1;
    $itemMapper->alloc($catid);
    $simpleItems = $itemMapper->getSimpleItems('active=1');
    $total = $itemMapper->countItems($simpleItems);
    $simpleItems = $itemMapper->filterSimpleItems('rdate', 'DESC', $start, $limit, $simpleItems);
    $pagination = $itemMapper->pagination();
?>
Total Items: <?php echo $total; ?>
<?php echo $pagination; ?>
...

Function, that create Item after form submit:

function create_review($rname, $remail, $rposition, $rmessage) {
    imanager();

    $item = new Item(1);
    $item->name = $rname;
    $item->active = 1;
    if($rposition !== 0)
        $item->setFieldValue('rposition', $rposition);
    $item->setFieldValue('rdate', date('d.m.Y', time()));

    if($remail !== 0)
        $item->setFieldValue('remail', $remail);
    $item->setFieldValue('rmessage', $rmessage);

    // Util::preformat($item);

    $item->save();

}

I can dysplay this Item only if update them on ItemManager page.

bigin commented 7 years ago

Yes, it's right and that's correct, because you are saving only a standard Item object, but you are trying to display an SimpleItem object.

Either you will have to save an SimpleItem object in your create_review() function too, or you will need to use the standard Items to display these on your page.

For saving SimpleItem object you can simple extend your create_review() function with following lines, after the $item->save() for instance:

...
$item->save();

$itemMapper = imanager()->getItemMapper();
$itemMapper->alloc(1);
$itemMapper->simplify($item);
$itemMapper->save();

Be careful during saving unfiltered user input:

$item->name = $rname;

better:

$item->name = $imanager->sanitizer->text($rname);

By using "setFieldValue()" method, the content will be sanitized automatically.

mittus commented 7 years ago

It's work, thank a lot!