Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. <?php
  2.  
  3. defined('BASEPATH') OR exit('No direct script access allowed');
  4.  
  5. // Menu Model + Crud Controller
  6. class Receiving extends Application
  7. {
  8. function __construct()
  9. {
  10. parent::__construct();
  11. $this->load->helper('formfields_helper');
  12. $this->error_messages = array();
  13. }
  14.  
  15. /**
  16. * Receiving Page for this controller.
  17. *
  18. * Maps to the following URL
  19. * http://example.com/welcome/receiving
  20. */
  21. public function index()
  22. {
  23. // Handle user-role to lock out certain types of users
  24. $userrole = $this->session->userdata('userrole');
  25. if ($userrole != 'admin') {
  26. $message = 'You are not authorized to access this page. Go away';
  27. $this->data['content'] = $message;
  28. $this->render();
  29. return;
  30. }
  31.  
  32.  
  33. // build the list of items, to pass on to our view
  34. /*$source = $this->supplies->all();
  35. $items = array ();
  36. foreach ($source as $record)
  37. {
  38. $items[] = array ('name' => $record['name'], 'receiving' => $record['receiving'], 'href' => $record['where']);
  39. }*/
  40. // this is the view we want shown
  41. $this->data['pagebody'] = 'receiving_view';
  42. $this->data['items'] = $this->supplies->all();
  43. $this->render();
  44. }
  45.  
  46. function edit($id = null)
  47. {
  48.  
  49. // try the session first
  50. $key = $this->session->userdata('key');
  51. $record = $this->session->userdata('record');
  52.  
  53.  
  54. if(empty($record)){
  55. $record = $this->supplies->get($id);
  56. $key = $id;
  57. $this->session->set_userdata('key',$id);
  58. $this->session->set_userdata('record',$record);
  59.  
  60. }
  61. //$this->data['content'] = "Looking at " . $key . ': ' . $record->name;
  62. $this->data['action'] = (empty($key)) ? 'Adding' : 'Editing';
  63. // build the form fields
  64. // $this->data['items'] = $this->supplies->get($id);
  65.  
  66. $this->data['fid'] = makeTextField('Id', 'id', $record->id);
  67. $this->data['fname'] = makeTextField('Name', 'name', $record->name);
  68. $this->data['fonhand'] = makeTextField('On Hand amount, each', 'qty_onhand', $record->qty_onhand);
  69. $this->data['freceiving'] = makeTextField('Receiving amount, each', 'qty_inventory', $record->qty_inventory);
  70. $this->data['fprice'] = makeTextField('Price, each', 'price', $record->price);
  71.  
  72.  
  73.  
  74. // show the editing form
  75. $this->data['pagebody'] = "inventory_view";
  76. $this->data['zsubmit'] = makeSubmitButton('Save', 'Submit changes');
  77. $this->show_any_errors();
  78. $this->render();
  79. }
  80. function cancel()
  81. {
  82. $this->session->unset_userdata('key');
  83. $this->session->unset_userdata('record');
  84. $this->index();
  85. }
  86. function save() {
  87. // try the session first
  88. $key = $this->session->userdata('key');
  89. $record = $this->session->userdata('record');
  90. // if not there, nothing is in progress
  91. if (empty($record)) {
  92. $this->index();
  93. return;
  94. }
  95.  
  96. // update our data transfer object
  97. $incoming = $this->input->post();
  98. foreach(get_object_vars($record) as $key=> $value)
  99. if (isset($incoming[$key]))
  100. $record->$key = $incoming[$key];
  101. $this->session->set_userdata('record',$record);
  102.  
  103. // validate
  104. $this->load->library('form_validation');
  105. $this->form_validation->set_rules($this->supplies->rules());
  106. if ($this->form_validation->run() != TRUE)
  107. $this->error_messages = $this->form_validation->error_array();
  108.  
  109. // check menu code for additions
  110. if ($key == null)
  111. if ($this->supplies->exists($record->id))
  112. $this->error_messages[] = 'Duplicate id adding new menu item';
  113. /* if (! $this->categories->exists($record->category))
  114. $this->error_messages[] = 'Invalid category code: ' . $record->category;*/
  115.  
  116. // save or not
  117. if (! empty($this->error_messages)) {
  118. $this->edit();
  119. return;
  120. }
  121.  
  122. // update our table, finally!
  123. if ($key == null)
  124. $this->supplies->add($record);
  125. else
  126. $this->supplies->update($record);
  127. // and redisplay the list
  128. $this->index();
  129. }
  130.  
  131. function show_any_errors() {
  132. $result = '';
  133. if (empty($this->error_messages)) {
  134. $this->data['error_messages'] = '';
  135. return;
  136. }
  137. // add the error messages to a single string with breaks
  138. foreach($this->error_messages as $onemessage)
  139. $result .= $onemessage . '<br/>';
  140. // and wrap these per our view fragment
  141. $this->data['error_messages'] = $this->parser->parse('mtce-errors',
  142. ['error_messages' => $result], true);
  143. }
  144. function delete() {
  145. $key = $this->session->userdata('key');
  146. $record = $this->session->userdata('record');
  147. // only delete if editing an existing record
  148. if (! empty($record)) {
  149. $this->supplies->delete($key);
  150. }
  151. $this->index();
  152. }
  153. function add() {
  154. $key = NULL;
  155. $record = $this->supplies->create();
  156. $this->session->set_userdata('key', $key);
  157. $this->session->set_userdata('record', $record);
  158. $this->edit();
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement