<?php

/*
 * phpMyEdit - instant MySQL table editor and code generator
 *
 * extensions/phpMyEdit-foreign.class.php - phpMyEdit calendar popup extension
 * ____________________________________________________________
 *
 * Contribution of Adam Hammond <php@pixolet.co.uk>, London, UK
 * Copyright (c) 2003-2004 Platon SDG, http://platon.sk/
 * All rights reserved.
 *
 * See README file for more information about this software.
 * See COPYING file for license information.
 *
 * Download the latest version from
 * http://platon.sk/projects/phpMyEdit/
 */
 

/*
    OVERVIEW
    --------

    This extension extends the standard phpMyEdit class to allow
    a columns from a linked table to be used in the 'sql'. It is
    useful if you wish to link a field in the main table to a foreign
    table, but don't wish to use 'values' to perform input lookups. If
    you are already using 'values' to join to the required table, you do
    not require this extension.

	
    USAGE
    -----

    1. Call to phpMyEdit-foreign.class.php instead
       of phpMyEdit.class.php.

       Example:

       require_once 'extensions/phpMyEdit-foreign.class.php';
       new phpMyEdit_foreign($opts);

    2. Add 'foreign' parameter to the field definitions of the columns
	   that join to the foreign tables:

       Example:

       $opts['fdd']['col_name'] = array(
         'name'     => 'Column',
         'select'   => 'T',
         'options'  => 'ACPVD',
         'required' => true,
         'foreign' => array(
           'table'       => 'for_table',
           'column'      => 'for_id'
         )
       );

       This joins table 'for_table' in using 'col_name' and 'for_id' as the
       joining fields.

	3. Columns from the foreign table 'for_table' are now available to use
	   in any 'sql' expression as 'PMEjoin<n>.<column>' where:
	        <n> is the field number of the column with the 'foreign' tag
	            (remembering to start from zero!)
			<columns> is the name of the column in 'for_table' you want to
			    include.
			    
    SEARCH KEYWORD
    --------------

	Search for "foreign" string in this source code,
	to find all extension related modifications.
*/

require_once dirname(__FILE__).'/../phpMyEdit.class.php';

class phpMyEdit_foreign extends phpMyEdit
{
function create_join_clause() /* {{{ */
	{
		$tbs[]       = $this->tb;
		$main_table  = 'PMEtable0';
		$join_clause = $this->tb." AS $main_table";
		for ($k = 0, $numfds = sizeof($this->fds); $k < $numfds; $k++) {
			$main_column = $this->fds[$k];
			if($this->fdd[$main_column]['values']['db']) {
				$db = $this->fdd[$field]['values']['db'];
			} else {
				$db = $this->db;
			}
			$table       = @$this->fdd[$main_column]['values']['table'];
			$join_column = @$this->fdd[$main_column]['values']['column'];
			$join_desc   = @$this->fdd[$main_column]['values']['description'];

/* foreign mod start here */
			if ($join_column == '' && $table == '') {
					 $table       = $this->fdd[$main_column]['foreign']['table'];
					 $join_column = $this->fdd[$main_column]['foreign']['column'];
			         $join_desc   = $join_column;
			};
/* foreign mod finishes here */
			if ($join_desc != '' && $join_column != '') {
				$join_table = 'PMEjoin'.$k;
				if (! in_array($join_table, $tbs)) {
					$ar = array(
							'main_table'       => $main_table,
							'main_column'      => $main_column,
							'join_table'       => $join_table,
							'join_column'      => $join_column,
							'join_description' => $join_desc);
					$join_clause .= " LEFT OUTER JOIN $db.$table AS $join_table ON ";
					$join_clause .= isset($this->fdd[$field]['values']['join'])
						? $this->substituteVars($this->fdd[$field]['values']['join'], $ar)
						: "$join_table.$join_column = $main_table.$main_column";
					$tbs[] = $join_table;
				}
			}
		}
		return $join_clause;
	} /* }}} */
}

?>
