Example #1
0
/**
name:rowsForSelect
parm:string Table_id
parm:string First_Letters
return:array rows

Returns an array of rows that can be put into a drop-down select box.
The first column is always "_value" and the second is always "_display".

The second parameter, if provided, filters to the results so that
only values of _display that start with "First_Letters" are returned.

For a multiple-column primary key, this routine will filter for any pk
column that exists in the session array "ajaxvars".  This feature is
controlled by an (as-yet undocumented) feature in [[ahInputsComprehensive]]
that can make inputs use Ajax when their value changes to store their
value in the session on the server.

This was created 1/15/07 to work with Ajax-dynamic-list from
dhtmlgoodies.com.
*/
function RowsForSelect($table_id, $firstletters = '', $matches = array(), $distinct = '', $allcols = false)
{
    $table = DD_TableRef($table_id);
    // Determine which columns to pull and get them
    // KFD 10/8/07, a DISTINCT means we are pulling a single column of
    //              a multiple column key, pull only that column
    if ($distinct != '') {
        $proj = $distinct;
    } else {
        if (ArraySafe($table['projections'], 'dropdown') == '') {
            if (!vgfGet('x6')) {
                $proj = $table['pks'];
            } else {
                $proj = $table['projections']['_uisearch'];
            }
        } else {
            $proj = $table['projections']['dropdown'];
        }
    }
    $aproj = explode(',', $proj);
    $acollist = array();
    foreach ($aproj as $aproj1) {
        $acollist[] = "COALESCE({$aproj1},'')";
    }
    $collist = str_replace(',', " || ' - ' || ", $proj);
    //$collist = implode(" || ' - ' || ",$acollist);
    //syslog($collist);
    // Get the primary key, and resolve which view we have perms for
    // KFD 10/8/07, do only one column if passed
    if ($distinct != '') {
        $pk = $distinct;
    } else {
        $pk = $table['pks'];
    }
    $view_id = ddtable_idResolve($table_id);
    // Initialize the filters
    $aWhere = array();
    // Generate a filter for each pk that exists in session ajaxvars.
    // There is a BIG unchecked for issue here, which is that a multi-column
    //  PK must have *all but one* column supplied, and it then returns
    //  the unsupplied column.
    $pkeys = explode(',', $table['pks']);
    $ajaxvars = afromGP('adl_');
    foreach ($pkeys as $index => $pkey) {
        if (isset($ajaxvars[$pkey])) {
            $aWhere[] = "{$pkey}=" . SQLFC($ajaxvars[$pkey]);
            // This is important!  Unset the pk column, we'll pick the leftover
            unset($pkeys[$index]);
        }
    }
    // If we did the multi-pk route, provide the missing column
    //  as the key value
    if (count($ajaxvars) > 0) {
        $pk = implode(',', $pkeys);
    }
    // Determine if this is a filtered table
    if (isset($table['flat']['flag_noselect'])) {
        $aWhere[] = "COALESCE(flag_noselect,'N')<>'Y'";
    }
    // Add more matches on
    foreach ($matches as $matchcol => $matchval) {
        $aWhere[] = $matchcol . ' = ' . SQLFC($matchval);
    }
    // See if there is a hardcoded filter in the program class
    $obj = dispatchObject($table_id);
    if (method_exists($obj, 'aSelect_where')) {
        $aWhere[] = $obj->aSelect_where();
        if (ConfigGet('LOG_SQL', 'Y') == 'Y') {
            sysLog(LOG_NOTICE, $obj->aSelect_Where());
        }
    }
    // If "firstletters" have been passed, we will filter each
    // select column on it
    //
    // KFD 8/8/07, a comma in first letters now means look in
    //             1st column only + second column only
    $SLimit = '';
    $xWhere = array();
    if ($firstletters == '*') {
        // do nothing, no where clauses
    } elseif ($firstletters != '') {
        $SLimit = "Limit 40 ";
        if (strpos($firstletters, ',') === false) {
            // original code, search all columns
            $implode = ' OR ';
            foreach ($aproj as $aproj1) {
                $type_id = $table['flat'][$aproj1]['type_id'];
                $subs = '';
                if (!in_array($type_id, array('char', 'vchar', 'text'))) {
                    $subs = '::varchar';
                }
                $sl = strlen($firstletters);
                $xWhere[] = "SUBSTRING(LOWER({$aproj1}{$subs}) FROM 1 FOR {$sl})" . "=" . strtolower(SQLFC($firstletters));
            }
        } else {
            // New code 8/8/07, search first column, 2nd, third only,
            // based on existence of commas
            $implode = ' AND ';
            $afl = explode(',', $firstletters);
            foreach ($afl as $x => $fl) {
                $type_id = $table['flat'][$aproj1]['type_id'];
                $subs = '';
                if (!in_array($type_id, array('char', 'vchar', 'text'))) {
                    $subs = '::varchar';
                }
                $sl = strlen($fl);
                $xWhere[] = "SUBSTRING(LOWER({$aproj[$x + 1]}{$subs}) FROM 1 FOR {$sl})" . "=" . strtolower(SQLFC($fl));
            }
        }
    }
    if (count($xWhere) > 0) {
        $aWhere[] = "(" . implode($implode, $xWhere) . ")";
    }
    // Finish off the where clause
    if (count($aWhere) > 0) {
        $SWhere = "WHERE " . implode(' AND ', $aWhere);
    } else {
        $SWhere = '';
    }
    // Execute and return
    $sDistinct = $distinct != '' ? ' DISTINCT ' : '';
    $SOB = $aproj[0];
    if ($allcols) {
        # KFD 6/9/08, added in automatic ordering on queuopos column
        $OB = isset($table['flat']['queuepos']) ? 'queuepos' : '2';
        $sq = "SELECT skey,{$proj}\n              FROM {$view_id}\n           {$SWhere}\n            ORDER BY {$OB} {$SLimit}";
    } else {
        $sq = "SELECT {$sDistinct} {$pk} as _value,{$collist} as _display\n              FROM {$view_id}\n           {$SWhere}\n             ORDER BY {$SOB} {$SLimit} ";
    }
    /*
    openlog(false,LOG_NDELAY,LOG_USER);
    if ( ConfigGet( 'flag_syslog', 'Y' ) == 'Y' ) {
    	   syslog(LOG_INFO,$table['projections']['dropdown']);
    	   syslogbodyRows
    	   (LOG_INFO,$sq);
    }
    closelog();
    */
    if (ConfigGet('flag_syslog', 'Y') == 'Y') {
        syslog(LOG_INFO, $sq);
    }
    $rows = SQL_Allrows($sq);
    return $rows;
}
Example #2
0
// to set up menus or anything like that.
//
$directlogin = false;
if (gp('gp_uid') != '') {
    $directlogin = true;
    $directclean = $AG['clean'];
    gpSet('loginUID', gp('gp_uid'));
    gpSet('loginPWD', gp('gp_pwd'));
    gpSet('gp_posted', 1);
    gpSet('gp_page', 'x_login');
}
$gp_page = gp('gp_page');
// KFD 3/6/08 Changed login processing from page x_login to
//            the st2login command
if (gp('st2login') == 1) {
    $obj_login = dispatchObject('x_login');
    $obj_login->directlogin = $directlogin;
    $obj_login->Login_Process();
    if (LoggedIn()) {
        // A direct login restores the "clean" array as it was
        if ($directlogin) {
            unset($directclean['gp_uid']);
            unset($directclean['gp_pwd']);
            unset($directclean['loginUID']);
            unset($directclean['loginPWD']);
            $AG['clean'] = $directclean;
        } elseif (count(SessionGet('clean', array())) != 0) {
            // These were a page attempt made w/o being logged in,
            // which is now being ok'd since the user is logged in.
            $GLOBALS['AG']['clean'] = SessionGet('clean');
            if (isset($GLOBALS['AG']['clean']['ajxBUFFER'])) {