Skip to content

Instantly share code, notes, and snippets.

@wulftone
Created April 9, 2010 19:35
Show Gist options
  • Save wulftone/361498 to your computer and use it in GitHub Desktop.
Save wulftone/361498 to your computer and use it in GitHub Desktop.
An API extension for FX.php, the PHP interface to FileMaker Pro
<?php
/**
* This file is an extension for FX.php, the PHP interface to FileMaker Pro.
* It will make your life much easier for petty FileMaker tasks!
*
* It also includes a "deluxe nav bar" function which is just a glorified
* version of the normal page navigation bar for sets of records.
*
* Go to the Psych Department Wiki for the latest version of this class--
* search for 'fxClass.php'
*
*
*/
// Makes sure that the php short open tag is turned on.
if (!ini_get('short_open_tag')) {
ini_set('short_open_tag', 1);
}
/**
* You might not want to put your FileMaker account name and password
* in here, but you should find some way to grab that in your code and
* throw it into this class.
*
* Example function call using defaults in __constructor: $fx = new fxClass();
* Example function call: $fx = new fxClass('xxx.xxx.xxx.xxx', 'xx', 'mydb', 'web_access', 'mypassword');
*
* @package FX
* @author Trevor Bortins (tbortins@u.washington.edu)
* @version 1.0
*/
class fxClass {
function __construct($hostname = '127.0.0.1', $port = '800', $database = 'mydb', $username = 'User', $password = 'xxxxxxxx') {
## DON'T FORGET TO CHECK THIS PATH!!!! #########################################################################
$fx_path = dirname(__FILE__) . '/../../FX/'; // FX.php should be here, along with the rest of that API junk.
################################################################################################################
// FMStudioFX v1.0
# FileName="Connection_php_FileMakerFX.htm"
# Type="FileMakerFX"
# FileMakerFX="true"
$path = preg_replace("#^(.*[/\\\\])[^/\\\\]*[/\\\\][^/\\\\]*$#", '\1', __FILE__);
set_include_path(get_include_path() . PATH_SEPARATOR . $path);
require_once($fx_path . 'FX.php');
require_once($fx_path . 'FMStudio_Tools.php');
$fmtype = "FMPro7";
$fmhttp = "HTTP";
$this->database = new FX($hostname, $port, $fmtype, $fmhttp);
$this->database->SetDBData($database);
$this->database->SetDBUserPass($username, $password);
}
/**
* Example function call: $find_result = $fx->fx_find('Customer_Libraries', 10, array('_kpln_CustomerID'=>'==SomeID'), array('CustomerName'=>'descend'));
*
* @param string $layout
* @param int $disp_num
* @param array $criterions
* @param array $sort
* @return array
*/
public function fx_find($layout, $disp_num, $criterions, $sort) {
$find_find = clone($this->database);
$find_find->layout = $layout;
foreach ($criterions as $key => $value) {
$find_find->AddDBParam($key, $value);
}
if ($sort) {
$i = 0;
foreach ($sort as $k => $v) {
$find_find->AddSortParam($k, $v, ++$i);
}
}
fmsSetPage($find_find, 'find', $disp_num);
$find_result = $find_find->FMFind();
fmsSetLastPage($find_result, 'find', $disp_num);
return $find_result;
}
/**
* Example function call: $find_result = $fx->fx_findOne('Customer_Libraries', 10, array('_kpln_CustomerID'=>'==SomeID'), array('CustomerName'=>'descend'));
*
* @param string $layout
* @param int $disp_num
* @param array $criterions
* @param array $sort
* @return array
*/
public function fx_findOne($layout, $disp_num, $criterions, $sort) {
$find_find = clone($this->database);
$find_find->layout = $layout;
foreach ($criterions as $key => $value) {
$find_find->AddDBParam($key, $value);
}
if ($sort) {
$i = 0;
foreach ($sort as $k => $v) {
$find_find->AddSortParam($k, $v, ++$i);
}
}
fmsSetPage($find_find, 'find', $disp_num);
$find_result = $find_find->FMFind();
fmsSetLastPage($find_result, 'find', $disp_num);
return current(current($find_result));
}
/**
* Create something with x number of fields of default data.
*
* Example function call: $create_result = $fx->fx_create('Customer_Libraries', array('_kpln_CustomerID'=>'SomeID','CustomerName'=>$_POST['name'],));
*
* @param string $layout
* @param array $create_fields
* @return array
*/
public function fx_create($layout, $create_fields) {
$create_add = clone($this->database);
$create_add->layout = $layout;
foreach ($create_fields as $key => $value) {
$create_add->AddDBParam($key, $value);
}
$create_result = $create_add->FMNew();
return $create_result;
}
/**
* Example function call: $edit_result = $fx->fx_edit('Customer_Libraries', array('_kpln_CustomerID'=>'SomeID','CustomerName'=>$_POST['name'],), 'recid_given_in_result_array_from_a_find_function');
*
* @param string $layout
* @param array $edit_fields
* @param string $recid
* @return array
*/
public function fx_edit($layout, $edit_fields, $recid) {
## NOTE: "$recid" is made with the following function: "echo array_shift(explode('.',$edit_row_key));"
$edit_edit = clone($this->database);
$edit_edit->layout = $layout;
$edit_edit->AddDBParam('-recid', $recid);
foreach ($edit_fields as $key => $value) {
$edit_edit->AddDBParam($key, $value);
}
$edit_result = $edit_edit->FMEdit();
return $edit_result;
}
/**
* Example function call: $delete_result = $fx->fx_delete('Customer_Libraries', '-recid', $_POST['-recid']);
*
* @param string $layout
* @param string $field
* @param string $criteria
* @return array
*/
public function fx_delete($layout, $field, $criteria) {
$delete_delete = clone($this->database);
$delete_delete->layout = $layout;
$delete_delete->AddDBParam($field, $criteria);
$delete_result = $delete_delete->FMDelete();
return $delete_result;
}
/**
* Debug tool. Formats print_r between pre tags
* @param array $a
*/
public function p($a) {
echo '<pre>';
echo htmlentities(print_r($a, TRUE));
#print_r($a);
echo '</pre>';
}
/**
* Take a url or partial url, (anything that has a format similar to this:
* "something?key=value&key=value") and return an array of the query values
*
* @param string $var
* @return array
*/
function parse_query($var) {
/**
* Use this function to parse out the query array element from
* the output of parse_url().
*/
$var = explode('?', $var);
$var = html_entity_decode($var[1]);
$var = explode('&', $var);
$arr = array();
foreach ($var as $val) {
$x = explode('=', $val);
$arr[$x[0]] = $x[1];
}
unset($val, $x, $var);
return $arr;
}
/**
* Probably obsolete by now! Very old function! Check other systems
* for instances of this thing....
*
* The fx_merge_all() function will iterate through an array of arrays
* (basically an array filled with multiple filemaker records and
* associated fields) and save the info to the database
*
* Example function call: $result = $fx->fx_merge($_POST, 'ipaddresses');
*
* @param array $bigarray
* @param string $layout
* @return array
*/
public function fx_merge($bigarray, $layout) {
## Extract small arrays from bigarray
foreach ($bigarray[key($bigarray)] as $key1 => $value1) {
$edit_fields = array();
$merged1 = array();
foreach ($bigarray as $key0 => $value0) {
$merged0 = array($key0 => $bigarray[$key0][$key1],);
$edit_fields = array_merge($merged0, $merged1);
$merged1 = &$edit_fields;
}
$data = $edit_fields;
unset($data['-recid']);
unset($data['delete']);
if ($edit_fields['delete'] == $edit_fields['-recid']) {
$delete_result = fx_delete($layout, '-recid', $edit_fields['-recid']);
}
foreach ($edit_fields as $key => $value) {
$edit_result = fx_edit($layout, $data, $edit_fields['-recid']);
}
$result[] = $edit_result;
}
return $result;
}
/**
* Enhanced NAVBAR based on FMStudio's Navbar,
* improvements v1.0 written by Troy C. Meyers April 9 2008
* v1.2 added "include custom query string" capability (Trevor Bortins)
*
* Example function call:
* echo fmsDlxNavBar('find', 'First/;/Previous/;/Page%20#page#%20of%20#total#/;/Next/;/Last/;/%20%7C%20/;/4', array);
*
* @param array $query Query string to include in URL
* @param string $name Name of found set (always = 'find' in fxClass)
* @param string $settings From schema information
* @return string $return
*/
public function fxNavBar($query = null, $name = 'find', $settings = 'First/;/Previous/;/Page%20#page#%20of%20#total#/;/Next/;/Last/;/%20%7C%20/;/4') {
if (fmsGetPageCount($name) == 1)
return;
// Settings example: 'First/;/Previous/;/Page%20#page#%20of%20#total#/;/Next/;/Last/;/%20--%20'/;/5
// $settings[0] - Text for First link
// $settings[1] - Text for Previous link
// $settings[2] - Text with substitute tags for current page indicator
// $settings[3] - Text for Next link
// $settings[4] - Text for Last link
// $settings[5] - Text for separator
// $settings[6] - Number of pages to show link for before and after current
$settings = fmsDecodeAdvDialogValues($settings);
$sep = $settings[5];
if (isset($settings[6])) {
$scope = $settings[6];
} else {
$scope = 0;
}
$query = http_build_query($query);
// Current page number
$page = fmsGetPage($name);
$total = fmsGetPageCount($name);
$settings[2] = str_replace(array('#page#', '#total#'), array($page, $total), $settings[2]);
$ret = '';
// First link if appropriate
if ($page != 1) {
$ret.='<a href="' . htmlentities(fmsFirstPage($name)) . '" class="fms_nav_first">' . $settings[0] . '</a>' . $sep;
}
// Fill with nearby page links below
if (($page != 1) and ($scope > 1)) {
$low_page = $page - $scope;
if ($low_page < 1)
$low_page = 1;
for ($this_link_page = $low_page; $this_link_page < $page; $this_link_page++) {
$ret.='<a href="' . htmlentities(fmsPageURL($name, $this_link_page, -1)) . '" class="fms_nav_num">' . $this_link_page . '</a>' . $sep;
}
}
// Prev link if appropriate
if ($page != 1) {
$ret.='<a href="' . htmlentities(fmsPrevPage($name)) . '" class="fms_nav_prev">' . $settings[1] . '</a>' . $sep;
}
// Current page (of how many) indicator
$ret.= $settings[2];
// Next link if appropriate
if ($page != $total) {
$ret.=$sep . '<a href="' . htmlentities(fmsNextPage($name)) . '" class="fms_nav_next">' . $settings[3] . '</a>' . $sep;
}
// Fill with nearby page links above
if (($page != $total) and ($scope > 1)) {
$hi_page = $page + $scope;
if ($hi_page > $total)
$hi_page = $total;
for ($this_link_page = $page + 1; $this_link_page <= $hi_page; $this_link_page++) {
$ret.='<a href="' . htmlentities(fmsPageURL($name, $this_link_page, -1)) . '" class="fms_nav_num">' . $this_link_page . '</a>' . $sep;
}
}
// Last link if appropriate
if ($page != $total) {
$ret.='<a href="' . htmlentities(fmsLastPage($name)) . '" class="fms_nav_last">' . $settings[4] . '</a>';
}
$ret = str_replace('?', '#' . $query . '&', $ret);
$return = '<span class="fms_nav_bar">' . $ret . '</span>';
return $return;
}
}
?>
@wulftone
Copy link
Author

wulftone commented Apr 9, 2010

This file is an extension for FX.php, the PHP interface to FileMaker Pro. It will make your life much easier for petty FileMaker tasks! It includes functions for creating, editing, updating, and deleting records, as well as a database connector, and a special array-edit-updater function ambiguously called "merge_all".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment