PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/func/macro.func.php

http://domuslink.googlecode.com/
PHP | 167 lines | 81 code | 12 blank | 74 comment | 46 complexity | 91f4af7ded54e013bccf799afcaa415b MD5 | raw file
  1. <?php
  2. /*
  3. * domus.Link :: PHP Web-based frontend for Heyu (X10 Home Automation)
  4. * Copyright (c) 2007, Istvan Hubay Cebrian (istvan.cebrian@domus.link.co.pt)
  5. * Project's homepage: http://domus.link.co.pt
  6. * Project's dev. homepage: http://domuslink.googlecode.com
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope's that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details. You should have
  17. * received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. /**
  22. * Strip code
  23. *
  24. * Description: Receives macro name in format 'tv_on' or 'tv_off',
  25. * strips and returns only alias name
  26. *
  27. * @param $macro represents complete macro name
  28. */
  29. function strip_code($macro) {
  30. $thePos = strrpos($macro, "_");
  31. if ($thePos)
  32. return substr($macro, 0, $thePos);
  33. else
  34. return $macro;
  35. //1. lower case $macro
  36. //2. find position of last occurrence of "_"
  37. //3. get substr from pos 0 to position returned from 2
  38. //4. place all in lowercase
  39. }
  40. /**
  41. * Description: Receives array of all macros along with
  42. * on and off macro. It will then search same array and return
  43. * another array with matching on/off macros.
  44. *
  45. * @param $macros complete array of macros
  46. * @param $onmacro the on macro name
  47. * @param $offmacro the off macro name
  48. */
  49. function get_specific_macros($macros, $onmacro, $offmacro) {
  50. $fmacros = array();
  51. foreach ($macros as $macro) {
  52. if ($macro->getLabel() == $onmacro || $macro->getLabel() == $offmacro)
  53. array_push($fmacros, $macro);
  54. }
  55. return $fmacros;
  56. }
  57. /**
  58. * Description: Checks against triggers and timers if macros are
  59. * used by any other timer or trigger.
  60. *
  61. * @param $scheds the schedule Obkect array
  62. * @param $onmacro the onmacro name
  63. * @param $offmacro the offmacro name
  64. * @param $line the command originating line number
  65. */
  66. function multiple_macro_use($scheds, $onmacro, $offmacro, $line) {
  67. foreach ($scheds as $sched) {
  68. if ($sched->getLineNum() == $line) continue; //ignore originating item
  69. elseif(strtolower(trim($sched->getType())) == TRIGGER_D) {
  70. if ($sched->getLabel() == $onmacro && $onmacro != "null") {
  71. //item found that use on/off macro
  72. if ($sched->isEnabled())
  73. return 2; //active item exists
  74. else
  75. return 1; //disabled item exists
  76. }
  77. elseif ($sched->getLabel() == $offmacro && $offmacro != "null") {
  78. //item found that use on/off macro
  79. if ($sched->isEnabled())
  80. return 2; //active item exists
  81. else
  82. return 1; //disabled item exists
  83. }
  84. }
  85. elseif(strtolower(trim($sched->getType())) == TIMER_D) {
  86. if ($sched->getStartMacro() == $onmacro && $onmacro != "null") {
  87. //item found that use on/off macro
  88. if ($sched->isEnabled())
  89. return 2; //active item exists
  90. else
  91. return 1; //disabled item exists
  92. }
  93. elseif ($sched->getStopMacro() == $onmacro && $onmacro != "null") {
  94. //item found that use on/off macro
  95. if ($sched->isEnabled())
  96. return 2; //active item exists
  97. else
  98. return 1; //disabled item exists
  99. }
  100. elseif ($sched->getStartMacro() == $offmacro && $offmacro != "null") {
  101. //item found that use on/off macro
  102. if ($sched->isEnabled())
  103. return 2; //active item exists
  104. else
  105. return 1; //disabled item exists
  106. }
  107. elseif ($sched->getStopMacro() == $offmacro && $offmacro != "null") {
  108. //item found that use on/off macro
  109. if ($sched->isEnabled())
  110. return 2; //active item exists
  111. else
  112. return 1; //disabled item exists
  113. }
  114. }
  115. }
  116. return 0;
  117. //return values
  118. //0 - no item in use that uses on/off macros
  119. //1 - disabled item exists that uses on/off macro
  120. //2 - active item exists that uses on/off macro
  121. }
  122. /**
  123. * Description: Function to enable or disable macros
  124. *
  125. * @param $macros array of macros that matched in get_specific_macros
  126. * @param $estate end state (enable/disable)
  127. * @param $file contents such as schedule file
  128. */
  129. function change_macro_states($macros, $estate) {
  130. foreach ($macros as $macro) {
  131. if($estate == "enable") {
  132. $macro->setEnabled(true);
  133. }
  134. elseif($estate == "disable") {
  135. $macro->setEnabled(false);
  136. }
  137. }
  138. }
  139. /**
  140. * Description: Receives array of all macros along with
  141. * a macro. It will then search same array and return
  142. * another array with matching macros.
  143. *
  144. * @param $macros complete array of macros
  145. * @param $amacro the on macro name
  146. */
  147. function get_specific_macro($macros, $amacro) {
  148. $fmacros = array();
  149. foreach ($macros as $macro) {
  150. if ($macro->getLabel() == $amacro)
  151. array_push($fmacros, $macro);
  152. }
  153. return $fmacros;
  154. }
  155. ?>