function pods_ui_form($object, $add = 0, $duplicate = 0) { if (!is_object($object)) { echo '<strong>Error:</strong> Pods UI needs an object to run from, see the User Guide for more information.'; return false; } if ($duplicate == 1 && $object->id > 0) { $add = 0; $fields = $object->ui['duplicate_fields'] != null ? $object->ui['duplicate_fields'] : $object->ui['edit_fields']; $access = $object->ui['duplicate_where']; $what = 'Add New '; if ($object->ui['label_duplicate'] !== null) { $object->ui['label'] = $object->ui['label_duplicate']; } if ($object->ui['label'] === null) { $object->ui['label'] = 'Add New ' . $object->ui['item']; } } elseif ($add == 0 && $object->id > 0) { $fields = $object->ui['edit_fields']; $access = $object->ui['edit_where']; $what = 'Edit '; if ($object->ui['label_edit'] !== null) { $object->ui['label'] = $object->ui['label_edit']; } if ($object->ui['label'] === null) { $object->ui['label'] = 'Save Changes'; } } else { $add = 1; $duplicate = 0; $fields = $object->ui['add_fields']; $access = null; $what = 'Add New '; if ($object->ui['label_add'] !== null) { $object->ui['label'] = $object->ui['label_add']; } if ($object->ui['label'] === null) { $object->ui['label'] = 'Add New ' . $object->ui['item']; } } ?> <h2><?php echo $what . $object->ui['item']; ?> <small>(<a href="<?php echo pods_ui_var_update($object->ui['manage_content'] !== null ? array('page' => 'pods-manage-' . $object->datatype) : array('action' . $object->ui['num'] => 'manage')); ?> ">« Back to Manage</a>)</small></h2> <?php $check = pods_ui_verify_access($object, $access, strtolower($what)); if ($check === false) { pods_ui_message('<strong>Error:</strong> You do not have the permissions required to ' . strtolower($what) . ' this ' . $object->ui['item'] . '.', 2); return; } $viewit = ''; if ($add == 0 && $object->get_field('detail_url') != get_bloginfo('url') . '/') { $viewit = ' | <a href="' . $object->get_field('detail_url') . '">View ' . $object->ui['item'] . ' »</a>'; } if (isset($_GET['updated' . $object->ui['num']])) { pods_ui_message($object->ui['item'] . ' updated.' . $viewit); } elseif (isset($_GET['duplicated' . $object->ui['num']])) { $redirect_array = array('action' . $object->ui['num'] => 'add', 'id' . $object->ui['num'] => '', 'updated' . $object->ui['num'] => '', 'duplicated' . $object->ui['num'] => '', 'added' . $object->ui['num'] => ''); $redirect_to = pods_ui_var_update($redirect_array, true); $redirect_array = array('action' . $object->ui['num'] => 'duplicate', 'id' . $object->ui['num'] => $object->id, 'updated' . $object->ui['num'] => '', 'duplicated' . $object->ui['num'] => '', 'added' . $object->ui['num'] => ''); $redirect_to_duplicate = pods_ui_var_update($redirect_array, true); pods_ui_message($object->ui['item'] . ' duplicated successfully. <a href="' . $redirect_to . '">Add another »</a>' . (!in_array('duplicate', $object->ui['disable_actions']) ? ' | <a href="' . $redirect_to_duplicate . '">Add another based on this one »</a>' : '') . $viewit); } elseif (isset($_GET['added' . $object->ui['num']])) { $redirect_array = array('action' . $object->ui['num'] => 'add', 'id' . $object->ui['num'] => '', 'updated' . $object->ui['num'] => '', 'duplicated' . $object->ui['num'] => '', 'added' . $object->ui['num'] => ''); $redirect_to = pods_ui_var_update($redirect_array, true); $redirect_array = array('action' . $object->ui['num'] => 'duplicate', 'id' . $object->ui['num'] => $object->id, 'updated' . $object->ui['num'] => '', 'duplicated' . $object->ui['num'] => '', 'added' . $object->ui['num'] => ''); $redirect_to_duplicate = pods_ui_var_update($redirect_array, true); pods_ui_message($object->ui['item'] . ' added successfully. <a href="' . $redirect_to . '">Add another »</a>' . (!in_array('duplicate', $object->ui['disable_actions']) ? ' | <a href="' . $redirect_to_duplicate . '">Add another based on this one »</a>' : '') . $viewit); } elseif ($duplicate == 1) { pods_ui_message('<strong>About Duplicating:</strong> The form below is filled with information based off an existing ' . $object->ui['item'] . '. By saving this information, you will create a new ' . $object->ui['item'] . ' and nothing will be overwritten.'); } ob_start(); $object->publicForm($fields, $object->ui['label']); $form = ob_get_clean(); $actionwhat = 'updated'; if ($duplicate == 1) { $actionwhat = 'duplicated'; } elseif ($add == 1) { $actionwhat = 'added'; } $redirect_array = array('action' . $object->ui['num'] => 'edit', 'id' . $object->ui['num'] => '', 'updated' . $object->ui['num'] => '', 'duplicated' . $object->ui['num'] => '', 'added' . $object->ui['num'] => '', $actionwhat . $object->ui['num'] => 1); $redirect_to = pods_ui_var_update($redirect_array, true) . '&id="+msg+"'; if ($object->ui['action_after_save'] != null) { $redirect_array = array('action' . $object->ui['num'] => $object->ui['action_after_save'], 'id' . $object->ui['num'] => '', 'updated' . $object->ui['num'] => '', 'duplicated' . $object->ui['num'] => '', 'added' . $object->ui['num'] => '', $actionwhat . $object->ui['num'] => 1); $redirect_to = pods_ui_var_update($redirect_array, true) . ($object->ui['action_after_save'] == 'edit' ? '&id="+msg+"' : ''); } $form = str_replace('window.location = "' . $_SERVER['REQUEST_URI'] . '";', 'window.location = "' . $redirect_to . '";', $form); if (!in_array('delete', $object->ui['disable_actions']) && $object->id != null && $duplicate == 0) { $form = str_replace('<input type="button" class="button btn_save" value="' . $object->ui['label'] . '" onclick="saveForm(1)" />', '<input type="button" class="button btn_save" value="' . $object->ui['label'] . '" onclick="saveForm(1)" /> <a style="color:#red;font-weight:normal;text-decoration:underline;font-style:italic;" title="Delete this item" href="' . pods_ui_var_update(array('action' . $object->ui['num'] => 'delete', 'id' . $object->ui['num'] => $object->get_field('id'))) . '" onclick="if ( confirm(\'You are about to delete this item \\\'' . htmlentities($object->get_field('name')) . '\\\'\\n \\\'Cancel\\\' to stop, \\\'OK\\\' to delete.\') ) { return true;}return false;">Delete this item</a>', $form); } if ($duplicate == 1) { $form = str_replace('<input type="hidden" class="form num pod_id" value="' . $object->get_pod_id() . '" />', '<input type="hidden" class="form num pod_id" value="0" />', $form); $form = str_replace('<input type="hidden" class="form num tbl_row_id" value="' . $object->get_field('id') . '" />', '<input type="hidden" class="form num tbl_row_id" value="0" />', $form); } echo $form; }
/** * @param $import * @param bool $output */ public function heres_the_beef($import, $output = true) { global $wpdb; $api = pods_api(); for ($i = 0; $i < 40000; $i++) { echo " \t"; // extra spaces } $default_data = array('pod' => null, 'table' => null, 'reset' => null, 'update_on' => null, 'where' => null, 'fields' => array(), 'row_filter' => null, 'pre_save' => null, 'post_save' => null, 'sql' => null, 'sort' => null, 'limit' => null, 'page' => null, 'output' => null, 'page_var' => 'ipg', 'bypass_helpers' => false); $default_field_data = array('field' => null, 'filter' => null); if (!is_array($import)) { $import = array($import); } elseif (empty($import)) { die('<h1 style="color:red;font-weight:bold;">ERROR: No imports configured</h1>'); } $import_counter = 0; $total_imports = count($import); $paginated = false; $avg_time = -1; $total_time = 0; $counter = 0; $avg_unit = 100; $avg_counter = 0; foreach ($import as $datatype => $data) { $import_counter++; flush(); @ob_end_flush(); usleep(50000); if (!is_array($data)) { $datatype = $data; $data = array('table' => $data); } if (isset($data[0])) { $data = array('table' => $data[0]); } $data = array_merge($default_data, $data); if (null === $data['pod']) { $data['pod'] = array('name' => $datatype); } if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - <em>" . $data['pod']['name'] . "</em> - <strong>Loading Pod: " . $data['pod']['name'] . "</strong>\n"; } if (2 > count($data['pod'])) { $data['pod'] = $api->load_pod(array('name' => $data['pod']['name'])); } if (empty($data['pod']['fields'])) { continue; } if (null === $data['table']) { $data['table'] = $data['pod']['name']; } if ($data['reset'] === true) { if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - <strong style='color:blue;'>Resetting Pod: " . $data['pod']['name'] . "</strong>\n"; } $api->reset_pod(array('id' => $data['pod']['id'], 'name' => $data['pod']['name'])); } if (null === $data['sort'] && null !== $data['update_on'] && isset($data['fields'][$data['update_on']])) { if (isset($data['fields'][$data['update_on']]['field'])) { $data['sort'] = $data['fields'][$data['update_on']]['field']; } else { $data['sort'] = $data['update_on']; } } $page = 1; if (false !== $data['page_var'] && isset($_GET[$data['page_var']])) { $page = absval($_GET[$data['page_var']]); } if (null === $data['sql']) { $data['sql'] = "SELECT * FROM {$data['table']}" . (null !== $data['where'] ? " WHERE {$data['where']}" : '') . (null !== $data['sort'] ? " ORDER BY {$data['sort']}" : '') . (null !== $data['limit'] ? " LIMIT " . (1 < $page ? ($page - 1) * $data['limit'] . ',' : '') . "{$data['limit']}" : ''); } if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Getting Results: " . $data['pod']['name'] . "\n"; } if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Using Query: <small><code>" . $data['sql'] . "</code></small>\n"; } $result = $wpdb->get_results($data['sql'], ARRAY_A); if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Results Found: " . count($result) . "\n"; } $avg_time = -1; $total_time = 0; $counter = 0; $avg_unit = 100; $avg_counter = 0; $result_count = count($result); $paginated = false; if (false !== $data['page_var'] && $result_count == $data['limit']) { $paginated = "<input type=\"button\" onclick=\"document.location=\\'" . pods_ui_var_update(array($data['page_var'] => $page + 1), false, false) . "\\';\" value=\" Continue Import » \" />"; } if ($result_count < $avg_unit && 5 < $result_count) { $avg_unit = number_format($result_count / 5, 0, '', ''); } elseif (2000 < $result_count && 10 < count($data['pod']['fields'])) { $avg_unit = 40; } $data['count'] = $result_count; timer_start(); if (false !== $output && 1 == $import_counter) { echo "<div style='width:50%;background-color:navy;padding:10px 10px 30px 10px;color:#FFF;position:absolute;top:10px;left:25%;text-align:center;'><p id='progress_status' align='center'>" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Running Importer..</p><br /><small>This will automatically update every " . $avg_unit . " rows</small></div>\n"; } foreach ($result as $k => $row) { flush(); @ob_end_flush(); usleep(50000); if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Processing Row #" . ($k + 1) . "\n"; } if (null !== $data['row_filter'] && function_exists($data['row_filter'])) { if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Filtering <strong>" . $data['row_filter'] . "</strong> on Row #" . ($k + 1) . "\n"; } $row = $data['row_filter']($row, $data); } if (!is_array($row)) { continue; } $params = array('datatype' => $data['pod']['name'], 'columns' => array(), 'bypass_helpers' => $data['bypass_helpers']); foreach ($data['pod']['fields'] as $fk => $field_info) { $field = $field_info['name']; if (!empty($data['fields']) && !isset($data['fields'][$field]) && !in_array($field, $data['fields'])) { continue; } if (isset($data['fields'][$field])) { if (is_array($data['fields'][$field])) { $field_data = $data['fields'][$field]; } else { $field_data = array('field' => $data['fields'][$field]); } } else { $field_data = array(); } if (!is_array($field_data)) { $field = $field_data; $field_data = array(); } $field_data = array_merge($default_field_data, $field_data); if (null === $field_data['field']) { $field_data['field'] = $field; } $data['fields'][$field] = $field_data; $value = ''; if (isset($row[$field_data['field']])) { $value = $row[$field_data['field']]; } if (null !== $field_data['filter']) { if (function_exists($field_data['filter'])) { if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Filtering <strong>" . $field_data['filter'] . "</strong> on Field: " . $field . "\n"; } $value = $field_data['filter']($value, $row, $data); } else { $value = ''; } } if (1 > strlen($value) && 1 == $field_info['required']) { die('<h1 style="color:red;font-weight:bold;">ERROR: Field Required for <strong>' . $field . '</strong></h1>'); } $params['columns'][$field] = $value; unset($value); unset($field_data); unset($field_info); unset($fk); } if (empty($params['columns'])) { continue; } $params['columns'] = pods_sanitize($params['columns']); if (null !== $data['update_on'] && isset($params['columns'][$data['update_on']])) { if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Checking for Existing Item\n"; } $check = new Pod($data['pod']['name']); $check->findRecords(array('orderby' => 't.id', 'limit' => 1, 'where' => "t.{$data['update_on']} = '{$params['columns'][$data['update_on']]}'", 'search' => false, 'page' => 1)); if (0 < $check->getTotalRows()) { $check->fetchRecord(); $params['tbl_row_id'] = $check->get_field('id'); $params['pod_id'] = $check->get_pod_id(); if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Found Existing Item w/ ID: " . $params['tbl_row_id'] . "\n"; } unset($check); } if (!isset($params['tbl_row_id']) && false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Existing item not found - Creating New\n"; } } if (null !== $data['pre_save'] && function_exists($data['pre_save'])) { if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Running Pre Save <strong>" . $data['pre_save'] . "</strong> on " . $data['pod']['name'] . "\n"; } $params = $data['pre_save']($params, $row, $data); } $id = $api->save_pod_item($params); if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - <strong>Saved Row #" . ($k + 1) . " w/ ID: " . $id . "</strong>\n"; } $params['tbl_row_id'] = $id; if (null !== $data['post_save'] && function_exists($data['post_save'])) { if (false !== $output) { echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - Running Post Save <strong>" . $data['post_save'] . "</strong> on " . $data['pod']['name'] . "\n"; } $data['post_save']($params, $row, $data); } unset($params); unset($result[$k]); unset($row); wp_cache_flush(); $wpdb->queries = array(); $avg_counter++; $counter++; if ($avg_counter == $avg_unit && false !== $output) { $avg_counter = 0; $avg_time = timer_stop(0, 10); $total_time += $avg_time; $rows_left = $result_count - $counter; $estimated_time_left = $total_time / $counter * $rows_left / 60; $percent_complete = 100 - $rows_left * 100 / $result_count; echo "<script type='text/javascript'>document.getElementById('progress_status').innerHTML = '" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em><br /><strong>" . $percent_complete . "% Complete</strong><br /><strong>Estimated Time Left:</strong> " . $estimated_time_left . " minute(s) or " . $estimated_time_left / 60 . " hours(s)<br /><strong>Time Spent:</strong> " . $total_time / 60 . " minute(s)<br /><strong>Rows Done:</strong> " . ($result_count - $rows_left) . "/" . $result_count . "<br /><strong>Rows Left:</strong> " . $rows_left . "';</script>\n"; echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - <strong>Updated Status:</strong> " . $percent_complete . "% Complete</strong>\n"; } } if (false !== $output) { $avg_counter = 0; $avg_time = timer_stop(0, 10); $total_time += $avg_time; $rows_left = $result_count - $counter; $estimated_time_left = $total_time / $counter * $rows_left / 60; $percent_complete = 100 - $rows_left * 100 / $result_count; echo "<script type='text/javascript'>document.getElementById('progress_status').innerHTML = '" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em><br /><strong style=\\'color:green;\\'>100% Complete</strong><br /><br /><strong>Time Spent:</strong> " . $total_time / 60 . " minute(s)<br /><strong>Rows Imported:</strong> " . $result_count . (false !== $paginated ? "<br /><br />" . $paginated : '') . "';</script>\n"; echo "<br />" . date('Y-m-d h:i:sa') . " - <em>" . $data['pod']['name'] . "</em> - <strong style='color:green;'>Done Importing: " . $data['pod']['name'] . "</strong>\n"; } unset($result); unset($import[$datatype]); unset($datatype); unset($data); wp_cache_flush(); $wpdb->queries = array(); } if (false !== $output) { $avg_counter = 0; $avg_time = timer_stop(0, 10); $total_time += $avg_time; $rows_left = $result_count - $counter; echo "<script type='text/javascript'>document.getElementById('progress_status').innerHTML = '" . date('Y-m-d h:i:sa') . " - <strong style=\\'color:green;\\'>Import Complete</strong><br /><br /><strong>Time Spent:</strong> " . $total_time / 60 . " minute(s)<br /><strong>Rows Imported:</strong> " . $result_count . (false !== $paginated ? "<br /><br />" . $paginated : '') . "';</script>\n"; echo "<br />" . date('Y-m-d h:i:sa') . " - <strong style='color:green;'>Import Complete</strong>\n"; } }
$result = pod_query("SELECT id, name FROM @wp_pod_types ORDER BY name ASC"); while ($row = mysql_fetch_assoc($result)) { $datatypes[$row['id']] = $row['name']; } // Figure out which tab to display $manage_action = 'manage'; $wp_page = pods_var('page', 'get'); $dtname = pods_var('pod', 'get'); if ('pods-manage-' == substr($wp_page, 0, 12)) { $manage_action = 'top-level-manage'; $dtname = substr($wp_page, 12); if (isset($_GET['action']) && ('add' == $_GET['action'] || 'duplicate' == $_GET['action'])) { ?> <script type="text/javascript"> document.location = "<?php echo pods_ui_var_update(array('page' => 'pods-add-' . $dtname, 'action' => $_GET['action'], 'id' => 'duplicate' == $_GET['action'] && isset($_GET['id']) ? absint($_GET['id']) : '')); ?> "; </script> <?php die; } } elseif ('pods-add-' == substr($wp_page, 0, 9)) { $manage_action = 'top-level-manage'; $dtname = substr($wp_page, 9); $_GET['page'] = 'pods-manage-' . $dtname; if (!isset($_GET['action'])) { $_GET['action'] = 'add'; } } elseif ('pod-' == substr($wp_page, 0, 4)) { $manage_action = 'sub-manage';