/public/application/models/company_model.php

https://gitlab.com/MichelZuniga/neoinvoice · PHP · 282 lines · 157 code · 22 blank · 103 comment · 33 complexity · 642964aea7e7f8e407b1108f380f3b60 MD5 · raw file

  1. <?php
  2. class Company_model extends Model {
  3. var $logo_hash = "SET-PASSWORD-SALT-HERE";
  4. function __construct() {
  5. parent::Model();
  6. }
  7. /**
  8. * @param int $company_id The ID of the company to be selected
  9. * @return array Associative array of extensive information regarding the company
  10. */
  11. function select_single($company_id) {
  12. #$data = $this->multicache->get("company:$company_id");
  13. #if (!$data) {
  14. $sql = "SELECT * FROM company WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  15. $query = $this->db->query($sql);
  16. $data = $query->row_array();
  17. $sql = "SELECT * FROM service WHERE id = {$data['service_id']} LIMIT 1";
  18. $query = $this->db->query($sql);
  19. $data['service'] = $query->row_array();
  20. $sql = "SELECT COUNT(id) AS count FROM user WHERE company_id = $company_id LIMIT 1";
  21. $query = $this->db->query($sql);
  22. $temp = $query->row_array();
  23. $data['user_count'] = $temp['count'];
  24. # $this->multicache->set("company:$company_id", $data);
  25. #}
  26. return $data;
  27. }
  28. /**
  29. * @param int $company_id The ID of the company to be selected
  30. * @return array Associative array of immediate information regarding the company
  31. */
  32. function select_single_simple($company_id) {
  33. #$data = $this->multicache->get("company:$company_id");
  34. #if (!$data) {
  35. $sql = "SELECT * FROM company WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  36. $query = $this->db->query($sql);
  37. $data = $query->row_array();
  38. # $this->multicache->set("company:$company_id", $data);
  39. #}
  40. return $data;
  41. }
  42. /**
  43. * @param int $company_id The ID of the company to be selected
  44. * @return array Associative array representing the company level preferences, or False if there is an error
  45. */
  46. function load_preferences($company_id) {
  47. #$company_prefs = $this->multicache->get("company_prefs:$company_id");
  48. #if (!$company_prefs) {
  49. $sql = "SELECT preferences FROM company WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  50. $query = $this->db->query($sql);
  51. if ($query->num_rows() > 0) {
  52. $row = $query->row();
  53. $company_prefs = json_decode($row->preferences, TRUE);
  54. } else {
  55. $company_prefs = FALSE;
  56. }
  57. #$this->multicache->set("company_prefs:$company_id", $company_prefs);
  58. #}
  59. return $company_prefs;
  60. }
  61. /**
  62. * @param int $company_id The ID of the company to be deleted
  63. * @return bool True or False depending on Success or Failure of deletion
  64. * @deprecated
  65. * @abstract This is used to delete companies, but we handle this functionality elsewhere
  66. */
  67. function delete($company_id) {
  68. $sql = "DELETE FROM company WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  69. if ($this->db->simple_query()) {
  70. $this->multicache->delete("company:$company_id");
  71. return $this->db->affected_rows();
  72. } else {
  73. return FALSE;
  74. }
  75. }
  76. /**
  77. * @param int $company_id The ID of the company to be marked for deletion
  78. * @return bool True or False depending on Success or Failure of deletion
  79. * @abstract This function is used to set a company to be deleted at a future date (handled by Cron) by adding a flag to the company db table row
  80. */
  81. function delete_mark($company_id) {
  82. $sql = "UPDATE company SET delete_date = (CURDATE() + INTERVAL 1 WEEK) WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  83. if ($this->db->simple_query($sql)) {
  84. $this->multicache->delete("company:$company_id");
  85. return $this->db->affected_rows();
  86. } else {
  87. return FALSE;
  88. }
  89. }
  90. /**
  91. * @param int $company_id The ID of the company whose future deletion event we want canceled
  92. * @return bool True or False depending on Success or Failure or the delete cancel
  93. * @abstract Cancels the future deletion date by removing the flag from the company table
  94. */
  95. function delete_cancel($company_id) {
  96. $sql = "UPDATE company SET delete_date = NULL WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  97. if ($this->db->simple_query($sql)) {
  98. $this->multicache->delete("company:$company_id");
  99. return $this->db->affected_rows();
  100. } else {
  101. return FALSE;
  102. }
  103. }
  104. /**
  105. * @param array $data Associative array of company information, ignores .id
  106. * @return mixed The ID of the newly added company, or False if there was an error
  107. * @abstract Creates a new company in our database
  108. */
  109. function insert($data) {
  110. if (isset($data['id'])) {
  111. unset($data['id']);
  112. }
  113. if ($this->db->insert('company', $data)) {
  114. return $this->db->insert_id();
  115. } else {
  116. return FALSE;
  117. }
  118. }
  119. /**
  120. * @param int $company_id The ID of the company to be updated
  121. * @param array $data Asociative array of company information, ignores .id
  122. * @return bool True or False depending on Success or Failure of company update
  123. * @abstract Updates a companies information in the database and deletes cache
  124. */
  125. function update($company_id, $data) {
  126. if (isset($data['id'])) {
  127. unset($data['id']);
  128. }
  129. $this->db->where('id', $company_id);
  130. if ($this->db->update('company', $data)) {
  131. /**
  132. * @todo update cache instead of deleting it
  133. */
  134. $this->multicache->delete("company:$company_id");
  135. return TRUE;
  136. } else {
  137. return FALSE;
  138. }
  139. }
  140. /**
  141. * @param int $company_id The ID of the company
  142. * @return bool True or False depending on Success or Failure of company update
  143. * @abstract Sets the companies modified time to now
  144. */
  145. function touch($company_id) {
  146. $sql = "UPDATE company SET modified = NOW() WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  147. $this->multicache->delete("company:$company_id");
  148. return $this->db->simple_query($sql);
  149. }
  150. /**
  151. * @param int $company_id The ID of the company to be updated
  152. * @param array $data Associated array of preference data to be updated
  153. * @return bool True or False depending on the Success or Failure of company update
  154. * @abstract Updates the companies preferences, stored in the database as JSON
  155. */
  156. function update_preferences($company_id, $data) {
  157. $prefs_json = json_encode($data);
  158. $sql = "UPDATE company SET preferences = '$prefs_json' WHERE id = " . $this->db->escape($company_id) . " LIMIT 1";
  159. if ($this->db->simple_query($sql)) {
  160. #$this->multicache->set("company_prefs:$company_id", $data);
  161. return TRUE;
  162. } else {
  163. return FALSE;
  164. }
  165. }
  166. /**
  167. * @param int $company_id The ID of the company to be updated
  168. * @param int $plan_id The ID of the plan the company is to be upgraded to
  169. * @param int $days The number of days the company is to be upgraded
  170. * @return <type> True or False depending on Success or Failure of upgrade
  171. * @abstract Upgrades (or downgrades) a company to the new plan level for the specified days
  172. */
  173. function upgrade_company($company_id, $plan_id, $days) {
  174. $company_id += 0;
  175. $plan_id += 0;
  176. $days += 0;
  177. if (!$company_id || !$plan_id || !$days) {
  178. return FALSE;
  179. }
  180. $sql = "SELECT * FROM company WHERE id = $company_id LIMIT 1";
  181. $query = $this->db->query($sql);
  182. $data = $query->row_array();
  183. if (!$data) {
  184. return FALSE;
  185. }
  186. $current_plan = $data['service_id'];
  187. $current_expire = $data['service_expire'];
  188. if (!$current_expire || $current_expire == '0000-00-00') {
  189. $current_expire = date('Y-m-d');
  190. }
  191. $sql = "UPDATE company SET service_id = $plan_id, service_expire = DATE_ADD('$current_expire', INTERVAL $days DAY) WHERE id = $company_id LIMIT 1";
  192. return $this->db->simple_query($sql);
  193. }
  194. /**
  195. * @param string $coupon_code The coupon code (e.g. BETATEST2010)
  196. * @return array Associative array of coupon information
  197. * @abstract Gets information from a coupon based on it's code, used during signup
  198. */
  199. function coupon_data_from_name($coupon_code) {
  200. $sql = "SELECT * FROM coupon WHERE name = " . $this->db->escape($coupon_code) . " LIMIT 1";
  201. $query = $this->db->query($sql);
  202. $data = $query->row_array();
  203. if (!$data) {
  204. return FALSE;
  205. }
  206. return $data;
  207. }
  208. /**
  209. * @return int Number of all companies using NeoInvoice
  210. * @abstract This is used for overall NeoInvoice statistics
  211. */
  212. function count_all() {
  213. return $this->db->count_all('company');
  214. }
  215. /**
  216. * @abstract Use this function to set a company logo (overwrite's existing)
  217. * @param int $company_id The ID of the company
  218. * @param int $uploaded_filename The file path to the newly uploaded image
  219. * @return File path to new logo image
  220. */
  221. function set_logo_image($company_id, $uploaded_filename) {
  222. $filename = $this->_get_filename_from_company_id($company_id);
  223. $size = getimagesize($uploaded_filename);
  224. if ($size[0] >= 16 && $size[0] <= 1000 && $size[1] >= 16 && $size[1] <= 400 && $size['mime'] == 'image/jpeg' && (filesize($uploaded_filename) <= 512 * 1024) ) {
  225. return move_uploaded_file($uploaded_filename, $filename);
  226. }
  227. return false;
  228. }
  229. /**
  230. * @abstract Use this function to get the path to the logo image
  231. * @param int $company_id The ID of the company
  232. * @return string The path to the company logo image, or false if one does not exist
  233. */
  234. function get_logo_image($company_id) {
  235. $filename = $this->_get_filename_from_company_id($company_id);
  236. if (file_exists($filename)) {
  237. return $filename;
  238. } else {
  239. return FALSE;
  240. }
  241. }
  242. /**
  243. * @abstract This function will delete the companies image
  244. * @param int $company_id The ID of the company
  245. * @return bool A true if the image was deleted, a false if otherwise
  246. */
  247. function remove_logo_image($company_id) {
  248. $filename = $this->_get_filename_from_company_id($company_id);
  249. return unlink($filename);
  250. }
  251. /**
  252. * @abstract Used to hash out the filename based on the company id
  253. * @param int $company_id The ID of the company
  254. * @return string The path to the image (even if it doesn't exist yet)
  255. */
  256. private function _get_filename_from_company_id($company_id) {
  257. $company_id += 0;
  258. return "assets/logos/$company_id-" . md5($this->logo_hash . $company_id) . ".jpg";
  259. }
  260. }