tiki-wiki tracker entry validation using another tracker

188 Views Asked by At

So here's the situation: I have two Tiki-Wiki trackers: One called Orders and the other called Customers. When a new item is added to the Orders tracker, one of the fields required is the customer, selected from the Customer tracker. Occasionally we have delinquent customers and need to "blacklist" them, preventing new order items being created for this customer. What is the best way to accomplish this?

I figured the best way to do this it to build a custom validator, as seen here: https://doc.tiki.org/Tracker+Field+Validation. I would then have a new field in the customers tracker that would indicate if they are on the blacklist. The validator would look up the customer and if they are blacklisted, disallow entering a new order.

My (poor) attempt at this is below:

 <?php 


 function validator_Blacklist($input, $parameter = '', $message = '')
 {
    $trklib = TikiLib::lib('trk');
    //parse_str($parameter, $arr);  
    //$info = $trklib->get_tracker_field($arr['fieldId']);

     $bl = $trklib->get_item(4,204,$input);
    if($bl>=1)
       return tra("Customer is blacklisted.");

    return true;
 }
 ?>
2

There are 2 best solutions below

0
On BEST ANSWER

Okay, so I did manage to solve this using a validator:

 <?php 

 function validator_Blacklist($input, $parameter = '', $message = '')
 {
    $trklib = TikiLib::lib('trk');  

    $query = strtoupper(trim($input)); //$input has a trailing space which affects the query, strtoupper probably not needed

    $result = $trklib->get_item_id(4,14,$query,false); //usage: get_item_id(tracker_id,field_id,string_query,partial_match);

    $info = $trklib->get_tracker_item($result); //pass item id retrieved above to get all it's fields

    $status = intval($info[204]); //array item 204 has the customer service status, 205 has a descriptive comment
    if ($status==1) {       
        return tra("<strong><font color=red>DO NOT SERVICE: " . $info[205] . "</font></strong>");    
    }   
    return true;
 }
 ?>

There was also an issue where my field type (Item Link) was passing undefined $input. Some digging showed that validatorslib.php wasn't handling Item Link as a drop-down type (the letter key for Item Link being 'r'):

                    if ( $field_value['type'] == 'g' or $field_value['type'] == 'e' or $field_value['type'] == 'y' or $field_value['type'] == 'd' or $field_value['type'] == 'D') {
                        // Let's handle drop-down style fields
                        $validationjs .= 'return $(\'select[name="'.$prefix.$field_value['fieldId'].'"] option:selected\').text(); ';
                    } else {    // Let's handle text style fields
                        $validationjs .= 'return $("#'.$prefix.$field_value['fieldId'].'").val(); ';

I changed the first line to:

                    if ( $field_value['type'] == 'g' or $field_value['type'] == 'e' or $field_value['type'] == 'y' or $field_value['type'] == 'd' or $field_value['type'] == 'D' or $field_value['type'] == 'r') {

Everything seems to be working now (and I don't think I broke any other functionality in the process.) I will probably make this more robust when I have time (take the tracker and fields as parameters, etc.).

1
On

I don't think a validator is the right way to go (sorry, not allowed to comment, but hoping to help). This surely is a permissions question.

Assuming that the Customer tracker is a "User Tracker" and these users are logged in, you could use the banning system to prevent those users from using various parts of Tiki, e.g. Trackers. More here on that: http://doc.tiki.org/Banning

Alternatively you put them in a "Blacklisted" group and remove the permission to create new tracker items from them (or even spot them seeing the order form in the first place).

If however these customers are not logged in (which would be odd) and the Customer field in the Orders tracker is an ItemLink, you could set that to only accept "open" status Customer items on the field options, and set the status of blacklisted Customers to "closed". That would in effect "validate" that field for you without needing to write new code.