Exemplo n.º 1
0
 /**
  * Remove items from the plan
  */
 public function plan_remove_items()
 {
     //check nounce and clear buffer
     $data = $this->start_ajax('plan_item_manager', '_plan_item_manager_nounce');
     try {
         $arguments = array_key_exists('data', $_POST) ? $_POST['data'] : null;
         if ($arguments) {
             $arguments = ForestUtils::get_instance()->json_decode($arguments);
             $arguments = $this->parse_plan_args($arguments);
             $plan_id = $arguments['plan_id'];
             $post_id = $arguments['id'];
             $query = new PlanDbQuery($plan_id);
             $query_result = $query->delete_plan_item($post_id);
             if ($query_result) {
                 $post_type = $arguments['filter']->post_types;
                 $search_term = $arguments['filter']->search_term;
                 $current_page = $arguments['current_page'];
                 $sort = $arguments['sort'];
                 $items = $query->get_plan_items_by_type($post_type, $search_term, $current_page, $sort);
                 $data['message'] = _tfm('Item removed.');
                 $data['type'] = 'success';
                 $data['posts'] = $items;
                 $data['currentPage'] = $current_page;
                 $data['pageCount'] = $query->max_num_pages;
                 $data['action'] = $this->get_ajax_action(__FUNCTION__);
                 //has to match the html tab id
             } else {
                 $data['message'] = _tfm('Item was not removed from the database');
                 $data['type'] = 'error';
                 $data['action'] = $this->get_ajax_action(__FUNCTION__);
             }
         } else {
             $data['type'] = 'error';
             $data['message'] = _tfm('Error adding post item.');
         }
     } catch (Exception $e) {
         $data['type'] = 'error';
         $data['message'] = _tfm('Error adding item. Server error was:') . $e->getMessage();
     }
     //add error bufferd to data and json encode
     echo $this->end_ajax($data);
     die;
     // this is required to return a proper result
 }
        /**
         * Output the HTML for the Plan Manager metabox
         * @param $post
         */
        public function plan_manager_meta_box($post)
        {
            if (!class_exists('ForestTemplateLoader')) {
                $this->admin_error_notice(__("Missing the forest ajax plugin."));
                return;
            }
            $screen = get_current_screen();
            $is_new = isset($screen) && $screen->id == 'forest_plan' && $screen->action == 'add' && $screen->base == 'post';
            //Security nounce
            wp_nonce_field('plan_item_manager', '_plan_item_manager_nounce');
            ?>

			<!-- wrapper -->
			<div id="plan_manager_wrapper">
				<div>

					<!-- Nav tabs -->
					<ul id="plan-tabs" class="nav nav-tabs" role="tablist">
						<li role="presentation">&nbsp;</li>
						<li role="presentation" class="active"><a href="#overview"  aria-controls="overview" role="tab" data-toggle="tab"><?php 
            _etfm('Overview');
            ?>
</a></li>
						<li role="presentation"><a href="#edit" aria-controls="edit" role="tab" data-toggle="tab"><?php 
            _etfm('Edit');
            ?>
</a></li>
						<li role="presentation"><a href="#maps" aria-controls="maps" role="tab" data-toggle="tab"><?php 
            _etfm('Maps');
            ?>
</a></li>
						<li role="presentation"><a href="#reports" aria-controls="reports" role="tab" data-toggle="tab"><?php 
            _etfm('Reports');
            ?>
</a></li>
						<li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab"><?php 
            _etfm('Settings');
            ?>
</a></li>
					</ul>

					<!-- Tab panes -->
					<div class="tab-content">

						<!-- OVERVIEW -->
						<div role="tabpanel" class="tab-pane active in" id="overview">

								<div class="page-header">
								<h1><?php 
            _etfm('Plan Overview');
            ?>
</h1>
							</div>

						</div>

						<!-- EDIT -->
						<div role="tabpanel" class="tab-pane" id="edit">
							<div class="page-header">
								<h1><?php 
            _etfm('Add & Remove Items');
            ?>
</h1>
							</div>

							<div id="edit-tab-filter" class="filter-box bg-info form-inline">
								<span><?php 
            _e('Filter', 'forest-manager');
            ?>
</span>
								<div class="form-group">
									<label for="plan-title-search" class="sr-only">Title</label>
									<input type="text" class="search-term form-control input-sm" placeholder="Name"/>
								</div>
								<div class="form-group">
									<label class="sr-only" for="plan-item-type">Item Type</label>
									<input type="text" class="post-type-selection selectize-selection form-control input-sm" placeholder="<?php 
            _etfm('Select Post Types');
            ?>
" value=""/>
								</div>

								<button class="btn btn-primary search btn-sm" title="<?php 
            _e('Click to filter', 'forest-manager');
            ?>
">
									<i class="fa fa-filter"></i></button>
								<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#add-items-modal">
									<i class="fa fa-plus"></i>
								</button>

								<div class="btn-group alignright">
									<button type="button" class="btn btn-default dropdown-toggle disabled btn-sm" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
										<?php 
            _etfm('Actions');
            ?>
 <span class="caret"></span>
									</button>
									<ul class="dropdown-menu">
										<li><a class="edit" href="#"><?php 
            _etfm('Edit');
            ?>
</a></li>
										<li><a class="lock" href="#"><?php 
            _etfm('Lock/Unlock');
            ?>
</a></li>
										<li role="separator" class="divider"></li>
										<li><a class="remove" href="#"><?php 
            _etfm('Remove');
            ?>
</a></li>
									</ul>
								</div>
							</div>

							<!--Main Plan Items Table-->
							<table class="post-type-table table table-hover table-condensed table-striped plan-items" data-filter-id="edit-tab-filter" data-chk-id="chk_item_select">

								<caption class="notice">
									<div class="river-waiting">
										<span class="spinner"></span>
									</div>
									<em class="query-notice-default"><?php 
            _e('Loaded recent objects. Enter a filter to search for more...', 'forest-manager');
            ?>
</em>
								</caption>

								<thead>
									<?php 
            $this->column_headers();
            ?>
								</thead>

								<tbody>

								<?php 
            //Get post id for plan. If new -1.
            $plan_id = array_key_exists('post', $_GET) ? $_GET['post'] : -1;
            //get recent plan objects
            $query = new PlanDbQuery($plan_id);
            $items = $query->get_plan_items_by_type();
            ?>

								<?php 
            if ($query->have_items()) {
                ?>

									<!-- the loop -->
									<?php 
                foreach ($items as $item) {
                    ?>

										<tr>
											<td>
												<input type="radio" name="chk_item_select" value="<?php 
                    echo $item->ID;
                    ?>
"/>
											</td>

											<td>
												<?php 
                    echo $item->info;
                    ?>
											</td>

											<td>
												<?php 
                    echo sprintf('<a href="%s">%s</a>', $item->edit_url, $item->post_title);
                    ?>
											</td>

											<td>
												<?php 
                    echo $item->post_date;
                    ?>
											</td>

											<td>
												<?php 
                    echo $item->post_author_name;
                    ?>
											</td>

										</tr>

									<?php 
                }
                ?>
									<!-- end of the loop -->

								<?php 
            } else {
                ?>
									<tr><td colspan="<?php 
                echo count($this->columns) + 1;
                ?>
" class="no-items"><?php 
                _etfm('Sorry, no items found');
                ?>
</td></tr>
								<?php 
            }
            ?>

								</tbody>

								<tfoot>
								<tr>

									<td colspan="6">
										<div class="pagination-container">
											<?php 
            $this->bootstrap_pagination($query);
            ?>
										</div>
									</td>

								</tr>
								</tfoot>
							</table>

							<?php 
            wp_reset_postdata();
            ?>

							<!-- Modal Dialog-->
							<div class="modal fade" id="add-items-modal" tabindex="-1" role="dialog" aria-labelledby="add-title-label">
								<div class="modal-dialog" role="document">
									<div class="modal-content">
										<div class="modal-header">
											<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
											<h4 class="modal-title" id="add-title-label"><?php 
            _e('The Title', 'forest-manager');
            ?>
</h4>
										</div>

										<div class="modal-body">

											<div id="add-items-filter" class="filter-box bg-info form-inline">

												<span><?php 
            _e('Filter', 'forest-manager');
            ?>
</span>
												<div class="form-group">
													<label for="plan-title-search" class="sr-only">Title</label>
													<input type="text" class="search-term form-control input-sm" placeholder="Name"/>
												</div>
												<div class="form-group">
													<label class="sr-only" for="plan-item-type">Item Type</label>
													<input type="text" class="post-type-selection selectize-selection form-control input-sm" placeholder="<?php 
            _etfm('Select Post Type');
            ?>
" value=""/>
												</div>
												<button type="submit" class="btn btn-primary btn-sm"><i class="fa fa-filter"></i></button>

											</div>

											<div class="plan-items">

												<table class="post-type-table table table-hover table-condensed table-striped" data-filter-id="add-items-filter" data-chk-id="chk_new_item_select">

													<caption>
														<div class="river-waiting">
															<span class="spinner"></span>
														</div>
														<em><?php 
            _e('Loading...', 'forest-manager');
            ?>
</em>
													</caption>

													<thead>
														<?php 
            $this->column_headers();
            ?>
													</thead>

													<tbody>
													<tr>
														<td colspan="6" class="message no-items"><?php 
            _e('No items', 'forest-manager');
            ?>
</td>
													</tr>
													<?php 
            //The rows and cells are added in the JS using Ajax call.
            //@see: plan-manager.js
            ?>
													</tbody>

													<tfoot class="hidden">
													<tr>
														<td colspan="6">
															<div class="pagination-container">
																<?php 
            //The pagination control is created using js.
            //@see:plan-manager.js
            ?>
															</div>
														</td>
													</tr>
													</tfoot>
												</table>

											</div>

										</div><!--modal body end-->

										<div class="modal-footer">
											<button type="button" class="btn btn-default btn-sm" data-dismiss="modal"><?php 
            _e('Close', 'forest-manager');
            ?>
</button>
											<button type="button" class="btn btn-primary btn-sm"><?php 
            _e('Add Item', 'forest-manager');
            ?>
</button>
										</div>
									</div>
								</div>
							</div>

						</div>

						<div role="tabpanel" class="tab-pane" id="maps">

							<div class="page-header">
								<h1><?php 
            _etfm('Plan Map');
            ?>
</h1>
							</div>

						</div>

						<div role="tabpanel" class="tab-pane" id="reports">

							<div class="page-header">
								<h1><?php 
            _etfm('Reports');
            ?>
</h1>
							</div>

						</div>

						<div role="tabpanel" class="tab-pane" id="settings">

							<div class="page-header">
								<h1><?php 
            _etfm('Edit Settings');
            ?>
</h1>
							</div>
							<div class="form-horizontal row">
								<div class="form-group">
									<label for="post_title" class="col-sm-2 control-label"><?php 
            _etfm('Title');
            ?>
</label>
									<div class="col-sm-6 less-padding">
										<input type="text" class="form-control input-sm" data-id="post_title" value="<?php 
            echo $this->plan->post_title;
            ?>
" placeholder="<?php 
            _etfm('Enter plan title here');
            ?>
">
									</div>
									<div class="col-sm-4 less-padding">
										<span class="text-muted help-txt"></span>
									</div>
								</div>
								<div class="form-group">
									<label for="post-types" class="col-sm-2 control-label"><?php 
            _etfm('Item types');
            ?>
</label>
									<div class="col-sm-6 less-padding">
										<input type="text" class="post-type-selection selectize-selection form-control input-sm" data-meta-id="supported_post_types" value="<?php 
            echo $this->plan->meta->supported_post_types->to_string();
            ?>
" placeholder="<?php 
            _etfm('Select item types');
            ?>
">
									</div>
									<div class="col-sm-4 less-padding">
							            <span class="text-muted help-txt">
							                <?php 
            _etfm('Items types allowed in this plan.');
            ?>
							            </span>
									</div>
								</div>
							</div>


						</div>
					</div>

				</div>

			</div><!--end wrapper-->

			<?php 
            //the text translations...for js
            $planMngrLabels = array('loading' => _tfm('Loading...'), 'ServerError' => _tfm('Server error. Please try again.'), 'ErrorSaving' => _tfm('Error saving plan. Please try and check plan settings and try again.'), 'PleaseSelect' => _tfm('Please select'), 'NoItems' => _tfm('Sorry, no items found'), 'MissingSearchCreteria' => _tfm('Missing Search Creteria.'), 'MissingItemType' => _tfm('Missing Item Type.'), 'ItemsFetched' => _tfm('items fetched'), 'ItemAdded' => _tfm('item added'), 'ItemRemoved' => _tfm('item removed'), 'Loading' => _tfm('Loading...'), 'Saving' => _tfm('Saving...'), 'NothingFound' => _tfm('No items found for the given search term'));
            if ($is_new) {
                //translations for the wizard
                $createWizLabels = array('UntitledPlan' => _tfm('Untitled Plan'), 'ServerError' => _tfm('Server error. Please try again.'), 'ErrorSaving' => _tfm('Error saving plan. Please try and check plan settings and try again.'), 'PleaseSelect' => _tfm('Please select'), 'NoItems' => _tfm('No Items'), 'MissingItemType' => _tfm('Missing Item Type.'));
            }
            ?>

			<script type="application/javascript">
				jQuery(document).ready(function () {
					try {
						var plan_manager_obj = new PlanManagerMetaboxClass(
							<?php 
            echo $post->ID;
            ?>
,
							<?php 
            echo json_encode($this->columns);
            ?>
,
							<?php 
            echo '{sort_col_id:\'post_date\', sort_dir:\'DESC\'}';
            ?>
,
							'<?php 
            echo get_the_title();
            ?>
',
							<?php 
            echo $this->get_plan_object_types_list_js_array();
            ?>
,
							<?php 
            echo json_encode($planMngrLabels);
            ?>
						);
						plan_manager_obj.init();
					}
					catch(e){
						console.log(e.message);
					}
				});
			</script>

			<?php 
        }