Forum

Thread tagged as: Add-on-development

perch add-on building: repeat regions

I need to do 2 things:

  1. upload multiple images for a store
  2. allow user to add multiple phone numbers for a store

is this something I can user to save in 1 table with json encoded or I need multiple tables to save this information.

How would I design admin so that it allows multiple field inputs?

sawan ruparel

sawan ruparel 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Would you not use a repeater?

Drew, you are absolutely right, and you actually saved me from a lot of work that I was headed in wrong direction.

I realised that I was suppose to define only static fields in edit.post.php and edit.pre.php. I was doing all fields :-(

repeater worked fine, except the images are not selected by default , here is the image of before saving https://snag.gy/B7Itj.jpg

after saving screenshot of db and admin area https://snag.gy/fDdnw.jpg

you can see that the address is also saved in db, but they are not populated with data from db at the time of edit

Here is how my edit.post.php looks like

<?php    
    # Side panel
    echo $HTML->side_panel_start();

    echo $HTML->side_panel_end();


    # Main panel
    echo $HTML->main_panel_start();
    include('_subnav.php');

    echo $HTML->heading1($heading1);


    if ($message) echo $message; 
    echo $HTML->heading2('Thing details');

    $template_help_html = $Template->find_help();
    if ($template_help_html) {
        echo $HTML->heading2('Help');
        echo '<div id="template-help">' . $template_help_html . '</div>';
    }


    echo $Form->form_start();

        echo $Form->text_field('storeName', 'Store Name', isset($details['storeName'])?$details['storeName']:false, 'xl');

        echo $Form->text_field('state', 'State', isset($details['state'])?$details['state']:false, 'm');
        echo $Form->text_field('city', 'City', isset($details['city'])?$details['city']:false, 'm');

        $opts = array();
        $opts[] = array('label'=>'Pink ladies 0', 'value'=>'0');
        $opts[] = array('label'=>'Pink ladies 1', 'value'=>'1');
        $opts[] = array('label'=>'Pink ladies 2', 'value'=>'2');
        $opts[] = array('label'=>'Pink ladies 3', 'value'=>'3');
        $opts[] = array('label'=>'Pink ladies 4', 'value'=>'4');
        $opts[] = array('label'=>'Pink ladies 5', 'value'=>'5');
        $opts[] = array('label'=>'Pink ladies 6', 'value'=>'6');
        echo $Form->select_field('rating', 'Rating', $opts);


        echo $Form->checkbox_field('active', 'Active', '1', isset($details['active'])?$details['active']:false);
        echo $Form->text_field('sh6_store_id', 'SH6 ID', isset($details['sh6_store_id'])?$details['sh6_store_id']:false, 'm');

        echo $Form->fields_from_template($Template, $details, $Things->static_fields);

        echo $Form->date_field('DateTime', 'Date', isset($details['DateTime'])?$details['DateTime']:false, true);

        echo $Form->submit_field('btnSubmit', 'Save', $API->app_path());


    echo $Form->form_end();



    echo $HTML->main_panel_end();


here is edit.pre.php file

<?php

    $Things = new MySample_Things($API);

    $HTML     = $API->get('HTML');
    $Form     = $API->get('Form');
    $Text     = $API->get('Text');
    $Template = $API->get('Template');

    // Load master template
    $Template->set('storelocator/thing.html', 'storelocator');

    $result = false;
    $message = false;

    //check to see if we have an ID on the QueryString
    if (isset($_GET['id']) && $_GET['id']!='') {
        //If yes, this is an edit. Get the object and turn it into an array
        $storeID = (int) $_GET['id'];    
        $Thing = $Things->find($storeID, true);
        $details = $Thing->to_array();

        $heading1 = 'Thing / Edit Thing';

    }else{
        //If no, we're adding a new one
        $Thing = false;
        $storeID = false;
        $details = array();

        $heading1 = 'Thing / Create New Thing';
    }

    // handle creation of new blocks
    $Form->handle_empty_block_generation($Template);

    //set required fields
    $Form->require_field('storeName', 'Required');
    $Form->set_required_fields_from_template($Template, $details);


    if ($Form->submitted()) {
        $postvars = array('storeID', 'storeName', 'state', 'city', 'rating', 'active', 'sh6_store_id');
        $data = $Form->receive($postvars);

        //picks up the date field and assembles a date that MySQL will be happy with
        $data['DateTime'] = $Form->get_date('DateTime');


        // Read in dynamic fields from the template
        $previous_values = false;

        if (isset($details['DynamicFields'])) {
            $previous_values = PerchUtil::json_safe_decode($details['DynamicFields'], true);
        }

        $dynamic_fields = $Form->receive_from_template_fields($Template, $previous_values, $Things, $Thing);
        $data['DynamicFields'] = PerchUtil::json_safe_encode($dynamic_fields);


        //if we have a Thing update it
        if (is_object($Thing)) {
            $result = $Thing->update($data);
        }else{
            //otherwise create it
            if (isset($data['storeID'])) unset($data['storeID']);

            $new_thing = $Things->create($data);

            if ($new_thing) {
                PerchUtil::redirect($API->app_path() .'/edit/?id='.$new_thing->id().'&created=1');
            }else{
                $message = $HTML->failure_message('Sorry, that thing could not be updated.');
            }
        }


        if ($result) {
            $message = $HTML->success_message('Your thing has been successfully updated. Return to %sthing listing%s', '<a href="'.$API->app_path() .'">', '</a>');  
        }else{
            $message = $HTML->failure_message('Sorry, that thing could not be updated.');
        }

        if (is_object($Thing)) {
            $details = $Thing->to_array();
        }else{
            $details = array();
        }

    }

    if (isset($_GET['created']) && !$message) {
        $message = $HTML->success_message('Your thing has been successfully created. Return to %sthing listing%s', '<a href="'.$API->app_path() .'">', '</a>'); 
    }

Drew McLellan

Drew McLellan 2638 points
Perch Support

Have you checked your debug?

Debug log seems fine. can't see any error.

Please see attached. https://snag.gy/CM3xY.jpg

I can see the information in db, its just not pre-populating the dynamic fields from db on edit screen

Drew McLellan

Drew McLellan 2638 points
Perch Support

Ok. Let me know what you find.

I can't find anything. I am hoping you can look at above code for 2 files and let me know why dynamic fields are not pre-populating

Drew McLellan

Drew McLellan 2638 points
Perch Support

It's probably your class files, but this isn't something I can do for you.