Guest User

Untitled

a guest
May 5th, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.07 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * This file is an extension for FX.php, the PHP interface to FileMaker Pro.
  5. * It will make your life much easier for petty FileMaker tasks!
  6. *
  7. * It also includes a "deluxe nav bar" function which is just a glorified
  8. * version of the normal page navigation bar for sets of records.
  9. *
  10. * Go to the Psych Department Wiki for the latest version of this class--
  11. * search for 'fxClass.php'
  12. *
  13. *
  14. */
  15. // Makes sure that the php short open tag is turned on.
  16. if (!ini_get('short_open_tag')) {
  17. ini_set('short_open_tag', 1);
  18. }
  19.  
  20. /**
  21. * You might not want to put your FileMaker account name and password
  22. * in here, but you should find some way to grab that in your code and
  23. * throw it into this class.
  24. *
  25. * Example function call using defaults in __constructor: $fx = new fxClass();
  26. * Example function call: $fx = new fxClass('xxx.xxx.xxx.xxx', 'xx', 'mydb', 'web_access', 'mypassword');
  27. *
  28. * @package FX
  29. * @author Trevor Bortins (tbortins@u.washington.edu)
  30. * @version 1.0
  31. */
  32. class fxClass {
  33.  
  34. function __construct($hostname = '127.0.0.1', $port = '800', $database = 'mydb', $username = 'User', $password = 'xxxxxxxx') {
  35. ## DON'T FORGET TO CHECK THIS PATH!!!! #########################################################################
  36. $fx_path = dirname(__FILE__) . '/../../FX/'; // FX.php should be here, along with the rest of that API junk.
  37. ################################################################################################################
  38. // FMStudioFX v1.0
  39. # FileName="Connection_php_FileMakerFX.htm"
  40. # Type="FileMakerFX"
  41. # FileMakerFX="true"
  42. $path = preg_replace("#^(.*[/\\\\])[^/\\\\]*[/\\\\][^/\\\\]*$#", '\1', __FILE__);
  43. set_include_path(get_include_path() . PATH_SEPARATOR . $path);
  44. require_once($fx_path . 'FX.php');
  45. require_once($fx_path . 'FMStudio_Tools.php');
  46. $fmtype = "FMPro7";
  47. $fmhttp = "HTTP";
  48. $this->database = new FX($hostname, $port, $fmtype, $fmhttp);
  49. $this->database->SetDBData($database);
  50. $this->database->SetDBUserPass($username, $password);
  51. }
  52.  
  53. /**
  54. * Example function call: $find_result = $fx->fx_find('Customer_Libraries', 10, array('_kpln_CustomerID'=>'==SomeID'), array('CustomerName'=>'descend'));
  55. *
  56. * @param string $layout
  57. * @param int $disp_num
  58. * @param array $criterions
  59. * @param array $sort
  60. * @return array
  61. */
  62. public function fx_find($layout, $disp_num, $criterions, $sort) {
  63. $find_find = clone($this->database);
  64. $find_find->layout = $layout;
  65. foreach ($criterions as $key => $value) {
  66. $find_find->AddDBParam($key, $value);
  67. }
  68.  
  69. if ($sort) {
  70. $i = 0;
  71. foreach ($sort as $k => $v) {
  72. $find_find->AddSortParam($k, $v, ++$i);
  73. }
  74. }
  75. fmsSetPage($find_find, 'find', $disp_num);
  76.  
  77. $find_result = $find_find->FMFind();
  78.  
  79. fmsSetLastPage($find_result, 'find', $disp_num);
  80.  
  81. return $find_result;
  82. }
  83.  
  84. /**
  85. * Example function call: $find_result = $fx->fx_findOne('Customer_Libraries', 10, array('_kpln_CustomerID'=>'==SomeID'), array('CustomerName'=>'descend'));
  86. *
  87. * @param string $layout
  88. * @param int $disp_num
  89. * @param array $criterions
  90. * @param array $sort
  91. * @return array
  92. */
  93. public function fx_findOne($layout, $disp_num, $criterions, $sort) {
  94. $find_find = clone($this->database);
  95. $find_find->layout = $layout;
  96. foreach ($criterions as $key => $value) {
  97. $find_find->AddDBParam($key, $value);
  98. }
  99.  
  100. if ($sort) {
  101. $i = 0;
  102. foreach ($sort as $k => $v) {
  103. $find_find->AddSortParam($k, $v, ++$i);
  104. }
  105. }
  106. fmsSetPage($find_find, 'find', $disp_num);
  107.  
  108. $find_result = $find_find->FMFind();
  109.  
  110. fmsSetLastPage($find_result, 'find', $disp_num);
  111.  
  112. return current(current($find_result));
  113. }
  114.  
  115. /**
  116. * Create something with x number of fields of default data.
  117. *
  118. * Example function call: $create_result = $fx->fx_create('Customer_Libraries', array('_kpln_CustomerID'=>'SomeID','CustomerName'=>$_POST['name'],));
  119. *
  120. * @param string $layout
  121. * @param array $create_fields
  122. * @return array
  123. */
  124. public function fx_create($layout, $create_fields) {
  125. $create_add = clone($this->database);
  126. $create_add->layout = $layout;
  127. foreach ($create_fields as $key => $value) {
  128. $create_add->AddDBParam($key, $value);
  129. }
  130.  
  131. $create_result = $create_add->FMNew();
  132.  
  133. return $create_result;
  134. }
  135.  
  136. /**
  137. * 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');
  138. *
  139. * @param string $layout
  140. * @param array $edit_fields
  141. * @param string $recid
  142. * @return array
  143. */
  144. public function fx_edit($layout, $edit_fields, $recid) {
  145. ## NOTE: "$recid" is made with the following function: "echo array_shift(explode('.',$edit_row_key));"
  146. $edit_edit = clone($this->database);
  147. $edit_edit->layout = $layout;
  148. $edit_edit->AddDBParam('-recid', $recid);
  149. foreach ($edit_fields as $key => $value) {
  150. $edit_edit->AddDBParam($key, $value);
  151. }
  152.  
  153. $edit_result = $edit_edit->FMEdit();
  154.  
  155. return $edit_result;
  156. }
  157.  
  158. /**
  159. * Example function call: $delete_result = $fx->fx_delete('Customer_Libraries', '-recid', $_POST['-recid']);
  160. *
  161. * @param string $layout
  162. * @param string $field
  163. * @param string $criteria
  164. * @return array
  165. */
  166. public function fx_delete($layout, $field, $criteria) {
  167. $delete_delete = clone($this->database);
  168. $delete_delete->layout = $layout;
  169. $delete_delete->AddDBParam($field, $criteria);
  170. $delete_result = $delete_delete->FMDelete();
  171.  
  172. return $delete_result;
  173. }
  174.  
  175. /**
  176. * Debug tool. Formats print_r between pre tags
  177. * @param array $a
  178. */
  179. public function p($a) {
  180. echo '<pre>';
  181. echo htmlentities(print_r($a, TRUE));
  182. #print_r($a);
  183. echo '</pre>';
  184. }
  185.  
  186. /**
  187. * Take a url or partial url, (anything that has a format similar to this:
  188. * "something?key=value&key=value") and return an array of the query values
  189. *
  190. * @param string $var
  191. * @return array
  192. */
  193. function parse_query($var) {
  194. /**
  195. * Use this function to parse out the query array element from
  196. * the output of parse_url().
  197. */
  198. $var = explode('?', $var);
  199. $var = html_entity_decode($var[1]);
  200. $var = explode('&', $var);
  201. $arr = array();
  202.  
  203. foreach ($var as $val) {
  204. $x = explode('=', $val);
  205. $arr[$x[0]] = $x[1];
  206. }
  207. unset($val, $x, $var);
  208. return $arr;
  209. }
  210.  
  211. /**
  212. * Probably obsolete by now! Very old function! Check other systems
  213. * for instances of this thing....
  214. *
  215. * The fx_merge_all() function will iterate through an array of arrays
  216. * (basically an array filled with multiple filemaker records and
  217. * associated fields) and save the info to the database
  218. *
  219. * Example function call: $result = $fx->fx_merge($_POST, 'ipaddresses');
  220. *
  221. * @param array $bigarray
  222. * @param string $layout
  223. * @return array
  224. */
  225. public function fx_merge($bigarray, $layout) {
  226. ## Extract small arrays from bigarray
  227. foreach ($bigarray[key($bigarray)] as $key1 => $value1) {
  228. $edit_fields = array();
  229. $merged1 = array();
  230.  
  231. foreach ($bigarray as $key0 => $value0) {
  232. $merged0 = array($key0 => $bigarray[$key0][$key1],);
  233. $edit_fields = array_merge($merged0, $merged1);
  234. $merged1 = &$edit_fields;
  235. }
  236.  
  237. $data = $edit_fields;
  238. unset($data['-recid']);
  239. unset($data['delete']);
  240.  
  241. if ($edit_fields['delete'] == $edit_fields['-recid']) {
  242. $delete_result = fx_delete($layout, '-recid', $edit_fields['-recid']);
  243. }
  244.  
  245. foreach ($edit_fields as $key => $value) {
  246. $edit_result = fx_edit($layout, $data, $edit_fields['-recid']);
  247. }
  248. $result[] = $edit_result;
  249. }
  250.  
  251. return $result;
  252. }
  253.  
  254. /**
  255. * Enhanced NAVBAR based on FMStudio's Navbar,
  256. * improvements v1.0 written by Troy C. Meyers April 9 2008
  257. * v1.2 added "include custom query string" capability (Trevor Bortins)
  258. *
  259. * Example function call:
  260. * echo fmsDlxNavBar('find', 'First/;/Previous/;/Page%20#page#%20of%20#total#/;/Next/;/Last/;/%20%7C%20/;/4', array);
  261. *
  262. * @param array $query Query string to include in URL
  263. * @param string $name Name of found set (always = 'find' in fxClass)
  264. * @param string $settings From schema information
  265. * @return string $return
  266. */
  267. public function fxNavBar($query = null, $name = 'find', $settings = 'First/;/Previous/;/Page%20#page#%20of%20#total#/;/Next/;/Last/;/%20%7C%20/;/4') {
  268. if (fmsGetPageCount($name) == 1)
  269. return;
  270.  
  271. // Settings example: 'First/;/Previous/;/Page%20#page#%20of%20#total#/;/Next/;/Last/;/%20--%20'/;/5
  272. // $settings[0] - Text for First link
  273. // $settings[1] - Text for Previous link
  274. // $settings[2] - Text with substitute tags for current page indicator
  275. // $settings[3] - Text for Next link
  276. // $settings[4] - Text for Last link
  277. // $settings[5] - Text for separator
  278. // $settings[6] - Number of pages to show link for before and after current
  279.  
  280. $settings = fmsDecodeAdvDialogValues($settings);
  281. $sep = $settings[5];
  282.  
  283. if (isset($settings[6])) {
  284. $scope = $settings[6];
  285. } else {
  286. $scope = 0;
  287. }
  288.  
  289. $query = http_build_query($query);
  290. // Current page number
  291. $page = fmsGetPage($name);
  292. $total = fmsGetPageCount($name);
  293. $settings[2] = str_replace(array('#page#', '#total#'), array($page, $total), $settings[2]);
  294. $ret = '';
  295.  
  296. // First link if appropriate
  297. if ($page != 1) {
  298. $ret.='<a href="' . htmlentities(fmsFirstPage($name)) . '" class="fms_nav_first">' . $settings[0] . '</a>' . $sep;
  299. }
  300.  
  301. // Fill with nearby page links below
  302. if (($page != 1) and ($scope > 1)) {
  303. $low_page = $page - $scope;
  304. if ($low_page < 1)
  305. $low_page = 1;
  306. for ($this_link_page = $low_page; $this_link_page < $page; $this_link_page++) {
  307. $ret.='<a href="' . htmlentities(fmsPageURL($name, $this_link_page, -1)) . '" class="fms_nav_num">' . $this_link_page . '</a>' . $sep;
  308. }
  309. }
  310.  
  311. // Prev link if appropriate
  312. if ($page != 1) {
  313. $ret.='<a href="' . htmlentities(fmsPrevPage($name)) . '" class="fms_nav_prev">' . $settings[1] . '</a>' . $sep;
  314. }
  315.  
  316. // Current page (of how many) indicator
  317. $ret.= $settings[2];
  318.  
  319. // Next link if appropriate
  320. if ($page != $total) {
  321. $ret.=$sep . '<a href="' . htmlentities(fmsNextPage($name)) . '" class="fms_nav_next">' . $settings[3] . '</a>' . $sep;
  322. }
  323.  
  324. // Fill with nearby page links above
  325. if (($page != $total) and ($scope > 1)) {
  326. $hi_page = $page + $scope;
  327. if ($hi_page > $total)
  328. $hi_page = $total;
  329. for ($this_link_page = $page + 1; $this_link_page <= $hi_page; $this_link_page++) {
  330. $ret.='<a href="' . htmlentities(fmsPageURL($name, $this_link_page, -1)) . '" class="fms_nav_num">' . $this_link_page . '</a>' . $sep;
  331. }
  332. }
  333.  
  334. // Last link if appropriate
  335. if ($page != $total) {
  336. $ret.='<a href="' . htmlentities(fmsLastPage($name)) . '" class="fms_nav_last">' . $settings[4] . '</a>';
  337. }
  338.  
  339. $ret = str_replace('?', '#' . $query . '&', $ret);
  340. $return = '<span class="fms_nav_bar">' . $ret . '</span>';
  341. return $return;
  342. }
  343.  
  344. }
  345.  
  346. ?>
Add Comment
Please, Sign In to add comment