Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 66.59 KB | None | 0 0
  1. <?php
  2. class PodAPI
  3. {
  4.     var $snap = false;
  5.     var $dt;
  6.     var $dtname;
  7.     var $format;
  8.     var $fields;
  9.     var $use_pod_id = false; // set to true for save_pod_item to operate off of pod_id (for backwards compatibility with functions using pod_ids)
  10.  
  11.     /**
  12.      * Store and retrieve data programatically
  13.      *
  14.      * @param string $dtname (optional) The pod name
  15.      * @param string $format (optional) Format for import/export, "php" or "csv"
  16.      * @since 1.7.1
  17.      */
  18.     function __construct($dtname = null, $format = 'php') {
  19.         $this->dtname = $dtname;
  20.         $this->format = $format;
  21.  
  22.         if (!empty($dtname)) {
  23.             $result = pod_query("SELECT id FROM @wp_pod_types WHERE name = '$dtname' LIMIT 1");
  24.             if (0 < mysql_num_rows($result)) {
  25.                 $this->dt = mysql_result($result, 0);
  26.                 $result = pod_query("SELECT id, name, coltype, pickval FROM @wp_pod_fields WHERE datatype = {$this->dt} ORDER BY weight");
  27.                 if (0 < mysql_num_rows($result)) {
  28.                     while ($row = mysql_fetch_assoc($result)) {
  29.                         $this->fields[$row['name']] = $row;
  30.                     }
  31.                     return true;
  32.                 }
  33.             }
  34.             return false;
  35.         }
  36.     }
  37.  
  38.     /**
  39.      * Throw an error or die (cake or death?)
  40.      *
  41.      * @param string $error Error message
  42.      * @since 1.9.0
  43.      */
  44.     function oh_snap($error) {
  45.         if (false!==$this->snap) {
  46.             throw new Exception($error);
  47.             return false;
  48.         }
  49.         die($error);
  50.     }
  51.  
  52.     /**
  53.      * Add or edit a content type
  54.      *
  55.      * $params['id'] int The datatype ID
  56.      * $params['name'] string The datatype name
  57.      * $params['label'] string The datatype label
  58.      * $params['is_toplevel'] bool Display the pod as a top-level admin menu
  59.      * $params['detail_page'] string The URI for single pod items
  60.      * $params['pre_save_helpers'] string Comma-separated list of helper names
  61.      * $params['pre_drop_helpers'] string Comma-separated list of helper names
  62.      * $params['post_save_helpers'] string Comma-separated list of helper names
  63.      * $params['post_drop_helpers'] string Comma-separated list of helper names
  64.      * $params['order'] string Comma-separated list of field IDs
  65.      *
  66.      * @todo Ability to edit a single DB column (e.g. "detail_page")
  67.      * @param array $params An associative array of parameters
  68.      * @since 1.7.9
  69.      */
  70.     function save_pod($params) {
  71.         $params = (object) str_replace('@wp_', '{prefix}', $params);
  72.  
  73.         // Add new pod
  74.         if (empty($params->id)) {
  75.             $params->name = pods_clean_name($params->name);
  76.             if (empty($params->name)) {
  77.                 return $this->oh_snap('<e>Enter a pod name');
  78.             }
  79.             $sql = "SELECT id FROM @wp_pod_types WHERE name = '$params->name' LIMIT 1";
  80.             pod_query($sql, 'Duplicate pod name', 'Pod name already exists');
  81.  
  82.             $pod_id = pod_query("INSERT INTO @wp_pod_types (name) VALUES ('$params->name')", 'Cannot add new pod');
  83.             pod_query("CREATE TABLE `@wp_pod_tbl_$params->name` (id int unsigned auto_increment primary key, name varchar(128), slug varchar(128)) DEFAULT CHARSET utf8", 'Cannot add pod database table');
  84.             pod_query("INSERT INTO @wp_pod_fields (datatype, name, label, comment, coltype, required, weight) VALUES ($pod_id, 'name', 'Name', '', 'txt', 1, 0),($pod_id, 'slug', 'Permalink', 'Leave blank to auto-generate', 'slug', 0, 1)");
  85.             return $pod_id; // return
  86.         }
  87.         // Edit existing pod
  88.         else {
  89.             $sql = "
  90.            UPDATE
  91.                @wp_pod_types
  92.            SET
  93.                label = '$params->label',
  94.                is_toplevel = '$params->is_toplevel',
  95.                detail_page = '$params->detail_page',
  96.                pre_save_helpers = '$params->pre_save_helpers',
  97.                pre_drop_helpers = '$params->pre_drop_helpers',
  98.                post_save_helpers = '$params->post_save_helpers',
  99.                post_drop_helpers = '$params->post_drop_helpers'
  100.            WHERE
  101.                id = $params->id
  102.            LIMIT
  103.                1
  104.            ";
  105.             pod_query($sql, 'Cannot change Pod settings');
  106.  
  107.             $weight = 0;
  108.             $order = (false !== strpos($params->order, ',')) ? explode(',', $params->order) : array($params->order);
  109.             foreach ($order as $key => $field_id) {
  110.                 pod_query("UPDATE @wp_pod_fields SET weight = '$weight' WHERE id = '$field_id' LIMIT 1", 'Cannot change column order');
  111.                 $weight++;
  112.             }
  113.         }
  114.     }
  115.  
  116.     /**
  117.      * Add or edit a column within a content type
  118.      *
  119.      * $params['id'] int The field ID
  120.      * $params['name'] string The field name
  121.      * $params['datatype'] int The datatype ID
  122.      * $params['coltype'] string The column type ("txt", "desc", "pick", etc)
  123.      * $params['sister_field_id'] int (optional) The related field ID
  124.      * $params['dtname'] string The datatype name
  125.      * $params['pickval'] string The related PICK pod name
  126.      * $params['label'] string The field label
  127.      * $params['comment'] string The field comment
  128.      * $params['display_helper'] string (optional) The display helper name
  129.      * $params['input_helper'] string (optional) The input helper name
  130.      * $params['pick_filter'] string (optional) WHERE clause for PICK fields
  131.      * $params['pick_orderby'] string (optional) ORDER BY clause for PICK fields
  132.      * $params['required'] bool Is the field required?
  133.      * $params['unique'] bool Is the field unique?
  134.      * $params['multiple'] bool Is the PICK dropdown a multi-select?
  135.      *
  136.      * @param array $params An associative array of parameters
  137.      * @since 1.7.9
  138.      */
  139.     function save_column($params) {
  140.         $params = (object) str_replace('@wp_', '{prefix}', $params);
  141.  
  142.         $dbtypes = array(
  143.             'bool' => 'bool default 0',
  144.             'date' => 'datetime',
  145.             'num' => 'decimal(12,2)',
  146.             'txt' => 'varchar(128)',
  147.             'slug' => 'varchar(128)',
  148.             'code' => 'longtext',
  149.             'desc' => 'longtext'
  150.         );
  151.         $dbtypes = apply_filters('pods_column_dbtypes', $dbtypes);
  152.  
  153.         // Add new column
  154.         if (empty($params->id)) {
  155.             $params->name = pods_clean_name($params->name);
  156.             if (empty($params->name)) {
  157.                 return $this->oh_snap('<e>Enter a column name');
  158.             }
  159.             elseif (in_array($params->name, array('id', 'name', 'type', 'created', 'modified'))) {
  160.                 return $this->oh_snap("<e>$params->name is a reserved name");
  161.             }
  162.             $sql = "SELECT id FROM @wp_pod_fields WHERE datatype = $params->datatype AND name = '$params->name' LIMIT 1";
  163.             pod_query($sql, 'Cannot get fields', 'Column by this name already exists');
  164.  
  165.             if ('slug' == $params->coltype) {
  166.                 $sql = "SELECT id FROM @wp_pod_fields WHERE datatype = $params->datatype AND coltype = 'slug' LIMIT 1";
  167.                 pod_query($sql, 'Too many permalinks', 'This pod already has a permalink column');
  168.             }
  169.  
  170.             // Sink the new column to the bottom of the list
  171.             $weight = 0;
  172.             $result = pod_query("SELECT weight FROM @wp_pod_fields WHERE datatype = $params->datatype ORDER BY weight DESC LIMIT 1");
  173.             if (0 < mysql_num_rows($result)) {
  174.                 $row = mysql_fetch_assoc($result);
  175.                 $weight = (int) $row['weight'] + 1;
  176.             }
  177.  
  178.             $params->sister_field_id = (int) $params->sister_field_id;
  179.             $field_id = pod_query("INSERT INTO @wp_pod_fields (datatype, name, label, comment, display_helper, input_helper, coltype, pickval, pick_filter, pick_orderby, sister_field_id, required, `unique`, `multiple`, weight) VALUES ('$params->datatype', '$params->name', '$params->label', '$params->comment', '$params->display_helper', '$params->input_helper', '$params->coltype', '$params->pickval', '$params->pick_filter', '$params->pick_orderby', '$params->sister_field_id', '$params->required', '$params->unique', '$params->multiple', '$weight')", 'Cannot add new field');
  180.  
  181.             if ('pick' != $params->coltype && 'file' != $params->coltype) {
  182.                 $dbtype = $dbtypes[$params->coltype];
  183.                 pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` ADD COLUMN `$params->name` $dbtype", 'Cannot create new column');
  184.             }
  185.             else {
  186.                 pod_query("UPDATE @wp_pod_fields SET sister_field_id = '$field_id' WHERE id = $params->sister_field_id LIMIT 1", 'Cannot update sister field');
  187.             }
  188.         }
  189.         // Edit existing column
  190.         else {
  191.             if ('id' == $params->name) {
  192.                 return $this->oh_snap("<e>$params->name is not editable.");
  193.             }
  194.  
  195.             $sql = "SELECT id FROM @wp_pod_fields WHERE datatype = $params->datatype AND id != $params->id AND name = '$params->name' LIMIT 1";
  196.             pod_query($sql, 'Column already exists', "$params->name already exists.");
  197.  
  198.             $sql = "SELECT name, coltype FROM @wp_pod_fields WHERE id = $params->id LIMIT 1";
  199.             $result = pod_query($sql);
  200.  
  201.             if (0 < mysql_num_rows($result)) {
  202.                 $row = mysql_fetch_assoc($result);
  203.                 $old_coltype = $row['coltype'];
  204.                 $old_name = $row['name'];
  205.  
  206.                 $dbtype = $dbtypes[$params->coltype];
  207.                 $pickval = ('pick' != $params->coltype || empty($params->pickval)) ? '' : "$params->pickval";
  208.                 $params->sister_field_id = (int) $params->sister_field_id;
  209.  
  210.                 if ($params->coltype != $old_coltype) {
  211.                     if ('pick' == $params->coltype || 'file' == $params->coltype) {
  212.                         if ('pick' != $old_coltype && 'file' != $old_coltype) {
  213.                             pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` DROP COLUMN `$old_name`");
  214.                         }
  215.                     }
  216.                     elseif ('pick' == $old_coltype || 'file' == $old_coltype) {
  217.                         pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` ADD COLUMN `$params->name` $dbtype", 'Cannot create column');
  218.                         pod_query("UPDATE @wp_pod_fields SET sister_field_id = NULL WHERE sister_field_id = $params->id");
  219.                         pod_query("DELETE FROM @wp_pod_rel WHERE field_id = $params->id");
  220.                     }
  221.                     else {
  222.                         pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` CHANGE `$old_name` `$params->name` $dbtype");
  223.                     }
  224.                 }
  225.                 elseif ($params->name != $old_name && 'pick' != $params->coltype && 'file' != $params->coltype) {
  226.                     pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` CHANGE `$old_name` `$params->name` $dbtype");
  227.                 }
  228.  
  229.                 $sql = "
  230.                UPDATE
  231.                    @wp_pod_fields
  232.                SET
  233.                    name = '$params->name',
  234.                    label = '$params->label',
  235.                    comment = '$params->comment',
  236.                    coltype = '$params->coltype',
  237.                    pickval = '$params->pickval',
  238.                    display_helper = '$params->display_helper',
  239.                    input_helper = '$params->input_helper',
  240.                    pick_filter = '$params->pick_filter',
  241.                    pick_orderby = '$params->pick_orderby',
  242.                    sister_field_id = '$params->sister_field_id',
  243.                    required = '$params->required',
  244.                    `unique` = '$params->unique',
  245.                    `multiple` = '$params->multiple'
  246.                WHERE
  247.                    id = $params->id
  248.                LIMIT
  249.                    1
  250.                ";
  251.                 pod_query($sql, 'Cannot edit column');
  252.             }
  253.         }
  254.     }
  255.  
  256.     /**
  257.      * Add or edit a Pod Template
  258.      *
  259.      * $params['id'] int The template ID
  260.      * $params['name'] string The template name
  261.      * $params['code'] string The template code
  262.      *
  263.      * @param array $params An associative array of parameters
  264.      * @since 1.7.9
  265.      */
  266.     function save_template($params) {
  267.         $params = (object) str_replace('@wp_', '{prefix}', $params);
  268.  
  269.         // Add new template
  270.         if (empty($params->id)) {
  271.             if (empty($params->name)) {
  272.                 return $this->oh_snap('<e>Enter a template name');
  273.             }
  274.  
  275.             $sql = "SELECT id FROM @wp_pod_templates WHERE name = '$params->name' LIMIT 1";
  276.             pod_query($sql, 'Cannot get Templates', 'Template by this name already exists');
  277.             $template_id = pod_query("INSERT INTO @wp_pod_templates (name, code) VALUES ('$params->name', '$params->code')", 'Cannot add new template');
  278.  
  279.             return $template_id; // return
  280.         }
  281.         // Edit existing template
  282.         else {
  283.             pod_query("UPDATE @wp_pod_templates SET code = '$params->code' WHERE id = $params->id LIMIT 1");
  284.         }
  285.     }
  286.  
  287.     /**
  288.      * Add or edit a Pod Page
  289.      *
  290.      * $params['id'] int The page ID
  291.      * $params['uri'] string The page URI
  292.      * $params['phpcode'] string The page code
  293.      *
  294.      * @param array $params An associative array of parameters
  295.      * @since 1.7.9
  296.      */
  297.     function save_page($params) {
  298.         $params = (object) str_replace('@wp_', '{prefix}', $params);
  299.  
  300.         // Add new page
  301.         if (empty($params->id)) {
  302.             if (empty($params->uri)) {
  303.                 return $this->oh_snap('<e>Enter a page URI');
  304.             }
  305.             // normalize URI (remove outside /
  306.             $params->uri = trim($params->uri,'/');
  307.             $sql = "SELECT id FROM @wp_pod_pages WHERE uri = '$params->uri' LIMIT 1";
  308.             pod_query($sql, 'Cannot get Pod Pages', 'Page by this URI already exists');
  309.             $page_id = pod_query("INSERT INTO @wp_pod_pages (uri) VALUES ('$params->uri')", 'Cannot add new page');
  310.             return $page_id; // return
  311.         }
  312.         // Edit existing page
  313.         else {
  314.             pod_query("UPDATE @wp_pod_pages SET title = '$params->page_title', page_template = '$params->page_template', phpcode = '$params->phpcode', precode = '$params->precode' WHERE id = $params->id LIMIT 1");
  315.         }
  316.     }
  317.  
  318.     /**
  319.      * Add or edit a Pod Helper
  320.      *
  321.      * $params['id'] int The helper ID
  322.      * $params['name'] string The helper name
  323.      * $params['helper_type'] string The helper type ("pre_save", "display", etc)
  324.      * $params['phpcode'] string The helper code
  325.      *
  326.      * @param array $params An associative array of parameters
  327.      * @since 1.7.9
  328.      */
  329.     function save_helper($params) {
  330.         $params = (object) str_replace('@wp_', '{prefix}', $params);
  331.  
  332.         // Add new helper
  333.         if (empty($params->id)) {
  334.             if (empty($params->name)) {
  335.                 return $this->oh_snap('<e>Enter a helper name');
  336.             }
  337.  
  338.             $sql = "SELECT id FROM @wp_pod_helpers WHERE name = '$params->name' LIMIT 1";
  339.             pod_query($sql, 'Cannot get helpers', 'helper by this name already exists');
  340.             $helper_id = pod_query("INSERT INTO @wp_pod_helpers (name, helper_type, phpcode) VALUES ('$params->name', '$params->helper_type', '$params->phpcode')", 'Cannot add new helper');
  341.             return $helper_id; // return
  342.         }
  343.         // Edit existing helper
  344.         else {
  345.             pod_query("UPDATE @wp_pod_helpers SET phpcode = '$params->phpcode' WHERE id = $params->id LIMIT 1");
  346.         }
  347.     }
  348.  
  349.     /**
  350.      * Add or edit a single menu item
  351.      *
  352.      * $params['id'] int The menu ID
  353.      * $params['parent_menu_id'] int The parent menu ID
  354.      * $params['menu_uri'] string The menu URI
  355.      * $params['menu_title'] string The menu title
  356.      *
  357.      * @param array $params An associative array of parameters
  358.      * @since 1.7.9
  359.      */
  360.     function save_menu_item($params) {
  361.         $params = (object) $params;
  362.  
  363.         // Add new menu item
  364.         if (empty($params->id)) {
  365.             // get the "rgt" value of the parent
  366.             $result = pod_query("SELECT rgt FROM @wp_pod_menu WHERE id = $params->parent_menu_id LIMIT 1");
  367.             $row = mysql_fetch_assoc($result);
  368.             $rgt = $row['rgt'];
  369.  
  370.             // Increase all "lft" values by 2 if > "rgt"
  371.             pod_query("UPDATE @wp_pod_menu SET lft = lft + 2 WHERE lft > $rgt");
  372.  
  373.             // Increase all "rgt" values by 2 if >= "rgt"
  374.             pod_query("UPDATE @wp_pod_menu SET rgt = rgt + 2 WHERE rgt >= $rgt");
  375.  
  376.             // Add new item: "lft" = rgt, "rgt" = rgt + 1
  377.             $lft = $rgt;
  378.             $rgt = ($rgt + 1);
  379.             $menu_id = pod_query("INSERT INTO @wp_pod_menu (uri, title, lft, rgt) VALUES ('$params->menu_uri', '$params->menu_title', $lft, $rgt)");
  380.  
  381.             return $menu_id; // return
  382.         }
  383.         // Edit existing menu item
  384.         else {
  385.             pod_query("UPDATE @wp_pod_menu SET uri = '$params->menu_uri', title = '$params->menu_title' WHERE id = $params->id LIMIT 1");
  386.         }
  387.     }
  388.  
  389.     /**
  390.      * Save the entire role structure
  391.      *
  392.      * @param array $params An associative array of parameters
  393.      * @since 1.7.9
  394.      */
  395.     function save_roles($params) {
  396.         $roles = array();
  397.         foreach ($params as $key => $val) {
  398.             if ('action' != $key) {
  399.                 $tmp = empty($val) ? array() : explode(',', $val);
  400.                 $roles[$key] = $tmp;
  401.             }
  402.         }
  403.         delete_option('pods_roles');
  404.         add_option('pods_roles', serialize($roles));
  405.     }
  406.  
  407.     /**
  408.      * Retrieve an associative array of table values
  409.      *
  410.      * $params['table'] string The table name (default: "types")
  411.      * $params['columns'] string Comma-separated string of columns (default: "*")
  412.      * $params['orderby'] string MySQL ORDER BY clause (default: "id ASC")
  413.      * $params['where'] string MySQL WHERE clause (default: 1)
  414.      * $params['array_key'] string The key column for the returned associative array (default: "id")
  415.      *
  416.      * @param array $params An associative array of parameters
  417.      * @return array The table data array
  418.      * @since 1.8.5
  419.      */
  420.     function get_table_data($params) {
  421.         $params = is_array($params) ? $params : array();
  422.         $defaults = array(
  423.             'table' => 'types',
  424.             'columns' => '*',
  425.             'orderby' => 'id ASC',
  426.             'where' => 1,
  427.             'array_key' => 'id'
  428.         );
  429.         $params = (object) array_merge($defaults, $params);
  430.         $result = pod_query("SELECT $params->columns FROM @wp_pod_$params->table WHERE $params->where ORDER BY $params->orderby");
  431.         if (0 < mysql_num_rows($result)) {
  432.             while ($row = mysql_fetch_assoc($result)) {
  433.                 $data[$row[$params->array_key]] = $row;
  434.             }
  435.             return $data;
  436.         }
  437.     }
  438.  
  439.     /**
  440.      * Add or edit a single pod item
  441.      *
  442.      * $params['datatype'] string The datatype name
  443.      * $params['columns'] array (optional) Associative array of column names + values
  444.      * $params['data'] array (optional) Associative array of a set of associative arrays of column names + values (for bulk operations)
  445.      * $params['pod_id'] int The item's ID from the wp_pod table (or alternatively use the tbl_row_id parameter instead)
  446.      * $params['tbl_row_id'] int The item's ID from the wp_pod_tbl_* table (or alternatively use the pod_id parameter instead)
  447.      *
  448.      * @param array $params An associative array of parameters
  449.      * @return int The table row ID
  450.      * @since 1.7.9
  451.      */
  452.     function save_pod_item($params) {
  453.         $params = (object) str_replace('@wp_', '{prefix}', $params);
  454.  
  455.         // support for multiple save_pod_item operations at the same time
  456.         if (isset($params->data) && !empty($params->data) && is_array($params->data)) {
  457.             foreach ($params->data as $columns){
  458.                 $new_params = $params;
  459.                 unset($new_params->data);
  460.                 $new_params->columns = $columns;
  461.                 $this->save_pod_item($new_params);
  462.             }
  463.         }
  464.  
  465.         // Allow Helpers to know what's going on, are we adding or saving?
  466.         $is_new_item = false;
  467.         if (empty($params->pod_id)&&empty($params->tbl_row_id)) {
  468.             $is_new_item = true;
  469.         }
  470.  
  471.         // Allow Helpers to bypass subsequent helpers in recursive save_pod_item calls
  472.         $bypass_helpers = false;
  473.         if (isset($params->bypass_helpers) && false !== $params->bypass_helpers) {
  474.             $bypass_helpers = true;
  475.         }
  476.  
  477.         // Get array of datatypes
  478.         $datatypes = $this->get_table_data(array('array_key' => 'name', 'columns' => 'id, name'));
  479.         $datatype_id = $datatypes[$params->datatype]['id'];
  480.  
  481.         // Get the datatype fields
  482.         $opts = array('table' => 'fields', 'where' => "datatype = $datatype_id", 'orderby' => 'weight', 'array_key' => 'name');
  483.         $columns = $this->get_table_data($opts);
  484.  
  485.         // Find the active columns (loop through $params->columns to retain order)
  486.         if (!empty($params->columns) && is_array($params->columns)) {
  487.             foreach ($params->columns as $column_name => $column_val) {
  488.                 // Support for Pre Key/Value Parameters in previous Pods versions
  489.                 if (isset($params->name)&&isset($params->$column_val)) {
  490.                     $column_name = $column_val;
  491.                     $column_val = $params->$column_name;
  492.                 }
  493.                 if (isset($columns[$column_name])) {
  494.                     $columns[$column_name]['value'] = $column_val;
  495.                     $active_columns[] = $column_name;
  496.                 }
  497.             }
  498.             unset($params->columns);
  499.         }
  500.  
  501.         // Load all helpers
  502.         $result = pod_query("SELECT pre_save_helpers, post_save_helpers FROM @wp_pod_types WHERE id = $datatype_id");
  503.         $row = mysql_fetch_assoc($result);
  504.         $pre_save_helpers = str_replace(',', "','", $row['pre_save_helpers']);
  505.         $post_save_helpers = str_replace(',', "','", $row['post_save_helpers']);
  506.  
  507.         // Plugin hook
  508.         do_action('pods_pre_save_pod_item');
  509.  
  510.         // Call any pre-save helpers (if not bypassed)
  511.         if(!$bypass_helpers) {
  512.             $result = pod_query("SELECT phpcode FROM @wp_pod_helpers WHERE name IN ('$pre_save_helpers')");
  513.             while ($row = mysql_fetch_assoc($result)) {
  514.                 eval('?>' . $row['phpcode']);
  515.                 }
  516.         }
  517.  
  518.         // Loop through each active column, validating and preparing the table data
  519.         foreach ($active_columns as $key) {
  520.             $val = $columns[$key]['value'];
  521.             $type = $columns[$key]['coltype'];
  522.             $label = $columns[$key]['label'];
  523.             $label = empty($label) ? $key : $label;
  524.  
  525.             // Verify required fields
  526.             if (1 == $columns[$key]['required']) {
  527.                 if ('' == $val || null == $val) {
  528.                     return $this->oh_snap("<e>$label is empty.");
  529.                 }
  530.                 elseif ('num' == $type && !is_numeric($val)) {
  531.                     return $this->oh_snap("<e>$label is not numeric.");
  532.                 }
  533.             }
  534.             // Verify unique fields
  535.             if (1 == $columns[$key]['unique'] && !in_array($type, array('pick', 'file'))) {
  536.                 $exclude = '';
  537.                 if (!empty($params->pod_id)) {
  538.                     $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = '$params->pod_id' AND datatype = '$datatype_id' LIMIT 1");
  539.                     if (0 < mysql_num_rows($result)) {
  540.                         $exclude = 'AND id != ' . mysql_result($result, 0);
  541.                     }
  542.                 }
  543.  
  544.                 // Trigger an error if not unique
  545.                 $sql = "SELECT id FROM `@wp_pod_tbl_$params->datatype` WHERE `$key` = '$val' $exclude LIMIT 1";
  546.                 pod_query($sql, 'Not unique', "$label needs to be unique.");
  547.             }
  548.             // Verify slug columns
  549.             if ('slug' == $type) {
  550.                 $slug_val = empty($val) ? $columns['name']['value'] : $val;
  551.                 $val = pods_unique_slug($slug_val, $key, $params->datatype, $datatype_id, $params->pod_id);
  552.             }
  553.  
  554.             // Prepare all table (non-relational) data
  555.             if (!in_array($type, array('pick', 'file'))) {
  556.                 $table_data[] = "`$key` = '$val'";
  557.             }
  558.             // Store relational column data to be looped through later
  559.             else {
  560.                 $rel_columns[$type][$key] = $val;
  561.             }
  562.         }
  563.  
  564.         // Create the pod_id if it doesn't exist
  565.         if (empty($params->pod_id)&&empty($params->tbl_row_id)) {
  566.             $current_time = current_time('mysql');
  567.             $user = 0;
  568.             if (is_user_logged_in()) {
  569.                 global $user_ID;
  570.                 get_currentuserinfo();
  571.                 $user = $user_ID;
  572.             }
  573.             $sql = "INSERT INTO @wp_pod (datatype, name, created, modified, author_id) VALUES ('$datatype_id', '{$columns['name']['value']}', '$current_time', '$current_time', '$user')";
  574.             $params->pod_id = pod_query($sql, 'Cannot add new content');
  575.             $params->tbl_row_id = pod_query("INSERT INTO `@wp_pod_tbl_$params->datatype` (name) VALUES (NULL)", 'Cannot add new table row');
  576.         }
  577.         elseif (!empty($params->tbl_row_id)) {
  578.             $result = pod_query("SELECT p.id FROM @wp_pod p INNER JOIN @wp_pod_types t ON t.id = p.datatype WHERE p.tbl_row_id = $params->tbl_row_id AND t.name = '$params->datatype' LIMIT 1",'Pod item not found',null,'Pod item not found');
  579.             $params->pod_id = mysql_result($result, 0);
  580.         }
  581.         elseif (!empty($params->pod_id)) {
  582.             $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = $params->pod_id LIMIT 1",'Item not found',null,'Item not found');
  583.             $params->tbl_row_id = mysql_result($result, 0);
  584.         }
  585.  
  586.         // Save the table row
  587.         if (isset($table_data)) {
  588.             $table_data = implode(',', $table_data);
  589.             pod_query("UPDATE `@wp_pod_tbl_$params->datatype` SET $table_data WHERE id = $params->tbl_row_id LIMIT 1");
  590.         }
  591.  
  592.         // Update wp_pod
  593.         $item_name = isset($columns['name']['value']) ? ", name = '" . $columns['name']['value'] . "'" : '';
  594.         pod_query("UPDATE @wp_pod SET tbl_row_id = $params->tbl_row_id, datatype = $datatype_id, modified = '" . current_time('mysql') . "' $item_name WHERE id = $params->pod_id LIMIT 1");
  595.  
  596.         // Save relational column data
  597.         if (isset($rel_columns)) {
  598.             // E.g. $rel_columns['pick']['related_events'] = '3,15';
  599.             foreach ($rel_columns as $rel_type => $rel_data) {
  600.                 foreach ($rel_data as $rel_name => $rel_values) {
  601.                     $field_id = $columns[$rel_name]['id'];
  602.  
  603.                     // Convert values from a comma-separated string into an array
  604.                     $rel_values = empty($rel_values) ? array() : explode(',', $rel_values);
  605.  
  606.                     // Remove existing relationships
  607.                     pod_query("DELETE FROM @wp_pod_rel WHERE pod_id = $params->pod_id AND field_id = $field_id");
  608.  
  609.                     // File relationships
  610.                     if ('file' == $rel_type) {
  611.                         $rel_weight = 0;
  612.                         foreach ($rel_values as $related_id) {
  613.                             pod_query("INSERT INTO @wp_pod_rel (pod_id, field_id, tbl_row_id, weight) VALUES ($params->pod_id, $field_id, ".(int) $related_id.", $rel_weight)");
  614.                             $rel_weight++;
  615.                         }
  616.                     }
  617.                     // Pick relationships
  618.                     elseif ('pick' == $rel_type) {
  619.                         $pickval = $columns[$rel_name]['pickval'];
  620.                         $sister_datatype_id = $datatypes[$pickval]['id'];
  621.                         $sister_field_id = $columns[$rel_name]['sister_field_id'];
  622.                         $sister_field_id = empty($sister_field_id) ? 0 : $sister_field_id;
  623.                         $sister_pod_ids = array();
  624.  
  625.                         // Delete parent and sister rels
  626.                         if (!empty($sister_field_id)) {
  627.                             // Get sister pod IDs (a sister pod's sister pod is the parent pod)
  628.                             $result = pod_query("SELECT pod_id FROM @wp_pod_rel WHERE sister_pod_id = $params->pod_id");
  629.                             if (0 < mysql_num_rows($result)) {
  630.                                 while ($row = mysql_fetch_assoc($result)) {
  631.                                     $sister_pod_ids[] = $row['pod_id'];
  632.                                 }
  633.                                 $sister_pod_ids = implode(',', $sister_pod_ids);
  634.  
  635.                                 // Delete the sister pod relationship
  636.                                 pod_query("DELETE FROM @wp_pod_rel WHERE pod_id IN ($sister_pod_ids) AND sister_pod_id = $params->pod_id AND field_id = $sister_field_id");
  637.                             }
  638.                         }
  639.  
  640.                         // Add rel values
  641.                         $rel_weight = 0;
  642.                         foreach ($rel_values as $related_id) {
  643.                             $sister_pod_id = 0;
  644.                             if (!empty($sister_field_id) && !empty($sister_datatype_id)) {
  645.                                 $result = pod_query("SELECT id FROM @wp_pod WHERE datatype = $sister_datatype_id AND tbl_row_id = $related_id LIMIT 1");
  646.                                 if (0 < mysql_num_rows($result)) {
  647.                                     $sister_pod_id = mysql_result($result, 0);
  648.                                     pod_query("INSERT INTO @wp_pod_rel (pod_id, sister_pod_id, field_id, tbl_row_id, weight) VALUES ($sister_pod_id, $params->pod_id, $sister_field_id, ".(int) $params->tbl_row_id.", $rel_weight)", 'Cannot add sister relationships');
  649.                                 }
  650.                             }
  651.                             pod_query("INSERT INTO @wp_pod_rel (pod_id, sister_pod_id, field_id, tbl_row_id, weight) VALUES ($params->pod_id, $sister_pod_id, $field_id, ".(int) $related_id.", $rel_weight)", 'Cannot add relationships');
  652.                             $rel_weight++;
  653.                         }
  654.                     }
  655.                 }
  656.             }
  657.         }
  658.  
  659.         // Plugin hook
  660.         do_action('pods_post_save_pod_item');
  661.  
  662.         // Call any post-save helpers (if not bypassed)
  663.         if(!$bypass_helpers) {
  664.             $result = pod_query("SELECT phpcode FROM @wp_pod_helpers WHERE name IN ('$post_save_helpers')");
  665.             while ($row = mysql_fetch_assoc($result)) {
  666.                 eval('?>' . $row['phpcode']);
  667.             }
  668.         }
  669.  
  670.         // Success! Return the id
  671.         if (false===$this->use_pod_id) {
  672.             return $params->tbl_row_id;
  673.         }
  674.         return $params->pod_id;
  675.     }
  676.  
  677.     /**
  678.      * Add or edit a single pod item
  679.      *
  680.      * $params['datatype'] string The datatype name
  681.      * $params['field'] string The column name of the field to reorder
  682.      * $params['order'] array The key=>value array of items to reorder (key should be an integer)
  683.      *
  684.      * @param array $params An associative array of parameters
  685.      * @since 1.9.0
  686.      */
  687.     function reorder_pod_item($params) {
  688.         $params = (object) $params;
  689.  
  690.         if (!is_array($params->order)) {
  691.             $params->order = explode(',',$params->order);
  692.         }
  693.         foreach ($params->order as $order => $id) {
  694.             pod_query("UPDATE @wp_pod_tbl_$params->datatype SET `$params->field`=$order WHERE id=$id");
  695.         }
  696.     }
  697.  
  698.     /**
  699.      * Delete all content for a content type
  700.      *
  701.      * $params['id'] int The datatype ID
  702.      * $params['name'] string The datatype name
  703.      *
  704.      * @todo Only require the datatype ID or name (not both!)
  705.      * @param array $params An associative array of parameters
  706.      * @since 1.9.0
  707.      */
  708.     function reset_pod($params) {
  709.         $params = (object) $params;
  710.  
  711.         $sql = "DELETE FROM p, r
  712.        USING @wp_pod_types AS t
  713.        LEFT JOIN @wp_pod AS p ON p.datatype = t.id
  714.        LEFT JOIN @wp_pod_fields AS f ON f.datatype = t.id
  715.        LEFT JOIN @wp_pod_rel AS r ON r.field_id = f.id
  716.        WHERE t.name = '$params->name'";
  717.  
  718.         pod_query($sql);
  719.  
  720.         $sql = "DELETE FROM r
  721.        USING @wp_pod_fields AS f
  722.        INNER JOIN @wp_pod_rel AS r ON r.field_id = f.id
  723.        WHERE f.pickval = '$params->name'";
  724.  
  725.         pod_query($sql);
  726.         pod_query("TRUNCATE `@wp_pod_tbl_$params->name`");
  727.     }
  728.  
  729.     /**
  730.      * Drop a content type and all its content
  731.      *
  732.      * $params['id'] int The datatype ID
  733.      * $params['name'] string The datatype name
  734.      *
  735.      * @todo Only require the datatype ID or name (not both!)
  736.      * @param array $params An associative array of parameters
  737.      * @since 1.7.9
  738.      */
  739.     function drop_pod($params) {
  740.         $params = (object) $params;
  741.  
  742.         $fields = '0';
  743.         pod_query("DELETE FROM @wp_pod_types WHERE id = $params->id LIMIT 1");
  744.         $result = pod_query("SELECT id FROM @wp_pod_fields WHERE datatype = $params->id");
  745.         while ($row = mysql_fetch_assoc($result)) {
  746.             $fields .= ',' . $row['id'];
  747.         }
  748.  
  749.         pod_query("UPDATE @wp_pod_fields SET sister_field_id = NULL WHERE sister_field_id IN ($fields)");
  750.  
  751.         $sql = "DELETE FROM @wp_pod,@wp_pod_rel
  752.        USING @wp_pod_fields
  753.        INNER JOIN @wp_pod_rel ON @wp_pod_rel.field_id = @wp_pod_fields.id
  754.        INNER JOIN @wp_pod ON @wp_pod.datatype = @wp_pod_fields.datatype
  755.        WHERE @wp_pod_fields.datatype = $params->id";
  756.  
  757.         pod_query($sql);
  758.         pod_query("DELETE FROM @wp_pod_fields WHERE datatype = $params->id");
  759.         pod_query("DROP TABLE `@wp_pod_tbl_$params->name`");
  760.     }
  761.  
  762.     /**
  763.      * Drop a column within a content type
  764.      *
  765.      * $params['id'] int The column ID
  766.      * $params['dtname'] string The datatype name
  767.      *
  768.      * @param array $params An associative array of parameters
  769.      * @since 1.7.9
  770.      */
  771.     function drop_column($params) {
  772.         $params = (object) $params;
  773.         $result = pod_query("SELECT name, coltype FROM @wp_pod_fields WHERE id = $params->id LIMIT 1");
  774.         list($field_name, $coltype) = mysql_fetch_array($result);
  775.  
  776.         if ('pick' == $coltype) {
  777.             // Remove any orphans
  778.             $result = pod_query("SELECT id FROM @wp_pod_fields WHERE sister_field_id = $params->id");
  779.             if (0 < mysql_num_rows($result)) {
  780.                 while ($row = mysql_fetch_assoc($result)) {
  781.                     $related_fields[] = $row['id'];
  782.                 }
  783.                 $related_fields = implode(',', $related_fields);
  784.                 pod_query("DELETE FROM @wp_pod_rel WHERE field_id IN ($related_fields)");
  785.                 pod_query("UPDATE @wp_pod_fields SET sister_field_id = NULL WHERE sister_field_id IN ($related_fields)");
  786.             }
  787.         }
  788.         elseif ('file' != $coltype) {
  789.             pod_query("ALTER TABLE `@wp_pod_tbl_$params->dtname` DROP COLUMN `$field_name`");
  790.         }
  791.  
  792.         pod_query("DELETE FROM @wp_pod_fields WHERE id = $params->id LIMIT 1");
  793.         pod_query("DELETE FROM @wp_pod_rel WHERE field_id = $params->id");
  794.     }
  795.  
  796.     /**
  797.      * Drop a Pod Template
  798.      *
  799.      * $params['id'] int The template ID
  800.      * $params['name'] string The template name
  801.      *
  802.      * @param array $params An associative array of parameters
  803.      * @since 1.7.9
  804.      */
  805.     function drop_template($params) {
  806.         $params = (object) $params;
  807.         $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
  808.         pod_query("DELETE FROM @wp_pod_templates WHERE $where LIMIT 1");
  809.     }
  810.  
  811.     /**
  812.      * Drop a Pod Page
  813.      *
  814.      * $params['id'] int The page ID
  815.      * $params['uri'] string The page URI
  816.      *
  817.      * @param array $params An associative array of parameters
  818.      * @since 1.7.9
  819.      */
  820.     function drop_page($params) {
  821.         $params = (object) $params;
  822.         $where = empty($params->id) ? "uri = '$params->uri'" : "id = $params->id";
  823.         pod_query("DELETE FROM @wp_pod_pages WHERE $where LIMIT 1");
  824.     }
  825.  
  826.     /**
  827.      * Drop a Pod Helper
  828.      *
  829.      * $params['id'] int The helper ID
  830.      * $params['name'] string The helper name
  831.      *
  832.      * @param array $params An associative array of parameters
  833.      * @since 1.7.9
  834.      */
  835.     function drop_helper($params) {
  836.         $params = (object) $params;
  837.         $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
  838.         pod_query("DELETE FROM @wp_pod_helpers WHERE $where LIMIT 1");
  839.     }
  840.  
  841.     /**
  842.      * Drop a menu item and all its children
  843.      *
  844.      * $params['id'] int The menu ID
  845.      *
  846.      * @param array $params An associative array of parameters
  847.      * @since 1.7.9
  848.      */
  849.     function drop_menu_item($params) {
  850.         $params = (object) $params;
  851.         $result = pod_query("SELECT lft, rgt, (rgt - lft + 1) AS width FROM @wp_pod_menu WHERE id = $params->id LIMIT 1");
  852.         list($lft, $rgt, $width) = mysql_fetch_array($result);
  853.  
  854.         pod_query("DELETE from @wp_pod_menu WHERE lft BETWEEN $lft AND $rgt");
  855.         pod_query("UPDATE @wp_pod_menu SET rgt = rgt - $width WHERE rgt > $rgt");
  856.         pod_query("UPDATE @wp_pod_menu SET lft = lft - $width WHERE lft > $rgt");
  857.     }
  858.  
  859.     /**
  860.      * Drop a single pod item
  861.      *
  862.      * $params['pod_id'] int The item's ID from the wp_pod table
  863.      * $params['tbl_row_id'] int (optional) The item's ID from the wp_pod_tbl_* table (used with datatype parameter)
  864.      * $params['datatype'] string (optional) The datatype name (used with tbl_row_id parameter)
  865.      * $params['datatype_id'] int (optional) The datatype ID (used with tbl_row_id parameter)
  866.      *
  867.      * @param array $params An associative array of parameters
  868.      * @since 1.7.9
  869.      */
  870.     function drop_pod_item($params) {
  871.         $params = (object) $params;
  872.  
  873.         if (isset($params->tbl_row_id)) {
  874.             if (isset($params->datatype_id)) {
  875.                 $select_dt = "p.datatype = '$params->datatype_id'";
  876.             }
  877.             else {
  878.                 $select_dt = "t.name = '$params->datatype'";
  879.             }
  880.             $sql = "
  881.            SELECT
  882.                p.id AS pod_id, p.tbl_row_id, t.id, t.name
  883.            FROM
  884.                @wp_pod p
  885.            INNER JOIN
  886.                @wp_pod_types t ON t.id = p.datatype
  887.            WHERE
  888.                p.tbl_row_id = $params->tbl_row_id AND
  889.                $select_dt
  890.            LIMIT
  891.                1
  892.            ";
  893.         }
  894.         else {
  895.             $sql = "
  896.            SELECT
  897.                p.id AS pod_id, p.tbl_row_id, t.id, t.name
  898.            FROM
  899.                @wp_pod p
  900.            INNER JOIN
  901.                @wp_pod_types t ON t.id = p.datatype
  902.            WHERE
  903.                p.id = $params->pod_id
  904.            LIMIT
  905.                1
  906.            ";
  907.         }
  908.  
  909.         $result = pod_query($sql);
  910.         $row = mysql_fetch_assoc($result);
  911.         $params->datatype_id = $row['id'];
  912.         $params->datatype = $row['name'];
  913.         $params->pod_id = $row['pod_id'];
  914.         $params->tbl_row_id = $row['tbl_row_id'];
  915.  
  916.         // Get helper code
  917.         $result = pod_query("SELECT pre_drop_helpers, post_drop_helpers FROM @wp_pod_types WHERE id = $params->datatype_id");
  918.         $row = mysql_fetch_assoc($result);
  919.         $pre_drop_helpers = str_replace(',', "','", $row['pre_drop_helpers']);
  920.         $post_drop_helpers = str_replace(',', "','", $row['post_drop_helpers']);
  921.  
  922.         // Plugin hook
  923.         do_action('pods_pre_drop_pod_item');
  924.  
  925.         // Pre-drop helpers
  926.         $result = pod_query("SELECT phpcode FROM @wp_pod_helpers WHERE name IN ('$pre_drop_helpers')");
  927.         while ($row = mysql_fetch_assoc($result)) {
  928.             eval('?>' . $row['phpcode']);
  929.         }
  930.  
  931.         pod_query("DELETE FROM `@wp_pod_tbl_$params->datatype` WHERE id = $params->tbl_row_id LIMIT 1");
  932.         pod_query("UPDATE @wp_pod_rel SET sister_pod_id = NULL WHERE sister_pod_id = $params->pod_id");
  933.         pod_query("DELETE FROM @wp_pod WHERE id = $params->pod_id LIMIT 1");
  934.         pod_query("DELETE FROM @wp_pod_rel WHERE pod_id = $params->pod_id");
  935.  
  936.         // Plugin hook
  937.         do_action('pods_post_drop_pod_item');
  938.  
  939.         // Post-drop helpers
  940.         $result = pod_query("SELECT phpcode FROM @wp_pod_helpers WHERE name IN ('$post_drop_helpers')");
  941.         while ($row = mysql_fetch_assoc($result)) {
  942.             eval('?>' . $row['phpcode']);
  943.         }
  944.     }
  945.  
  946.     /**
  947.      * Load a content type and all of its fields
  948.      *
  949.      * $params['id'] int The datatype ID
  950.      * $params['name'] string The datatype name
  951.      *
  952.      * @param array $params An associative array of parameters
  953.      * @since 1.7.9
  954.      */
  955.     function load_pod($params) {
  956.         $params = (object) $params;
  957.         $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
  958.         $result = pod_query("SELECT * FROM @wp_pod_types WHERE $where LIMIT 1");
  959.         $module = mysql_fetch_assoc($result);
  960.  
  961.         $sql = "
  962.            SELECT
  963.                id, name, coltype, pickval, required, weight
  964.            FROM
  965.                @wp_pod_fields
  966.            WHERE
  967.                datatype = {$module['id']}
  968.            ORDER BY
  969.                weight
  970.            ";
  971.  
  972.         $fields = array();
  973.         $result = pod_query($sql);
  974.         while ($row = mysql_fetch_assoc($result)) {
  975.             $fields[] = $row;
  976.         }
  977.  
  978.         // Combine the fields into the $module array
  979.         $module['fields'] = $fields;
  980.         return $module;
  981.     }
  982.  
  983.     /**
  984.      * Load a column
  985.      *
  986.      * $params['id'] int The field ID
  987.      *
  988.      * @param array $params An associative array of parameters
  989.      * @since 1.7.9
  990.      */
  991.     function load_column($params) {
  992.         $params = (object) $params;
  993.         $result = pod_query("SELECT * FROM @wp_pod_fields WHERE id = $params->id LIMIT 1");
  994.         return mysql_fetch_assoc($result);
  995.     }
  996.  
  997.     /**
  998.      * Load a Pod Template
  999.      *
  1000.      * $params['id'] int The template ID
  1001.      * $params['name'] string The template name
  1002.      *
  1003.      * @param array $params An associative array of parameters
  1004.      * @since 1.7.9
  1005.      */
  1006.     function load_template($params) {
  1007.         $params = (object) $params;
  1008.         $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
  1009.         $result = pod_query("SELECT * FROM @wp_pod_templates WHERE $where LIMIT 1");
  1010.         return mysql_fetch_assoc($result);
  1011.     }
  1012.  
  1013.     /**
  1014.      * Load a Pod Page
  1015.      *
  1016.      * $params['id'] int The page ID
  1017.      * $params['uri'] string The page URI
  1018.      *
  1019.      * @param array $params An associative array of parameters
  1020.      * @since 1.7.9
  1021.      */
  1022.     function load_page($params) {
  1023.         $params = (object) $params;
  1024.         $where = empty($params->id) ? "uri = '$params->uri'" : "id = $params->id";
  1025.         $result = pod_query("SELECT * FROM @wp_pod_pages WHERE $where LIMIT 1");
  1026.         return mysql_fetch_assoc($result);
  1027.     }
  1028.  
  1029.     /**
  1030.      * Load a Pod Helper
  1031.      *
  1032.      * $params['id'] int The helper ID
  1033.      * $params['name'] string The helper name
  1034.      *
  1035.      * @param array $params An associative array of parameters
  1036.      * @since 1.7.9
  1037.      */
  1038.     function load_helper($params) {
  1039.         $params = (object) $params;
  1040.         $where = empty($params->id) ? "name = '$params->name'" : "id = $params->id";
  1041.         $result = pod_query("SELECT * FROM @wp_pod_helpers WHERE $where LIMIT 1");
  1042.         return mysql_fetch_assoc($result);
  1043.     }
  1044.  
  1045.     /**
  1046.      * Load a single menu item
  1047.      *
  1048.      * $params['id'] int The menu ID
  1049.      *
  1050.      * @param array $params An associative array of parameters
  1051.      * @since 1.7.9
  1052.      */
  1053.     function load_menu_item($params) {
  1054.         $params = (object) $params;
  1055.         $result = pod_query("SELECT * FROM @wp_pod_menu WHERE id = $params->id LIMIT 1");
  1056.         return mysql_fetch_assoc($result);
  1057.     }
  1058.  
  1059.     /**
  1060.      * Load the input form for a pod item
  1061.      *
  1062.      * $params['datatype'] string The datatype name
  1063.      * $params['pod_id'] int The item's pod ID
  1064.      * $params['tbl_row_id'] int (optional) The item's ID
  1065.      * $params['public_columns'] array An associative array of columns
  1066.      *
  1067.      * @param array $params An associative array of parameters
  1068.      * @since 1.7.9
  1069.      */
  1070.     function load_pod_item($params) {
  1071.         $params = (object) $params;
  1072.  
  1073.         $params->tbl_row_id = (int) (isset($params->tbl_row_id)?$params->tbl_row_id:null);
  1074.         $params->pod_id = (int) (isset($params->pod_id)?$params->pod_id:null);
  1075.         if (empty($params->tbl_row_id)) {
  1076.             $params->tbl_row_id = null;
  1077.             if (!empty($params->pod_id)) {
  1078.                 $result = pod_query("SELECT tbl_row_id FROM @wp_pod WHERE id = $params->pod_id LIMIT 1",'Item not found',null,'Item not found');
  1079.                 $params->tbl_row_id = mysql_result($result, 0);
  1080.             }
  1081.         }
  1082.         $obj = new Pod($params->datatype,$params->tbl_row_id);
  1083.         $pod_id = 0;
  1084.         if (!empty($params->tbl_row_id) && !empty($obj->data)) {
  1085.             $pod_id = $obj->get_pod_id();
  1086.         }
  1087.         return $obj->showform($pod_id, $params->public_columns = null);
  1088.     }
  1089.  
  1090.     /**
  1091.      * Load a bi-directional (sister) column
  1092.      *
  1093.      * $params['pickval'] string The related PICK pod name
  1094.      * $params['datatype'] int The datatype ID
  1095.      *
  1096.      * @param array $params An associative array of parameters
  1097.      * @since 1.7.9
  1098.      */
  1099.     function load_sister_fields($params) {
  1100.         $params = (object) $params;
  1101.  
  1102.         if (!empty($params->pickval) && is_string($params->pickval)) {
  1103.             $result = pod_query("SELECT id FROM @wp_pod_types WHERE name = '$params->pickval' LIMIT 1");
  1104.             if (0 < mysql_num_rows($result)) {
  1105.                 $sister_datatype = mysql_result($result, 0);
  1106.  
  1107.                 $result = pod_query("SELECT name FROM @wp_pod_types WHERE id = $params->datatype LIMIT 1");
  1108.                 if (0 < mysql_num_rows($result)) {
  1109.                     $datatype_name = mysql_result($result, 0);
  1110.  
  1111.                     $result = pod_query("SELECT id, name FROM @wp_pod_fields WHERE datatype = $sister_datatype AND pickval = '$datatype_name'");
  1112.                     if (0 < mysql_num_rows($result)) {
  1113.                         while ($row = mysql_fetch_assoc($result)) {
  1114.                             $sister_fields[] = $row;
  1115.                         }
  1116.                         return $sister_fields;
  1117.                     }
  1118.                 }
  1119.             }
  1120.         }
  1121.     }
  1122.  
  1123.     /**
  1124.      * Export a package
  1125.      *
  1126.      * $params['pod'] string Pod Type IDs to export
  1127.      * $params['template'] string Template IDs to export
  1128.      * $params['podpage'] string Pod Page IDs to export
  1129.      * $params['helper'] string Helper IDs to export
  1130.      *
  1131.      * @param array $params An associative array of parameters
  1132.      * @since 1.9.0
  1133.      */
  1134.     function export_package($params) {
  1135.         $export = array(
  1136.             'meta' => array(
  1137.                 'version' => get_option('pods_version'),
  1138.                 'build' => date('U'),
  1139.             )
  1140.         );
  1141.  
  1142.         $pod_ids = $params['pod'];
  1143.         $template_ids = $params['template'];
  1144.         $pod_page_ids = $params['podpage'];
  1145.         $helper_ids = $params['helper'];
  1146.  
  1147.         // Get pods
  1148.         if (!empty($pod_ids)) {
  1149.             $result = pod_query("SELECT * FROM @wp_pod_types WHERE id IN ($pod_ids)");
  1150.             while ($row = mysql_fetch_assoc($result)) {
  1151.                 $dt = $row['id'];
  1152.                 unset($row['id']);
  1153.                 $export['pods'][$dt] = $row;
  1154.             }
  1155.  
  1156.             // Get pod fields
  1157.             $result = pod_query("SELECT * FROM @wp_pod_fields WHERE datatype IN ($pod_ids)");
  1158.             while ($row = mysql_fetch_assoc($result)) {
  1159.                 unset($row['id']);
  1160.                 $dt = $row['datatype'];
  1161.                 unset($row['datatype']);
  1162.                 $export['pods'][$dt]['fields'][] = $row;
  1163.             }
  1164.         }
  1165.  
  1166.         // Get templates
  1167.         if (!empty($template_ids)) {
  1168.             $result = pod_query("SELECT * FROM @wp_pod_templates WHERE id IN ($template_ids)");
  1169.             while ($row = mysql_fetch_assoc($result)) {
  1170.                 unset($row['id']);
  1171.                 $export['templates'][] = $row;
  1172.             }
  1173.         }
  1174.  
  1175.         // Get pod pages
  1176.         if (!empty($pod_page_ids)) {
  1177.             $result = pod_query("SELECT * FROM @wp_pod_pages WHERE id IN ($pod_page_ids)");
  1178.             while ($row = mysql_fetch_assoc($result)) {
  1179.                 unset($row['id']);
  1180.                 $export['pod_pages'][] = $row;
  1181.             }
  1182.         }
  1183.  
  1184.         // Get helpers
  1185.         if (!empty($helper_ids)) {
  1186.             $result = pod_query("SELECT * FROM @wp_pod_helpers WHERE id IN ($helper_ids)");
  1187.             while ($row = mysql_fetch_assoc($result)) {
  1188.                 unset($row['id']);
  1189.                 $export['helpers'][] = $row;
  1190.             }
  1191.         }
  1192.  
  1193.         return $export;
  1194.     }
  1195.  
  1196.     /**
  1197.      * Import a package
  1198.      *
  1199.      *
  1200.      * @param mixed $data (optional) An associative array containing a package, or the json encoded package
  1201.      * @since 1.9.0
  1202.      */
  1203.     function import_package($data = false) {
  1204.         $output = false;
  1205.         if (false===$data || isset($data['action'])) {
  1206.             $data = get_option('pods_package');
  1207.             $output = true;
  1208.         }
  1209.         if (!is_array($data)) {
  1210.             $data = @json_decode(stripslashes($data), true);
  1211.         }
  1212.         if (!is_array($data) || empty($data)) {
  1213.             return false;
  1214.         }
  1215.  
  1216.         $dbtypes = array(
  1217.             'bool' => 'bool default 0',
  1218.             'date' => 'datetime',
  1219.             'num' => 'decimal(9,2)',
  1220.             'txt' => 'varchar(128)',
  1221.             'slug' => 'varchar(128)',
  1222.             'code' => 'mediumtext',
  1223.             'desc' => 'mediumtext'
  1224.         );
  1225.         $dbtypes = apply_filters('pods_column_dbtypes', $dbtypes, &$this);
  1226.  
  1227.         if (isset($data['pods'])) {
  1228.             $pod_columns = '';
  1229.             foreach ($data['pods'] as $key => $val) {
  1230.                 $table_columns = array();
  1231.                 $pod_fields = $val['fields'];
  1232.                 unset($val['fields']);
  1233.  
  1234.                 // Escape the values
  1235.                 foreach ($val as $k => $v) {
  1236.                     $val[$k] = pods_sanitize($v);
  1237.                 }
  1238.  
  1239.                 if (empty($pod_columns)) {
  1240.                     $pod_columns = implode("`,`", array_keys($val));
  1241.                 }
  1242.                 // Backward-compatibility (before/after helpers)
  1243.                 $pod_columns = str_replace('before_helpers', 'pre_save_helpers', $pod_columns);
  1244.                 $pod_columns = str_replace('after_helpers', 'post_save_helpers', $pod_columns);
  1245.  
  1246.                 $values = implode("','", $val);
  1247.                 $dt = pod_query("INSERT INTO @wp_pod_types (`$pod_columns`) VALUES ('$values')");
  1248.  
  1249.                 $tupples = array();
  1250.                 $field_columns = '';
  1251.                 foreach ($pod_fields as $key => $fieldval) {
  1252.                     // Escape the values
  1253.                     foreach ($fieldval as $k => $v) {
  1254.                         $fieldval[$k] = empty($v) ? 'null' : pods_sanitize($v);
  1255.                     }
  1256.  
  1257.                     // Store all table columns
  1258.                     if ('pick' != $fieldval['coltype'] && 'file' != $fieldval['coltype']) {
  1259.                         $table_columns[$fieldval['name']] = $fieldval['coltype'];
  1260.                     }
  1261.  
  1262.                     $fieldval['datatype'] = $dt;
  1263.                     if (empty($field_columns)) {
  1264.                         $field_columns = implode("`,`", array_keys($fieldval));
  1265.                     }
  1266.                     $tupples[] = implode("','", $fieldval);
  1267.                 }
  1268.                 $tupples = implode("'),('", $tupples);
  1269.                 $tupples = str_replace("'null'", 'null', $tupples);
  1270.                 pod_query("INSERT INTO @wp_pod_fields (`$field_columns`) VALUES ('$tupples')");
  1271.  
  1272.                 // Create the actual table with any non-PICK columns
  1273.                 $definitions = array("id INT unsigned auto_increment primary key");
  1274.                 foreach ($table_columns as $colname => $coltype) {
  1275.                     $definitions[] = "`$colname` {$dbtypes[$coltype]}";
  1276.                 }
  1277.                 $definitions = implode(',', $definitions);
  1278.                 pod_query("CREATE TABLE @wp_pod_tbl_{$val['name']} ($definitions)");
  1279.             }
  1280.         }
  1281.  
  1282.         if (isset($data['templates'])) {
  1283.             $columns = '';
  1284.             $tupples = array();
  1285.             foreach ($data['templates'] as $key => $val) {
  1286.                 // Escape the values
  1287.                 foreach ($val as $k => $v) {
  1288.                     $val[$k] = pods_sanitize($v);
  1289.                 }
  1290.  
  1291.                 if (empty($columns)) {
  1292.                     $columns = implode("`,`", array_keys($val));
  1293.                 }
  1294.                 $tupples[] = implode("','", $val);
  1295.             }
  1296.             $tupples = implode("'),('", $tupples);
  1297.             pod_query("INSERT INTO @wp_pod_templates (`$columns`) VALUES ('$tupples')");
  1298.         }
  1299.  
  1300.         if (isset($data['pod_pages'])) {
  1301.             $columns = '';
  1302.             $tupples = array();
  1303.             foreach ($data['pod_pages'] as $key => $val) {
  1304.                 // Escape the values
  1305.                 foreach ($val as $k => $v) {
  1306.                     $val[$k] = pods_sanitize($v);
  1307.                 }
  1308.  
  1309.                 if (empty($columns)) {
  1310.                     $columns = implode("`,`", array_keys($val));
  1311.                 }
  1312.                 $tupples[] = implode("','", $val);
  1313.             }
  1314.             $tupples = implode("'),('", $tupples);
  1315.             pod_query("INSERT INTO @wp_pod_pages (`$columns`) VALUES ('$tupples')");
  1316.         }
  1317.  
  1318.         if (isset($data['helpers'])) {
  1319.             $columns = '';
  1320.             $tupples = array();
  1321.             foreach ($data['helpers'] as $key => $val) {
  1322.                 // Escape the values
  1323.                 foreach ($val as $k => $v) {
  1324.                     // Backward-compatibility (before/after helpers)
  1325.                     if ('helper_type' == $k) {
  1326.                         $v = ('before' == $v) ? 'pre_save' : $v;
  1327.                         $v = ('after' == $v) ? 'post_save' : $v;
  1328.                     }
  1329.                     $val[$k] = pods_sanitize($v);
  1330.                 }
  1331.  
  1332.                 if (empty($columns)) {
  1333.                     $columns = implode("`,`", array_keys($val));
  1334.                 }
  1335.                 $tupples[] = implode("','", $val);
  1336.             }
  1337.             $tupples = implode("'),('", $tupples);
  1338.             pod_query("INSERT INTO @wp_pod_helpers (`$columns`) VALUES ('$tupples')");
  1339.         }
  1340.         if (true===$output) {
  1341.             echo "<p><strong>Success!</strong></p>";
  1342.         }
  1343.         return true;
  1344.     }
  1345.  
  1346.     /**
  1347.      * Validate a package
  1348.      *
  1349.      *
  1350.      * @param mixed $data (optional) An associative array containing a package, or the json encoded package
  1351.      * @since 1.9.0
  1352.      */
  1353.     function validate_package($data = false) {
  1354.         $output = false;
  1355.         if (is_array($data)&&isset($data['data'])) {
  1356.             $data = $data['data'];
  1357.             $output = true;
  1358.         }
  1359.         if (is_array($data)) {
  1360.             $data = htmlspecialchars(json_encode($data));
  1361.         }
  1362.         $warnings = array();
  1363.  
  1364.         update_option('pods_package', $data);
  1365.  
  1366.         $data = @json_decode(stripslashes($data), true);
  1367.  
  1368.         if (!is_array($data) || empty($data)) {
  1369.             $warnings[] = "This is not a valid package. Please try again.";
  1370.         }
  1371.  
  1372.         if (isset($data['pods'])) {
  1373.             foreach ($data['pods'] as $id => $val) {
  1374.                 $pod_name = $val['name'];
  1375.                 $result = pod_query("SELECT id FROM @wp_pod_types WHERE name = '$pod_name' LIMIT 1");
  1376.                 if (0 < mysql_num_rows($result)) {
  1377.                     $warnings[] = "The pod <b>$pod_name</b> already exists!";
  1378.                 }
  1379.             }
  1380.         }
  1381.  
  1382.         if (isset($data['pod_pages'])) {
  1383.             foreach ($data['pod_pages'] as $id => $val) {
  1384.                 $uri = $val['uri'];
  1385.                 $result = pod_query("SELECT id FROM @wp_pod_pages WHERE uri = '$uri' LIMIT 1");
  1386.                 if (0 < mysql_num_rows($result)) {
  1387.                     $warnings[] = "The pod page <b>$uri</b> already exists!";
  1388.                 }
  1389.             }
  1390.         }
  1391.  
  1392.         if (isset($data['helpers'])) {
  1393.             foreach ($data['helpers'] as $id => $val) {
  1394.                 $helper_name = $val['name'];
  1395.                 $result = pod_query("SELECT id FROM @wp_pod_helpers WHERE name = '$helper_name' LIMIT 1");
  1396.                 if (0 < mysql_num_rows($result)) {
  1397.                     $warnings[] = "The helper <b>$helper_name</b> already exists!";
  1398.                 }
  1399.             }
  1400.         }
  1401.  
  1402.         if (0 < count($warnings)) {
  1403.             if (true===$output) {
  1404.                 echo '<p class="red">The import cannot continue because of the following warnings:</p>';
  1405.                 echo '<p>' . implode('</p><p>', $warnings) . '</p>';
  1406.                 return false;
  1407.             }
  1408.             else {
  1409.                 return $warnings;
  1410.             }
  1411.         }
  1412.         else {
  1413.             if (true===$output) {
  1414.                 echo '<p><input type="button" class="button" onclick="podsImport(true)" value="Looking good. Finalize!" />';
  1415.             }
  1416.             return true;
  1417.         }
  1418.     }
  1419.  
  1420.     /**
  1421.      * Import data
  1422.      *
  1423.      * @param mixed $data PHP associative array or CSV input
  1424.      * @param bool $numeric_mode Use IDs instead of the name field when matching
  1425.      * @since 1.7.1
  1426.      */
  1427.     function import($data, $numeric_mode = false) {
  1428.         global $wpdb;
  1429.         if ('csv' == $this->format) {
  1430.             $data = $this->csv_to_php($data);
  1431.         }
  1432.  
  1433.         pod_query("SET NAMES utf8");
  1434.         pod_query("SET CHARACTER SET utf8");
  1435.  
  1436.         // Get the id/name pairs of all associated pick/file tables
  1437.         $pick_values = $file_values = array();
  1438.         foreach ($this->fields as $field_name => $field_data) {
  1439.             $pickval = $field_data['pickval'];
  1440.             if ('file' == $field_data['coltype']) {
  1441.                 $res = pod_query("SELECT ID as id, guid as name FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY id");
  1442.                 while ($item = mysql_fetch_assoc($res)) {
  1443.                     $file_url = str_replace(get_bloginfo('url'), '', $item['name']);
  1444.                     $file_values[$field_name][$file_url] = $item['id'];
  1445.                     $file_values[$field_name][$item['name']] = $item['id'];
  1446.                 }
  1447.             }
  1448.             elseif ('pick' == $field_data['coltype']) {
  1449.                 if ('wp_taxonomy' == $pickval) {
  1450.                     $res = pod_query("SELECT term_id AS id, name FROM $wpdb->terms ORDER BY id");
  1451.                     while ($item = mysql_fetch_assoc($res)) {
  1452.                         $pick_values[$field_name][$item['name']] = $item['id'];
  1453.                     }
  1454.                 }
  1455.                 elseif ('wp_page' == $pickval || 'wp_post' == $pickval) {
  1456.                     $pickval = str_replace('wp_', '', $pickval);
  1457.                     $res = pod_query("SELECT ID as id, post_title as name FROM $wpdb->posts WHERE post_type = '$pickval' ORDER BY id");
  1458.                     while ($item = mysql_fetch_assoc($res)) {
  1459.                         $pick_values[$field_name][$item['name']] = $item['id'];
  1460.                     }
  1461.                 }
  1462.                 elseif ('wp_user' == $pickval) {
  1463.                     $res = pod_query("SELECT ID as id, display_name as name FROM $wpdb->users ORDER BY id");
  1464.                     while ($item = mysql_fetch_assoc($res)) {
  1465.                         $pick_values[$field_name][$item['name']] = $item['id'];
  1466.                     }
  1467.                 }
  1468.                 else {
  1469.                     $res = pod_query("SELECT id, name FROM @wp_pod_tbl_{$pickval} ORDER BY id");
  1470.                     while ($item = mysql_fetch_assoc($res)) {
  1471.                         $pick_values[$field_name][$item['name']] = $item['id'];
  1472.                     }
  1473.                 }
  1474.             }
  1475.         }
  1476.  
  1477.         // Loop through the array of items
  1478.         $ids = array();
  1479.  
  1480.         // Test to see if it's an array of arrays
  1481.         foreach ($data as $key => $data_row) {
  1482.             if(!is_array($data_row)){
  1483.                 $data = array($data);
  1484.             }
  1485.             break;
  1486.         }
  1487.         foreach ($data as $key => $data_row) {
  1488.             $columns = array();
  1489.  
  1490.             // Loop through each field (use $this->fields so only valid columns get parsed)
  1491.             foreach ($this->fields as $field_name => $field_data) {
  1492.                 $field_id = $field_data['id'];
  1493.                 $coltype = $field_data['coltype'];
  1494.                 $pickval = $field_data['pickval'];
  1495.                 $field_value = $data_row[$field_name];
  1496.  
  1497.                 if (null != $field_value && false !== $field_value) {
  1498.                     if ('pick' == $coltype || 'file' == $coltype) {
  1499.                         $field_values = is_array($field_value) ? $field_value : array($field_value);
  1500.                         $pick_value = array();
  1501.                         foreach ($field_values as $key => $pick_title) {
  1502.                             if (is_int($pick_title) && false !== $numeric_mode) {
  1503.                                 $pick_value[] = $pick_title;
  1504.                             }
  1505.                             elseif (!empty($pick_values[$field_name][$pick_title])) {
  1506.                                 $pick_value[] = $pick_values[$field_name][$pick_title];
  1507.                             }
  1508.                         }
  1509.                         $field_value = implode(',',$pick_value);
  1510.                     }
  1511.                     $columns[$field_name] = mysql_real_escape_string(trim($field_value));
  1512.                 }
  1513.             }
  1514.             if (!empty($columns)) {
  1515.                 $params = array('datatype'=>$this->dtname,'columns'=>$columns);
  1516.                 $ids[] = $this->save_pod_item($params);
  1517.             }
  1518.         }
  1519.         return $ids;
  1520.     }
  1521.  
  1522.     /**
  1523.      * Export data
  1524.      *
  1525.      * @since 1.7.1
  1526.      */
  1527.     function export() {
  1528.         global $wpdb;
  1529.         $data = array();
  1530.         $fields = array();
  1531.         $pick_values = array();
  1532.  
  1533.         // Find all pick/file fields
  1534.         $result = pod_query("SELECT id, name, coltype, pickval FROM @wp_pod_fields WHERE datatype = {$this->dt} ORDER BY weight");
  1535.         while ($row = mysql_fetch_assoc($result)) {
  1536.             $field_id = $row['id'];
  1537.             $field_name = $row['name'];
  1538.             $coltype = $row['coltype'];
  1539.             $pickval = $row['pickval'];
  1540.  
  1541.             // Store all pick/file values into an array
  1542.             if ('file' == $coltype) {
  1543.                 $res = pod_query("SELECT ID AS id, guid AS name FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY id");
  1544.                 while ($item = mysql_fetch_assoc($res)) {
  1545.                     $pick_values[$field_name][$item['id']] = $item['name'];
  1546.                 }
  1547.             }
  1548.             elseif ('pick' == $coltype) {
  1549.                 if ('wp_taxonomy' == $pickval) {
  1550.                     $res = pod_query("SELECT term_id AS id, name FROM $wpdb->terms ORDER BY id");
  1551.                     while ($item = mysql_fetch_assoc($res)) {
  1552.                         $pick_values[$field_name][$item['id']] = $item['name'];
  1553.                     }
  1554.                 }
  1555.                 elseif ('wp_page' == $pickval || 'wp_post' == $pickval) {
  1556.                     $pickval = str_replace('wp_', '', $pickval);
  1557.                     $res = pod_query("SELECT ID as id, post_title as name FROM $wpdb->posts WHERE post_type = '$pickval' ORDER BY id");
  1558.                     while ($item = mysql_fetch_assoc($res)) {
  1559.                         $pick_values[$field_name][$item['id']] = $item['name'];
  1560.                     }
  1561.                 }
  1562.                 elseif ('wp_user' == $pickval) {
  1563.                     $res = pod_query("SELECT ID as id, display_name as name FROM $wpdb->users ORDER BY id");
  1564.                     while ($item = mysql_fetch_assoc($res)) {
  1565.                         $pick_values[$field_name][$item['id']] = $item['name'];
  1566.                     }
  1567.                 }
  1568.                 else {
  1569.                     $res = pod_query("SELECT id, name FROM @wp_pod_tbl_{$pickval} ORDER BY id");
  1570.                     while ($item = mysql_fetch_assoc($res)) {
  1571.                         $pick_values[$field_name][$item['id']] = $item['name'];
  1572.                     }
  1573.                 }
  1574.             }
  1575.             $fields[$field_id] = $field_name;
  1576.         }
  1577.  
  1578.         // Get all pick rel values
  1579.         $sql = "
  1580.        SELECT
  1581.            p.tbl_row_id, r.field_id, r.tbl_row_id AS item_id
  1582.        FROM
  1583.            @wp_pod_rel r
  1584.        INNER JOIN
  1585.            @wp_pod p ON p.id = r.pod_id AND p.datatype = {$this->dt}
  1586.        ORDER BY
  1587.            p.tbl_row_id
  1588.        ";
  1589.         $result = pod_query($sql);
  1590.         while ($row = mysql_fetch_assoc($result)) {
  1591.             $item_id = $row['item_id'];
  1592.             $tbl_row_id = $row['tbl_row_id'];
  1593.             $field_name = $fields[$row['field_id']];
  1594.             $pick_array[$field_name][$tbl_row_id][] = $pick_values[$field_name][$item_id];
  1595.         }
  1596.  
  1597.         // Access the current datatype
  1598.         $result = pod_query("SELECT * FROM @wp_pod_tbl_{$this->dtname} ORDER BY id");
  1599.         while ($row = mysql_fetch_assoc($result)) {
  1600.             $tmp = array();
  1601.             $row_id = $row['id'];
  1602.  
  1603.             foreach ($fields as $junk => $fname) {
  1604.                 if (isset($pick_array[$fname][$row_id])) {
  1605.                     $tmp[$fname] = $pick_array[$fname][$row_id];
  1606.                 }
  1607.                 else {
  1608.                     $tmp[$fname] = $row[$fname];
  1609.                 }
  1610.             }
  1611.             $data[] = $tmp;
  1612.         }
  1613.         return $data;
  1614.     }
  1615.  
  1616.     /**
  1617.      * Convert CSV to a PHP array
  1618.      *
  1619.      * @param string $data The CSV input
  1620.      * @since 1.7.1
  1621.      */
  1622.     function csv_to_php($data) {
  1623.         $delimiter = ",";
  1624.         $expr = "/$delimiter(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))/";
  1625.         $data = str_replace("\r\n", "\n", $data);
  1626.         $data = str_replace("\r", "\n", $data);
  1627.         $lines = explode("\n", $data);
  1628.         $field_names = explode($delimiter, array_shift($lines));
  1629.         $field_names = preg_replace("/^\"(.*)\"$/s", "$1", $field_names);
  1630.         foreach ($lines as $line) {
  1631.             // Skip the empty line
  1632.             if (empty($line)) continue;
  1633.             $fields = preg_split($expr, trim($line));
  1634.             $fields = preg_replace("/^\"(.*)\"$/s", "$1", $fields);
  1635.             foreach ($field_names as $key => $field) {
  1636.                 $tmp[$field] = $fields[$key];
  1637.             }
  1638.             $out[] = $tmp;
  1639.         }
  1640.         return $out;
  1641.     }
  1642. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement