Jump to content

Smart Index / Counter (Pro?)field


kater
 Share

Recommended Posts

I'm currently discussing a project where we'd need some sort of automated, smart id/indexing to meet internal documentation standards.

Having this available as premade field may be very useful for many.

1) searchable

2) bind counting to template & case or sitewide

3) allow some smart pattern programming or excel-like logic: like range, steps, numbers, strings, external fields, time, day, month, rules, ....

4) example: invoice_2015-01-25_123_customername

5) optional overrideable with continuation or filling gaps

7) error states for overrides? (like exists, out of range, ...)

8) optional as array if it makes sense (not for me atm).

cheers!

Link to comment
Share on other sites

That's quite a hefty feature set you're talking about, especially with the "smart programming" and "different binding" options. I'm not aware of any similar modules out there, but by now I think you would need to build such a thing by yourself. It doesn't necessarily need to be a new fieldtype as you could also use the existing fields and do the logic in a autoload module, which runs on page save. 

Link to comment
Share on other sites

If it's only about the knowledge, there are more people besides Ryan here on the forums, which have the ability to build such modules. The thing is, there has to be a big  enough market to sell something like this to. But maybe this topic can act as a display for the need of such a module in the community.

  • Like 2
Link to comment
Share on other sites

One possibility is to hook ProcessPageEdit::buildForm and build your wanted values from there. So no need for special fieldtype & Inputfield because it looks like overkill to me. Ps, the code here is not tested and written in the browser, it's more to give you an idea.

<?php

public function init() {
    $this->addHookAfter('ProcessPageEdit::buildForm', $this, 'afterPageEditBuildForm');
}

public function afterPageEditBuildForm(HookEvent $event) {

    $form = $event->return;
    $page = $event->object->getPage();
    $wantend_template = 'your-template'; // make this your template

    if (!$page->id) return;

    // return if wrong template
    $template = $page->template;
    $string = 'invoice_'; // for building your field value
    
    if ($template->name !== $wantend_template) return; // Skip if not wanted template
    
    $fieldnames = array(
        'invoice_date', // Field named invoice_date
        'customer', // Field named customer
        'follow_up', // integer field.
        );
    
    foreach ($fieldnames as $fieldname) {

        // Inputfield
        $obj = $form->getChildByName($fieldname);

        // Build field value
        if ($obj->type == 'FieldtypeDatetime') {
            $string .= strtotime('Y-m-d', $obj->value);
        } else if ($obj->type == 'FieldtypeInteger') {
            /**
             * Make some logic to find the follow_up number field value
             *
             */

            $integer = '123456789'; // calculate it here

            $obj = $form->getChildByName($fieldname);

            if ($obj->value != $integer) {
                $obj->value = $integer;
                $obj->resetTrackChanges();
                $obj->trackChange('value'); 
            }

            $string .= $integer;
   
        } else if ($obj->type == 'FieldtypePage') {
            $string .= $obj->value->name;
        }
    }
    
    // We use the title for your invoice value
    $InputfieldTitle = $form->getChildByName('title');
    $value = $InputfieldTitle->value;
    
    if ($string !== $value) {
        $InputfieldTitle->resetTrackChanges();
        $InputfieldTitle->trackChange('value');
        $InputfieldTitle->value = $string;
    }
}

Edited by Martijn Geerts
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...