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

Dropdown box field with another category items #16

Closed mittus closed 5 years ago

mittus commented 5 years ago

Hello, friend!! I have a question about function with update Dropdown box field.

My structure:

Category_1

Category_2

Now I use script, that starting after saving Category_1 items:

    $imanager = new \imanager();

    $category_1 = $imanager->getCategory('name=Category_1');
    $itemMapper = $imanager->getItemMapper();
    $itemMapper->alloc($category_1->id);

    $items = $itemMapper->getSimpleItems('active=1');
    $options = array();
    $options[] = ''; // it's first empty item

    foreach($items as $item) {
        $options[] = $item->name;
    }

    $category_2 = $imanager->getCategory('name=Category_2');

    $fieldMapper = new \FieldMapper();
    $fieldMapper->init($category_2->id);

    $dropdown_field = $fieldMapper->getField('name=dropdown_field');
    if($dropdown_field) {
        $dropdown_field->set('position', 1);
        $dropdown_field->set('type', 'dropdown');
        $dropdown_field->set('options', $options);
        $dropdown_field->save();

    }

And I did some fix in /lib/Field.php:

    public function set($key, $val)
    {
        $key = strtolower($key);

        // id is readonly
        if(!in_array($key, array('name', 'label', 'type', 'position',
            'default', 'options', 'created', 'updated', 'info', 'required',
            'minimum', 'maximum', 'areaclass', 'labelclass', 'fieldclass')))
            return false;

        // save data depending on data type
        if($key == 'name' || $key == 'label' || $key == 'type' || $key == 'default'
            || $key == 'info' || $key == 'areaclass' || $key == 'labelclass' || $key == 'fieldclass')
        {
            $this->$key = imanager('sanitizer')->text($val);
        } elseif($key == 'options')
        {
                        // ADD UNSET FUNCTION
            if(!empty($this->options))
            {
                unset($this->options);
            }
            if(is_array($val)) {
                $this->options = $val;
            } else $this->options[] = $val;

        } else
            $this->$key = (int) $val;
    }

AND now it's work. But I whant use ID's from items instead names, Now I whant write some script with names map for ID's to Category_2 Chunk field. But I can't write "" in $chunk_field->set('options', $script);, becouse set function use sanitizer.

What you think about that? It need some new method, or I can change set function with some crutch?

Thank You!! :))

bigin commented 5 years ago

Hi mittus, I'm not sure I understand what you're up to.

if($dropdown_field) {
    $dropdown_field->set('position', 1);
    $dropdown_field->set('type', 'dropdown');
    $dropdown_field->set('options', $options);
    $contest->save();

What does $contest->save() actually do in your example?

If I have got it right, do you want to create some kind of 1:n relationship between the items? If yes, why does it have to be a dropdown field?

mittus commented 5 years ago

My customer need create items in category_1 and chose them for items in category_2

List items from category_1 screenshot_39

List Dropdown box field options from item in category_2 screenshot_40

mittus commented 5 years ago

$contest->save(); - it's my error. It is $dropdown_field->save();

bigin commented 5 years ago

Okay, I get it. I'm on my way, check it out later... ;-)

bigin commented 5 years ago

I'd take a different approach: I would extend the functionality of ItemManager admin methods by adding your own logic. Here are the methods you should work with: Admin::buildItemEditor(), Manager::deleteItem($id, $catid) and Manager::saveItem(& $input).

Take a look at this article, there I described how you can extend ItemManager functionality without having to modify the core files. It's relatively easy because you only have to copy most of the native function code. Note: you'll need to copy the plugins/imanager/lib/inc/config.php file to data/imanager/settings/config.php directory and set the $this->injectActions variable to true, so you are able to inject your logic into the ItemManager methods.

I had to do something similar once, at that time I worked with cities and countries. I'll give you an example so you can see how I solved that. Just a moment, please.

bigin commented 5 years ago

Here is a plugin that should demonstrate the functionality, you can play with it if you want: https://gist.github.com/bigin/7ace7c26314fc394b83a38498c3a0c09

To be able to test this example, you have to create 2 categories:

countries-category

and ...

category-cities

Now, you've to create a bunch of cities:

cities

When you create the countries you will see the dropdown field:

russia countries

Hope that helps you a little ;-)

mittus commented 5 years ago

I used similar method, variant with custom option and $selected[$city->id] = $city->name; will solve my problem. Thank You!!

https://gist.github.com/mittus/dc0118a366b3b527588581bd3afc268c