コード例 #1
0
ファイル: admin-options.php プロジェクト: kalushta/darom
function cp_form_layouts()
{
    global $options_new_form, $wpdb, $current_user;
    $current_user = wp_get_current_user();
    // check to prevent php "notice: undefined index" msg when php strict warnings is on
    if (isset($_GET['action'])) {
        $theswitch = $_GET['action'];
    } else {
        $theswitch = '';
    }
    ?>

	<script type="text/javascript">
	/* <![CDATA[ */
	/* initialize the form validation */
	jQuery(document).ready(function($) {
		$("#mainform").validate({errorClass: "invalid"});
	});
	/* ]]> */
	</script>

	<?php 
    switch ($theswitch) {
        case 'addform':
            ?>

			<div class="wrap">
				<div class="icon32" id="icon-themes"><br /></div>
				<h2><?php 
            _e('New Form Layout', APP_TD);
            ?>
</h2>

				<?php 
            cp_admin_info_box();
            ?>

				<?php 
            // check and make sure the form was submitted and the hidden fcheck id matches the cookie fcheck id
            if (isset($_POST['submitted'])) {
                if (!isset($_POST['post_category'])) {
                    wp_die('<p style="color:red;">' . __("Error: Please select at least one category. <a href='#' onclick='history.go(-1);return false;'>Go back</a>", APP_TD) . '</p>');
                }
                $data = array('form_name' => appthemes_clean(cp_make_custom_name($_POST['form_label'])), 'form_label' => appthemes_clean($_POST['form_label']), 'form_desc' => appthemes_clean($_POST['form_desc']), 'form_cats' => serialize($_POST['post_category']), 'form_status' => appthemes_clean($_POST['form_status']), 'form_owner' => appthemes_clean($_POST['form_owner']), 'form_created' => gmdate('Y-m-d H:i:s'));
                $insert = $wpdb->insert($wpdb->cp_ad_forms, $data);
                if ($insert) {
                    ?>

						<p style="text-align:center;padding-top:50px;font-size:22px;"><?php 
                    _e('Creating your form.....', APP_TD);
                    ?>
<br /><br /><img src="<?php 
                    bloginfo('template_directory');
                    ?>
/images/loader.gif" alt="" /></p>
						<meta http-equiv="refresh" content="0; URL=?page=layouts">

					<?php 
                }
                // end $insert
            } else {
                ?>

            <form method="post" id="mainform" action="">

                <?php 
                echo cp_admin_fields($options_new_form);
                ?>

                <p class="submit"><input class="btn button-primary" name="save" type="submit" value="<?php 
                _e('Create New Form', APP_TD);
                ?>
" />&nbsp;&nbsp;&nbsp;
                <input name="cancel" type="button" onClick="location.href='?page=layouts'" value="<?php 
                _e('Cancel', APP_TD);
                ?>
" /></p>
                <input name="submitted" type="hidden" value="yes" />
                <input name="form_owner" type="hidden" value="<?php 
                echo $current_user->user_login;
                ?>
" />

            </form>

        <?php 
            }
            // end isset $_POST
            ?>

        </div><!-- end wrap -->

    <?php 
            break;
        case 'editform':
            ?>

			<div class="wrap">
				<div class="icon32" id="icon-themes"><br /></div>
				<h2><?php 
            _e('Edit Form Properties', APP_TD);
            ?>
</h2>

				<?php 
            if (isset($_POST['submitted']) && $_POST['submitted'] == 'yes') {
                if (!isset($_POST['post_category'])) {
                    wp_die('<p style="color:red;">' . __("Error: Please select at least one category. <a href='#' onclick='history.go(-1);return false;'>Go back</a>", APP_TD) . '</p>');
                }
                $data = array('form_label' => appthemes_clean($_POST['form_label']), 'form_desc' => appthemes_clean($_POST['form_desc']), 'form_cats' => serialize($_POST['post_category']), 'form_status' => appthemes_clean($_POST['form_status']), 'form_owner' => appthemes_clean($_POST['form_owner']), 'form_modified' => gmdate('Y-m-d H:i:s', time() + get_option('gmt_offset') * 3600));
                $wpdb->update($wpdb->cp_ad_forms, $data, array('id' => $_GET['id']));
                ?>

					<p style="text-align:center;padding-top:50px;font-size:22px;"><?php 
                _e('Saving your changes.....', APP_TD);
                ?>
<br /><br /><img src="<?php 
                bloginfo('template_directory');
                ?>
/images/loader.gif" alt="" /></p>
					<meta http-equiv="refresh" content="0; URL=?page=layouts">

				<?php 
            } else {
                ?>

            <form method="post" id="mainform" action="">

            <?php 
                echo cp_admin_db_fields($options_new_form, 'cp_ad_forms', 'id');
                ?>

                <p class="submit"><input class="btn button-primary" name="save" type="submit" value="<?php 
                _e('Save changes', APP_TD);
                ?>
" />&nbsp;&nbsp;&nbsp;
                <input name="cancel" type="button" onClick="location.href='?page=layouts'" value="<?php 
                _e('Cancel', APP_TD);
                ?>
" /></p>
                <input name="submitted" type="hidden" value="yes" />
                <input name="form_owner" type="hidden" value="<?php 
                echo $current_user->user_login;
                ?>
" />

            </form>

        <?php 
            }
            // end isset $_POST
            ?>

        </div><!-- end wrap -->

    <?php 
            break;
            /**
             * Form Builder Page
             * Where fields are added to form layouts
             */
        /**
         * Form Builder Page
         * Where fields are added to form layouts
         */
        case 'formbuilder':
            ?>

			<div class="wrap">
				<div class="icon32" id="icon-themes"><br /></div>
				<h2><?php 
            _e('Edit Form Layout', APP_TD);
            ?>
</h2>

				<?php 
            cp_admin_info_box();
            ?>

				<?php 
            // add fields to page layout on left side
            if (isset($_POST['field_id'])) {
                // take selected checkbox array and loop through ids
                foreach ($_POST['field_id'] as $value) {
                    $data = array('form_id' => appthemes_clean($_POST['form_id']), 'field_id' => appthemes_clean($value));
                    $insert = $wpdb->insert($wpdb->cp_ad_meta, $data);
                }
                // end foreach
            }
            // end $_POST
            // update form layout positions and required fields on left side.
            if (isset($_POST['formlayout'])) {
                // loop through the post array and update the required checkbox and field position
                foreach ($_POST as $key => $value) {
                    // since there's some $_POST values we don't want to process, only give us the
                    // numeric ones which means it contains a meta_id and we want to update it
                    if (is_numeric($key)) {
                        // quick hack to prevent php "notice: undefined index:" msg when php strict warnings is on
                        if (!isset($value['field_req'])) {
                            $value['field_req'] = '';
                        }
                        if (!isset($value['field_search'])) {
                            $value['field_search'] = '';
                        }
                        $data = array('field_req' => appthemes_clean($value['field_req']), 'field_search' => appthemes_clean($value['field_search']));
                        $wpdb->update($wpdb->cp_ad_meta, $data, array('meta_id' => $key));
                    }
                    // end if_numeric
                }
                // end for each
                echo '<p class="info">' . __('Your changes have been saved.', APP_TD) . '</p>';
            }
            // end isset $_POST
            // check to prevent php "notice: undefined index" msg when php strict warnings is on
            if (isset($_GET['del_id'])) {
                $theswitch = $_GET['del_id'];
            } else {
                $theswitch = '';
            }
            // Remove items from form layout
            if ($theswitch) {
                $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->cp_ad_meta} WHERE meta_id = %s", $_GET['del_id']));
            }
            //update the forms modified date
            $data = array('form_modified' => gmdate('Y-m-d H:i:s', time() + get_option('gmt_offset') * 3600));
            $wpdb->update($wpdb->cp_ad_forms, $data, array('id' => $_GET['id']));
            ?>


        <table>
            <tr style="vertical-align:top;">
                <td style="width:800px;padding:0 20px 0 0;">


                <h3><?php 
            _e('Form Name', APP_TD);
            ?>
 - <?php 
            echo ucfirst(urldecode($_GET['title']));
            ?>
&nbsp;&nbsp;&nbsp;&nbsp;<span id="loading"></span></h3>

                <form method="post" id="mainform" action="">

                    <table class="widefat">
                        <thead>
                            <tr>
                                <th scope="col" colspan="2"><?php 
            _e('Form Preview', APP_TD);
            ?>
</th>
								<th scope="col" style="width:75px;text-align:center;" title="<?php 
            _e('Show field in the category refine search sidebar', APP_TD);
            ?>
"><?php 
            _e('Advanced Search', APP_TD);
            ?>
</th>
                                <th scope="col" style="width:75px;text-align:center;"><?php 
            _e('Required', APP_TD);
            ?>
</th>
                                <th scope="col" style="width:75px;text-align:center;"><?php 
            _e('Remove', APP_TD);
            ?>
</th>
                            </tr>
                        </thead>



                        <tbody class="sortable">

                        <?php 
            // If this is the first time this form is being customized then auto
            // create the core fields and put in cp_meta db table
            echo cp_add_core_fields($_GET['id']);
            // Then go back and select all the fields assigned to this
            // table which now includes the added core fields.
            $sql = $wpdb->prepare("SELECT f.field_label, f.field_name, f.field_type, f.field_values, f.field_perm, m.meta_id, m.field_pos, m.field_search, m.field_req, m.form_id " . "FROM {$wpdb->cp_ad_fields} f " . "INNER JOIN {$wpdb->cp_ad_meta} m " . "ON f.field_id = m.field_id " . "WHERE m.form_id = %s " . "ORDER BY m.field_pos asc", $_GET['id']);
            $results = $wpdb->get_results($sql);
            if ($results) {
                echo cp_admin_formbuilder($results);
            } else {
                ?>

                        <tr>
                            <td colspan="5" style="text-align: center;"><p><br /><?php 
                _e('No fields have been added to this form layout yet.', APP_TD);
                ?>
<br /><br /></p></td>
                        </tr>

                        <?php 
            }
            // end $results
            ?>

                        </tbody>

                    </table>

                    <p class="submit">
                        <input class="btn button-primary" name="save" type="submit" value="<?php 
            _e('Save Changes', APP_TD);
            ?>
" />&nbsp;&nbsp;&nbsp;
                        <input name="cancel" type="button" onClick="location.href='?page=layouts'" value="<?php 
            _e('Cancel', APP_TD);
            ?>
" />
                        <input name="formlayout" type="hidden" value="yes" />
                        <input name="form_owner" type="hidden" value="<?php 
            $current_user->user_login;
            ?>
" />
                    </p>
                </form>

                </td>
                <td>

                <h3><?php 
            _e('Available Fields', APP_TD);
            ?>
</h3>

                <form method="post" id="mainform" action="">


                <div class="fields-panel">

                    <table class="widefat">
                        <thead>
                            <tr>
                                <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"/></th>
                                <th scope="col"><?php 
            _e('Field Name', APP_TD);
            ?>
</th>
                                <th scope="col"><?php 
            _e('Type', APP_TD);
            ?>
</th>
                            </tr>
                        </thead>


                        <tbody>

                        <?php 
            // Select all available fields not currently on the form layout.
            // Also exclude any core fields since they cannot be removed from the layout.
            $sql = $wpdb->prepare("SELECT f.field_id,f.field_label,f.field_type " . "FROM {$wpdb->cp_ad_fields} f " . "WHERE f.field_id " . "NOT IN (SELECT m.field_id " . "FROM {$wpdb->cp_ad_meta} m " . "WHERE m.form_id =  %s) " . "AND f.field_perm <> '1'", $_GET['id']);
            $results = $wpdb->get_results($sql);
            if ($results) {
                foreach ($results as $result) {
                    ?>

                        <tr class="even">
                            <th class="check-column" scope="row"><input type="checkbox" value="<?php 
                    echo $result->field_id;
                    ?>
" name="field_id[]"/></th>
                            <td><?php 
                    echo esc_html(translate($result->field_label, APP_TD));
                    ?>
</td>
                            <td><?php 
                    echo $result->field_type;
                    ?>
</td>
                        </tr>

                        <?php 
                }
                // end foreach
            } else {
                ?>

                        <tr>
                            <td colspan="4" style="text-align: center;"><p><br /><?php 
                _e('No fields are available.', APP_TD);
                ?>
<br /><br /></p></td>
                        </tr>

                        <?php 
            }
            // end $results
            ?>

                        </tbody>

                    </table>

                </div>

                    <p class="submit"><input class="btn button-primary" name="save" type="submit" value="<?php 
            _e('Add Fields to Form Layout', APP_TD);
            ?>
" /></p>
                        <input name="form_id" type="hidden" value="<?php 
            echo $_GET['id'];
            ?>
" />
                        <input name="submitted" type="hidden" value="yes" />


                </form>

                </td>
            </tr>
        </table>

    </div><!-- /wrap -->

    <?php 
            break;
        case 'delete':
            // delete the form based on the form id
            cp_delete_form($_GET['id']);
            ?>
        <p style="text-align:center;padding-top:50px;font-size:22px;"><?php 
            _e('Deleting form layout.....', APP_TD);
            ?>
<br /><br /><img src="<?php 
            bloginfo('template_directory');
            ?>
/images/loader.gif" alt="" /></p>
        <meta http-equiv="refresh" content="0; URL=?page=layouts">

    <?php 
            break;
        default:
            $results = $wpdb->get_results("SELECT * FROM {$wpdb->cp_ad_forms} ORDER BY id desc");
            ?>

        <div class="wrap">
        <div class="icon32" id="icon-themes"><br /></div>
        <h2><?php 
            _e('Form Layouts', APP_TD);
            ?>
&nbsp;<a class="button add-new-h2" href="?page=layouts&amp;action=addform"><?php 
            _e('Add New', APP_TD);
            ?>
</a></h2>

        <?php 
            cp_admin_info_box();
            ?>

        <p class="admin-msg"><?php 
            _e('Form layouts allow you to create your own custom ad submission forms. Each form is essentially a container for your fields and can be applied to one or all of your categories. If you do not create any form layouts, the default one will be used. To change the default form, create a new form layout and apply it to all categories.', APP_TD);
            ?>
</p>

        <table id="tblspacer" class="widefat fixed">

            <thead>
                <tr>
                    <th scope="col" style="width:35px;">&nbsp;</th>
                    <th scope="col"><?php 
            _e('Name', APP_TD);
            ?>
</th>
                    <th scope="col"><?php 
            _e('Description', APP_TD);
            ?>
</th>
                    <th scope="col"><?php 
            _e('Categories', APP_TD);
            ?>
</th>
                    <th scope="col" style="width:150px;"><?php 
            _e('Modified', APP_TD);
            ?>
</th>
                    <th scope="col" style="width:75px;"><?php 
            _e('Status', APP_TD);
            ?>
</th>
                    <th scope="col" style="text-align:center;width:100px;"><?php 
            _e('Actions', APP_TD);
            ?>
</th>
                </tr>
            </thead>

            <?php 
            if ($results) {
                $rowclass = '';
                $i = 1;
                ?>

              <tbody id="list">

            <?php 
                foreach ($results as $result) {
                    $rowclass = 'even' == $rowclass ? 'alt' : 'even';
                    ?>

                <tr class="<?php 
                    echo $rowclass;
                    ?>
">
                    <td style="padding-left:10px;"><?php 
                    echo $i;
                    ?>
.</td>
                    <td><a href="?page=layouts&amp;action=editform&amp;id=<?php 
                    echo $result->id;
                    ?>
"><strong><?php 
                    echo $result->form_label;
                    ?>
</strong></a></td>
                    <td><?php 
                    echo $result->form_desc;
                    ?>
</td>
                    <td><?php 
                    echo cp_match_cats(unserialize($result->form_cats));
                    ?>
</td>
                    <td><?php 
                    echo mysql2date(get_option('date_format') . ' ' . get_option('time_format'), $result->form_modified);
                    ?>
 <?php 
                    _e('by', APP_TD);
                    ?>
 <?php 
                    echo $result->form_owner;
                    ?>
</td>
                    <td><?php 
                    echo cp_get_status_i18n($result->form_status);
                    ?>
</td>
                    <td style="text-align:center"><a href="?page=layouts&amp;action=formbuilder&amp;id=<?php 
                    echo $result->id;
                    ?>
&amp;title=<?php 
                    echo urlencode($result->form_label);
                    ?>
"><img src="<?php 
                    bloginfo('template_directory');
                    ?>
/images/layout_add.png" alt="<?php 
                    _e('Edit form layout', APP_TD);
                    ?>
" title="<?php 
                    _e('Edit form layout', APP_TD);
                    ?>
" /></a>&nbsp;&nbsp;&nbsp;
                        <a href="?page=layouts&amp;action=editform&amp;id=<?php 
                    echo $result->id;
                    ?>
"><img src="<?php 
                    bloginfo('template_directory');
                    ?>
/images/edit.png" alt="<?php 
                    _e('Edit form properties', APP_TD);
                    ?>
" title="<?php 
                    _e('Edit form properties', APP_TD);
                    ?>
" /></a>&nbsp;&nbsp;&nbsp;
                        <a onclick="return confirmBeforeDelete();" href="?page=layouts&amp;action=delete&amp;id=<?php 
                    echo $result->id;
                    ?>
"><img src="<?php 
                    bloginfo('template_directory');
                    ?>
/images/cross.png" alt="<?php 
                    _e('Delete form layout', APP_TD);
                    ?>
" title="<?php 
                    _e('Delete form layout', APP_TD);
                    ?>
" /></a></td>
                </tr>

              <?php 
                    $i++;
                }
                // end for each
                ?>

              </tbody>

            <?php 
            } else {
                ?>

                <tr>
                    <td colspan="7"><?php 
                _e('No form layouts found.', APP_TD);
                ?>
</td>
                </tr>

            <?php 
            }
            // end $results
            ?>

            </table>


        </div><!-- end wrap -->

    <?php 
    }
    // end switch
    ?>
    <script type="text/javascript">
        /* <![CDATA[ */
            function confirmBeforeDelete() { return confirm("<?php 
    _e('Are you sure you want to delete this?', APP_TD);
    ?>
"); }
            function confirmBeforeRemove() { return confirm("<?php 
    _e('Are you sure you want to remove this?', APP_TD);
    ?>
"); }
        /* ]]> */
    </script>

<?php 
}
コード例 #2
0
function cp_form_layouts()
{
    global $options_new_form, $wpdb, $current_user;
    $current_user = wp_get_current_user();
    // check to prevent php "notice: undefined index" msg when php strict warnings is on
    if (isset($_GET['action'])) {
        $theswitch = $_GET['action'];
    } else {
        $theswitch = '';
    }
    ?>

	<script type="text/javascript">
	/* <![CDATA[ */
	/* initialize the form validation */
	jQuery(document).ready(function($) {
		$("#mainform").validate({errorClass: "invalid"});
	});
	/* ]]> */
    </script>

	<?php 
    switch ($theswitch) {
        // Adds new AV Form to DB
        case 'addform':
            ?>

        <div class="wrap">
            <div class="icon32" id="icon-themes"><br/></div>
            <h2><?php 
            _e('New Form Layout', 'appthemes');
            ?>
</h2>

            <?php 
            cp_admin_info_box();
            ?>

        <?php 
            // check and make sure the form was submitted and the hidden fcheck id matches the cookie fcheck id
            if (isset($_POST['submitted'])) {
                if (!isset($_POST['post_category'])) {
                    wp_die('<p style="color:red;">' . __("Error: Please select at least one category. <a href='#' onclick='history.go(-1);return false;'>Go back</a>", 'appthemes') . '</p>');
                }
                // EKELEME (Form tablosuna cat id ekler)
                $cat_id = split('"', serialize($_POST['post_category']));
                // @todo Change to Insert
                $insert = $wpdb->prepare("INSERT INTO {$wpdb->cp_ad_forms}" . " (form_name, form_label, form_desc, form_cats, form_status, form_owner, form_created, cat_id) " . "VALUES ( %s, %s, %s, %s, %s, %s, %s , %s)", appthemes_clean(cp_make_custom_name($_POST['form_label'])), appthemes_clean($_POST['form_label']), appthemes_clean($_POST['form_desc']), serialize($_POST['post_category']), appthemes_clean($_POST['form_status']), appthemes_clean($_POST['form_owner']), gmdate('Y-m-d H:i:s'), $cat_id[1]);
                // EKELEME BITIS
                $results = $wpdb->query($insert);
                if ($results) {
                    ?>

                <p style="text-align:center;padding-top:50px;font-size:22px;"><?php 
                    _e('Creating your form.....', 'appthemes');
                    ?>
<br /><br /><img src="<?php 
                    echo bloginfo('template_directory');
                    ?>
/images/loader.gif" alt="" /></p>
                <meta http-equiv="refresh" content="0; URL=?page=layouts">

            <?php 
                }
                // end $results
            } else {
                ?>

            <form method="post" id="mainform" action="">

                <?php 
                echo cp_admin_fields($options_new_form);
                ?>

                <p class="submit"><input class="btn button-primary" name="save" type="submit" value="<?php 
                _e('Create New Form', 'appthemes');
                ?>
" />&nbsp;&nbsp;&nbsp;
                <input name="cancel" type="button" onClick="location.href='?page=layouts'" value="<?php 
                _e('Cancel', 'appthemes');
                ?>
" /></p>
                <input name="submitted" type="hidden" value="yes" />
                <input name="form_owner" type="hidden" value="<?php 
                echo $current_user->user_login;
                ?>
" />

            </form>

        <?php 
            }
            // end isset $_POST
            ?>

        </div><!-- end wrap -->

    <?php 
            break;
            // Updates new AV Form from DB
        // Updates new AV Form from DB
        case 'editform':
            ?>

        <div class="wrap">
        <div class="icon32" id="icon-themes"><br/></div>
        <h2><?php 
            _e('Erisim Formu Ozelliklerini Duzenle', 'appthemes');
            ?>
</h2>

        <?php 
            if (isset($_POST['submitted']) && $_POST['submitted'] == 'yes') {
                if (!isset($_POST['post_category'])) {
                    wp_die('<p style="color:red;">' . __("Error: Please select at least one category. <a href='#' onclick='history.go(-1);return false;'>Go back</a>", 'appthemes') . '</p>');
                }
                // @todo Change to Update
                // EKLEME (Form duzenleme sirasinda cat id update eder)
                $update_cat_id = split('"', serialize($_POST['post_category']));
                $update = $wpdb->prepare("UPDATE {$wpdb->cp_ad_forms} SET" . " form_label    = %s," . " form_desc     = %s," . " form_cats     = %s," . " form_status   = %s," . " form_owner    = %s," . " form_modified = %s," . " cat_id = %s" . " WHERE id      = %s", appthemes_clean($_POST['form_label']), appthemes_clean($_POST['form_desc']), serialize($_POST['post_category']), appthemes_clean($_POST['form_status']), $_POST['form_owner'], gmdate('Y-m-d H:i:s', time() + get_option('gmt_offset') * 3600), $update_cat_id[1], $_GET['id']);
                // EKLEME BITIS
                $results = $wpdb->get_row($update);
                ?>

            <p style="text-align:center;padding-top:50px;font-size:22px;"><?php 
                _e('Saving your changes.....', 'appthemes');
                ?>
<br /><br /><img src="<?php 
                echo bloginfo('template_directory');
                ?>
/images/loader.gif" alt="" /></p>
            <meta http-equiv="refresh" content="0; URL=?page=layouts">

        <?php 
            } else {
                ?>

            <form method="post" id="mainform" action="">

            <?php 
                echo cp_admin_db_fields($options_new_form, 'cp_ad_forms', 'id');
                ?>

                <p class="submit"><input class="btn button-primary" name="save" type="submit" value="<?php 
                _e('Save changes', 'appthemes');
                ?>
" />&nbsp;&nbsp;&nbsp;
                <input name="cancel" type="button" onClick="location.href='?page=layouts'" value="<?php 
                _e('Cancel', 'appthemes');
                ?>
" /></p>
                <input name="submitted" type="hidden" value="yes" />
                <input name="form_owner" type="hidden" value="<?php 
                echo $current_user->user_login;
                ?>
" />

            </form>

        <?php 
            }
            // end isset $_POST
            ?>

        </div><!-- end wrap -->

    <?php 
            break;
            /**
             * Form Builder Page
             * Where fields are added to form layouts
             */
        /**
         * Form Builder Page
         * Where fields are added to form layouts
         */
        case 'formbuilder':
            ?>

        <div class="wrap">
        <div class="icon32" id="icon-themes"><br/></div>
        <h2><?php 
            _e('Erisim Formu Duzenleme', 'appthemes');
            ?>
</h2>

        <?php 
            cp_admin_info_box();
            ?>

        <?php 
            // add fields to page layout on left side
            if (isset($_POST['field_id'])) {
                // take selected checkbox array and loop through ids
                foreach ($_POST['field_id'] as $value) {
                    // @todo Change to Insert
                    $insert = $wpdb->prepare("INSERT INTO {$wpdb->cp_ad_meta}" . " (form_id, field_id) VALUES ( %s, %s)", appthemes_clean($_POST['form_id']), appthemes_clean($value));
                    $results = $wpdb->query($insert);
                }
                // end foreach
            }
            // end $_POST
            // update form layout positions and required fields on left side.
            if (isset($_POST['formlayout'])) {
                // loop through the post array and update the required checkbox and field position
                foreach ($_POST as $key => $value) {
                    // since there's some $_POST values we don't want to process, only give us the
                    // numeric ones which means it contains a meta_id and we want to update it
                    if (is_numeric($key)) {
                        // quick hack to prevent php "notice: undefined index:" msg when php strict warnings is on
                        if (!isset($value['field_req'])) {
                            $value['field_req'] = '';
                        }
                        if (!isset($value['field_search'])) {
                            $value['field_search'] = '';
                        }
                        $update = "UPDATE {$wpdb->cp_ad_meta} SET " . "field_req = '" . $wpdb->escape(appthemes_clean($value['field_req'])) . "', " . "field_search = '" . $wpdb->escape(appthemes_clean($value['field_search'])) . "' " . "WHERE meta_id ='" . $wpdb->escape($key) . "'";
                        $wpdb->query($update);
                    }
                    // end if_numeric
                }
                // end for each
                echo '<p class="info">' . __('Your changes have been saved.', 'appthemes') . '</p>';
            }
            // end isset $_POST
            // check to prevent php "notice: undefined index" msg when php strict warnings is on
            if (isset($_GET['del_id'])) {
                $theswitch = $_GET['del_id'];
            } else {
                $theswitch = '';
            }
            // Remove items from form layout
            if ($theswitch) {
                $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->cp_ad_meta} WHERE meta_id = %s", $_GET['del_id']));
            }
            // @todo Change to Update
            //update the forms modified date
            $update = $wpdb->prepare("UPDATE {$wpdb->cp_ad_forms} SET" . " form_modified = %s WHERE id = %s", gmdate('Y-m-d H:i:s', time() + get_option('gmt_offset') * 3600), $_GET['id']);
            $results = $wpdb->get_row($update);
            ?>


        <table>
            <tr style="vertical-align:top;">
                <td style="width:800px;padding:0 20px 0 0;">


                <h3><?php 
            _e('Erisim Form Ismi', 'appthemes');
            ?>
 - <?php 
            echo ucfirst(urldecode($_GET['title']));
            ?>
&nbsp;&nbsp;&nbsp;&nbsp;<span id="loading"></span></h3>

                <form method="post" id="mainform" action="">

                    <table class="widefat">
                        <thead>
                            <tr>
                                <th scope="col" colspan="2"><?php 
            _e('Erisim Formu Onizleme', 'appthemes');
            ?>
</th>
								<th scope="col" style="width:75px;text-align:center;" title="<?php 
            _e('Show field in the category refine search sidebar', 'appthemes');
            ?>
"><?php 
            _e('Gelismis Arama', 'appthemes');
            ?>
</th>
                                <th scope="col" style="width:75px;text-align:center;"><?php 
            _e('Gerekli Alan', 'appthemes');
            ?>
</th>
                                <th scope="col" style="width:75px;text-align:center;"><?php 
            _e('Kaldir', 'appthemes');
            ?>
</th>
                            </tr>
                        </thead>



                        <tbody class="sortable">

                        <?php 
            // If this is the first time this form is being customized then auto
            // create the core fields and put in cp_meta db table
            echo cp_add_core_fields($_GET['id']);
            // Then go back and select all the fields assigned to this
            // table which now includes the added core fields.
            $sql = $wpdb->prepare("SELECT f.field_label, f.field_name, f.field_type, f.field_values, f.field_perm, m.meta_id, m.field_pos, m.field_search, m.field_req, m.form_id " . "FROM {$wpdb->cp_ad_fields} f " . "INNER JOIN {$wpdb->cp_ad_meta} m " . "ON f.field_id = m.field_id " . "WHERE m.form_id = %s " . "ORDER BY m.field_pos asc", $_GET['id']);
            $results = $wpdb->get_results($sql);
            if ($results) {
                echo cp_admin_formbuilder($results);
            } else {
                ?>

                        <tr>
                            <td colspan="5" style="text-align: center;"><p><br/><?php 
                _e('No fields have been added to this form layout yet.', 'appthemes');
                ?>
<br/><br/></p></td>
                        </tr>

                        <?php 
            }
            // end $results
            ?>

                        </tbody>

                    </table>

                    <p class="submit">
                        <input class="btn button-primary" name="save" type="submit" value="<?php 
            _e('Save Changes', 'appthemes');
            ?>
" />&nbsp;&nbsp;&nbsp;
                        <input name="cancel" type="button" onClick="location.href='?page=layouts'" value="<?php 
            _e('Cancel', 'appthemes');
            ?>
" />
                        <input name="formlayout" type="hidden" value="yes" />
                        <input name="form_owner" type="hidden" value="<?php 
            $current_user->user_login;
            ?>
" />
                    </p>
                </form>

                </td>
                <td>

                <h3><?php 
            _e('Mevcut Erisim Alanlari', 'appthemes');
            ?>
</h3>

                <form method="post" id="mainform" action="">


                <div class="fields-panel">

                    <table class="widefat">
                        <thead>
                            <tr>
                                <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"/></th>
                                <th scope="col"><?php 
            _e('Alan Adi', 'appthemes');
            ?>
</th>
                                <th scope="col"><?php 
            _e('Alan Tipi', 'appthemes');
            ?>
</th>
                            </tr>
                        </thead>


                        <tbody>

                        <?php 
            // Select all available fields not currently on the form layout.
            // Also exclude any core fields since they cannot be removed from the layout.
            $sql = $wpdb->prepare("SELECT f.field_id,f.field_label,f.field_type " . "FROM {$wpdb->cp_ad_fields} f " . "WHERE f.field_id " . "NOT IN (SELECT m.field_id " . "FROM {$wpdb->cp_ad_meta} m " . "WHERE m.form_id =  %s) " . "AND f.field_perm <> '1'", $_GET['id']);
            $results = $wpdb->get_results($sql);
            if ($results) {
                foreach ($results as $result) {
                    ?>

                        <tr class="even">
                            <th class="check-column" scope="row"><input type="checkbox" value="<?php 
                    echo $result->field_id;
                    ?>
" name="field_id[]"/></th>
                            <td><?php 
                    echo esc_html(translate($result->field_label, 'appthemes'));
                    ?>
</td>
                            <td><?php 
                    echo $result->field_type;
                    ?>
</td>
                        </tr>

                        <?php 
                }
                // end foreach
            } else {
                ?>

                        <tr>
                            <td colspan="4" style="text-align: center;"><p><br /><?php 
                _e('No fields are available.', 'appthemes');
                ?>
<br /><br /></p></td>
                        </tr>

                        <?php 
            }
            // end $results
            ?>

                        </tbody>

                    </table>

                </div>

                    <p class="submit"><input class="btn button-primary" name="save" type="submit" value="<?php 
            _e('Forma Alan Ekle', 'appthemes');
            ?>
" /></p>
                        <input name="form_id" type="hidden" value="<?php 
            echo $_GET['id'];
            ?>
" />
                        <input name="submitted" type="hidden" value="yes" />


                </form>

                </td>
            </tr>
        </table>

    </div><!-- /wrap -->

    <?php 
            break;
        case 'delete':
            // delete the form based on the form id
            cp_delete_form($_GET['id']);
            ?>
        <p style="text-align:center;padding-top:50px;font-size:22px;"><?php 
            _e('Deleting form layout.....', 'appthemes');
            ?>
<br /><br /><img src="<?php 
            echo bloginfo('template_directory');
            ?>
/images/loader.gif" alt="" /></p>
        <meta http-equiv="refresh" content="0; URL=?page=layouts">

    <?php 
            break;
        default:
            $results = $wpdb->get_results($wpdb->prepare("SELECT * FROM {$wpdb->cp_ad_forms} ORDER BY id desc"));
            ?>

        <div class="wrap">
        <div class="icon32" id="icon-themes"><br /></div>
        <h2><?php 
            _e('Erisim Form Duzenlemesi', 'appthemes');
            ?>
&nbsp;<a class="button add-new-h2" href="?page=layouts&amp;action=addform">Yeni Form Ekle</a></h2>

        <?php 
            cp_admin_info_box();
            ?>

        <p class="admin-msg"><?php 
            _e('Formlar yeni bir Erisim Engeli eklenecegi sirada o Erisim Engelini olusturan alanlardan olusur. Her Erisim Kategorisine ozel Form ekleyebilirsiniz. Asagida tum Erisim Formlari listelenmektedir. Yeni bir Form ekleyebilir yada var olan Formu duzenleyebilirsiniz. Ayrica Formlara istediginiz alanlari ekleyebilirsiniz.', 'appthemes');
            ?>
</p>

        <table id="tblspacer" class="widefat fixed">

            <thead>
                <tr>
                    <th scope="col" style="width:35px;">&nbsp;</th>
                    <th scope="col"><?php 
            _e('Form Ismi', 'appthemes');
            ?>
</th>
                    <th scope="col"><?php 
            _e('Form Aciklamasi', 'appthemes');
            ?>
</th>
                    <th scope="col"><?php 
            _e('Erisim Kategorileri', 'appthemes');
            ?>
</th>
                    <th scope="col" style="width:150px;"><?php 
            _e('Degistiren', 'appthemes');
            ?>
</th>
                    <th scope="col" style="width:75px;"><?php 
            _e('Durumu', 'appthemes');
            ?>
</th>
                    <th scope="col" style="text-align:center;width:100px;"><?php 
            _e('Aksiyonlar', 'appthemes');
            ?>
</th>
                </tr>
            </thead>

            <?php 
            if ($results) {
                $rowclass = '';
                $i = 1;
                ?>

              <tbody id="list">

            <?php 
                foreach ($results as $result) {
                    $rowclass = 'even' == $rowclass ? 'alt' : 'even';
                    ?>

                <tr class="<?php 
                    echo $rowclass;
                    ?>
">
                    <td style="padding-left:10px;"><?php 
                    echo $i;
                    ?>
.</td>
                    <td><a href="?page=layouts&amp;action=editform&amp;id=<?php 
                    echo $result->id;
                    ?>
"><strong><?php 
                    echo $result->form_label;
                    ?>
</strong></a></td>
                    <td><?php 
                    echo $result->form_desc;
                    ?>
</td>
                    <td><?php 
                    echo cp_match_cats(unserialize($result->form_cats));
                    ?>
</td>
                    <td><?php 
                    echo mysql2date(get_option('date_format') . ' ' . get_option('time_format'), $result->form_modified);
                    ?>
 <?php 
                    _e('by', 'appthemes');
                    ?>
 <?php 
                    echo $result->form_owner;
                    ?>
</td>
                    <td><?php 
                    echo ucfirst($result->form_status);
                    ?>
</td>
                    <td style="text-align:center"><a href="?page=layouts&amp;action=formbuilder&amp;id=<?php 
                    echo $result->id;
                    ?>
&amp;title=<?php 
                    echo urlencode($result->form_label);
                    ?>
"><img src="<?php 
                    echo bloginfo('template_directory');
                    ?>
/images/layout_add.png" alt="<?php 
                    echo _e('Edit form layout', 'appthemes');
                    ?>
" title="Formu Duzenle" /></a>&nbsp;&nbsp;&nbsp;
                        <a href="?page=layouts&amp;action=editform&amp;id=<?php 
                    echo $result->id;
                    ?>
"><img src="<?php 
                    echo bloginfo('template_directory');
                    ?>
/images/edit.png" alt="<?php 
                    echo _e('Edit form properties', 'appthemes');
                    ?>
" title="Form Ozelliklerini Duzenle" /></a>&nbsp;&nbsp;&nbsp;
                        <a onclick="return confirmBeforeDelete();" href="?page=layouts&amp;action=delete&amp;id=<?php 
                    echo $result->id;
                    ?>
"><img src="<?php 
                    echo bloginfo('template_directory');
                    ?>
/images/cross.png" alt="<?php 
                    echo _e('Delete form layout', 'appthemes');
                    ?>
" title="Formu Sil" /></a></td>
                </tr>

              <?php 
                    $i++;
                }
                // end for each
                ?>

              </tbody>

            <?php 
            } else {
                ?>

                <tr>
                    <td colspan="7"><?php 
                _e('No form layouts found.', 'appthemes');
                ?>
</td>
                </tr>

            <?php 
            }
            // end $results
            ?>

            </table>


        </div><!-- end wrap -->

    <?php 
    }
    // end switch
    ?>
    <script type="text/javascript">
        /* <![CDATA[ */
            function confirmBeforeDelete() { return confirm("<?php 
    _e('Are you sure you want to delete this?', 'appthemes');
    ?>
"); }
            function confirmBeforeRemove() { return confirm("<?php 
    _e('Are you sure you want to remove this?', 'appthemes');
    ?>
"); }
        /* ]]> */
    </script>

<?php 
}