How can i render formatted field directly in server script

How can i render formatted field directly in server script

Aryan1703Aryan1703 Posts: 65Questions: 15Answers: 1
edited May 14 in General

I have added a formatter for a field which is working perfectly fine(OL.dos). But when i am trying to format all my rows together the field "Ol.dos" return raw value i..e 1or 2. Is there a way to get the formatted value.

Thanks in advance.


Field::inst('OL.dos') ->getFormatter(function ($val, $data, $opts) { if ($val === 1) { return "Manual"; } else if ($val === 2) { return "Auto"; } }), Field::inst('OL.comment', 'comment') ->getFormatter(function ($val, $data, $field) { $details = [ ['key' => 'U1.username', 'label' => 'Operator'], ['key' => 'OM.Mode', 'label' => 'Mode'], ['key' => 'OL.dos', 'label' => 'Door Operations Switch'], ['key' => 'Y4.yesNo', 'label' => 'Are you Properly Berthed?'], ['key' => 'OL.qfmID', 'label' => 'QFM #'], ['key' => 'U4.username', 'label' => 'Other Operators'] ]; $comment = $val; // Starting with the original comment value $commentReturn = ''; foreach ($details as $detail) { if (isset ($data[$detail['key']]) && !empty ($data[$detail['key']])) { $value = $data[$detail['key']]; $label = $detail['label']; $commentReturn .= '<br><div class="data-detail col">[' . $label . ': ' . $value . ']</div>'; } } $commentReturn = $comment . '<br><div class="row">' . $commentReturn . '</div>'; return '<div class="comment-details">' . $commentReturn . '</div>'; }),

Answers

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    But when i am trying to format all my rows together the field "Ol.dos" return raw value i..e 1or 2.

    I'm not sure what you mean by formatting all of the rows together? What you have looks like OL.dos should be Manual or Auto when it hits the client-side. Is that not happening?

    Allan

  • Aryan1703Aryan1703 Posts: 65Questions: 15Answers: 1
    edited May 15

    Yes, in my cllient side it is giving me 1 or 2 instead of manual or auto .Below is a picture attached.

  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Can you give me a link to the page so I can take a bit more of a look please? The full PHP being used for that controller would be useful to.

    Thanks,
    Allan

  • Aryan1703Aryan1703 Posts: 65Questions: 15Answers: 1

    Sure, here is the link and the code(I have removed extra piece of code: "https://stagingc.assettrack.cx/operations/DailyLog.php"

    Editor::inst($db, 'ops_dailyLog OL', 'OL.id')
        ->field(
            //Table Fields
            Field::inst('OL.id', 'id'),
            Field::inst('OL.date', 'date')
                ->getFormatter(function ($val, $data) {
                    $date = new DateTime($val);
                    return $date->format('d M y');
                }),
            Field::inst('OL.endTime', 'endTime')
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('OL.entryType', 'editentryType'),
            Field::inst('OL.category', 'editCat'),
            Field::inst('U3.username', 'updatedBy'),
            Field::inst('U.username', 'controller')
                ->getFormatter(function ($val, $data, $field) {
                    $controllerReturn = $val;
                    if (isset ($data['U3.username'])) {
                        if ($data['U3.username'] != $val) {
                            $controllerReturn .= '<br><div class="data-detail">[updated:<br>' . $data['U3.username'] . '<br>' . $data['OL.updatedOn'] . ']</div>';
                        }
                    }
                    return $controllerReturn;
                }),
            Field::inst('OC.category', 'category'),
            Field::inst('OS.subCategory', 'subCategory'),
            Field::inst('OTY.type', 'type'),
            Field::inst('OL.YccBocc', 'YccBocc'),
            Field::inst('L1.location2LA', 'locationTo'),
            Field::inst('OTR.track', 'specificLoc'),
            Field::inst('OD.direction', 'direction'),
            Field::inst('L.location2LA', 'location')
                ->getFormatter(function ($val, $data, $field) {
                    $locationReturn = $val;
                    if (isset ($data['L1.location2LA'])) {
                        $locationReturn .= ' to ' . $data['L1.location2LA'];
                    }
                    if (isset ($data['OTR.track'])) {
                        $locationReturn .= '<br>' . $data['OTR.track'];
                    }
                    if (isset ($data['OD.direction'])) {
                        $locationReturn .= ' ' . $data['OD.direction'];
                    }
                    return $locationReturn;
                }),
            Field::inst('U1.username', 'operator'),
            Field::inst('OM.Mode', 'mode'),
            Field::inst('EB.EB', 'EBtype'),
            Field::inst('OL.speed', 'speed')
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('Y2.yesNo', 'wheelFlats'),
            Field::inst('OL.oscRating', 'rating')
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('OL.vatc', 'vatcReboot')
                ->options(function () {
                    return array (
                        array ('label' => 'No', 'value' => '0'),
                        array ('label' => 'Lead', 'value' => '1'),
                        array ('label' => 'Trail', 'value' => '2'),
                    );
                })
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('OL.vatcActive', 'vatcActive')
                ->options(function () {
                    return array (
                        array ('label' => 'None', 'value' => '0'),
                        array ('label' => 'A', 'value' => '1'),
                        array ('label' => 'B', 'value' => '2'),
                    );
                })
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('Y3.yesNo', 'BP'),
            Field::inst('Y4.yesNo'),
            Field::inst('Y1.yesNo', 'outOfService'),
            Field::inst('OL.qfmID', 'qfm')
                ->setFormatter(Format::ifEmpty(null)),
            Field::inst('OL.dos')
                ->getFormatter(function ($val, $data, $opts) {
                    if ($val === 1) {
                        return "Manual";
                    } else if ($val === 2) {
                        return "Auto";
                    }
                }),
            Field::inst('U4.username', 'otherOperators'),
            Field::inst('OL.comment', 'comment')
                ->getFormatter(function ($val, $data, $field) {
                    $details = [
                        ['key' => 'U1.username', 'label' => 'Operator'],
                        ['key' => 'OM.Mode', 'label' => 'Mode'],
                        ['key' => 'EB.EB', 'label' => 'EB Type'],
                        ['key' => 'OL.speed', 'label' => 'Speed'],
                        ['key' => 'Y2.yesNo', 'label' => 'Flats'],
                        ['key' => 'OL.oscRating', 'label' => 'Rating'],
                        ['key' => 'OL.vatc', 'label' => 'VATC Reboot'],
                        ['key' => 'Y3.yesNo', 'label' => 'Braking/Propulsion'],
                        ['key' => 'Y1.yesNo', 'label' => 'Out of Service'],
                        ['key' => 'OL.dos', 'label' => 'Door Operations Switch'],
                        ['key' => 'Y4.yesNo', 'label' => 'Are you Properly Berthed?'],
                        ['key' => 'OL.qfmID', 'label' => 'QFM #'],
                        ['key' => 'U4.username', 'label' => 'Other Operators']
                    ];
                    $comment = $val; // Starting with the original comment value
                    $commentReturn = '';
    
                    foreach ($details as $detail) {
                        if (isset ($data[$detail['key']]) && !empty ($data[$detail['key']])) {
                            $value = $data[$detail['key']];
                            $label = $detail['label'];
                            $commentReturn .= '<br><div class="data-detail col">[' . $label . ': ' . $value . ']</div>';
                        }
                    }
    
                    $commentReturn = $comment . '<br><div class="row">' . $commentReturn . '</div>';
                    return '<div class="comment-details">' . $commentReturn . '</div>';
                }),
            Field::inst('OL.comment', 'commentEdit'),
    
            Field::inst('A2.lrv_no', 'midLRV'),
            Field::inst('A3.lrv_no', 'trailLRV'),
            Field::inst('AI.lrv_no', 'issueLRV'),
            Field::inst('A1.lrv_no', 'leadLRV')
                ->getFormatter(function ($val, $data, $field) {
                    $leadLRV = $val;
                    $midLRV = $data['A2.lrv_no'] ?? null;
                    $trailLRV = $data['A3.lrv_no'] ?? null;
                    $issueLRV = $data['AI.lrv_no'] ?? null;
                    $lrvReturn = null;
                    if (isset ($leadLRV)) {
                        $issueClass = ($leadLRV == $issueLRV) ? ' class="text-danger"' : '';
                        $lrvReturn .= "<br><div{$issueClass}>" . $leadLRV . '</div>';
                    }
                    if (isset ($midLRV)) {
                        $issueClass = ($midLRV == $issueLRV) ? ' class="text-danger"' : '';
                        $lrvReturn .= "<br><div{$issueClass}>" . $midLRV . '</div>';
                    }
                    if (isset ($trailLRV)) {
                        $issueClass = ($trailLRV == $issueLRV) ? ' class="text-danger"' : '';
                        $lrvReturn .= "<br><div{$issueClass}>" . $trailLRV . '</div>';
                    }
                    return $lrvReturn;
                }),
    
            Field::inst('OL.followOn', 'followOn')
                ->getFormatter(function ($val, $data, $field) {
                    $follow = $val;
                    $followvalue = '';
                    $options = [];
                    $checkbox = $data['OL.checkbox'] ?? null;
                    if ($checkbox) {
                        $query = "SELECT C.label FROM ops_analysisCheckbox C WHERE FIND_IN_SET(C.id, '$checkbox')";
                        $result = query($query);
                        while ($row = mysqli_fetch_assoc($result)) {
                            $options[] = $row['label'];
                        }
                    }
                    if (!empty ($options)) {
                        $followvalue = '<br><div class="data-detail col">[Additional Actions: ' . implode(',', $options) . ']</div>';
                    }
                    $followvalue = $follow . '<br><div class="row">' . $followvalue . '</div>';
                    return $followvalue;
                }),
        )
    
        ->where(function ($q) use ($CC, $Launches, $OCS, $Day, $today1, $yesterday1, $Type, $Oscillation, $Emergency, $date) {
    
            if (isset ($CC)) {
                if ($CC == 2) {
                    $q->where('OL.YccBocc', 1, '=')
                        ->or_where('OL.YccBocc', 2, '=');
                } else if ($CC == 3) {
                    $q->where('OL.YccBocc', 1, '=')
                        ->or_where('OL.YccBocc', 3, '=');
                }
                unset ($CC);
            }
    
    
       
        })
        ->leftJoin('users_enc U', 'U.id', '=', 'OL.controllerID')
        ->leftJoin('users_enc U1', 'U1.id', '=', 'OL.operator')
        ->leftJoin('users_enc U3', 'U3.id', '=', 'OL.updatedBy')
        ->leftJoin('users_enc U4', 'U4.id', '=', 'OL.operator1')
        ->leftJoin('ops_subcategory OS', 'OS.id', '=', 'OL.subcategory')
        ->leftJoin('ops_category OC', 'OC.id', '=', 'OS.category')
        ->leftJoin('ops_type OTY', 'OTY.id', '=', 'OC.type')
        ->leftJoin('loc L', 'L.id', '=', 'OL.locationFrom')
        ->leftJoin('loc L1', 'L1.id', '=', 'OL.locationTo')
        ->leftJoin('ops_track OTR', 'OTR.id', '=', 'OL.specificLoc')
        ->leftJoin('ops_direction OD', 'OD.id', '=', 'OL.direction')
        ->leftJoin('lrv_list A1', 'A1.id', '=', 'OL.leadLRV')
        ->leftJoin('lrv_list A2', 'A2.id', '=', 'OL.midLRV')
        ->leftJoin('lrv_list A3', 'A3.id', '=', 'OL.trailLRV')
        ->leftJoin('lrv_list AI', 'AI.id', '=', 'OL.issueLRV')
        ->leftJoin('yesno Y', 'Y.id', '=', 'OL.ocs')
        ->leftJoin('yesno Y1', 'Y1.id', '=', 'OL.trainService')
        ->leftJoin('yesno Y2', 'Y2.id', '=', 'OL.EBwheels')
        ->leftJoin('yesno Y3', 'Y3.id', '=', 'OL.BP')
        ->leftJoin('yesno Y4', 'Y4.id', '=', 'OL.berth')
        ->leftJoin('EB_types EB', 'EB.id', '=', 'OL.EBtype')
        ->leftJoin('ops_operationMode OM', 'OM.id', '=', 'OL.operationMode')
    
        ->debug(true)
        ->process($_POST)
        ->json();
    
  • allanallan Posts: 61,972Questions: 1Answers: 10,160 Site admin

    Looks like it needs a login. You can PM it to me by clicking my forum user name and then "Send message".

    Allan

Sign In or Register to comment.