/programs/utilit/organization.class.php

https://bitbucket.org/cantico/absences · PHP · 244 lines · 129 code · 59 blank · 56 comment · 10 complexity · a03c882130ddc89a2bf2e8dbf35d5124 MD5 · raw file

  1. <?php
  2. /************************************************************************
  3. * OVIDENTIA http://www.ovidentia.org *
  4. ************************************************************************
  5. * Copyright (c) 2003 by CANTICO ( http://www.cantico.fr ) *
  6. * *
  7. * This file is part of Ovidentia. *
  8. * *
  9. * Ovidentia is free software; you can redistribute it and/or modify *
  10. * it under the terms of the GNU General Public License as published by *
  11. * the Free Software Foundation; either version 2, or (at your option) *
  12. * any later version. *
  13. * *
  14. * This program is distributed in the hope that it will be useful, but *
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  17. * See the GNU General Public License for more details. *
  18. * *
  19. * You should have received a copy of the GNU General Public License *
  20. * along with this program; if not, write to the Free Software *
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,*
  22. * USA. *
  23. ************************************************************************/
  24. require_once dirname(__FILE__).'/record.class.php';
  25. require_once dirname(__FILE__).'/collection.class.php';
  26. require_once dirname(__FILE__).'/vacincl.php';
  27. /**
  28. * Organization
  29. *
  30. * @property string $name
  31. * @property string $description
  32. */
  33. class absences_Organization extends absences_Record
  34. {
  35. /**
  36. * @return absences_Organization
  37. */
  38. public static function getById($id)
  39. {
  40. $organization = new absences_Organization;
  41. $organization->id = $id;
  42. return $organization;
  43. }
  44. /**
  45. * get by name or return null
  46. * @return absences_Organization
  47. */
  48. public static function getByName($name)
  49. {
  50. global $babDB;
  51. $query = "SELECT * FROM absences_organization WHERE name LIKE '".$babDB->db_escape_like($name)."'";
  52. $res = $babDB->db_query($query);
  53. $row = $babDB->db_fetch_assoc($res);
  54. if (!$row)
  55. {
  56. return null;
  57. }
  58. $organization = new absences_Organization();
  59. $organization->setRow($row);
  60. return $organization;
  61. }
  62. /**
  63. * Table row as an array
  64. * @return array
  65. */
  66. public function getRow()
  67. {
  68. if (null === $this->row)
  69. {
  70. global $babDB;
  71. $query = 'SELECT * FROM absences_organization WHERE id='.$babDB->quote($this->id);
  72. $res = $babDB->db_query($query);
  73. $row = $babDB->db_fetch_assoc($res);
  74. if (!$row)
  75. {
  76. throw new Exception('This organization does not exists id='.$this->id);
  77. }
  78. $this->setRow($row);
  79. return $this->row;
  80. }
  81. return $this->row;
  82. }
  83. /**
  84. * Save organization
  85. * @return bool
  86. */
  87. public function save()
  88. {
  89. global $babDB;
  90. if (empty($this->id))
  91. {
  92. $babDB->db_query('
  93. INSERT INTO absences_organization (name
  94. ) VALUES (
  95. '.$babDB->quote($this->name).'
  96. )
  97. ');
  98. } else {
  99. $babDB->db_query('UPDATE absences_organization
  100. SET
  101. name='.$babDB->quote($this->name).'
  102. WHERE
  103. id='.$babDB->quote($this->id));
  104. }
  105. return true;
  106. }
  107. /**
  108. * Remove references and delete
  109. * @return bool
  110. */
  111. public function delete()
  112. {
  113. global $babDB;
  114. if (!$this->id) {
  115. throw new Exception('Missing ID');
  116. }
  117. $babDB->db_query("UPDATE absences_personnel SET id_organization='0'
  118. WHERE id_organization=".$babDB->quote($this->id));
  119. $babDB->db_query("DELETE FROM absences_organization
  120. WHERE id=".$babDB->quote($this->id));
  121. return (1 == $babDB->db_affected_rows());
  122. }
  123. /**
  124. * Get agents
  125. * @return absences_AgentIterator | false
  126. */
  127. public function getAgentInterator()
  128. {
  129. if (!$this->id) {
  130. return false;
  131. }
  132. require_once dirname(__FILE__).'/agent.class.php';
  133. $agents = new absences_AgentIterator();
  134. $agents->setOrganization($this);
  135. return $agents;
  136. }
  137. /**
  138. * Create organizations from the organization directory field "organisationname"
  139. * this method must not modify the organizations ID
  140. *
  141. */
  142. public static function createFromDirectory()
  143. {
  144. global $babDB;
  145. $res = $babDB->db_query("
  146. SELECT
  147. e.organisationname name,
  148. o.id
  149. FROM bab_dbdir_entries e
  150. LEFT JOIN absences_organization o ON o.name LIKE e.organisationname,
  151. absences_personnel p
  152. WHERE e.id_user=p.id_user
  153. AND e.organisationname<>''
  154. GROUP BY organisationname
  155. ");
  156. while ($arr = $babDB->db_fetch_assoc($res)) {
  157. $org = new absences_Organization();
  158. $org->setRow($arr);
  159. $org->save();
  160. }
  161. }
  162. }
  163. class absences_OrganizationIterator extends absences_Iterator
  164. {
  165. public function getObject($data)
  166. {
  167. $org = new absences_Organization;
  168. $org->setRow($data);
  169. return $org;
  170. }
  171. public function executeQuery()
  172. {
  173. if(is_null($this->_oResult))
  174. {
  175. global $babDB;
  176. $query = '
  177. SELECT
  178. o.*
  179. FROM
  180. absences_organization o
  181. ';
  182. $this->setMySqlResult($this->getDataBaseAdapter()->db_query($query));
  183. }
  184. }
  185. }