PageRenderTime 78ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/models/eimadmin/CustomExport.php

https://bitbucket.org/wildanm/orangehrm
PHP | 481 lines | 302 code | 92 blank | 87 comment | 32 complexity | 77decefa86a82464d2a66b5761574928 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, AGPL-3.0, BSD-3-Clause, AGPL-1.0, GPL-2.0, LGPL-2.1, LGPL-3.0
  1. <?php
  2. /**
  3. * OrangeHRM is a comprehensive Human Resource Management (HRM) System that captures
  4. * all the essential functionalities required for any enterprise.
  5. * Copyright (C) 2006 OrangeHRM Inc., http://www.orangehrm.com
  6. *
  7. * OrangeHRM is free software; you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by the Free Software Foundation; either
  9. * version 2 of the License, or (at your option) any later version.
  10. *
  11. * OrangeHRM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  12. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. * See the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along with this program;
  16. * if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA
  18. *
  19. */
  20. require_once ROOT_PATH.'/lib/dao/DMLFunctions.php';
  21. require_once ROOT_PATH.'/lib/dao/SQLQBuilder.php';
  22. require_once ROOT_PATH.'/lib/confs/sysConf.php';
  23. require_once ROOT_PATH.'/lib/common/CommonFunctions.php';
  24. require_once ROOT_PATH . '/lib/common/UniqueIDGenerator.php';
  25. require_once ROOT_PATH . '/lib/models/eimadmin/CustomFields.php';
  26. class CustomExport {
  27. /**
  28. * Table Name
  29. */
  30. const TABLE_NAME = 'hs_hr_custom_export';
  31. const DB_FIELDS_ID = 'export_id';
  32. const DB_FIELDS_NAME = 'name';
  33. const DB_FIELDS_FIELDS = 'fields';
  34. const DB_FIELDS_HEADINGS = 'headings';
  35. /** CSV Field name constants */
  36. const FIELD_EMPID = 'empId';
  37. const FIELD_LASTNAME = 'lastName';
  38. const FIELD_FIRSTNAME = 'firstName';
  39. const FIELD_MIDDLENAME = 'middleName';
  40. const FIELD_STREET1 = 'street1';
  41. const FIELD_STREET2 = 'street2';
  42. const FIELD_CITY = 'city';
  43. const FIELD_STATE = 'state';
  44. const FIELD_ZIP = 'zip';
  45. const FIELD_GENDER = 'gender';
  46. const FIELD_BIRTHDATE = 'birthDate';
  47. const FIELD_SSN = 'ssn';
  48. const FIELD_EMPSTATUS = 'empStatus';
  49. const FIELD_JOINEDDATE = 'joinedDate';
  50. const FIELD_WORKSTATION = 'workStation';
  51. const FIELD_LOCATION = 'location';
  52. const FIELD_CUSTOM1 = 'custom1';
  53. const FIELD_CUSTOM2 = 'custom2';
  54. const FIELD_CUSTOM3 = 'custom3';
  55. const FIELD_CUSTOM4 = 'custom4';
  56. const FIELD_CUSTOM5 = 'custom5';
  57. const FIELD_CUSTOM6 = 'custom6';
  58. const FIELD_CUSTOM7 = 'custom7';
  59. const FIELD_CUSTOM8 = 'custom8';
  60. const FIELD_CUSTOM9 = 'custom9';
  61. const FIELD_CUSTOM10 = 'custom10';
  62. const FIELD_WORKSTATE = 'workState';
  63. const FIELD_SALARY = 'salary';
  64. const FIELD_PAYFREQUENCY = 'payFrequency';
  65. const FIELD_FITWSTATUS = 'FITWStatus';
  66. const FIELD_FITWEXCEMPTIONS = 'FITWExemptions';
  67. const FIELD_SITWSTATE = 'SITWState';
  68. const FIELD_SITWSTATUS = 'SITWStatus';
  69. const FIELD_SITWEXCEMPTIONS = 'SITWExemptions';
  70. const FIELD_SUISTATE = 'SUIState';
  71. const FIELD_DD1ROUTING = 'DD1Routing';
  72. const FIELD_DD1ACCOUNT = 'DD1Account';
  73. const FIELD_DD1AMOUNT = 'DD1Amount';
  74. const FIELD_DD1AMOUNTCODE = 'DD1AmountCode';
  75. const FIELD_DD1CHECKING = 'DD1Checking';
  76. const FIELD_DD2ROUTING = 'DD2Routing';
  77. const FIELD_DD2ACCOUNT = 'DD2Account';
  78. const FIELD_DD2AMOUNT = 'DD2Amount';
  79. const FIELD_DD2AMOUNTCODE = 'DD2AmountCode';
  80. const FIELD_DD2CHECKING = 'DD2Checking';
  81. /**
  82. * Class Attributes
  83. */
  84. private $id;
  85. private $name;
  86. private $assignedFields;
  87. private $headings;
  88. /**
  89. * Setter method followed by getter method for each
  90. * attribute
  91. */
  92. public function setId($id) {
  93. $this->id = $id;
  94. }
  95. public function getId() {
  96. return $this->id;
  97. }
  98. public function setName($name){
  99. $this->name = $name;
  100. }
  101. public function getName(){
  102. return $this->name;
  103. }
  104. public function setAssignedFields($fields) {
  105. $this->assignedFields = $fields;
  106. }
  107. public function getAssignedFields() {
  108. return $this->assignedFields;
  109. }
  110. public function setHeadings($headings) {
  111. return $this->headings = $headings;
  112. }
  113. public function getHeadings() {
  114. return $this->headings;
  115. }
  116. public function __construct() {
  117. }
  118. /**
  119. * Get CustomExport with given Id
  120. *
  121. * @param int Custom export id
  122. * @return CustomExport Custom Export object if found or null if not
  123. */
  124. public static function getCustomExport($id) {
  125. if (!CommonFunctions::isValidId($id)) {
  126. throw new CustomExportException("Invalid parameters to getCustomExport(): id = $id", CustomExportException::INVALID_PARAMETERS);
  127. }
  128. $selectCondition[] = self::DB_FIELDS_ID . " = $id";
  129. $list = self::_getList($selectCondition);
  130. $export = count($list) == 0 ? null : $list[0];
  131. return $export;
  132. }
  133. public static function getAllFields() {
  134. $allFields = array(self::FIELD_EMPID, self::FIELD_LASTNAME, self::FIELD_FIRSTNAME, self::FIELD_MIDDLENAME,
  135. self::FIELD_STREET1, self::FIELD_STREET2, self::FIELD_CITY, self::FIELD_STATE,
  136. self::FIELD_ZIP, self::FIELD_GENDER, self::FIELD_BIRTHDATE, self::FIELD_SSN,
  137. self::FIELD_EMPSTATUS, self::FIELD_JOINEDDATE, self::FIELD_WORKSTATION, self::FIELD_LOCATION);
  138. $restOfAllFields = array(self::FIELD_WORKSTATE, self::FIELD_SALARY, self::FIELD_PAYFREQUENCY, self::FIELD_FITWSTATUS,
  139. self::FIELD_FITWEXCEMPTIONS, self::FIELD_SITWSTATE, self::FIELD_SITWSTATUS, self::FIELD_SITWEXCEMPTIONS,
  140. self::FIELD_SUISTATE, self::FIELD_DD1ROUTING, self::FIELD_DD1ACCOUNT, self::FIELD_DD1AMOUNT,
  141. self::FIELD_DD1AMOUNTCODE, self::FIELD_DD1CHECKING, self::FIELD_DD2ROUTING, self::FIELD_DD2ACCOUNT,
  142. self::FIELD_DD2AMOUNT, self::FIELD_DD2AMOUNTCODE, self::FIELD_DD2CHECKING);
  143. $availableCustomFields = CustomFields::getCustomFieldList();
  144. $customFields = array();
  145. foreach($availableCustomFields as $fieldObj) {
  146. $customFields[] = 'custom' . $fieldObj->getFieldNumber();
  147. }
  148. $allFields = array_merge($allFields, $customFields, $restOfAllFields);
  149. return $allFields;
  150. }
  151. /**
  152. * Get list of Custom Export objects in the database
  153. * @return Array Array of CustomExport objects
  154. */
  155. public static function getCustomExportList() {
  156. return self::_getList();
  157. }
  158. /**
  159. * Get list of defined Custom Exports in format suitable for view.php
  160. * @return Array 2D array representing custom export objects defined in database.
  161. */
  162. public static function getCustomExportListForView($pageNO,$schStr,$mode,$sortField = 0, $sortOrder = 'ASC') {
  163. $tableName = self::TABLE_NAME;
  164. $arrFieldList[0] = self::DB_FIELDS_ID;
  165. $arrFieldList[1] = self::DB_FIELDS_NAME;
  166. $arrFieldList[2] = self::DB_FIELDS_FIELDS;
  167. $arrFieldList[3] = self::DB_FIELDS_HEADINGS;
  168. $sqlBuilder = new SQLQBuilder();
  169. $sqlBuilder->table_name = $tableName;
  170. $sqlBuilder->flg_select = 'true';
  171. $sqlBuilder->arr_select = $arrFieldList;
  172. $sqlQString = $sqlBuilder->passResultSetMessage($pageNO,$schStr,$mode, $sortField, $sortOrder);
  173. $dbConnection = new DMLFunctions();
  174. $result = $dbConnection -> executeQuery($sqlQString); //Calling the addData() function
  175. $i=0;
  176. while ($line = mysql_fetch_array($result, MYSQL_NUM)) {
  177. $arrayDispList[$i][0] = $line[0];
  178. $arrayDispList[$i][1] = $line[1];
  179. $arrayDispList[$i][2] = $line[2];
  180. $i++;
  181. }
  182. if (isset($arrayDispList)) {
  183. return $arrayDispList;
  184. } else {
  185. $arrayDispList = '';
  186. return $arrayDispList;
  187. }
  188. }
  189. /**
  190. * Get the available fields (fields not yet assigned to this CustomExport)
  191. *
  192. * @return array Array of fields not yet assigned to this CustomExport object
  193. */
  194. public function getAvailableFields() {
  195. $allFields = CustomExport::getAllFields();
  196. $available = array_diff($allFields, $this->assignedFields);
  197. return $available;
  198. }
  199. /**
  200. * Delete custom exports with the given ids
  201. *
  202. * @param array $ids Array of export id's
  203. * @return int the number of CustomExport's actually deleted
  204. */
  205. public static function deleteExports($ids) {
  206. $count = 0;
  207. if (!is_array($ids)) {
  208. throw new CustomExportException("Invalid parameter to deleteExports(): ids should be an array", CustomExportException::INVALID_PARAMETERS);
  209. }
  210. foreach ($ids as $id) {
  211. if (!CommonFunctions::isValidId($id)) {
  212. throw new CustomExportException("Invalid parameter to deleteExports(): id = $id", CustomExportException::INVALID_PARAMETERS);
  213. }
  214. }
  215. if (!empty($ids)) {
  216. $sql = sprintf("DELETE FROM %s WHERE `%s` IN (%s)", self::TABLE_NAME,
  217. self::DB_FIELDS_ID, implode(",", $ids));
  218. $conn = new DMLFunctions();
  219. $result = $conn->executeQuery($sql);
  220. if ($result) {
  221. $count = mysql_affected_rows();
  222. }
  223. }
  224. return $count;
  225. }
  226. /**
  227. * Save this CustomExport Object.
  228. * If an id is available the existing values are updated, if not a new
  229. * id is assigned and a new CustomExport is saved
  230. *
  231. */
  232. public function save() {
  233. // Validate fieleds
  234. if (empty($this->name)) {
  235. throw new CustomExportException("Empty name", CustomExportException::EMPTY_EXPORT_NAME);
  236. }
  237. if ($this->_isNameInUse()) {
  238. throw new CustomExportException("Duplicate name", CustomExportException::DUPLICATE_EXPORT_NAME);
  239. }
  240. if (empty($this->assignedFields) || !is_array($this->assignedFields)) {
  241. throw new CustomExportException("No valid Assigned fields", CustomExportException::NO_ASSIGNED_FIELDS);
  242. }
  243. $allFields = self::getAllFields();
  244. foreach ($this->assignedFields as $field) {
  245. if (!in_array($field, $allFields)) {
  246. throw new CustomExportException("Invalid field name", CustomExportException::INVALID_FIELD_NAME);
  247. }
  248. }
  249. if (!empty($this->headings)) {
  250. if (count($this->assignedFields) != count($this->headings)) {
  251. throw new CustomExportException("Header count should match field count", CustomExportException::HEADER_COUNT_DOESNT_MATCH_FIELD_COUNT);
  252. }
  253. foreach ($this->headings as $heading) {
  254. if (strpos($heading, ",") !== false) {
  255. throw new CustomExportException("Invalid heading name", CustomExportException::INVALID_HEADER_NAME);
  256. }
  257. }
  258. }
  259. if (empty($this->id)) {
  260. $this->_insert();
  261. } else {
  262. $this->_update();
  263. }
  264. }
  265. /**
  266. * Add new CustomExport object to database
  267. */
  268. private function _insert() {
  269. $fields[0] = self::DB_FIELDS_ID;
  270. $fields[1] = self::DB_FIELDS_NAME;
  271. $fields[2] = self::DB_FIELDS_FIELDS;
  272. $fields[3] = self::DB_FIELDS_HEADINGS;
  273. $this->id = UniqueIDGenerator::getInstance()->getNextID(self::TABLE_NAME, self::DB_FIELDS_ID);
  274. $values[0] = $this->id;
  275. $values[1] = "'{$this->name}'";
  276. $values[2] = "'" . implode(",", $this->assignedFields) . "'";
  277. $values[3] = empty($this->headings) ? "''" : "'". implode(",", $this->headings) . "'";
  278. $sqlBuilder = new SQLQBuilder();
  279. $sqlBuilder->table_name = self::TABLE_NAME;
  280. $sqlBuilder->flg_insert = 'true';
  281. $sqlBuilder->arr_insert = $values;
  282. $sqlBuilder->arr_insertfield = $fields;
  283. $sql = $sqlBuilder->addNewRecordFeature2();
  284. $conn = new DMLFunctions();
  285. $result = $conn->executeQuery($sql);
  286. if (!$result || (mysql_affected_rows() != 1)) {
  287. throw new CustomExportException("Insert failed. $sql", CustomExportException::DB_EXCEPTION);
  288. }
  289. }
  290. /**
  291. * Update existing CustomExport data
  292. */
  293. private function _update() {
  294. $fields[0] = self::DB_FIELDS_ID;
  295. $fields[1] = self::DB_FIELDS_NAME;
  296. $fields[2] = self::DB_FIELDS_FIELDS;
  297. $fields[3] = self::DB_FIELDS_HEADINGS;
  298. $values[0] = $this->id;
  299. $values[1] = "'{$this->name}'";
  300. $values[2] = "'" . implode(",", $this->assignedFields) . "'";
  301. $values[3] = empty($this->headings) ? "''" : "'". implode(",", $this->headings) . "'";
  302. $sqlBuilder = new SQLQBuilder();
  303. $sqlBuilder->table_name = self::TABLE_NAME;
  304. $sqlBuilder->flg_update = 'true';
  305. $sqlBuilder->arr_update = $fields;
  306. $sqlBuilder->arr_updateRecList = $values;
  307. $sql = $sqlBuilder->addUpdateRecord1(0);
  308. $conn = new DMLFunctions();
  309. $result = $conn->executeQuery($sql);
  310. // Here we don't check mysql_affected_rows because update may be called
  311. // without any changes.
  312. if (!$result) {
  313. throw new CustomExportException("Update failed. SQL=$sql", CustomExportException::DB_EXCEPTION);
  314. }
  315. }
  316. /**
  317. * Check if this objects name is in use
  318. *
  319. * @return boolean true if that name is in use, false otherwise
  320. */
  321. private function _isNameInUse() {
  322. $sql = 'SELECT COUNT(*) FROM ' . self::TABLE_NAME . ' WHERE ' . self::DB_FIELDS_NAME . " = '" . $this->name . "'";
  323. // exclude this object
  324. if (!empty($this->id)) {
  325. $sql .= ' AND ' . self::DB_FIELDS_ID . ' <> ' . $this->id;
  326. }
  327. $result = mysql_query($sql);
  328. $row = mysql_fetch_array($result, MYSQL_NUM);
  329. $count = $row[0];
  330. return ($count != 0);
  331. }
  332. /**
  333. * Get a list of custom export objects with the given conditions.
  334. *
  335. * @param array $selectCondition Array of select conditions to use.
  336. * @return array Array of CustomExport objects. Returns an empty (length zero) array if none found.
  337. */
  338. private static function _getList($selectCondition = null) {
  339. $fields[0] = self::DB_FIELDS_ID;
  340. $fields[1] = self::DB_FIELDS_NAME;
  341. $fields[2] = self::DB_FIELDS_FIELDS;
  342. $fields[3] = self::DB_FIELDS_HEADINGS;
  343. $sqlBuilder = new SQLQBuilder();
  344. $sql = $sqlBuilder->simpleSelect(self::TABLE_NAME, $fields, $selectCondition, $fields[1], "ASC");
  345. $actList = array();
  346. $conn = new DMLFunctions();
  347. $result = $conn->executeQuery($sql);
  348. while ($result && ($row = mysql_fetch_assoc($result))) {
  349. $actList[] = self::_createFromRow($row);
  350. }
  351. return $actList;
  352. }
  353. /**
  354. * Creates a CustomExport object from a resultset row
  355. *
  356. * @param array $row Resultset row from the database.
  357. * @return CustomExport Custom Export object.
  358. */
  359. private static function _createFromRow($row) {
  360. $tmp = new CustomExport();
  361. $tmp->setId($row[self::DB_FIELDS_ID]);
  362. $tmp->setName($row[self::DB_FIELDS_NAME]);
  363. $assignedFields = $row[self::DB_FIELDS_FIELDS];
  364. if (!empty($assignedFields)) {
  365. $tmp->setAssignedFields(explode(",", $assignedFields));
  366. } else {
  367. $tmp->setAssignedFields(array());
  368. }
  369. $headers = $row[self::DB_FIELDS_HEADINGS];
  370. if (!empty($headers)) {
  371. $tmp->setHeadings(explode(",", $headers));
  372. } else {
  373. $tmp->setHeadings(array());
  374. }
  375. return $tmp;
  376. }
  377. }
  378. class CustomExportException extends Exception {
  379. const INVALID_FIELD_NAME = 0;
  380. const INVALID_HEADER_NAME = 1;
  381. const HEADER_COUNT_DOESNT_MATCH_FIELD_COUNT = 2;
  382. const NO_ASSIGNED_FIELDS = 3;
  383. const DUPLICATE_EXPORT_NAME = 4;
  384. const EMPTY_EXPORT_NAME = 5;
  385. const DB_EXCEPTION = 6;
  386. const INVALID_PARAMETERS = 7;
  387. const ID_NOT_FOUND = 8;
  388. }
  389. ?>