phptiki-wiki

tiki-wiki tracker entry validation using another tracker


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;
 }
 ?>

Solution

  • 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.).