PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/application/models/dbXML/dbProperties.php

https://github.com/sashafr/open-context-code
PHP | 356 lines | 270 code | 82 blank | 4 comment | 32 complexity | 6b5fe72f9b13a6d4623b71cdebfbcbcf MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. class dbXML_dbProperties {
  3. public $properties; //array of properties
  4. public $notes; //array of item notes
  5. public $dbName;
  6. public $dbPenelope;
  7. public $db;
  8. public function initialize($db = false){
  9. if(!$db){
  10. $db_params = OpenContext_OCConfig::get_db_config();
  11. $db = new Zend_Db_Adapter_Pdo_Mysql($db_params);
  12. $db->getConnection();
  13. }
  14. $this->db = $db;
  15. $this->properties = false;
  16. $this->notes = false;
  17. }
  18. public function getProperties($id, $obsNumbers = false){
  19. if(!is_array($obsNumbers)){
  20. $obsNumbers = array(0 => false);
  21. }
  22. foreach($obsNumbers as $obs){
  23. if(strlen($obs)<1){
  24. $obs = false;
  25. }
  26. $this->getPropsByObs($id, $obs);
  27. $this->getNotesByObs($id, $obs);
  28. }
  29. }
  30. public function getPropsByObs($id, $obs = false){
  31. $db = $this->db;
  32. if(!$obs){
  33. $obsTerm = "";
  34. }
  35. else{
  36. $obsTerm = "AND observe.obs_num = $obs ";
  37. }
  38. $sql = "SELECT properties.property_uuid,
  39. properties.val_num,
  40. '' as xml_date,
  41. var_tab.var_label,
  42. val_tab.val_text,
  43. IF (
  44. val_tab.val_text IS NULL , (
  45. IF (
  46. properties.val_num =0, properties.val_num, properties.val_num)
  47. ),
  48. val_tab.val_text
  49. ) AS allprop,
  50. var_tab.var_type,
  51. var_tab.variable_uuid,
  52. val_tab.value_uuid,
  53. var_tab.sort_order,
  54. var_tab.hideLink
  55. FROM observe
  56. LEFT JOIN properties ON observe.property_uuid = properties.property_uuid
  57. LEFT JOIN var_tab ON properties.variable_uuid = var_tab.variable_uuid
  58. LEFT JOIN val_tab ON properties.value_uuid = val_tab.value_uuid
  59. LEFT JOIN var_notes ON var_tab.variable_uuid = var_notes.variable_uuid
  60. WHERE observe.subject_uuid = '$id' AND properties.variable_uuid <> 'NOTES'
  61. $obsTerm
  62. ORDER BY var_tab.sort_order, var_notes.field_num
  63. ";
  64. //echo $sql;
  65. $result = $db->fetchAll($sql, 2);
  66. $result = false;
  67. if($result){
  68. if(!$obs){
  69. $obsNum = 1;
  70. }
  71. else{
  72. $obsNum = $obs;
  73. }
  74. $properties = $this->properties;
  75. if(!is_array($properties)){
  76. $properties = array();
  77. $properties[$obsNum] = array();
  78. }
  79. else{
  80. if(!array_key_exists($obsNum, $properties)){
  81. $properties[$obsNum] = array();
  82. }
  83. }
  84. foreach($result as $row){
  85. $propertyUUID = $row["property_uuid"];
  86. $varUUID = $row["variable_uuid"];
  87. $varLabel = $row["var_label"];
  88. $varType = strtolower($row["var_type"]);
  89. $varSort = $row["sort_order"] + 0;
  90. $hideLink = $row["hideLink"];
  91. if($hideLink == 1){
  92. $hideLink = true;
  93. }
  94. else{
  95. $hideLink = false;
  96. }
  97. $valUUID = $row["value_uuid"];
  98. if(strlen($row["val_num"])>0){
  99. $valueNum = $row["val_num"] + 0;
  100. }
  101. else{
  102. $valueNum = false;
  103. }
  104. $valueDate = $row["xml_date"];
  105. $showVal = $row["allprop"];
  106. $showVal = html_entity_decode($showVal, ENT_QUOTES, 'UTF-8');
  107. //$showVal = str_replace("\r\n", "<br/>", $showVal);
  108. if(strlen($showVal)>140){
  109. $varType = "alphanumeric";
  110. }
  111. if(stristr($varType, "calend")){
  112. $cal_test_string = str_replace("/", "-", $showVal);
  113. if (($timestamp = strtotime($cal_test_string)) === false) {
  114. $calendardTest = false;
  115. }
  116. else{
  117. $calendardTest = true;
  118. }
  119. if($calendardTest && strlen($valueDate)<1){
  120. $valueDate = date("Y-m-d", strtotime($cal_test_string));
  121. }
  122. else{
  123. $valueDate = false;
  124. }
  125. }
  126. else{
  127. $valueDate = false;
  128. }
  129. $xmlNote = "<div>".chr(13);
  130. $xmlNote .= $showVal.chr(13);
  131. $xmlNote .= "</div>".chr(13);
  132. @$xml = simplexml_load_string($xmlNote);
  133. if($xml){
  134. $validForXML = true;
  135. }
  136. else{
  137. $validForXML = false;
  138. }
  139. unset($xml);
  140. //get linked data relations for the variable and the property
  141. $varUnitsData = $this->pen_getUnitData($varUUID);
  142. $linkedDataVar = $this->pen_getLinkedData($varUUID);
  143. $linkedDataProp = $this->pen_getLinkedData($propertyUUID, true);
  144. $actPropArray = array("propertyUUID" => $propertyUUID,
  145. "varUUID" => $varUUID,
  146. "varLabel" => $varLabel,
  147. "varType" => $varType,
  148. "varSort" => $varSort,
  149. "valUUID" => $valUUID,
  150. "valueNum" => $valueNum,
  151. "valueDate" => $valueDate,
  152. "showVal" => $showVal,
  153. "validForXML" => $validForXML,
  154. "varLinkedData" => $linkedDataVar,
  155. "varUnitsData" => $varUnitsData,
  156. "propLinkedData" => $linkedDataProp,
  157. "hideLink" => $hideLink
  158. );
  159. if(!array_key_exists($propertyUUID, $properties[$obsNum]) && $varUUID != false){
  160. $properties[$obsNum][$propertyUUID] = $actPropArray;
  161. }
  162. }//end loop
  163. $this->properties = $properties;
  164. }
  165. } //end function
  166. public function pen_getLinkedData($itemUUID, $allLinks = false){
  167. $db = $this->db;
  168. $output = false;
  169. $sql = "SELECT linked_data.linkedLabel, linked_data.linkedURI, linked_data.vocabulary, linked_data.vocabURI
  170. FROM linked_data
  171. WHERE linked_data.itemUUID = '$itemUUID' AND linked_data.linkedType = 'type' ";
  172. $result = $db->fetchAll($sql, 2);
  173. if($result){
  174. $output = array();
  175. if(!$allLinks){
  176. $output = $result[0];
  177. }
  178. else{
  179. $output = $result;
  180. }
  181. }
  182. return $output;
  183. }
  184. public function pen_getUnitData($itemUUID){
  185. $db = $this->db;
  186. $output = false;
  187. $sql = "SELECT linked_data.linkedLabel, linked_data.linkedURI, linked_data.linkedAbrv
  188. FROM linked_data
  189. WHERE linked_data.itemUUID = '$itemUUID' AND linked_data.linkedType = 'unit' ";
  190. $result = $db->fetchAll($sql, 2);
  191. if($result){
  192. $output = array();
  193. $output = $result[0];
  194. }
  195. return $output;
  196. }
  197. public function getNotesByObs($id, $obs = false){
  198. $db = $this->db;
  199. if($this->dbPenelope){
  200. if(!$obs){
  201. $obsTerm = "";
  202. }
  203. else{
  204. $obsTerm = "AND observe.obs_num = $obs ";
  205. }
  206. $sql = "SELECT DISTINCT val_tab.val_text, properties.property_uuid, properties.value_uuid
  207. FROM observe
  208. LEFT JOIN properties ON observe.property_uuid = properties.property_uuid
  209. LEFT JOIN val_tab ON properties.value_uuid = val_tab.value_uuid
  210. WHERE observe.subject_uuid = '$id' AND properties.variable_uuid = 'NOTES'
  211. $obsTerm
  212. ";
  213. }
  214. else{
  215. if(!$obs){
  216. $obsTerm = "";
  217. }
  218. else{
  219. $obsTerm = "AND observe.obs_num = $obs ";
  220. }
  221. $sql = "SELECT DISTINCT val_tab.val_text, properties.property_uuid, properties.value_uuid
  222. FROM observe
  223. LEFT JOIN properties ON observe.property_uuid = properties.property_uuid
  224. LEFT JOIN val_tab ON properties.value_uuid = val_tab.value_uuid
  225. WHERE observe.subject_uuid = '$id' AND properties.variable_uuid = 'NOTES'
  226. $obsTerm
  227. ";
  228. }
  229. //echo $sql;
  230. $result = $db->fetchAll($sql, 2);
  231. if($result){
  232. if(!$obs){
  233. $obsNum = 1;
  234. }
  235. else{
  236. $obsNum = $obs;
  237. }
  238. $notes = $this->notes;
  239. if(!is_array($notes)){
  240. $notes= array();
  241. $notes[$obsNum] = array();
  242. }
  243. else{
  244. if(!array_key_exists($obsNum, $notes)){
  245. $notes[$obsNum] = array();
  246. }
  247. }
  248. foreach($result as $row){
  249. $propertyUUID = $row["property_uuid"];
  250. $valUUID = $row["value_uuid"];
  251. $noteText = $row["val_text"];
  252. $xmlNote = "<div>".chr(13);
  253. $xmlNote .= $noteText.chr(13);
  254. $xmlNote .= "</div>".chr(13);
  255. @$xml = simplexml_load_string($xmlNote);
  256. if($xml){
  257. $validForXML = true;
  258. }
  259. else{
  260. $validForXML = false;
  261. }
  262. unset($xml);
  263. $actNoteArray = array("propertyUUID" => $propertyUUID,
  264. "valueUUID"=> $valUUID,
  265. "noteText" => $noteText,
  266. "validForXML" => $validForXML);
  267. if(!array_key_exists($propertyUUID, $notes[$obsNum])){
  268. $notes[$obsNum][$propertyUUID] = $actNoteArray;
  269. }
  270. }//end loop
  271. $this->notes = $notes;
  272. }
  273. } //end function
  274. }