· 9 years ago · Mar 24, 2017, 08:36 AM
1<?php
2/*
3Plugin Name: Custom Searchable Data Entry System
4Plugin URI: http://piqibo.me.pn/custom-searchable-data-entry-system
5Description: Fully customizable data entry forms creator. Make your own forms and enter your data (such as your clients details, their name, project, logo, etc) in the form. Your entries are stored in organized tables. You can then search the data from a single search field on front-end which has many capabilities. To have a quick look of the features, take a look at the Welcome page of this plugin.
6Author: Ghazale Shirazi
7Version: 1.6.0
8Text Domain: custom-searchable
9Domain Path: /languages
10Author URI: http://piqibo.me.pn
11
12 Copyright 2015 Ghazale Shirazi (email : ghsh88@gmail.com)
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License, version 2, as
16 published by the Free Software Foundation.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
26 */
27require_once('sds-session.php');
28
29/**
30 * register settings
31 */
32function ghazale_sds_register_settings(){
33 register_setting('ghazale_sds_options','ghazale_sds_separate_search');
34}
35add_action('admin_init','ghazale_sds_register_settings');
36/**
37 * Welcome Page
38 */
39
40function ghazale_sds_welcome_page(){
41 ?>
42 <h2><?php _e('Welcome to Custom Searchable Data Entry System','custom-searchable-data-entry-system'); ?></h2>
43 <h3><?php _e('Making Your First Form','custom-searchable-data-entry-system'); ?></h3>
44 <p><?php _e('To make a new form, follow these steps','custom-searchable-data-entry-system'); ?>:</p>
45 <ol>
46 <li><?php _e('Click on "Create Form" menu. Name your new form and create it.','custom-searchable-data-entry-system'); ?></li>
47 <li><?php _e('Click on "Add Fields" menu. Select your newly created form, name your field and choose field type (e.g.: Text Input, Checkbox, DropDown, etc). You can also choose whether you want this field to be "Hidden" or not. (Hidden fields won\'t be shown on user\'s search query)','custom-searchable-data-entry-system'); ?></li>
48 <li><?php _e('Repeat number 2 and add as much as fields you like. (You can add unlimited number of fields to your form)','custom-searchable-data-entry-system'); ?></li>
49 <li><?php _e('Your form is ready to use. Click on "Forms" menu. You can see the form that you have made in previous steps. You can also edit or delete any of the form fields.','custom-searchable-data-entry-system'); ?></li>
50 </ol>
51 <div style="color:#404040 ;background-color:#b6c669; padding: 10px">
52 <h3><?php _e('Having Your Search Field on Front-end','custom-searchable-data-entry-system'); ?></h3>
53 <p><?php _e('The default functionality of the plugin search engine is to search ALL entries. To have the default functionality, just put the shortcode [sds-search] on any post or page and you\'ll have it up an running.','custom-searchable-data-entry-system'); ?></p>
54 <p><?php _e('If you wish to use a unique search engine for each table, please refer to the <a href="'.get_admin_url().'admin.php?page=sds-options_page'.'">Options</a> page for more instructions.','custom-searchable-data-entry-system'); ?></p>
55 </div>
56 <hr>
57 <div style="color:#404040 ;background-color:#aaffaa ; padding: 10px"><strong><?php _e('If you like this plugin and find it helpful, please rate it 5 stars!','custom-searchable-data-entry-system'); ?><a href="https://wordpress.org/support/view/plugin-reviews/custom-searchable-data-entry-system?rate=5#postform"><img src="<?php echo plugins_url('images/5stars.png',__FILE__); ?>"></a> <br><?php _e('Your donation will be used to protect helpless animals. It is highly appreciated!','custom-searchable-data-entry-system'); ?><br><i><?php _e('You will automatically receive a receipt after donation. Please forward me the receipt or <a href="mailto:ghsh88@gmail.com" target="_blank">send me an email</a> once you have donated. Thank you so much in advance!','custom-searchable-data-entry-system'); ?></i></p></strong><p><a href="http://www.vafashelter.com/main/en/helping-us/paypal-donation" target="_blank"><img src="<?php echo plugins_url('images/btn_donatecc_lg.gif',__FILE__); ?>" alt="btn donatecc lg"></a></p></div>
58 <h3><?php _e('A Quick Look of the Features','custom-searchable-data-entry-system'); ?></h3>
59 <ol>
60 <li><?php _e('You have full control over the creation of your forms, number of fields and the field types.','custom-searchable-data-entry-system'); ?></li>
61 <li><?php _e('You can create unlimited number of forms.','custom-searchable-data-entry-system'); ?></li>
62 <li><?php _e('You can have unlimited number of fields in each form.','custom-searchable-data-entry-system'); ?></li>
63 <li><?php _e('You can have full control over the search query results and can decide which data to be displayed or to be hidden in the user\'s search result.','custom-searchable-data-entry-system'); ?></li>
64 <li><?php _e('The user can search the form based on ANY of the entries that you have in your backend.','custom-searchable-data-entry-system') ?></li>
65 <h4><?php _e('Example:','custom-searchable-data-entry-system') ?></h4>
66 <p><?php _e('Let\'s assume these are your entries in the backend:','custom-searchable-data-entry-system') ?></p>
67 <style>
68 table, td, th {
69 border: 1px solid #979797;
70 border-collapse: collapse;
71 padding: 5px;
72 }
73
74 th {
75 background-color: #404040;
76 color: #FFFFFF;
77 }
78 </style>
79 <table>
80 <tr>
81 <th>Client Name</th><th>Client Project</th><th>Client City</th><th>Client Reference ID</th>
82 </tr>
83 <tr>
84 <td>David</td><td>Bridge</td><td>London</td><td>746</td>
85 </tr>
86 <tr>
87 <td>Paul</td><td>Road</td><td>New York</td><td>412</td>
88 </tr>
89 </table>
90 <p><?php _e('In this example the user can search based on any of the entries. (It can be Client Name, Client Project, Client City or Client Reference ID)','custom-searchable-data-entry-system') ?></p>
91 <p><?php _e('If the user search is "David" the first row will be returned to the user. If the user search is "London", again the first row will be returned. And if the user search is "New York", the second row will be returned to the user. So basically the user is able to search based on any information that you provide.','custom-searchable-data-entry-system') ?></p>
92 <p><?php _e('And of course if you make a particular field as "Hidden", those data won\'t be shown to the user.','custom-searchable-data-entry-system') ?></p>
93 <li><?php _e('You can have your search form on front-end with a single shortcode. The search results will appear on the same page right below the search form.','custom-searchable-data-entry-system') ?></li>
94 <li><?php _e('You can Edit or Delete any of the fields that you don\'t want anymore.' ,'custom-searchable-data-entry-system') ?></li>
95 <li><?php _e('You can Update the entries that you already entered/uploaded to the plugin.' ,'custom-searchable-data-entry-system') ?></li>
96 <li><?php _e('The search result shows partial matches as well and it is case-insensitive.' ,'custom-searchable-data-entry-system') ?></li>
97 <li><?php _e('You can change the hidden fields to be shown or vice versa anytime you wish.' ,'custom-searchable-data-entry-system') ?></li>
98 <li><?php _e('You can search ALL the entries with a single search engine OR You can search tables SEPARATELY with their unique search engine.' ,'custom-searchable-data-entry-system') ?></li>
99 <li><?php _e('You can also upload the data that is already on your local computer (in CSV format that comes from your Excel file) and have the same features like the tables that you create through plugin. You can add records, edit/delete fields or delete selected inputs.','custom-searchable-data-entry-system') ?></li>
100 </ol>
101 <h4><?php _e('Additional Note: The "File Upload" field type is intended for image uploads only.','custom-searchable-data-entry-system') ?></h4>
102 <h3><?php _e('Entries','custom-searchable-data-entry-system'); ?></h3>
103 <p><?php _e('Your entries are all being stored on "Entries" page in organized tabs and tables. If you wanted to update/delete any single entry you can do it there as well.','custom-searchable-data-entry-system'); ?></p>
104 <p><?php _e('You also have the ability to totally delete the selected entries table, or both the entries table and its correspondent form','custom-searchable-data-entry-system'); ?></p>
105 <div style="color:#707070; background-color: #cccccc ; border: 1px solid #cccccc; padding: 10px"><?php _e('If you ever had questions, suggestions or comments feel free to express yourself on support forum','custom-searchable-data-entry-system'); ?> <a href="mailto:ghsh88@gmail.com" target="_blank"><?php _e('or drop me a line','custom-searchable-data-entry-system'); ?></a> :)</div>
106<?php
107}
108function ghazale_sds_welcome_page_menu(){
109 add_menu_page('Custom Searchable DES','Custom Searchable DES','manage_options','custom-searchable-data-entry-system','ghazale_sds_welcome_page', plugin_dir_url(__FILE__) . 'images/sds-ic.png');
110 add_submenu_page('custom-searchable-data-entry-system',__('Welcome','custom-searchable-data-entry-system'),__('Welcome','custom-searchable-data-entry-system'),'manage_options','custom-searchable-data-entry-system','ghazale_sds_welcome_page');
111}
112add_action('admin_menu','ghazale_sds_welcome_page_menu');
113
114/**
115 * Create Form Admin Page
116 */
117function ghazale_sds_create_form(){
118 ?>
119 <form action="" method="post" id="ghazale_ds_new_form">
120 <?php echo ghazale_sds_message(); ?>
121 <h2><?php _e('Create New Form','custom-searchable-data-entry-system'); ?></h2>
122 <?php _e('Form Name','custom-searchable-data-entry-system'); ?>: <input type="text" name="ghazale_sds_new_form_name" id="ghazale_sds_new_form_name" value="<?php if(isset($_POST['ghazale_sds_new_form_name'])){ echo $_POST['ghazale_sds_new_form_name']; }else{ echo '';} ?>"/><i> <?php _e('Alphanumeric characters only. No spaces.','custom-searchable-data-entry-system'); ?></i>
123 <br><br><input type="submit" name="ghazale_sds_submit_new_form" value="<?php _e('Create Form','custom-searchable-data-entry-system'); ?>">
124 </form>
125<?php
126}
127function ghazale_sds_create_form_menu(){
128 add_submenu_page('custom-searchable-data-entry-system','Create Form','Create Form','manage_options','sds-create-form','ghazale_sds_create_form');
129}
130add_action('admin_menu','ghazale_sds_create_form_menu');
131/**
132 * creates 2 relational tables upon creating a new form
133 */
134function ghazale_sds_create_tables(){
135 if(isset($_POST['ghazale_sds_submit_new_form'])) {
136 if (ctype_alnum(trim($_POST['ghazale_sds_new_form_name']))) {
137 global $wpdb;
138 $table_name = $wpdb->prefix . "ghazale_sds_" . strtolower(trim($_POST['ghazale_sds_new_form_name']));
139 $tables= $wpdb->get_results("SHOW TABLES LIKE "."'" .$table_name."%'");
140 if (count($tables)== 0) {
141 $sql_form = "CREATE TABLE " . $table_name . "_fields (id INTEGER(10) UNSIGNED AUTO_INCREMENT, field_name VARCHAR (300) COLLATE utf8_bin, field_type VARCHAR (300) COLLATE ascii_bin, field_ext VARCHAR (300) COLLATE utf8_bin, field_hide VARCHAR (4) COLLATE ascii_bin, PRIMARY KEY (id))";
142 $wpdb->query($sql_form);
143 $sql_input = "CREATE TABLE " . $table_name . "_inputs (id INTEGER(10) UNSIGNED AUTO_INCREMENT, field_id INTEGER(10), field_input VARCHAR(3000) COLLATE utf8_bin, PRIMARY KEY (id))";
144 $wpdb->query($sql_input);
145 $_SESSION['sds-message'] = esc_html__('Form Created Successfully','custom-searchable-data-entry-system');
146 } else {
147 $_SESSION['sds-message'] = esc_html__('That name already exists','custom-searchable-data-entry-system');
148 }
149
150 } else {
151 $_SESSION['sds-message'] = esc_html__('The form name SHOULD be alphanumeric. Please enter a valid name','custom-searchable-data-entry-system');
152 }
153 }
154}
155add_action('wp_loaded','ghazale_sds_create_tables');
156
157/**
158 * admin page for adding fields to the selected form
159 */
160
161function ghazale_sds_add_fields(){
162 global $wpdb;
163 $table_name = $wpdb->prefix . "ghazale_sds_";
164 $tables= $wpdb->get_results("SHOW TABLES LIKE "."'" .$table_name."%_fields'",ARRAY_A);
165 ?>
166 <form action="" method="post" id="ghazale_sds_add_fields">
167 <div><?php echo ghazale_sds_message(); ?></div>
168 <h2><?php _e('Add Fields To The Form', 'custom-searchable-data-entry-system'); ?></h2>
169 <p><strong><?php _e('Once you\'ve added the fields you can see your form under the (Forms) menu.','custom-searchable-data-entry-system'); ?></strong></p>
170 <p><i><?php _e('Note: You cannot add fields to the forms that has already received inputs. The forms that have inputs are locked against changes. If you wish to edit or add fields to those forms, you should first delete their correspondent input data.', 'custom-searchable-data-entry-system'); ?></i></p>
171 <?php _e('Select Form','custom-searchable-data-entry-system'); ?>: <select name="ghazale_sds_select_form">
172 <option value=""> -- <?php _e('Select Form','custom-searchable-data-entry-system'); ?> -- </option>
173 <?php
174 foreach($tables as $table){
175 foreach ($table as $select_table) {
176 $input_table_query = 'SELECT * FROM '. str_replace('_fields', '_inputs', $select_table);
177 $input_table_query_result = $wpdb->get_results($input_table_query,ARRAY_A);
178 if (empty($input_table_query_result)) {
179 echo "<option value='" . $select_table . "'";
180 if (isset($_POST['ghazale_sds_select_form']) && $_POST['ghazale_sds_select_form'] == $select_table) {
181 echo " selected";
182 }
183 echo ">" . ucfirst(str_replace(array($table_name, '_fields'), '', $select_table)) . "</option>";
184 }
185 }
186 }
187 ?>
188 </select><i> <?php _e('Select the form to which you want to add the field','custom-searchable-data-entry-system'); ?></i><br><br>
189 <?php _e('Field Name','custom-searchable-data-entry-system'); ?>: <input type="text" name="ghazale_sds_field_name" id="ghazale_sds_field_name" maxlength="300" placeholder="Enter Field Name" value="<?php if(isset($_POST['ghazale_ds_field_name'])){ echo trim($_POST['ghazale_ds_field_name']); }else{ echo ''; } ?>"/><i> <?php _e('Example: Name, Email, Address, etc... (Should be Alphanumeric. Can have space and allowed Symbols: @-_.,\/:?\'!;&~())','custom-searchable-data-entry-system'); ?></i><br><br>
190 <?php $type_array = array("Text Field","Text Area","Drop Down","Multiple Choice","Email","File Upload","Number","Telephone","URL","Date","CheckBox"); ?>
191 <?php _e('Field Type','custom-searchable-data-entry-system'); ?>: <select name="ghazale_sds_field_type" class="ghazale_sds_field_type">
192 <option value=""> -- <?php _e('Select Field Type','custom-searchable-data-entry-system'); ?> -- </option>
193 <?php
194 foreach($type_array as $type){
195 echo "<option value='" . $type . "'" ;
196 if(isset($_POST['ghazale_sds_field_type']) && $_POST['ghazale_sds_field_type'] == $type){
197 echo " selected";
198 }
199 echo ">" . $type ."</option>";
200 }
201 ?>
202 </select><i> <?php _e('Select the field type','custom-searchable-data-entry-system'); ?></i><br><br>
203 <strong><p class="file_upload_guide" style="color: #ff5500"></p></strong>
204 <span id="ghazale_sds_hide"><input type="checkbox" name="ghazale_sds_hide" value="Hide" <?php if(isset($_POST['ghazale_sds_hide'])){echo 'checked';} ?>/> <?php _e('Hide This Field From Search Query','custom-searchable-data-entry-system'); ?> <br><i> (<?php _e('If checked, the field information will not be shown in the search query on front-end. You can change this later by editing this field..','custom-searchable-data-entry-system'); ?>)</i><br><br></span>
205 <div class="field_ext">
206 <i><?php _e('You can have up to 10 values for this field','custom-searchable-data-entry-system'); ?> :</i><br>
207 <?php
208 for($i=0;$i<=9;$i++){
209 echo "<input type=\"text\" name=\"field_ext_".$i."\" value=\"". (isset($_POST["field_ext_{$i}"])? $_POST["field_ext_{$i}"] :'') ."\" /><br>";
210 }
211 ?>
212 </div>
213
214 <br><input type="submit" name="ghazale-sds-submit-field-name" value="<?php _e('Add Field','custom-searchable-data-entry-system'); ?>">
215 </form>
216<?php
217}
218
219function ghazale_sds_add_fields_admin_menu(){
220 $page_suffix = add_submenu_page('custom-searchable-data-entry-system',__('Add Fields','custom-searchable-data-entry-system'),__('Add Fields','custom-searchable-data-entry-system'),'manage_options','sds-add-field','ghazale_sds_add_fields');
221 add_action('admin_print_scripts-' . $page_suffix, 'ghazale_sds_admin_field_ext');
222}
223add_action('admin_menu','ghazale_sds_add_fields_admin_menu');
224function ghazale_sds_admin_init_field_ext(){
225 wp_register_script('sds-field-ext',plugins_url('js/sds-data-storage.js',__FILE__),array('jquery'));
226}
227add_action('admin_init','ghazale_sds_admin_init_field_ext');
228
229function ghazale_sds_admin_field_ext(){
230 wp_enqueue_script('sds-field-ext');
231}
232function ghazale_sds_allowed_characters( $string )
233{
234 return !preg_match('/[^\w\p{L}\p{N}\p{Pd} @-_.,\/:?!;&~()\'\n\r]/u', $string);
235}
236/**
237 * add fields to the selected form in db
238 */
239function ghazale_sds_add_fields_to_table(){
240 if(isset($_POST['ghazale-sds-submit-field-name'])) {
241 $output ="";
242 for ($i = 0; $i <= 9; $i++) {
243 if (trim($_POST["field_ext_{$i}"] !="")){
244 $output .= trim($_POST["field_ext_{$i}"]) . " | ";
245 }
246 }
247 if (!empty($_POST['ghazale_sds_select_form']) && ghazale_sds_allowed_characters($_POST['ghazale_sds_field_name']) && trim($_POST['ghazale_sds_field_name']) !="" && !empty($_POST['ghazale_sds_field_type'])) {
248 global $wpdb;
249 if(!empty($output)){
250 $wpdb->insert($_POST['ghazale_sds_select_form'], array('field_name' => stripcslashes(trim($_POST['ghazale_sds_field_name'])), 'field_type' => $_POST['ghazale_sds_field_type'], 'field_ext'=> $output, 'field_hide'=> (isset($_POST['ghazale_sds_hide'])? $_POST['ghazale_sds_hide'] : '') ), array('%s'));
251 $_SESSION['sds-message'] = esc_html__('Field Added Successfully','custom-searchable-data-entry-system');
252 }else {
253 $wpdb->insert($_POST['ghazale_sds_select_form'], array('field_name' => stripcslashes(trim($_POST['ghazale_sds_field_name'])), 'field_type' => $_POST['ghazale_sds_field_type'], 'field_hide'=> (isset($_POST['ghazale_sds_hide'])? $_POST['ghazale_sds_hide'] : '')), array('%s'));
254 $_SESSION['sds-message'] = esc_html__('Field Added Successfully','custom-searchable-data-entry-system');
255 }
256 }else{
257 $_SESSION['sds-message'] = esc_html__('All fields are required. Please make sure you have filled all fields (Also make sure the entered Field Name is not containing disallowed symbols)','custom-searchable-data-entry-system');
258 }
259 }
260}
261add_action('init','ghazale_sds_add_fields_to_table');
262/**
263 * showing the created forms in the backend
264 */
265function ghazale_sds_created_forms(){
266 global $wpdb;
267 $table_name = $wpdb->prefix . "ghazale_sds_";
268 $sql = "SHOW TABLES LIKE '" . $table_name . "%_fields'";
269 $tables = $wpdb->get_results($sql,ARRAY_A);
270 echo ghazale_sds_message();
271 if(!empty($tables)) {
272 echo "<div id=\"form_tabs\">";
273 echo "<ul>";
274 foreach ($tables as $table) {
275 foreach ($table as $fields_table) {
276 echo "<li><a href=\"#". $fields_table ."\">" . ucfirst(str_replace(array($table_name,'_fields'),'',$fields_table)) . "</a></li>";
277 }
278 }
279 echo "</ul>";
280 foreach ($tables as $table) {
281 foreach ($table as $fields_table) {
282 $sql_fields = "SELECT * FROM " . $fields_table;
283 $fields = $wpdb->get_results($sql_fields, ARRAY_A);
284 echo "<p>" . ghazale_sds_message(). "</p>";
285 echo "<p>" . ghazale_sds_update() . "</p>";
286 echo "<div id=\"".$fields_table."\">";
287 echo "<h2>".ucfirst(str_replace(array($table_name,'_fields'),'',$fields_table))."</h2>";
288 if($fields) {
289 echo "<p>". __("Guide: <strong style='color: #ff5500'>Hidden on front-end search query</strong> means the data for that particular field will not be shown on front-end search query when the user searches the data. You can control which data to be hidden/shown by editing the field.", "custom-searchable-data-entry-system") ."</p>";
290 echo "<p style='color: #008867'><strong>".esc_html('Form Shortcode').":</strong> [sds-form form=\"".str_replace(array($table_name,'_fields'),'',$fields_table)."\"]</p>";
291 echo "<form action=\"\" method=\"post\" id=\"ds-form {$fields_table}\" enctype=\"multipart/form-data\">";
292
293 foreach ($fields as $field) {
294 echo $field['field_name'] . ": " . "<a href='". get_admin_url() ."admin.php?page=sds-edit-field&sds-edit-field-id=".$field['id'] ."&sds-edit-field-table=". $fields_table."#". $fields_table."' >(".__('Edit','custom-searchable-data-entry-system').")</a>". " | <a href='". get_admin_url() ."admin.php?page=sds-forms&sds-delete-field-id=".$field['id'] ."&sds-delete-field-table=". $fields_table."#". $fields_table."' onclick=\"return confirm('".__('IMPORTANT: The data that you previously entered in this field will be also deleted from the Entries table. Continue? (THIS ACTION CANNOT BE UNDONE)','custom-searchable-data-entry-system')."');\">(".__('Delete','custom-searchable-data-entry-system').")</a>";
295 if($field['field_hide'] == "Hide"){
296 echo "<i style='color: #979797;'> (" . __('Hidden on front-end search query.','custom-searchable-data-entry-system') .")</i>";
297 }
298
299 switch ($field['field_type']) {
300 case "Text Field":
301 echo "<br><input type =\"text\" name= \"" . $field['id'] . "\" size=\"30\" maxlength=\"3000\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" ."<br><br>";
302 break;
303 case "Email":
304 echo "<br><input type=\"email\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"200\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
305 break;
306 case "Text Area":
307 echo "<br><textarea rows=\"4\" cols=\"40\" name=\"" . $field['id'] . "\" maxlength=\"3000\" >" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "</textarea>" . "<br><br>";
308 break;
309 case "Number":
310 echo "<br><input type=\"number\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"3000\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
311 break;
312 case "Telephone":
313 echo "<br><input type=\"tel\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"50\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
314 break;
315 case "URL":
316 echo "<br><input type=\"url\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"100\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" placeholder=\"" . __('Should Start with http', 'custom-searchable-data-entry-system') . "\" />" . "<br><br>";
317 break;
318 case "Date":
319 echo "<br><input type=\"date\" name=\"" . $field['id'] . "\" size=\"30\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
320 break;
321 case "CheckBox":
322 echo " <input type=\"checkbox\" name=\"" . $field['id'] . "\" value=\"Yes\" ";
323 if (isset($_POST[$field['id']]) && $_POST[$field['id']] == "Yes") {
324 echo " checked";
325 }
326 echo "/>" . "<br><br>";
327 break;
328 case "File Upload":
329 echo "<br><input type=\"file\" class=\"sds_file\" id=\"sds_reset_upload_field_" . $field['id'] . "\" " . "name=\"sds_file_" . $field['id'] . "\" />" . "<br><br>";
330 break;
331 case "Drop Down":
332 echo "<br><select name=\"" . $field['id'] . "\" >";
333 echo "<option value=\"\" >-- " . __('Select', 'custom-searchable-data-entry-system') . " --</option>";
334 $field_options = explode(" | ", $field['field_ext']);
335 foreach ($field_options as $field_option) {
336 if ($field_option != "") {
337 echo "<option value='" . $field_option . "'";
338 if (isset($_POST[$field['id']]) && $_POST[$field['id']] == $field_option) {
339 echo " selected";
340 }
341 echo ">" . $field_option . "</option>";
342 }
343 }
344 echo "</select>" . "<br><br>";
345 break;
346 case "Multiple Choice":
347 echo "<br><select name=\"sds_multiplechoice_" . $field['id'] . "[]\" multiple >";
348 $field_options = explode(" | ", $field['field_ext']);
349 foreach ($field_options as $field_option) {
350 if ($field_option != "") {
351 echo "<option value='" . $field_option . "'";
352 if (isset($_POST["sds_multiplechoice_" . $field['id']])) {
353 foreach ($_POST["sds_multiplechoice_" . $field['id']] as $multiple) {
354 if ($multiple == $field_option) {
355 echo " selected";
356 }
357 }
358 }
359 echo ">" . $field_option . "</option>";
360 }
361 }
362 echo "</select>" . "<br>";
363 echo "<i>" . __('Hold down the Ctrl (windows) / Command (Mac) button to select multiple options', 'custom-searchable-data-entry-system') . "</i><br><br>";
364 break;
365 default:
366 break;
367 }
368 }
369
370 echo "<input type=\"submit\" id=\"submit_ghazale_sds_form\" name=\"submit_" . $fields_table . "\" value=\"";
371 echo __('Submit', 'custom-searchable-data-entry-system');
372 echo "\" >";
373
374 echo "</form>";
375 }else{
376 echo "<a href=\"".get_admin_url()."admin.php?page=sds-add-field"."\">". __('Please add fields to this form first.','custom-searchable-data-entry-system') ."</a><br>".__('Once you added the desired fields, you can start entering your data in the form','custom-searchable-data-entry-system');
377 }
378 echo "<br><br><p><a href=\"".get_admin_url() ."admin.php?page=sds-forms&sds-total-del-form-table=". $fields_table ."\" onclick=\"return confirm('".__('Are you sure? The corresponding data will be removed as well. (THIS ACTION CANNOT BE UNDONE)','custom-searchable-data-entry-system')."');\" style=\"color:#ff0000\" >".__('Totally Delete This Form AND Its Corresponding Data.','custom-searchable-data-entry-system')."</a></p>";
379 echo "</div>";
380 }
381 }
382 }else{
383 echo "<div style=\"color:#ffffff ;background-color:#47a447 ; padding: 10px\"><strong>". __('No form(s) to display. Please Create a new form first. Once you create your first form and add fields to it, it will appear on this page in an organized table. You will then be able to enter your data.','custom-searchable-data-entry-system') ."</strong></div>";
384 }
385 echo "</div>";
386
387}
388function ghazale_sds_created_tables_admin_menu(){
389 $page_suffix = add_submenu_page('custom-searchable-data-entry-system',__('Forms','custom-searchable-data-entry-system'),__('Forms','custom-searchable-data-entry-system'),'manage_options','sds-forms','ghazale_sds_created_forms');
390 add_action('admin_print_scripts-' . $page_suffix, 'ghazale_sds_admin_form_table_tabs');
391}
392add_action('admin_menu','ghazale_sds_created_tables_admin_menu');
393
394function ghazale_sds_admin_form_table_tabs(){
395 wp_enqueue_style('sds-tabs-style');
396 wp_enqueue_script('sds-tabs-script');
397}
398
399function ghazale_sds_admin_form_table_tabs_register(){
400 wp_register_style('sds-tabs-style',plugins_url('css/jquery-ui.min.css',__FILE__));
401 wp_register_script('sds-tabs-script', plugins_url('js/sds-admin-tabs.js',__FILE__),array('jquery','jquery-ui-core','jquery-ui-tabs'));
402 $translation_array = array(
403 'refresh' => __( 'Refresh the page to restore "Update"/"Delete" links', 'custom-searchable-data-entry-system' )
404 );
405 wp_localize_script( 'sds-tabs-script', 'sds_object', $translation_array );
406}
407add_action('admin_init','ghazale_sds_admin_form_table_tabs_register');
408
409/**
410 * showing the created forms on frontend by shortcode
411 */
412function ghazale_sds_created_forms_shortcode($atts){
413 global $wpdb;
414 $table_name = $wpdb->prefix . "ghazale_sds_";
415 $sql = "SHOW TABLES LIKE '" . $table_name . "%_fields'";
416 $tables = $wpdb->get_results($sql,ARRAY_A);
417 if(!empty($tables)) {
418 $output = "<div id=\"form_tabs\">";
419 foreach ($tables as $table) {
420 foreach ($table as $fields_table) {
421 shortcode_atts(array('form' => str_replace(array($table_name, '_fields'), '', $fields_table)), $atts);
422 if (str_replace(array($table_name, '_fields'), '', $fields_table) == $atts['form']){
423 $sql_fields = "SELECT * FROM " . $fields_table;
424 $fields = $wpdb->get_results($sql_fields, ARRAY_A);
425 $output .= "<p>" . ghazale_sds_update() . "</p>";
426 $output .= "<div id=\"" . $fields_table . "\">";
427 if ($fields) {
428 $output .= "<form action=\"\" method=\"post\" id=\"ds-form {$fields_table}\" enctype=\"multipart/form-data\">";
429
430 foreach ($fields as $field) {
431 $output .= $field['field_name'] . ": ";
432
433 switch ($field['field_type']) {
434 case "Text Field":
435 $output .= "<br><input type =\"text\" name= \"" . $field['id'] . "\" size=\"30\" maxlength=\"3000\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
436 break;
437 case "Email":
438 $output .= "<br><input type=\"email\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"200\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
439 break;
440 case "Text Area":
441 $output .= "<br><textarea rows=\"4\" cols=\"40\" name=\"" . $field['id'] . "\" maxlength=\"3000\" >" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "</textarea>" . "<br><br>";
442 break;
443 case "Number":
444 $output .= "<br><input type=\"number\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"3000\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
445 break;
446 case "Telephone":
447 $output .= "<br><input type=\"tel\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"50\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
448 break;
449 case "URL":
450 $output .= "<br><input type=\"url\" name=\"" . $field['id'] . "\" size=\"30\" maxlength=\"100\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" placeholder=\"" . __('Should Start with http', 'custom-searchable-data-entry-system') . "\" />" . "<br><br>";
451 break;
452 case "Date":
453 $output .= "<br><input type=\"date\" name=\"" . $field['id'] . "\" size=\"30\" value=\"" . (isset($_POST[$field['id']]) ? $_POST[$field['id']] : '') . "\" />" . "<br><br>";
454 break;
455 case "CheckBox":
456 $output .= " <input type=\"checkbox\" name=\"" . $field['id'] . "\" value=\"Yes\" ";
457 if (isset($_POST[$field['id']]) && $_POST[$field['id']] == "Yes") {
458 $output .= " checked";
459 }
460 $output .= "/>" . "<br><br>";
461 break;
462 case "File Upload":
463 $output .= "<br><input type=\"file\" class=\"sds_file\" id=\"sds_reset_upload_field_" . $field['id'] . "\" " . "name=\"sds_file_" . $field['id'] . "\" />" . "<br><br>";
464 break;
465 case "Drop Down":
466 $output .= "<br><select name=\"" . $field['id'] . "\" >";
467 $output .= "<option value=\"\" >-- " . __('Select', 'custom-searchable-data-entry-system') . " --</option>";
468 $field_options = explode(" | ", $field['field_ext']);
469 foreach ($field_options as $field_option) {
470 if ($field_option != "") {
471 $output .= "<option value='" . $field_option . "'";
472 if (isset($_POST[$field['id']]) && $_POST[$field['id']] == $field_option) {
473 $output .= " selected";
474 }
475 $output .= ">" . $field_option . "</option>";
476 }
477 }
478 $output .= "</select>" . "<br><br>";
479 break;
480 case "Multiple Choice":
481 $output .= "<br><select name=\"sds_multiplechoice_" . $field['id'] . "[]\" multiple >";
482 $field_options = explode(" | ", $field['field_ext']);
483 foreach ($field_options as $field_option) {
484 if ($field_option != "") {
485 $output .= "<option value='" . $field_option . "'";
486 if (isset($_POST["sds_multiplechoice_" . $field['id']])) {
487 foreach ($_POST["sds_multiplechoice_" . $field['id']] as $multiple) {
488 if ($multiple == $field_option) {
489 $output .= " selected";
490 }
491 }
492 }
493 $output .= ">" . $field_option . "</option>";
494 }
495 }
496 $output .= "</select>" . "<br>";
497 $output .= "<i>" . __('Hold down the Ctrl (windows) / Command (Mac) button to select multiple options', 'custom-searchable-data-entry-system') . "</i><br><br>";
498 break;
499 default:
500 break;
501 }
502 }
503
504 $output .= "<input type=\"submit\" id=\"submit_ghazale_sds_form\" name=\"frontend_submit_" . $fields_table . "\" value=\"";
505 $output .= __('Submit', 'custom-searchable-data-entry-system');
506 $output .= "\" >";
507
508 $output .= "</form>";
509 } else {
510 $output .= __('No Fields to Display Yet.', 'custom-searchable-data-entry-system');
511 }
512 $output .= "</div>";
513 }
514 }
515 }
516 $output .= "</div>";
517 return $output;
518 }
519}
520add_shortcode('sds-form','ghazale_sds_created_forms_shortcode');
521/**
522 * delete selected form table
523 */
524function ghazale_sds_totally_delete_form_table(){
525 if(isset($_GET['sds-total-del-form-table']) && $_GET['page'] == 'sds-forms'){
526 global $wpdb;
527 $table_name = $wpdb->prefix . "ghazale_sds_";
528 $sql = "SHOW TABLES LIKE '" . $table_name . "%_fields'";
529 $tables = $wpdb->get_results($sql,ARRAY_A);
530 foreach ($tables as $table) {
531 foreach ($table as $fields_table) {
532 if($fields_table == $_GET['sds-total-del-form-table']){
533 $wpdb->query("DROP TABLE IF EXISTS ". $fields_table);
534 $_SESSION['sds-message'] = esc_html__('Deleted Successfully','custom-searchable-data-entry-system');
535 }
536 }
537 }
538 $sql_inputs = "SHOW TABLES LIKE '" . $table_name . "%_inputs'";
539 $input_tables = $wpdb->get_results($sql_inputs, ARRAY_A);
540 $entry_table = str_replace("_fields","_inputs",$_GET['sds-total-del-form-table']);
541 foreach($input_tables as $input_table){
542 foreach($input_table as $inputs){
543 if($inputs == $entry_table){
544 $wpdb->query("DROP TABLE IF EXISTS ". $inputs);
545 $_SESSION['sds-message'] = esc_html__('Deleted Successfully','custom-searchable-data-entry-system');
546 }
547 }
548 }
549 }
550}
551add_action('init','ghazale_sds_totally_delete_form_table');
552
553/**
554 * delete selected field from the form
555 */
556
557function ghazale_sds_delete_selected_field(){
558 if(isset($_GET['sds-delete-field-id']) && isset($_GET['sds-delete-field-table'])){
559 global $wpdb;
560 $fields_table_name = $_GET['sds-delete-field-table'];
561 $inputs_table_name = str_replace("_fields","_inputs",$_GET['sds-delete-field-table']);
562 $field_id = $_GET['sds-delete-field-id'];
563 $wpdb -> delete($fields_table_name, array('id' => $field_id));
564 $wpdb -> delete($inputs_table_name, array('field_id' => $field_id));
565 $_SESSION['sds-message'] = esc_html__('Field Deleted Successfully','custom-searchable-data-entry-system');
566 }
567}
568add_action('init','ghazale_sds_delete_selected_field');
569
570/**
571 * admin page for editing fields
572 */
573
574function ghazale_sds_edit_fields(){
575 global $wpdb;
576 $db_table_name = $wpdb->prefix . "ghazale_sds_";
577 $id = $_GET['sds-edit-field-id'];
578 $table = $_GET['sds-edit-field-table'];
579 $sql = "SELECT * FROM {$table} WHERE id={$id}";
580 $field = $wpdb->get_row ($sql, ARRAY_A);
581 ?>
582 <h2><?php _e('Edit Field','custom-searchable-data-entry-system'); ?></h2>
583 <?php echo ghazale_sds_message(); ?>
584 <form action="" method="post" id="ghazale_sds_edit_field">
585 <h4><?php _e('Form','custom-searchable-data-entry-system'); ?> : <?php echo ucfirst(str_replace(array($db_table_name, "_fields"),"", $_GET['sds-edit-field-table'])); ?> </h4>
586 <?php _e('Field Name','custom-searchable-data-entry-system'); ?> : <input type="text" name="ghazale_sds_edit_field_name" id="ghazale_sds_edit_field_name" value="<?php echo $field['field_name']; ?>"/><i> Should be alphanumeric. Can have space and allowed symbols @-_.,\/:?'!;&~()</i><br><br>
587 <?php $type_array = array("Text Field","Text Area","Drop Down","Multiple Choice","Email","File Upload","Number","Telephone","URL","Date","CheckBox"); ?>
588 <?php _e('Field Type','custom-searchable-data-entry-system'); ?>: <select name="ghazale_sds_edit_field_type" class="ghazale_sds_field_type">
589 <option value=""> -- <?php _e('Select Field Type','custom-searchable-data-entry-system'); ?> -- </option>
590 <?php
591 foreach($type_array as $type){
592 echo "<option value='" . $type . "'" ;
593 if($field['field_type'] == $type){
594 echo " selected";
595 }
596 echo ">" . $type ."</option>";
597 }
598 ?>
599 </select><br><br>
600 <span id="ghazale_sds_field_hide"><input type="checkbox" name="ghazale_sds_field_hide_edit" value="Hide" <?php if($field['field_hide'] == 'Hide'){echo 'checked';} ?>/> <?php _e('Hide This Field From Search Query.','custom-searchable-data-entry-system'); ?> <i> (<?php _e('If checked, the field information will not be shown in the search query on front-end. You can change this later by editing this field.','custom-searchable-data-entry-system'); ?>)</i><br><br></span>
601 <div class="field_ext">
602 <i><?php _e('You can have up to 10 values for this field','custom-searchable-data-entry-system'); ?> :</i><br>
603 <?php
604 $options = explode(" | ", $field['field_ext']);
605 for($i=0;$i<=9;$i++){
606 echo "<input type=\"text\" name=\"field_ext_edit_".$i."\" value=\"". (isset($options[$i])? $options[$i]: '') ."\" /><br>";
607 }
608 ?>
609 </div>
610 <p><input type="submit" name="ghazale_sds_edit_field" value="<?php _e('Update Field','custom-searchable-data-entry-system'); ?>">
611 <a href="<?php echo get_admin_url()?>admin.php?page=sds-forms#<?php echo $_GET['sds-edit-field-table'] ?>" style="text-decoration: none"><input type="button" value="<?php _e('Back to Forms','custom-searchable-data-entry-system'); ?>"></a></p>
612 </form>
613<?php
614}
615
616function ghazale_sds_edit_fields_admin_menu(){
617 $page_suffix = add_submenu_page(null,__('Edit Fields','custom-searchable-data-entry-system'),__('Edit Fields','custom-searchable-data-entry-system'),'manage_options','sds-edit-field','ghazale_sds_edit_fields');
618 add_action('admin_print_scripts-' . $page_suffix, 'ghazale_sds_admin_field_ext');
619}
620add_action('admin_menu','ghazale_sds_edit_fields_admin_menu');
621
622/**
623 * updating field name and type in db
624 */
625
626function ghazale_sds_update_field(){
627 if(isset($_POST['ghazale_sds_edit_field'])){
628 $output ="";
629 for ($i = 0; $i <= 9; $i++) {
630 if (ghazale_sds_allowed_characters($_POST["field_ext_edit_{$i}"]) && trim($_POST["field_ext_edit_{$i}"] !="")){
631 $output .= trim($_POST["field_ext_edit_{$i}"]) . " | ";
632 }
633 }
634 if(ghazale_sds_allowed_characters($_POST['ghazale_sds_edit_field_name']) && !empty($_POST['ghazale_sds_edit_field_type'])){
635 global $wpdb;
636 $id = $_GET['sds-edit-field-id'];
637 $table = $_GET['sds-edit-field-table'];
638 $wpdb->update($table, array('field_name' => (isset($_POST['ghazale_sds_edit_field_name'])? trim($_POST['ghazale_sds_edit_field_name']): ''), 'field_type' => (isset($_POST['ghazale_sds_edit_field_type'])? $_POST['ghazale_sds_edit_field_type']: ''), 'field_ext'=> $output, 'field_hide'=> (isset($_POST['ghazale_sds_field_hide_edit'])? $_POST['ghazale_sds_field_hide_edit']: '')),array('id'=>$id), array('%s'));
639 $_SESSION['sds-message'] = esc_html__('Updated Successfully','custom-searchable-data-entry-system');
640
641 }else{
642 $_SESSION['sds-message'] = esc_html__('Please make sure the entered Field Name is not containing disallowed symbols, and also make sure you have selected Field Type','custom-searchable-data-entry-system');
643 }
644 }
645}
646add_action('init','ghazale_sds_update_field');
647/**
648 * adding field inputs to input tables
649 */
650function ghazale_sds_insert_field_inputs(){
651 global $wpdb;
652 $table_name = $wpdb->prefix . "ghazale_sds_";
653 $tables= $wpdb->get_results("SHOW TABLES LIKE "."'" .$table_name."%_fields'",ARRAY_A);
654 $not_alphanumeric = array();
655 if(!empty($tables)) {
656
657 foreach ($tables as $table) {
658 foreach ($table as $fields_table) {
659 if (isset($_POST['submit_' . $fields_table])) {
660
661 $sql_fields = "SELECT id,field_name,field_hide,field_type FROM " . $fields_table;
662 $fields = $wpdb->get_results($sql_fields, ARRAY_A);
663 $input_table = str_replace('_fields', '_inputs', $fields_table);
664
665 foreach($fields as $field){
666 if(isset($_POST[$field['id']]) && !ghazale_sds_allowed_characters($_POST[$field['id']])){
667 array_push($not_alphanumeric,"not alphanumeric");
668 }
669 }
670 if (empty($not_alphanumeric)) {
671 $new_entry = array();
672 foreach ($fields as $field) {
673 if(!empty($_POST['sds_multiplechoice_'.$field['id']])){
674 $multiple_choice=array();
675 foreach($_POST["sds_multiplechoice_".$field['id']] as $multiple){
676 array_push($multiple_choice,$multiple);
677 }
678 $_POST[$field['id']] = implode(" | ", $multiple_choice);
679 }
680 if (isset($_FILES["sds_file_{$field['id']}"]) && $_FILES["sds_file_{$field['id']}"]['size']>0) {
681 if (!function_exists('wp_handle_upload')) {
682 require_once(ABSPATH . 'wp-admin/includes/file.php');
683 }
684 $uploadedfile = $_FILES["sds_file_{$field['id']}"];
685 $upload_overrides = array('test_form' => false);
686 if(!function_exists('ghazale_sds_change_upload_dir')) {
687 function ghazale_sds_change_upload_dir($upload)
688 {
689 global $wpdb;
690 $table_name = $wpdb->prefix . "ghazale_sds_";
691 $tables = $wpdb->get_results("SHOW TABLES LIKE " . "'" . $table_name . "%_fields'", ARRAY_A);
692 foreach ($tables as $table) {
693 foreach ($table as $fields_table) {
694 if (isset($_POST['submit_' . $fields_table])) {
695 $new_dir = '/sds-' . str_replace(array($table_name, '_fields'), '', $fields_table);
696 }
697 }
698 }
699
700 $upload['path'] = str_replace($upload['subdir'], '', $upload['path']);
701 $upload['url'] = str_replace($upload['subdir'], '', $upload['url']);
702 $upload['subdir'] = $new_dir;
703 $upload['path'] .= $new_dir;
704 $upload['url'] .= $new_dir;
705
706 return $upload;
707 }
708 }
709 add_filter('upload_dir', 'ghazale_sds_change_upload_dir');
710 $upload = wp_upload_dir();
711 $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
712 remove_filter('upload_dir', 'ghazale_sds_change_upload_dir');
713
714
715 if ($movefile && !isset($movefile['error'])) {
716 // echo "File is valid, and was successfully uploaded.\n";
717 // var_dump($movefile);
718 $_POST[$field['id']] = $movefile['url'];
719 } else {
720 /**
721 * Error generated by _wp_handle_upload()
722 * @see _wp_handle_upload() in wp-admin/includes/file.php
723 */
724 $_SESSION['sds-message'] = $movefile['error'];
725 }
726 }
727 $wpdb->insert($input_table, array('field_id' => $field['id'], 'field_input' => (isset($_POST[$field['id']])? $_POST[$field['id']]: '')), array('%s'));
728 $_SESSION["sds-update"] = esc_html__('Entry Submitted!','custom-searchable-data-entry-system');
729
730 if($field['field_type'] == "Multiple Choice" && !empty($_POST["sds_multiplechoice_".$field['id']])){
731 $new_entry[$field['field_name']] = implode(" | ", $multiple_choice);
732 }elseif($field['field_type'] == "File Upload" && !empty($movefile)){
733 $new_entry[$field['field_name']] = $movefile['url'];
734 }
735
736 }
737
738
739 }elseif(!empty($not_alphanumeric)) {
740 $_SESSION["sds-error"] = esc_html__('Some Symbols are not allowed for security. Please reconsider your entries. Allowed symbols are: @-_.,\/:?\'!;&~()','custom-searchable-data-entry-system');
741 }
742 wp_redirect(get_permalink().'#'.$fields_table);
743 exit;
744 }
745 if (isset($_POST['frontend_submit_' . $fields_table])) {
746
747 $sql_fields = "SELECT id,field_name,field_hide,field_type FROM " . $fields_table;
748 $fields = $wpdb->get_results($sql_fields, ARRAY_A);
749 $input_table = str_replace('_fields', '_inputs', $fields_table);
750
751 foreach($fields as $field){
752 if(isset($_POST[$field['id']]) && !ghazale_sds_allowed_characters($_POST[$field['id']])){
753 array_push($not_alphanumeric,"not alphanumeric");
754 }
755 }
756 if (empty($not_alphanumeric)) {
757 $new_entry = array();
758 foreach ($fields as $field) {
759 if(!empty($_POST['sds_multiplechoice_'.$field['id']])){
760 $multiple_choice=array();
761 foreach($_POST["sds_multiplechoice_".$field['id']] as $multiple){
762 array_push($multiple_choice,$multiple);
763 }
764 $_POST[$field['id']] = implode(" | ", $multiple_choice);
765 }
766 if (isset($_FILES["sds_file_{$field['id']}"]) && $_FILES["sds_file_{$field['id']}"]['size']>0) {
767 if (!function_exists('wp_handle_upload')) {
768 require_once(ABSPATH . 'wp-admin/includes/file.php');
769 }
770 $uploadedfile = $_FILES["sds_file_{$field['id']}"];
771 $upload_overrides = array('test_form' => false);
772 if(!function_exists('ghazale_sds_change_upload_dir')) {
773 function ghazale_sds_change_upload_dir($upload)
774 {
775 global $wpdb;
776 $table_name = $wpdb->prefix . "ghazale_sds_";
777 $tables = $wpdb->get_results("SHOW TABLES LIKE " . "'" . $table_name . "%_fields'", ARRAY_A);
778 foreach ($tables as $table) {
779 foreach ($table as $fields_table) {
780 if (isset($_POST['submit_' . $fields_table])) {
781 $new_dir = '/sds-' . str_replace(array($table_name, '_fields'), '', $fields_table);
782 }
783 }
784 }
785
786 $upload['path'] = str_replace($upload['subdir'], '', $upload['path']);
787 $upload['url'] = str_replace($upload['subdir'], '', $upload['url']);
788 $upload['subdir'] = $new_dir;
789 $upload['path'] .= $new_dir;
790 $upload['url'] .= $new_dir;
791
792 return $upload;
793 }
794 }
795 add_filter('upload_dir', 'ghazale_sds_change_upload_dir');
796 $upload = wp_upload_dir();
797 $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
798 remove_filter('upload_dir', 'ghazale_sds_change_upload_dir');
799
800
801 if ($movefile && !isset($movefile['error'])) {
802 // echo "File is valid, and was successfully uploaded.\n";
803 // var_dump($movefile);
804 $_POST[$field['id']] = $movefile['url'];
805 } else {
806 /**
807 * Error generated by _wp_handle_upload()
808 * @see _wp_handle_upload() in wp-admin/includes/file.php
809 */
810 $_SESSION['sds-message'] = $movefile['error'];
811 }
812 }
813 $wpdb->insert($input_table, array('field_id' => $field['id'], 'field_input' => (isset($_POST[$field['id']])? $_POST[$field['id']]: '')), array('%s'));
814 $_SESSION["sds-update"] = esc_html__('Entry Submitted!','custom-searchable-data-entry-system');
815
816 if($field['field_type'] == "Multiple Choice" && !empty($_POST["sds_multiplechoice_".$field['id']])){
817 $new_entry[$field['field_name']] = implode(" | ", $multiple_choice);
818 }elseif($field['field_type'] == "File Upload" && !empty($movefile)){
819 $new_entry[$field['field_name']] = $movefile['url'];
820 }
821
822 }
823
824
825 }elseif(!empty($not_alphanumeric)) {
826 $_SESSION["sds-error"] = esc_html__('Some Symbols are not allowed for security. Please reconsider your entries. Allowed symbols are: @-_.,\/:?\'!;&~()','custom-searchable-data-entry-system');
827 }
828 }
829 }
830 }
831 }
832}
833add_action('wp_loaded','ghazale_sds_insert_field_inputs');
834
835/**
836 * shows form inputs on the backend
837 */
838
839function ghazale_sds_form_inputs(){
840 global $wpdb;
841 $tables = $wpdb-> prefix. "ghazale_sds_";
842 $input_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_inputs'",ARRAY_A);
843 $field_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_fields'",ARRAY_A);
844
845 if(!empty($input_tables)) {
846 echo ghazale_sds_message();
847 echo ghazale_sds_update();
848 echo "<div id=\"form_tabs\">";
849 echo "<ul>";
850 foreach ($input_tables as $input_table) {
851 foreach ($input_table as $table) {
852 echo "<li><a href=\"#". $table. "\">" . ucfirst(str_replace(array($tables, "_inputs"), "", $table)) . " Entries</a></li>";
853 }
854 }
855 echo "</ul>";
856 foreach ($input_tables as $input_table) {
857 foreach ($input_table as $table) {
858 echo "<div id=\"".$table ."\">";
859 echo "<h3>" . ucfirst(str_replace(array($tables, "_inputs"), "", $table)) . " Entries</h3>";
860 echo "<p><strong>".esc_html('Table Shortcode: ').'</strong>'.esc_html('[sds-entry table="').str_replace(array($tables, '_inputs'), '', $table).'"]</p>';
861 if(get_option('ghazale_sds_separate_search')) {
862 echo "<p><strong>" . esc_html('Search Engine Shortcode: ') . '</strong>' . esc_html('[sds-search table="') . str_replace(array($tables, '_inputs'), '', $table) . '"]</p>';
863 }
864 echo "<p><input type=\"button\" name=\"sds_download_table_csv\" class=\"sds_download_table_csv_".$table."\" value=\"".__('Download Table In CSV Format','custom-searchable-data-entry-system')."\" /><i>". __('After you downloaded the table, rename the file and put .csv at the end of the file name.','custom-searchable-data-entry-system') ."</i></p>";
865 echo "<h4 class=\"sds_refresh_page\" style=\"color: #f6a828\"></h4>";
866 echo "<table class = \"table\" id=\"sds_download_table_csv_".$table."\">";
867 $inputs_sql = "SELECT * FROM " . $table. " ORDER BY id ASC";
868 $inputs = $wpdb->get_results($inputs_sql, ARRAY_A);
869
870 foreach($field_tables as $field_table){
871 foreach($field_table as $field_table_single){
872 if($field_table_single == str_replace("_inputs","_fields","$table")){
873 $fields_sql = "SELECT id,field_name,field_type FROM ". $field_table_single;
874 $fields = $wpdb->get_results($fields_sql,ARRAY_A);
875 if(!empty($inputs)){
876 echo "<thead>";
877 echo "<tr>";
878 foreach($fields as $field){
879 echo "<th>" . $field['field_name'] . "</th>";
880 }
881 echo "<th><span class=\"sds_del_link\">". __('Delete', 'custom-searchable-data-entry-system'). "</span></th>";
882 echo "</tr>";
883 echo "</thead>";
884
885 foreach($fields as $field){
886 $first_id = $field['id'];
887 break;
888 }
889
890 foreach($fields as $field){
891 $last_id = $field['id'];
892 }
893 echo "<tbody>";
894 foreach($inputs as $input){
895 if($input['field_id'] == $first_id) {
896 $first_input_id = $input['id'];
897 echo "<tr>";
898 }
899 foreach ($input as $key => $input_single) {
900 if ($input_single != "" && $key != 'id' && $key != 'field_id') {
901 $link_or_img = strlen($input_single)-3;
902 $jpeg_img = strlen($input_single)-4;
903 if (substr($input_single, 0, 4) === "http" && (substr($input_single, $link_or_img, 3) == "png" || substr($input_single, $link_or_img, 3) == "jpg" || substr($input_single, $link_or_img, 3) == "gif" || substr($input_single, $link_or_img, 3) == "bmp" || substr($input_single, $jpeg_img, 4) == "jpeg")) {
904 echo "<td><img src='" . $input_single . "' width=\"300\"><span class=\"sds_update_link\"><a href=\"".get_admin_url()."admin.php?page=sds-update-single-entry&update-single-entry-id=".$input['id']."&sds-entry-table=".$table."\" style=\"color: #ff5500\"><br>(".__('Update','custom-searchable-data-entry-system').")</a></span></td>";
905 }elseif(substr($input_single, 0, 4) === "http" && (substr($input_single, $link_or_img, 3) != "png" || substr($input_single, $link_or_img, 3) != "jpg" || substr($input_single, $link_or_img, 3) != "gif" || substr($input_single, $link_or_img, 3) != "bmp" || substr($input_single, $link_or_img, 4) != "jpeg")) {
906 echo "<td><a href='".$input_single."'>".$input_single."</a> <span class=\"sds_update_link\"><a href=\"".get_admin_url()."admin.php?page=sds-update-single-entry&update-single-entry-id=".$input['id']."&sds-entry-table=".$table."\" style=\"color: #ff5500\"><br>(".__('Update','custom-searchable-data-entry-system').")</a></span></td>";
907 }else {
908 echo "<td>" . stripslashes($input_single) . "<span class=\"sds_update_link\"><a href=\"".get_admin_url()."admin.php?page=sds-update-single-entry&update-single-entry-id=".$input['id']."&sds-entry-table=".$table."\" style=\"color: #ff5500\">(".__('Update','custom-searchable-data-entry-system').")</a></span></td>";
909 }
910 } elseif ($input_single == "") {
911 echo "<td><span class=\"sds_update_link\"><a href=\"".get_admin_url()."admin.php?page=sds-update-single-entry&update-single-entry-id=".$input['id']."&sds-entry-table=".$table."\" style=\"color: #ff5500\">(".__('Update','custom-searchable-data-entry-system').")</a></span></td>";
912 }
913
914 }
915
916 if($input['field_id'] == $last_id) {
917 $last_input_id = $input['id'];
918 echo "<td><span class=\"sds_del_link\"><a href=\"".get_admin_url()."admin.php?page=sds-form-entries&sds-del-entry-first-entry-id=".$first_input_id."&sds-del-entry-last-entry-id=".$last_input_id."&sds-del-entry-table-row=".$table."#".$table."\" onclick=\"return confirm('".__('Are you sure? (THIS ACTION CANNOT BE UNDONE)','custom-searchable-data-entry-system')."');\" style=\"color: #ff5500\">".__('Delete Row','custom-searchable-data-entry-system')."</a></span></td>";
919 echo "</tr>";
920 }
921 }
922 echo "</tbody>";
923 }else{
924 echo "<h4>". __('There is no input','custom-searchable-data-entry-system')."</h4>";
925 }
926 }
927 }
928 }
929 echo "</table>";
930 echo "<p><a href=\"" . get_admin_url() . "admin.php?page=sds-form-entries&sds-del-data-input-table=" . $table . "#". $table."\" onclick=\"return confirm('".__('Are you sure? (THIS ACTION CANNOT BE UNDONE)','custom-searchable-data-entry-system')."');\" style=\"color:#ff0000\">".__('Delete All Entries in This Table','custom-searchable-data-entry-system')."</a></p>";
931 echo "<p><a href=\"" . get_admin_url() . "admin.php?page=sds-form-entries&sds-total-del-input-table-and-corresponding-form=" . $table . "\" onclick=\"return confirm('".__('Are you sure? The corresponding form will be deleted as well. (THIS ACTION CANNOT BE UNDONE)','custom-searchable-data-entry-system')."');\" style=\"color:#ff0000\">".__('Totally Delete This Table AND Its Corresponding Form','custom-searchable-data-entry-system')."</a></p>";
932 echo "</div>";
933 }
934
935 }
936 echo "</div>";
937 }else{
938 echo "<div style=\"color:#ffffff ;background-color:#47a447 ; padding: 10px\"> <strong>".__('No inputs to display. Once you had inputs from your form, they will appear on this page in an organized table.','custom-searchable-data-entry-system')."</strong></div>";
939 }
940
941}
942
943function ghazale_sds_form_inputs_admin_menu(){
944 $page_suffix = add_submenu_page('custom-searchable-data-entry-system',__('Entries','custom-searchable-data-entry-system'),__('Entries','custom-searchable-data-entry-system'),'manage_options','sds-form-entries','ghazale_sds_form_inputs');
945 add_action('admin_print_scripts-' . $page_suffix, 'ghazale_sds_admin_input_table_tabs');
946}
947add_action('admin_menu','ghazale_sds_form_inputs_admin_menu');
948function ghazale_sds_admin_input_table_tabs(){
949 wp_enqueue_style('sds-tabs-style');
950 wp_enqueue_style('ghazale-sds-table-style');
951 wp_enqueue_script('sds-tabs-script');
952 wp_enqueue_script('sds-jquery-base64');
953 wp_enqueue_script('ghazale-sds-table-export');
954}
955
956function ghazale_sds_admin_input_table_page_scripts(){
957 wp_register_script('ghazale-sds-table-export', plugins_url('js/tableExport.js',__FILE__),array('jquery'));
958 wp_register_script('ghazale-sds-jquery-base64',plugins_url('js/jquery.base64.js',__FILE__),array('jquery'));
959 wp_register_style('ghazale-sds-table-style', plugins_url('css/sds-table-style.css', __FILE__));
960 wp_register_script('sds-jquery-base64', plugins_url('js/jquery.base64.js',__FILE__),array('jquery'));
961 wp_register_script('sds-table-export',plugins_url('js/tableExport.js',__FILE__),array('jquery'));
962}
963add_action('admin_init','ghazale_sds_admin_input_table_page_scripts');
964
965function ghazale_sds_enqueue_table_style(){
966 wp_enqueue_style('ghazale-sds-table-style', plugins_url('css/sds-table-style.css',__FILE__));
967}
968add_action('wp_enqueue_scripts','ghazale_sds_enqueue_table_style');
969
970/**
971 * show inputted entries by shortcode
972 */
973function ghazale_sds_show_inputs_on_frontend($atts){
974 global $wpdb;
975 $tables = $wpdb-> prefix. "ghazale_sds_";
976 $input_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_inputs'",ARRAY_A);
977 $field_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_fields'",ARRAY_A);
978
979 if(!empty($input_tables)) {
980
981 foreach ($input_tables as $input_table) {
982 foreach ($input_table as $table) {
983 shortcode_atts(array('table' => str_replace(array($tables, '_inputs'), '', $table)), $atts);
984 $output = "<div id=\"" . $table . "\">";
985 if (str_replace(array($tables, '_inputs'), '', $table) == $atts['table']) {
986 $output .= "<table class = \"table\" id=\"sds_download_table_csv_" . $table . "\">";
987 $inputs_sql = "SELECT * FROM " . $table . " ORDER BY id ASC";
988 $inputs = $wpdb->get_results($inputs_sql, ARRAY_A);
989
990 foreach ($field_tables as $field_table) {
991 foreach ($field_table as $field_table_single) {
992 if ($field_table_single == str_replace("_inputs", "_fields", "$table")) {
993 $fields_sql = "SELECT id,field_name,field_type FROM " . $field_table_single;
994 $fields = $wpdb->get_results($fields_sql, ARRAY_A);
995 if (!empty($inputs)) {
996 $output .= "<tr>";
997 foreach ($fields as $field) {
998 $output .= "<th>" . $field['field_name'] . "</th>";
999 }
1000 $output .= "</tr>";
1001 foreach ($fields as $field) {
1002 $first_id = $field['id'];
1003 break;
1004 }
1005
1006 foreach ($fields as $field) {
1007 $last_id = $field['id'];
1008 }
1009 foreach ($inputs as $input) {
1010 if ($input['field_id'] == $first_id) {
1011 $output .= "<tr>";
1012 }
1013 foreach ($input as $key => $input_single) {
1014 if ($input_single != "" && $key != 'id' && $key != 'field_id') {
1015 $link_or_img = strlen($input_single) - 3;
1016 $jpeg_img = strlen($input_single) - 4;
1017 if (substr($input_single, 0, 4) === "http" && (substr($input_single, $link_or_img, 3) == "png" || substr($input_single, $link_or_img, 3) == "jpg" || substr($input_single, $link_or_img, 3) == "gif" || substr($input_single, $link_or_img, 3) == "bmp" || substr($input_single, $jpeg_img, 4) == "jpeg")) {
1018 $output .= "<td><img src='" . $input_single . "' width=\"100\"></td>";
1019 } elseif (substr($input_single, 0, 4) === "http" && (substr($input_single, $link_or_img, 3) != "png" || substr($input_single, $link_or_img, 3) != "jpg" || substr($input_single, $link_or_img, 3) != "gif" || substr($input_single, $link_or_img, 3) != "bmp" || substr($input_single, $link_or_img, 4) != "jpeg")) {
1020 $output .= "<td><a href='" . $input_single . "'>" . $input_single . "</a></td>";
1021 } else {
1022 $output .= "<td>" . stripslashes($input_single) . "</td>";
1023 }
1024 } elseif ($input_single == "") {
1025 $output .= "<td></td>";
1026 }
1027
1028 }
1029
1030 if ($input['field_id'] == $last_id) {
1031 $output .= "</tr>";
1032 }
1033 }
1034
1035 } else {
1036 $output .= "<h4>" . __('There is no input', 'custom-searchable-data-entry-system') . "</h4>";
1037 }
1038 }
1039 }
1040 }
1041 $output .= "</table>";
1042 $output .= "</div>";
1043 return $output;
1044 }
1045 }
1046
1047 }
1048 }else{
1049 echo "<div style=\"color:#ffffff ;background-color:#47a447 ; padding: 10px\"> <strong>".__('No inputs to display. Once you had inputs from your form, they will appear on this page in an organized table.','custom-searchable-data-entry-system')."</strong></div>";
1050 }
1051
1052}
1053add_shortcode('sds-entry','ghazale_sds_show_inputs_on_frontend');
1054/**
1055 * edit entries admin page
1056 */
1057function ghazale_sds_edit_single_entry_page(){
1058 $entry_table = $_GET['sds-entry-table'];
1059 $entry_id = $_GET['update-single-entry-id'];
1060 global $wpdb;
1061 $table_prefix = $wpdb-> prefix. "ghazale_sds_";
1062 $selected_entry_sql = "SELECT field_input FROM ".$entry_table ." WHERE id=". $entry_id;
1063 $selected_entry = $wpdb -> get_var($selected_entry_sql);
1064 ?>
1065 <h2><?php _e('Edit Entry','custom-searchable-data-entry-system'); ?></h2>
1066 <p><strong><?php _e('Table Name: ','custom-searchable-data-entry-system'); ?></strong><?php echo ucfirst(str_replace(array($table_prefix,'_inputs'),'',$entry_table) . ' Entries'); ?></p>
1067 <p><strong><?php _e('ID: ','custom-searchable-data-entry-system'); ?></strong><?php echo $entry_id; ?></p>
1068 <p><strong><?php _e('Change Entry: ','custom-searchable-data-entry-system'); ?></strong>
1069 <form id="sds-update-single-entry" method="post">
1070 <input type="text" name="sds-update-single-entry" size="50" value="<?php echo $selected_entry; ?>"></p>
1071 <p><input type="submit" name="submit-update-single-entry" value="<?php _e('Update Entry','custom-searchable-data-entry-system'); ?>"><a href="<?php echo get_admin_url().'admin.php?page=sds-form-entries#'.$entry_table ?>" style="text-decoration: none"><input type="button" value="<?php _e('Cancel','custom-searchable-data-entry-system'); ?>"</a></p>
1072 </form>
1073 <?php
1074}
1075function ghazale_sds_edit_single_entry_page_menu(){
1076 add_submenu_page(null, __('Update Single Entry','custom-searchable-data-entry-system'), __('Update Single Entry','custom-searchable-data-entry-system'),'manage_options','sds-update-single-entry','ghazale_sds_edit_single_entry_page');
1077}
1078add_action('admin_menu','ghazale_sds_edit_single_entry_page_menu');
1079/**
1080 * update single entry in db
1081 */
1082function ghazale_sds_update_single_entry(){
1083 if(isset($_POST['submit-update-single-entry'])){
1084 $entry_table = $_GET['sds-entry-table'];
1085 $entry_id = $_GET['update-single-entry-id'];
1086 global $wpdb;
1087 $wpdb -> update($entry_table,array('field_input'=> $_POST['sds-update-single-entry']), array('id'=> $entry_id));
1088 $_SESSION['sds-update'] = __('Entry Updated Successfully','custom-searchable-data-entry-system');
1089 wp_redirect(get_admin_url().'admin.php?page=sds-form-entries#'.$entry_table);
1090 exit;
1091 }
1092}
1093add_action('init','ghazale_sds_update_single_entry');
1094/**
1095 * delete selected input table
1096 */
1097function ghazale_sds_totally_delete_input_table(){
1098 if(isset($_GET['sds-del-data-input-table']) && $_GET['page'] == 'sds-form-entries'){
1099 global $wpdb;
1100 $tables = $wpdb-> prefix. "ghazale_sds_";
1101 $input_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_inputs'",ARRAY_A);
1102 foreach($input_tables as $input_table){
1103 foreach($input_table as $table){
1104 if($table == $_GET['sds-del-data-input-table']){
1105 $del_sql= "SELECT id FROM ". $table;
1106 $rows= $wpdb->get_results($del_sql,ARRAY_A);
1107 foreach($rows as $row){
1108 $wpdb->delete($table,array('id' => $row['id']));
1109 }
1110 $_SESSION['sds-message'] = '<strong>'.esc_html__('Deleted Successfully','custom-searchable-data-entry-system').'.</strong> '.esc_html__('Note: If you had file uploads in your inputs, you should manually remove their directory via FTP connection.','custom-searchable-data-entry-system');
1111 }
1112 }
1113 }
1114 }
1115}
1116
1117add_action('init','ghazale_sds_totally_delete_input_table');
1118/**
1119 * delete input table and its corresponding form table
1120 */
1121function ghazale_sds_totally_delete_input_table_and_corresponding_form_table(){
1122 if(isset($_GET['sds-total-del-input-table-and-corresponding-form'])){
1123 global $wpdb;
1124 $tables = $wpdb-> prefix. "ghazale_sds_";
1125 $input_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_inputs'",ARRAY_A);
1126 foreach($input_tables as $input_table){
1127 foreach($input_table as $table){
1128 if($table == $_GET['sds-total-del-input-table-and-corresponding-form']){
1129 $wpdb->query("DROP TABLE IF EXISTS ". $table);
1130 $_SESSION['sds-message'] = '<strong>'.esc_html__('Deleted Successfully','custom-searchable-data-entry-system').'.</strong> '.esc_html__('Note: If you had file uploads in your inputs, you should manually remove their directory via FTP connection.','custom-searchable-data-entry-system');
1131 }
1132 }
1133 }
1134 $field_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_fields'",ARRAY_A);
1135 $form = str_replace("_inputs","_fields",$_GET['sds-total-del-input-table-and-corresponding-form']);
1136 foreach($field_tables as $field_table){
1137 foreach($field_table as $form_table){
1138 if($form_table == $form){
1139 $wpdb->query("DROP TABLE IF EXISTS ". $form);
1140 $_SESSION['sds-message'] = '<strong>'.esc_html__('Deleted Successfully','custom-searchable-data-entry-system').'.</strong> '.esc_html__('Note: If you had file uploads in your inputs, you should manually remove their directory via FTP connection.','custom-searchable-data-entry-system');
1141 }
1142 }
1143 }
1144 }
1145}
1146add_action('init','ghazale_sds_totally_delete_input_table_and_corresponding_form_table');
1147/**
1148 * delete selected row from entries table
1149 */
1150function ghazale_sds_delete_entries_table_row(){
1151
1152 if(isset($_GET['sds-del-entry-first-entry-id'])){
1153 global $wpdb;
1154 $table_name = $_GET['sds-del-entry-table-row'];
1155 $entry_row = 'SELECT id FROM '. $_GET['sds-del-entry-table-row'] . ' WHERE id >= '.$_GET['sds-del-entry-first-entry-id'] .' AND id <= '.$_GET['sds-del-entry-last-entry-id'];
1156 $related_entries = $wpdb->get_results($entry_row, ARRAY_A);
1157
1158 foreach ($related_entries as $related_entry){
1159 $wpdb -> delete($table_name ,array('id' => $related_entry['id']));
1160 $_SESSION['sds-message'] = __('Deleted Successfully','custom-searchable-data-entry-system');
1161 }
1162 }
1163}
1164add_action('init','ghazale_sds_delete_entries_table_row');
1165/**
1166 * admin page for uploading CSV file
1167 */
1168function ghazale_sds_upload_csv_file_admin_page(){
1169 ?>
1170 <h3><?php _e('Upload CSV File','custom-searchable-data-entry-system') ?></h3>
1171 <form id="sds-upload-csv-file" method="post" enctype="multipart/form-data">
1172 <p><?php echo ghazale_sds_message(); ?></p>
1173 <h2><?php _e('CSV File: *','custom-searchable-data-entry-system') ?></h2>
1174 <input type="file" name="sds-upload-csv-file" id="ghazale-sds-upload-csv-file" required="required">
1175 <hr>
1176 <p><input type="submit" name="sds-submit-upload-csv-file" value="<?php _e('Upload File', 'custom-searchable-data-entry-system') ?>"></p>
1177 </form>
1178 <div style="color:#404040 ;background-color:#aaffaa ; padding: 10px">
1179 <h3><?php _e('Important Notes Before Uploading Your File.','custom-searchable-data-entry-system') ?></h3>
1180 <strong><p><?php _e('Follow these notes carefully to avoid any messed up input to the tables.','custom-searchable-data-entry-system'); ?></p></strong>
1181 <ol>
1182 <li><?php _e('The file SHOULD be in CSV format.','custom-searchable-data-entry-system') ?></li>
1183 <li><?php _e('Put the letters : <strong>sds</strong> (Exactly the same letters - All lowercase) at the first row of your file (Which contains the column names). <strong>This is super important.</strong>','custom-searchable-data-entry-system') ?></li>
1184
1185 <p><strong><?php _e('Example','custom-searchable-data-entry-system'); ?></strong></p>
1186 <p><?php _e('Assume you have an Excel file that contains a table like this:','custom-searchable-data-entry-system') ?></p>
1187 <style>
1188 table, td, th {
1189 border: 1px solid #979797;
1190 border-collapse: collapse;
1191 padding: 5px;
1192 }
1193 </style>
1194 <table>
1195 <tr>
1196 <th>Client Name</th><th>Client Project</th><th>Client City</th><th>Client Reference ID</th>
1197 </tr>
1198 <tr>
1199 <td>David</td><td>Bridge</td><td>London</td><td>746</td>
1200 </tr>
1201 <tr>
1202 <td>Paul</td><td>Road</td><td>New York</td><td>412</td>
1203 </tr>
1204 <tr>
1205 <td>Jane</td><td>Play Area</td><td>Boston</td><td>233</td>
1206 </tr>
1207 </table>
1208 <p><?php _e('The first row of this example file contains the table headers. The structure is probably the same in your own file as well. When you save your Excel file as CSV, make sure you add these three letters: (<strong>sds</strong>) to the beginning of the first row.<strong>(Note that there is No spaces between "sds" and "Clients Name")</strong>. Like below:','custom-searchable-data-entry-system') ?></p>
1209 <table>
1210 <tr>
1211 <th><strong style="color: #ff0000">sds</strong>Client Name</th><th>Client Project</th><th>Client City</th><th>Client Reference ID</th>
1212 </tr>
1213 <tr>
1214 <td>David</td><td>Bridge</td><td>London</td><td>746</td>
1215 </tr>
1216 <tr>
1217 <td>Paul</td><td>Road</td><td>New York</td><td>412</td>
1218 </tr>
1219 <tr>
1220 <td>Jane</td><td>Play Area</td><td>Boston</td><td>233</td>
1221 </tr>
1222 </table>
1223 <li><?php _e('Done! Your data properly get inserted to the database.','custom-searchable-data-entry-system') ?></li>
1224
1225
1226 </ol>
1227 <h4><?php _e('Good To Know','custom-searchable-data-entry-system') ?></h4>
1228 <ol>
1229 <li><?php _e('Your table header (That contains the column names) can be seen under Forms menu on its own tab. And the column names (Fields Names) can be edited/ deleted just like the other forms that you create by this plugin. The field types for the uploaded fields are "Text Field" by default, and you can change them as you like.','custom-searchable-data-entry-system') ?></li>
1230 <li><?php _e('Your main data can be seen under Entries menu on its own tab.','custom-searchable-data-entry-system') ?></li>
1231 <li><?php _e('Just like other tables, you have full control over showing/hiding your desired fields in user\'s search query','custom-searchable-data-entry-system') ?></li>
1232 <li><?php _e('There is absolutely no constraint on the number of columns or anything else for your file. So enjoy uploading!') ?></li>
1233 </ol>
1234 <p><strong><?php _e('IMPORTANT NOTE: When you save your file as CSV(Comma Delimited), be careful about the commas(,) that you have in your file. Because when the system reads the file and it faces commas, it thinks the comma means the end of the field or row and it should skip it while in reality it shouldn\'t; consequently it leads to a messed up table. Therefore it is recommended to replace all the commas in your file (by pressing CTRL+F (Windows)/ Command+F(Mac)) with an alternative like dash(-) like below example.') ?></strong></p>
1235 <p><strong>Example: Change John<span style="font-size: 25px; color: #ff0000">,</span>Mary to John <span style="font-size: 25px; color: #ff0000">-</span> Mary</strong></p>
1236 <h4 style="color: #ff5500"><?php _e('Do you reside in a latin country and a Windows user? Or does your computer language is Spanish, Portuguese, or some other latin language? If yes, you may want to read this as well:','custom-searchable-data-entry-system') ?></h4>
1237 <p>
1238 <?php _e('Most probably the fields in your CSV file is separated with semicolon (;) instead of comma (,). This will result in unexpected outcome when you upload(or download) CSV file. To avoid this, you may want to change your "Windows Regional Settings" as per below instructions:','custom-searchable-data-entry-system') ?>
1239 <ol>
1240 <li><?php _e('Go to your "Region and Language Settings".','custom-searchable-data-entry-system') ?></li>
1241 <li><?php _e('Click on "Additional Settings".','custom-searchable-data-entry-system') ?></li>
1242 <li><?php _e('A new window opens. Make sure you\'re viewing the "Formats" tab.','custom-searchable-data-entry-system') ?></li>
1243 <li><?php _e('Click on "Additional Settings"','custom-searchable-data-entry-system') ?></li>
1244 <li><?php _e('A new window opens. Change the "List Separator" to comma ","','custom-searchable-data-entry-system') ?></li>
1245 </ol>
1246 <?php _e('Optional: to avoid any messed up table, you can also change the "Decimal Symbol" to dot "."','custom-searchable-data-entry-system') ?>
1247 <br><?php _e('Click "OK" on both windows and you\'re done.','custom-searchable-data-entry-system') ?>
1248 </p>
1249 </div>
1250<?php
1251
1252}
1253function ghazale_sds_upload_csv_file_admin_menu(){
1254 add_submenu_page('custom-searchable-data-entry-system',__('Upload CSV File','custom-searchable-data-entry-system'), __('Upload CSV File','custom-searchable-data-entry-system'),'manage_options','sds-upload-csv','ghazale_sds_upload_csv_file_admin_page');
1255}
1256add_action('admin_menu','ghazale_sds_upload_csv_file_admin_menu');
1257/**
1258 * upload CSV file to db
1259 */
1260function ghazale_sds_insert_csv_file(){
1261 if(isset($_POST['sds-submit-upload-csv-file'])) {
1262
1263 global $wpdb;
1264 if (isset($_FILES["sds-upload-csv-file"]) && $_FILES["sds-upload-csv-file"]['size']>0) {
1265 if (!function_exists('wp_handle_upload')) {
1266 require_once(ABSPATH . 'wp-admin/includes/file.php');
1267 }
1268 $uploadedfile = $_FILES["sds-upload-csv-file"];
1269 $upload_overrides = array('test_form' => false, 'mimes' => array('csv' => 'text/csv'));
1270 if(!function_exists('ghazale_sds_change_upload_dir')) {
1271 function ghazale_sds_change_upload_dir($upload)
1272 {
1273 $new_dir = '/sds-csv';
1274
1275 $upload['path'] = str_replace($upload['subdir'], '', $upload['path']);
1276 $upload['url'] = str_replace($upload['subdir'], '', $upload['url']);
1277 $upload['subdir'] = $new_dir;
1278 $upload['path'] .= $new_dir;
1279 $upload['url'] .= $new_dir;
1280
1281 return $upload;
1282 }
1283 }
1284 add_filter('upload_dir', 'ghazale_sds_change_upload_dir');
1285 $upload = wp_upload_dir();
1286 $movefile = wp_handle_upload($uploadedfile, $upload_overrides);
1287 remove_filter('upload_dir', 'ghazale_sds_change_upload_dir');
1288
1289
1290 if ($movefile && !isset($movefile['error'])) {
1291 // echo "File is valid, and was successfully uploaded.\n";
1292 // var_dump($movefile);
1293 $csv_column_names_loc = $movefile['file'];
1294 } else {
1295 /**
1296 * Error generated by _wp_handle_upload()
1297 * @see _wp_handle_upload() in wp-admin/includes/file.php
1298 */
1299 $_SESSION['sds-message'] = $movefile['error'];
1300 }
1301 $table_name = $wpdb->prefix . "ghazale_sds_" . mt_rand(11111,99999).'csv';
1302 $tables= $wpdb->get_results("SHOW TABLES LIKE "."'" .$table_name."%'");
1303 if (count($tables)== 0) {
1304 $sql_form = "CREATE TABLE " . $table_name . "_fields (id INTEGER(10) UNSIGNED AUTO_INCREMENT, field_name VARCHAR (300) COLLATE utf8_bin, field_type VARCHAR (300) COLLATE ascii_bin, field_ext VARCHAR (300) COLLATE utf8_bin, field_hide VARCHAR (4) COLLATE ascii_bin, PRIMARY KEY (id))";
1305 $wpdb->query($sql_form);
1306 $sql_input = "CREATE TABLE " . $table_name . "_inputs (id INTEGER(10) UNSIGNED AUTO_INCREMENT, field_id INTEGER(10), field_input VARCHAR(3000) COLLATE utf8_bin, PRIMARY KEY (id))";
1307 $wpdb->query($sql_input);
1308 //$_SESSION['sds-message'] = esc_html__('Form Created Successfully','custom-searchable-data-entry-system');
1309 } else {
1310 $_SESSION['sds-message'] = esc_html__('An error occur. Please try again. This error will not happen next time! ','custom-searchable-data-entry-system');
1311 }
1312 // create temporary table
1313 if($wpdb->get_var("SHOW TABLES LIKE 'temp'") == NULL) {
1314 $wpdb->query('CREATE TEMPORARY TABLE temp (field_name VARCHAR (300) COLLATE utf8_bin)');
1315 }
1316 //$local_infile_on = "SET GLOBAL local_infile = 'ON'";
1317 //$wpdb->query($local_infile_on);
1318
1319 $upload_fields_sql = "LOAD DATA LOCAL INFILE '" . $csv_column_names_loc . "' INTO TABLE temp CHARACTER SET UTF8 LINES STARTING BY 'sds'";
1320 $true = $wpdb -> query($upload_fields_sql);
1321 $temp_table = "SELECT * FROM temp";
1322 $temp_table_query = $wpdb -> get_var($temp_table);
1323 $fields = explode(',',$temp_table_query);
1324
1325 if($fields) {
1326 foreach ($fields as $field) {
1327 $wpdb->insert($table_name . '_fields', array('field_name' => $field, 'field_type' => 'Text Field'));
1328 }
1329 }
1330
1331 // create temporary table
1332 if($wpdb->get_var("SHOW TABLES LIKE 'temp_input'") == NULL) {
1333 $wpdb->query('CREATE TEMPORARY TABLE temp_input (field_input VARCHAR (300) COLLATE utf8_bin)');
1334 }
1335
1336 $upload_data_sql_input = "LOAD DATA LOCAL INFILE '" . $csv_column_names_loc . "' INTO TABLE temp_input CHARACTER SET UTF8 LINES TERMINATED BY '\n' IGNORE 1 LINES";
1337 $wpdb -> query($upload_data_sql_input);
1338 $temp_table_input = "SELECT * FROM temp_input";
1339 $temp_table_input_query = $wpdb -> get_results($temp_table_input,ARRAY_A);
1340 $inputs_array = array();
1341 foreach($temp_table_input_query as $inputs){
1342 array_push($inputs_array, $inputs['field_input']);
1343 }
1344 $inputs_implode = implode(',', $inputs_array);
1345 $inputs_tmp = explode(',',$inputs_implode);
1346 $field_ids_array = array();
1347 $field_ids_sql = 'SELECT id FROM '.$table_name . '_fields';
1348 $field_ids_query = $wpdb ->get_results($field_ids_sql, ARRAY_A);
1349 foreach($field_ids_query as $field_ids){
1350 array_push($field_ids_array, $field_ids['id']);
1351 }
1352 $iteration_number = (count($inputs_tmp)) / (count($field_ids_array));
1353 $field_ids_array_object = new ArrayObject($field_ids_array);
1354 $copy_result = array();
1355 for ($i=1; $i<= $iteration_number; $i++){
1356 $copy_array = $field_ids_array_object -> getArrayCopy();
1357 $copy_result = array_merge($copy_result,$copy_array);
1358 }
1359
1360 foreach($inputs_tmp as $index => $final_input){
1361
1362 $wpdb->insert($table_name . '_inputs', array('field_id' => $copy_result[$index],'field_input' => $final_input));
1363
1364 }
1365 $_SESSION["sds-message"] = __('Uploaded Successfully. You can now update/delete or hide any field from search query on Forms menu page. And can see the main data on Entries menu page.','custom-searchable-data-entry-system');
1366 }
1367
1368 }
1369}
1370add_action('init','ghazale_sds_insert_csv_file');
1371/**
1372 * options page
1373 */
1374function ghazale_sds_options_page(){
1375 echo "<h3>". esc_html__('Options','custom-searchable-data-entry-system')."</h3>";
1376 echo "<h4>".esc_html__('You have 2 options to search the tables:','custom-searchable-data-entry-system')."</h4>";
1377 echo "<h4><ol>";
1378 echo "<li>". esc_html__('You can choose to search ALL tables with a single search engine.','custom-searchable-data-entry-system')."<i style='color: #ff5500'> Example shortcode: [sds-search]</i></li>";
1379 echo "<li>".esc_html__('You can choose to search tables SEPARATELY with a unique search engine for each table.','custom-searchable-data-entry-system')."<i style='color: #ff5500'> Example shortcode: [sds-search table=\"name\"]</i></li>";
1380 echo "</ol></h4>";
1381 echo "<h4 style='color: #0064cd'>".esc_html__('If the option is enabled you should use the shortcode WITH ITS ATTRIBUTE [sds-search table="name"]','custom-searchable-data-entry-system');
1382 echo "<br>".esc_html__('and if the option is disabled you need to use the simple form of shortcode [sds-search]','custom-searchable-data-entry-system');
1383 echo "</h4>";
1384 echo "<form action='options.php' id='sds_options_form' method='post'>";
1385 settings_errors();
1386 settings_fields('ghazale_sds_options');
1387 echo "<p><input type='checkbox' name='ghazale_sds_separate_search' value='1' ";
1388 if(get_option('ghazale_sds_separate_search')){
1389 echo "checked";
1390 }
1391 echo " />". esc_html__('Search Tables Separately With Separate Search Engines','custom-searchable-data-entry-system')."</p>";
1392
1393 echo "<input type='submit' name='sds_submit_options'>";
1394 echo "</form>";
1395 echo "<br><br>";
1396 echo "<div style='background:#47a447 ;color: #FFFFFF;padding: 5px'>";
1397 echo "<h4>".esc_html__('GUIDE NOTES:','custom-searchable-data-entry-system')."</h4>";
1398 echo "<h4>".esc_html__('After you enabled the above option, the shortcode WILL HAVE AN ATTRIBUTE THAT HAS TO BE ADDED in order to make the search engine work.','custom-searchable-data-entry-system');
1399 echo "<br>".__('For example the search shortcode would be <pre>[sds-search table="name"]</pre> Where "name" is the name of the table that you wish the search engine to search.','custom-searchable-data-entry-system');
1400 echo "</h4>";
1401 echo "</div>";
1402 echo "<div style='background:#005580 ;color: #FFFFFF; padding: 5px'>";
1403 echo "<h4>". esc_html('When this option is enabled, each input table will have its own unique "Search Engine" shortcode that will appear above each respective table under "Entries" submenu. See below screenshot to realize where it is located','custom-searchable-data-entry-system')."</h4>";
1404 echo "<img src='". plugins_url('images/separate-search-engine.png',__FILE__)."'>";
1405 echo "</div>";
1406}
1407function ghazale_sds_options_page_admin_menu()
1408{
1409 add_submenu_page('custom-searchable-data-entry-system', __('Options', 'custom-searchable-data-entry-system'), __('Options', 'custom-searchable-data-entry-system'), 'manage_options', 'sds-options_page', 'ghazale_sds_options_page');
1410}
1411add_action('admin_menu','ghazale_sds_options_page_admin_menu');
1412/**
1413 * front-end search field
1414 */
1415function ghazale_sds_front_end_search_field($atts){
1416 $output = '<form id="sds-search-entries" method="post">';
1417 $output .= '<h3>'. __('Search','custom-searchable-data-entry-system') .':</h3>';
1418 $output .= '<p><input type="text" name="sds-search-data" size="40" placeholder="'.__('SEARCH HERE...','custom-searchable-data-entry-system').'"></p>';
1419 $output .= '<p><input type="submit" name="submit-sds-search-query" value="'.__('Search','custom-searchable-data-entry-system').'"></p>';
1420 $output .= '</form>';
1421 if(isset($_POST['submit-sds-search-query'])){
1422 global $wpdb;
1423 $tables = $wpdb-> prefix. "ghazale_sds_";
1424 $input_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_inputs'",ARRAY_A);
1425 $field_tables = $wpdb->get_results("SHOW TABLES LIKE "."'" .$tables."%_fields'",ARRAY_A);
1426 $has_result = array();
1427 if(!empty($input_tables)) {
1428 $hidden_field = array();
1429 $shown_field = array();
1430 $results_array = array();
1431 foreach ($input_tables as $input_table) {
1432 foreach ($input_table as $table) {
1433 if(get_option('ghazale_sds_separate_search')) {
1434 shortcode_atts(array('table' => str_replace(array($tables, '_inputs'), '', $table)), $atts);
1435 }
1436 if (get_option('ghazale_sds_separate_search') && str_replace(array($tables, '_inputs'), '', $table) == $atts['table']) {
1437
1438 $inputs_sql = "SELECT * FROM " . $table . " ORDER BY id ASC";
1439 $inputs = $wpdb->get_results($inputs_sql, ARRAY_A);
1440 if (trim($_POST['sds-search-data'] != '')) {
1441 $results_sql = "SELECT id,field_id,field_input FROM " . $table . " WHERE field_input COLLATE UTF8_GENERAL_CI LIKE '" . $_POST['sds-search-data'] . "%' ORDER BY id ASC";
1442 $results = $wpdb->get_results($results_sql, ARRAY_A);
1443 foreach ($field_tables as $field_table) {
1444 foreach ($field_table as $field_table_single) {
1445 if ($field_table_single == str_replace("_inputs", "_fields", "$table")) {
1446 $fields_sql = "SELECT id,field_name,field_type,field_hide FROM " . $field_table_single;
1447 $fields = $wpdb->get_results($fields_sql, ARRAY_A);
1448
1449 if (!empty($inputs)) {
1450
1451 foreach ($fields as $field) {
1452 $first_id = $field['id'];
1453 break;
1454 }
1455
1456 foreach ($fields as $field) {
1457 $last_id = $field['id'];
1458 }
1459 if ($results) {
1460
1461 foreach ($results as $result) {
1462 if (strstr($result['field_input'], '@') != true) {
1463 $output .= "<div id=\"" . $table . "\">";
1464 $output .= "<table class = \"table\">";
1465 $range_initial_sql = 'SELECT id FROM ' . $table . ' WHERE field_id<= ' . $result['field_id'] . ' AND field_id =' . $first_id . ' AND id<= ' . $result['id'] . ' ORDER BY id DESC LIMIT 1';
1466 $range_initial = $wpdb->get_results($range_initial_sql, ARRAY_A);
1467 $range_final_sql = 'SELECT id FROM ' . $table . ' WHERE field_id>= ' . $result['field_id'] . ' AND field_id =' . $last_id . ' AND id>= ' . $result['id'] . ' ORDER BY id ASC LIMIT 1';
1468 $range_final = $wpdb->get_results($range_final_sql, ARRAY_A);
1469
1470 $result_range_sql = 'SELECT id,field_id,field_input FROM ' . $table . ' WHERE id BETWEEN ' . $range_initial[0]['id'] . ' and ' . $range_final[0]['id'];
1471 $result_range = $wpdb->get_results($result_range_sql, ARRAY_A);
1472 foreach ($fields as $field) {
1473 if ($field['field_hide'] == "Hide") {
1474 //if the field is hidden don't show anything
1475 array_push($hidden_field, $field['id']);
1476 } else {
1477 //otherwise show it on search query
1478 array_push($shown_field, $field['field_name']);
1479 }
1480 }
1481 foreach ($result_range as $single_result) {
1482 if (!in_array($single_result['field_id'], $hidden_field)) {
1483 array_push($results_array, $single_result['field_input']);
1484 }
1485 }
1486
1487
1488 foreach ($shown_field as $index => $single_field) {
1489 $output .= "<tr>";
1490 if ($results_array[$index] != "") {
1491 $link_or_img = strlen($results_array[$index]) - 3;
1492 $jpeg_img = strlen($results_array[$index]) - 4;
1493 if (substr($results_array[$index], 0, 4) === "http" && (substr($results_array[$index], $link_or_img, 3) == "png" || substr($results_array[$index], $link_or_img, 3) == "jpg" || substr($results_array[$index], $link_or_img, 3) == "gif" || substr($results_array[$index], $link_or_img, 3) == "bmp" || substr($results_array[$index], $jpeg_img, 4) == "jpeg")) {
1494 $output .= "<th style='text-align: center'>" . $single_field . "</th><td style='text-align: center'><img src='" . $results_array[$index] . "' width=\"100\"></td>";
1495 } elseif (substr($results_array[$index], 0, 4) === "http" && (substr($results_array[$index], $link_or_img, 3) != "png" || substr($results_array[$index], $link_or_img, 3) != "jpg" || substr($results_array[$index], $link_or_img, 3) != "gif" || substr($results_array[$index], $link_or_img, 3) != "bmp" || substr($results_array[$index], $jpeg_img, 4) != "jpeg")) {
1496 $output .= "<th style='text-align: center'>" . $single_field . "</th><td style='text-align: center'><a href='" . $results_array[$index] . "'>" . $results_array[$index] . "</a></td>";
1497 } else {
1498 $output .= "<th style='text-align: center'>" . $single_field . "</th><td style='text-align: center'>" . stripslashes($results_array[$index]) . "</td>";
1499 }
1500 }
1501
1502 $output .= "</tr>";
1503
1504 }
1505 $output .= "</table>";
1506 $output .= "</div>";
1507 unset($hidden_field);
1508 $hidden_field = array();
1509 unset($shown_field);
1510 $shown_field = array();
1511 unset($results_array);
1512 $results_array = array();
1513 }
1514 }
1515
1516 array_push($has_result, 'Has Result');
1517 }
1518 }
1519 }
1520 }
1521 }
1522 }
1523 }elseif(!get_option('ghazale_sds_separate_search')){
1524 $inputs_sql = "SELECT * FROM " . $table . " ORDER BY id ASC";
1525 $inputs = $wpdb->get_results($inputs_sql, ARRAY_A);
1526 if (trim($_POST['sds-search-data'] != '')) {
1527 $results_sql = "SELECT id,field_id,field_input FROM " . $table . " WHERE field_input COLLATE UTF8_GENERAL_CI LIKE '" . $_POST['sds-search-data'] . "%' ORDER BY id ASC";
1528 $results = $wpdb->get_results($results_sql, ARRAY_A);
1529 foreach ($field_tables as $field_table) {
1530 foreach ($field_table as $field_table_single) {
1531 if ($field_table_single == str_replace("_inputs", "_fields", "$table")) {
1532 $fields_sql = "SELECT id,field_name,field_type,field_hide FROM " . $field_table_single;
1533 $fields = $wpdb->get_results($fields_sql, ARRAY_A);
1534
1535 if (!empty($inputs)) {
1536
1537 foreach ($fields as $field) {
1538 $first_id = $field['id'];
1539 break;
1540 }
1541
1542 foreach ($fields as $field) {
1543 $last_id = $field['id'];
1544 }
1545 if ($results) {
1546
1547 foreach ($results as $result) {
1548 if (strstr($result['field_input'], '@') != true) {
1549 $output .= "<div id=\"" . $table . "\">";
1550 $output .= "<table class = \"table\">";
1551 $range_initial_sql = 'SELECT id FROM ' . $table . ' WHERE field_id<= ' . $result['field_id'] . ' AND field_id =' . $first_id . ' AND id<= ' . $result['id'] . ' ORDER BY id DESC LIMIT 1';
1552 $range_initial = $wpdb->get_results($range_initial_sql, ARRAY_A);
1553 $range_final_sql = 'SELECT id FROM ' . $table . ' WHERE field_id>= ' . $result['field_id'] . ' AND field_id =' . $last_id . ' AND id>= ' . $result['id'] . ' ORDER BY id ASC LIMIT 1';
1554 $range_final = $wpdb->get_results($range_final_sql, ARRAY_A);
1555
1556 $result_range_sql = 'SELECT id,field_id,field_input FROM ' . $table . ' WHERE id BETWEEN ' . $range_initial[0]['id'] . ' and ' . $range_final[0]['id'];
1557 $result_range = $wpdb->get_results($result_range_sql, ARRAY_A);
1558 foreach ($fields as $field) {
1559 if ($field['field_hide'] == "Hide") {
1560 //if the field is hidden don't show anything
1561 array_push($hidden_field, $field['id']);
1562 } else {
1563 //otherwise show it on search query
1564 array_push($shown_field, $field['field_name']);
1565 }
1566 }
1567 foreach ($result_range as $single_result) {
1568 if (!in_array($single_result['field_id'], $hidden_field)) {
1569 array_push($results_array, $single_result['field_input']);
1570 }
1571 }
1572
1573
1574 foreach ($shown_field as $index => $single_field) {
1575 $output .= "<tr>";
1576 if ($results_array[$index] != "") {
1577 $link_or_img = strlen($results_array[$index]) - 3;
1578 $jpeg_img = strlen($results_array[$index]) - 4;
1579 if (substr($results_array[$index], 0, 4) === "http" && (substr($results_array[$index], $link_or_img, 3) == "png" || substr($results_array[$index], $link_or_img, 3) == "jpg" || substr($results_array[$index], $link_or_img, 3) == "gif" || substr($results_array[$index], $link_or_img, 3) == "bmp" || substr($results_array[$index], $jpeg_img, 4) == "jpeg")) {
1580 $output .= "<th style='text-align: center'>" . $single_field . "</th><td style='text-align: center'><img src='" . $results_array[$index] . "' width=\"100\"></td>";
1581 } elseif (substr($results_array[$index], 0, 4) === "http" && (substr($results_array[$index], $link_or_img, 3) != "png" || substr($results_array[$index], $link_or_img, 3) != "jpg" || substr($results_array[$index], $link_or_img, 3) != "gif" || substr($results_array[$index], $link_or_img, 3) != "bmp" || substr($results_array[$index], $jpeg_img, 4) != "jpeg")) {
1582 $output .= "<th style='text-align: center'>" . $single_field . "</th><td style='text-align: center'><a href='" . $results_array[$index] . "'>" . $results_array[$index] . "</a></td>";
1583 } else {
1584 $output .= "<th style='text-align: center'>" . $single_field . "</th><td style='text-align: center'>" . stripslashes($results_array[$index]) . "</td>";
1585 }
1586 }
1587
1588 $output .= "</tr>";
1589
1590 }
1591 $output .= "</table>";
1592 $output .= "</div>";
1593 unset($hidden_field);
1594 $hidden_field = array();
1595 unset($shown_field);
1596 $shown_field = array();
1597 unset($results_array);
1598 $results_array = array();
1599 }
1600 }
1601
1602 array_push($has_result, 'Has Result');
1603 }
1604 }
1605 }
1606 }
1607 }
1608 }
1609 }
1610 }
1611
1612 }
1613
1614 }
1615 if(count($has_result) == 0){
1616 $output.= "<h4>". __('No Result','custom-searchable-data-entry-system')."</h4>";
1617 }
1618
1619 }
1620
1621 return $output;
1622}
1623add_shortcode('sds-search','ghazale_sds_front_end_search_field');
1624
1625/**
1626 * loading language file
1627 */
1628function ghazale_sds_load_plugin_textdomain() {
1629 load_plugin_textdomain( 'custom-searchable-data-entry-system', FALSE, basename( dirname( __FILE__ ) ) . '/languages/' );
1630}
1631add_action( 'plugins_loaded', 'ghazale_sds_load_plugin_textdomain' );