supplier = model('Supplier'); } public function getIndex(): void { helper('tabular'); $data['table_headers'] = get_suppliers_manage_table_headers(); echo view('people/manage', $data); } /** * Gets one row for a supplier manage table. This is called using AJAX to update one row. * @param $row_id * @return void */ public function getRow($row_id): void { $data_row = get_supplier_data_row($this->supplier->get_info($row_id)); $data_row['category'] = $this->supplier->get_category_name($data_row['category']); echo json_encode($data_row); } /** * Returns Supplier table data rows. This will be called with AJAX. * @return void */ public function getSearch(): void { $search = $this->request->getVar('search', FILTER_SANITIZE_STRING); $limit = $this->request->getVar('limit', FILTER_SANITIZE_NUMBER_INT); $offset = $this->request->getVar('offset', FILTER_SANITIZE_NUMBER_INT); $sort = $this->request->getVar('sort', FILTER_SANITIZE_STRING); $order = $this->request->getVar('order', FILTER_SANITIZE_STRING); $suppliers = $this->supplier->search($search, $limit, $offset, $sort, $order); $total_rows = $this->supplier->get_found_rows($search); $data_rows = []; foreach($suppliers->getResult() as $supplier) { $row = get_supplier_data_row($supplier); $row['category'] = $this->supplier->get_category_name($row['category']); $data_rows[] = $row; } echo json_encode (['total' => $total_rows, 'rows' => $data_rows]); } /* Gives search suggestions based on what is being searched for */ public function suggest(): void { $suggestions = $this->supplier->get_search_suggestions($this->request->getVar('term', FILTER_SANITIZE_STRING), TRUE); echo json_encode($suggestions); } public function suggest_search() { $suggestions = $this->supplier->get_search_suggestions($this->request->getPost('term', FILTER_SANITIZE_STRING), FALSE); echo json_encode($suggestions); } /* Loads the supplier edit form */ public function getView(int $supplier_id = NEW_ENTRY): void { $info = $this->supplier->get_info($supplier_id); foreach(get_object_vars($info) as $property => $value) { $info->$property = $value; } $data['person_info'] = $info; $data['categories'] = $this->supplier->get_categories(); echo view("suppliers/form", $data); } /* Inserts/updates a supplier */ public function postSave(int $supplier_id = NEW_ENTRY): void { $first_name = $this->request->getPost('first_name', FILTER_SANITIZE_STRING); //TODO: Duplicate code $last_name = $this->request->getPost('last_name', FILTER_SANITIZE_STRING); $email = strtolower($this->request->getPost('email', FILTER_SANITIZE_EMAIL)); // format first and last name properly $first_name = $this->nameize($first_name); $last_name = $this->nameize($last_name); $person_data = [ 'first_name' => $first_name, 'last_name' => $last_name, 'gender' => $this->request->getPost('gender', FILTER_SANITIZE_STRING), 'email' => $email, 'phone_number' => $this->request->getPost('phone_number', FILTER_SANITIZE_STRING), 'address_1' => $this->request->getPost('address_1', FILTER_SANITIZE_STRING), 'address_2' => $this->request->getPost('address_2', FILTER_SANITIZE_STRING), 'city' => $this->request->getPost('city', FILTER_SANITIZE_STRING), 'state' => $this->request->getPost('state', FILTER_SANITIZE_STRING), 'zip' => $this->request->getPost('zip', FILTER_SANITIZE_STRING), 'country' => $this->request->getPost('country', FILTER_SANITIZE_STRING), 'comments' => $this->request->getPost('comments', FILTER_SANITIZE_STRING) ]; $supplier_data = [ 'company_name' => $this->request->getPost('company_name', FILTER_SANITIZE_STRING), 'agency_name' => $this->request->getPost('agency_name', FILTER_SANITIZE_STRING), 'category' => $this->request->getPost('category', FILTER_SANITIZE_STRING), 'account_number' => $this->request->getPost('account_number') == '' ? NULL : $this->request->getPost('account_number', FILTER_SANITIZE_STRING), 'tax_id' => $this->request->getPost('tax_id', FILTER_SANITIZE_NUMBER_INT) ]; if($this->supplier->save_supplier($person_data, $supplier_data, $supplier_id)) { //New supplier if($supplier_id == NEW_ENTRY) { echo json_encode ([ 'success' => TRUE, 'message' => lang('Suppliers.successful_adding') . ' ' . $supplier_data['company_name'], 'id' => $supplier_data['person_id'] ]); } else //Existing supplier { echo json_encode ([ 'success' => TRUE, 'message' => lang('Suppliers.successful_updating') . ' ' . $supplier_data['company_name'], 'id' => $supplier_id]); } } else//failure { echo json_encode ([ 'success' => FALSE, 'message' => lang('Suppliers.error_adding_updating') . ' ' . $supplier_data['company_name'], 'id' => NEW_ENTRY ]); } } /* This deletes suppliers from the suppliers table */ public function postDelete(): void { $suppliers_to_delete = $this->request->getPost('ids', FILTER_SANITIZE_NUMBER_INT); if($this->supplier->delete_list($suppliers_to_delete)) { echo json_encode ([ 'success' => TRUE, 'message' => lang('Suppliers.successful_deleted') . ' ' . count($suppliers_to_delete) . ' ' . lang('Suppliers.one_or_multiple') ]); } else { echo json_encode (['success' => FALSE, 'message' => lang('Suppliers.cannot_be_deleted')]); } } }