Advertisement
sc0ttkclark

Gravity Forms to Pods mapping

May 3rd, 2011
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. <?php
  2. add_action('gform_post_submission', 'my_gf_to_pods', 10, 2);
  3. function my_gf_to_pods ($entry, $form) {
  4.     $pod = false;
  5.     $mapping = array();
  6.     $id = null;
  7.     $related = false;
  8.     if (1 == $form['id']) {
  9.         $pod = 'my_pod';
  10.         $mapping = array('1' => 'my_field',
  11.                          '2.1' => 'first_name',
  12.                          '2.2' => 'last_name',
  13.                          '10' => 'other_field');
  14.         pods_gf_map($pod, $mapping);
  15.     }
  16. }
  17.  
  18. function pods_gf_map ($pod = false, $mapping = array(), $id = null, $related = false) {
  19.     if (false !== $pod && !empty($mapping)) {
  20.         $fields = array();
  21.         foreach ($mapping as $field_id => $map) {
  22.             if (!isset($entry[$field_id]))
  23.                 continue;
  24.             $value = $entry[$field_id];
  25.             $etc = $map;
  26.             if (is_array($etc) && !isset($etc['field']))
  27.                 continue;
  28.             elseif (is_array($etc) && isset($etc['field']))
  29.                 $map = $etc['field'];
  30.             $the_pod = $pod;
  31.             if (isset($etc['pod']))
  32.                 $the_pod = $etc['pod'];
  33.             $bool = false;
  34.             if (isset($etc['bool']))
  35.                 $bool = $etc['bool'];
  36.             if (false !== $bool) {
  37.                 if (is_bool($bool) && true === $bool) {
  38.                     if (0 < strlen($value))
  39.                         $value = 1;
  40.                     else
  41.                         $value = 0;
  42.                 }
  43.                 elseif ($value == $bool)
  44.                     $value = 1;
  45.                 else
  46.                     $value = 0;
  47.             }
  48.             if (!isset($fields[$the_pod]))
  49.                 $fields[$the_pod] = array();
  50.             $fields[$the_pod][$map] = $value;
  51.         }
  52.         $ids = array();
  53.         foreach ($fields as $the_pod => $columns) {
  54.             $api = new PodAPI($the_pod);
  55.             $params = array('datatype' => $the_pod, 'columns' => $columns);
  56.             if (false === $related && !empty($id))
  57.                 $params['tbl_row_id'] = $id;
  58.             if (!isset($ids[$the_pod]))
  59.                 $ids[$the_pod] = array();
  60.             $save_id = $api->save_pod_item($params);
  61.             if (0 < $save_id)
  62.                 $ids[$the_pod] = $save_id;
  63.         }
  64.         if (false !== $related && is_array($related)) {
  65.             $api = new PodAPI($pod);
  66.             $params = array('datatype' => $pod, 'columns' => array());
  67.             if (!empty($id))
  68.                 $params['tbl_row_id'] = $id;
  69.             foreach ($ids as $the_pod => $the_id) {
  70.                 if (!isset($related[$the_pod]))
  71.                     continue;
  72.                 $related_field = $related[$the_pod];
  73.                 $params['columns'][$related_field] = $the_id;
  74.             }
  75.             $id = $api->save_pod_item($params);
  76.         }
  77.         return $id;
  78.     }
  79.     return false;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement