If statement for datatables editor

If statement for datatables editor

davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
edited February 2021 in Free community support

I am trying to add an if statement inside the editor PHP file for the DB connection and query.

if($_SESSION['role'] != 'Admin') {
    ->where( 'locations.district', $location )
    }else{}

    ->leftJoin( 'locations', 'allUpPlanChange.Agent_ID', '=','locations.Outlet_ID'  )

This throws an ajax error.

I want to remove that where clause if user is admin. Where and how can I achieve this?

Full Query

<?php

session_start();

$location = $_SESSION['district'];


require '../../../vendor/autoload.php'; //Dont forget to edit this
include( "lib/DataTables.php" );

use
    DataTables\Editor,
    DataTables\Editor\Field,
    DataTables\Editor\Format,
    DataTables\Editor\Mjoin,
    DataTables\Editor\Options,
    DataTables\Editor\Upload,
    DataTables\Editor\Validate,
    DataTables\Editor\ValidateOptions;

    Editor::inst( $db, 'allUpPlanChange' )
        ->fields(
            Field::inst( 'allUpPlanChange.Agent_ID' ),
            Field::inst( 'allUpPlanChange.Tran_Year' ),
            Field::inst( 'allUpPlanChange.Tran_Period' ),
            Field::inst( 'allUpPlanChange.Original_Mobile_ID' ),
            Field::inst( 'allUpPlanChange.Mobile_ID' ),
            Field::inst( 'allUpPlanChange.Alt_Mobile_ID' ),
            Field::inst( 'allUpPlanChange.Device_ID' ),
            Field::inst( 'allUpPlanChange.Account_Number' ),
            Field::inst( 'allUpPlanChange.Device_Change_Date' )
            ->validator( Validate::dateFormat( 'Y-m-d' ) )
             ->getFormatter( Format::dateSqlToFormat( 'Y-m-d' ) )
             ->setFormatter( Format::dateFormatToSql('Y-m-d' ) ),
            Field::inst( 'allUpPlanChange.Previous_Price_Plan' ),
            Field::inst( 'allUpPlanChange.Current_Price_Plan' ),
            Field::inst( 'allUpPlanChange.Previous_Access_Charge' ),
            Field::inst( 'allUpPlanChange.Current_Access_Charge' ),
            Field::inst( 'allUpPlanChange.Agent_SSO_ID' ),
            Field::inst( 'allUpPlanChange.Kicker_Eligible' ),
            Field::inst( 'allUpPlanChange.Kicker_Amount2' ),
            Field::inst( 'locations.district' ),
            Field::inst( 'locations.RQ_Name' ),





        )

if($_SESSION['role'] != 'Admin') {
    ->where( 'locations.district', $location )
    }else{}


        ->leftJoin( 'locations', 'allUpPlanChange.Agent_ID', '=','locations.Outlet_ID'  )


    ->debug( true )
        ->process( $_POST )
        ->json();

This question has an accepted answers - jump to answer

Answers

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    At a glance, your "else" condition is empty.

        }else{}
    

    Also, what does your error actually say?

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
    edited February 2021

    @tangerine

    I also tried this and same error

    if($_SESSION['role'] != 'Admin') {
        ->where( 'locations.district', $location )
        ->leftJoin( 'locations', 'allUpPlanChange.Agent_ID', '=','locations.Outlet_ID'  )
        }else{
        ->leftJoin( 'locations', 'allUpPlanChange.Agent_ID', '=','locations.Outlet_ID'  )
            }
    
  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    The link provided in the error message would be where to start debugging.

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
    edited February 2021

    @tangerine I have done this. It provided me no information in my instance. Im getting a 500 error when using the if statement.

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    It provided me no information in my instance. Im getting a 500 error when using the if statement.

    It provided this information:

    500 - Internal Error.
    The server encountered an error while responding to the request.
    Check the server's error log for information as to why this error occurred.

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0

    @tangerine Thank you for your input. I am aware of what that says. Thanks.

    Anyone else?

  • tangerinetangerine Posts: 3,365Questions: 39Answers: 395

    If your original post had explained that you had followed the diagnostic steps and understood that you needed to check your server's error logs, then I needn't have bothered replying at all. I won't be bothering again.

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0
    edited February 2021

    @tangerine and as I explained that yielded no results for me. Nothing on my server log. I am frustrated by this simple task that should be working and for whatever reason datatables editor is making it impossible.

  • kthorngrenkthorngren Posts: 21,174Questions: 26Answers: 4,923
    edited February 2021 Answer ✓

    Don't know anything about PHP syntax but my guess is you are breaking the use of -> chaining by introducing the {} in the middle. Seems like this would throw a syntax error. I might be completely wrong though :smile:

    Take a look at this thread at how the OP used an if / else statement and completed the chain of ->.

    Kevin

  • davidjmorindavidjmorin Posts: 101Questions: 31Answers: 0

    @kthorngren Thank you! That was it.

This discussion has been closed.