/** * RESTORE an item * * (the model migth allow soft deletes!) * * Make sure the new sequence number fits sequentially into * the list of sequence numbers of the existing items for a plan * and that all current sequence numbers are in 1.0 steps * * @param object $items * @param number $new_seq_no */ function restoreItem($id) { // this item should be restored $item = Item::onlyTrashed()->find($id); if (!$item) { return false; } $item->restore(); // get all items of the related plan $plan = Plan::find($item->plan_id); $items = $plan->items()->orderBy('seq_no')->get(); // numbering them countering with 1.0 $counter = 1.0; foreach ($items as $item) { if ($item->seq_no != $counter) { $i = Item::find($item->id); # get the actual DB record $item->seq_no = $counter; # update the current selection $i->seq_no = $counter; # update the seq_no $i->save(); # save the record } $counter += 1.0; } return true; }
/** * RESTORE all trashed items of a plan * */ public function restoreAllTrashed($plan_id) { // this item should be restored $items = Item::onlyTrashed()->where('plan_id', $plan_id)->get(); if (!$items) { return false; } // restore all items and try to restore their correct sequence number foreach ($items as $item) { restoreItem($item->id); } flash('All trashed items restored. Please review the sequence!'); return \Redirect::back(); }
/** * PLAN DETAILS form * * @param int $id * @param int $new_item_id indicates a newly inserted item * @return \Illuminate\Http\Response */ public function edit($id) { // find a single plan by ID $plan = Plan::with(['items' => function ($query) { $query->withTrashed()->orderBy('seq_no'); }])->find($id); if ($plan) { // get list of service types $types = Type::get(); // get list of users $users = User::orderBy('first_name')->get(); // get list of trashed items (if any) $trashedItemsCount = Item::onlyTrashed()->where('plan_id', $id)->count(); // check if a new item was just now inserted (used for highlighing in the view) $newest_item_id = 0; if (session()->has('newest_item_id')) { $newest_item_id = session()->get('newest_item_id'); session()->forget('newest_item_id'); } // get service times from plan dates $plan->start = Carbon::instance($plan->date)->toTimeString(); // for backwards compatibility, we allowed for null as end date if ($plan->date_end) { $plan->end = Carbon::instance($plan->date_end)->toTimeString(); } else { $plan->end = "23:59"; } return view($this->view_one, array('plan' => $plan, 'types' => $types, 'users' => $users, 'versionsEnum' => json_decode(env('BIBLE_VERSIONS')), 'newest_item_id' => $newest_item_id, 'trashedItemsCount' => $trashedItemsCount)); } flashError('Plan with id "' . $id . '" not found'); return \Redirect::route($this->view_idx); }