libraries.php 9.2 KB
Newer Older
nextime's avatar
nextime committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
<?
require_once "session.php";
require_once "HTTP/Request.php";
require_once "constants.php";
require_once "config.inc.php";
require_once("meekrodb.php");
require_once("conn.php");


/* OLD FUNCTIONS: deprecated! */
function changeRelay($id=0) //id relay
{
   $req =& new HTTP_Request(DOMOTIKAD_BASEURI."/rest/v1.0/getIOStatus?command=relays_".intval($id));
   if (!PEAR::isError($req->sendRequest())) {
      //$ret=$req->getResponseBody();
      $code = $req->getResponseCode();
      if($code=="302") {
         $req2 =& new HTTP_Request($req->getResponseHeader()["location"]);
         if (!PEAR::isError($req2->sendRequest())) {
            return TRUE;
         }  
         return FALSE;
      }
      return TRUE;
   }
   return FALSE;
}

function wget($url)
{
   $req =& new HTTP_Request($url);
   if (!PEAR::isError($req->sendRequest())) {
      return $req->getResponseBody();
   }
   return '';
}


function getWebSections($excludelist=array("home", "none"), $addlist=array(), $querytype="websection")
{
   if($querytype=="websection")
      $tables=(array)unserialize(DOMODB_WEBSECTION_TABLES);
   else
      $tables=(array)unserialize(DOMODB_DEVSECTION_TABLES);
   $query="";
   foreach($tables as $table)
   {
      if($query!="") $query.=" UNION DISTINCT ";
      $query.="SELECT websection FROM $table";
      foreach($excludelist as $k => $exclude)
      {
         if($k==0) 
            $query.=" WHERE ";
         else
            $query.=" AND ";
         $query.="websection != '$exclude'";
      }
   }
   $query.= " ORDER BY websection";
   $result = DB::queryOneColumn('websection', $query);
   //print_r($query."\n");
   //print_r($result);
   return array_unique(array_merge($result, $addlist));
}

function getDevSections($excludelist=array("home", "none"), $addlist=array())
{
   return getWebSections($excludelist, $addlist, "devsection");
}


function getSectionElements($section, $table='relay', $activeonly=true) {
   $active="";
   switch($table)
   {
      case "relay":
         if($activeonly) $active="AND relay.active=1";
         $query="SELECT relay.*, analog.id AS analogid, input.id AS inputid FROM (relay LEFT JOIN analog ON (analog.ananame=relay.domain AND analog.active=1 $active))  
               LEFT JOIN input ON (input.inpname=relay.domain AND input.active=1) WHERE relay.websection='$section' $active
               ORDER by relay.position,relay.button_name,relay.id";
         break;

      default:
         if($activeonly) $active="AND active=1";
         $query="SELECT * FROM $table WHERE websection='$section' $active ORDER BY button_name";
   }
   return DB::query($query);
} 

90 91
/* NEW FUNCTIONS */

92 93
//$numnum=0;

94
function getPanelIO($table, $selector, $content, $websection, $activeonly=true, $where="",$orderby="", $limit="")
95
{
96 97 98 99 100 101
   /*
   global $numnum;
   $numnum++;
   print_r("\n");
   print_r($numnum);
   */
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
   if($table=='input')
   {
      $dom="inpname";
      $ljoin="inpstatus on input.id=inpstatus.buttonid";
      $add="inpstatus.status as status";
   }
   elseif($table=='analog')
   {
      $dom="ananame";
      $ljoin="anastatus on analog.id=anastatus.buttonid";
      $add="anastatus.status as status";
   }
   elseif($table=="relay")
   {
      $add="relstatus.status as status,relstatus.ampere as ampere";
      $dom="domain";
      $ljoin="relstatus on relay.id=relstatus.buttonid";
   }
   elseif($table=='actions')
   {
nextime's avatar
nextime committed
122
      $dom="select_domain";
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      $ljoin="actstatus on actions.id=actstatus.buttonid";
      $add="actstatus.status as status, actstatus.status2 as status2";
   }
   else // output 
   {
      $add="";
      $ljoin="";
      $dom="domain";
   }
   $sqlquery="SELECT $table.*,'$table' as devtype";
   if($add!="")
      $sqlquery.=",$add";
   $sqlquery.=" FROM $table";
   if($ljoin!="")
      $sqlquery.=" LEFT JOIN ".$ljoin;
   if($websection!="*" || $content!="" || $activeonly || $where)
      $sqlquery.=" WHERE";
   if($websection!="*")
   {
      $wbss=explode(',',str_replace('DMDOMAIN:','',$websection));
      if(count($wbss)>1)
      {
      }
      if(startsWith($websection, 'DMDOMAIN:'))
      {
         if(count($wbss)>1)
         {
            $sqlquery.=" (";
            $wbstart=TRUE;
            foreach($wbss as $wbs)
            {
               if($wbstart==TRUE)
                  $wbstart=FALSE;
               else
                  $sqlquery.=" OR ";
               $sqlquery.=" DMDOMAIN(websection, '".$wbs."')=1";
            }
            $sqlquery.=" )";
         }
         else
            $sqlquery.=" DMDOMAIN(websection, '".str_replace('DMDOMAIN:','',$websection)."')=1";
      }
      else
      {
         if(count($wbss)>1)
         {
            $sqlquery.=" websection IN (";
            $wbstart=TRUE;
            foreach($wbss as $wbs)
            {
               if($wbstart==TRUE)
                  $wbstart=FALSE;
               else
                  $sqlquery.=",";
               $sqlquery.="'".$wbs."'";
            }
            $sqlquery.=")";
         }
         else
            $sqlquery.=" websection='".$websection."'";
      }
   }
   if($websection!="*" && $activeonly)
      $sqlquery.=" AND";
   if($activeonly)
      $sqlquery.=" active>0";
189
   if(($websection!="*" || $activeonly) && ($content!="" && $selector!="any"))
190
      $sqlquery.=" AND";
191
   if($content!="" && $selector=='dmdomain')
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
   {
      $dmds=explode(',',$content);
      if(count($dmds)<2)
         $sqlquery.=" DMDOMAIN($table.$dom, '".$content."')=1";
      else
      {
         $sqlquery.=" (";
         $dmstart=TRUE;
         foreach($dmds as $dmd)
         {
            if($dmstart==TRUE)
               $dmstart=FALSE;
            else
               $sqlquery.=" OR ";
            $sqlquery.=" DMDOMAIN($table.$dom, '".$dmd."')=1";

         }
         $sqlquery.=" )";
      }
   }
   if(($websection!="*" || $activeonly || $content!="") && $where!="")
      $sqlquery.=" AND";
   if($where!="")
      $sqlquery.=" ".$where;
   if($orderby=="")
      $sqlquery.=" ORDER by position,button_name ASC";
   else
      $sqlquery.=" ORDER by $orderby";
   if($limit!="")
       $sqlquery.=" LIMIT ".intval($limit);
   //print_r($sqlquery."\n");
   $ret=DB::query($sqlquery);
   if($table=="output")
   {
      foreach($ret as $k => $row)
      {
         $ret[$k]['relays'] = array();
         $ret[$k]['pwms'] = array();
         $ret[$k]['inputs'] = array();
         $ret[$k]['analogs'] = array();
         if(intval($row['has_relays'])>0) {
233
            $ret[$k]['relays'] = getPanelIO('relay', $selector, $ret[$k]['domain'], $websection, $activeonly,
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                                    "relay.board_ip='".$row['board_ip']."' AND relay.outnum='".$row['outnum']."' and relay.outtype='".$row['outtype']."'",
                                    "position,button_name");
         }
         if(intval($row['has_pwms'])>0) {
            $ret[$k]['pwms'] = array();
         }
         $wheredomain_inp="(DMDOMAIN(input.inpname, '".$ret[$k]['domain']."')=1";
         $wheredomain_ana="(DMDOMAIN(analog.ananame, '".$ret[$k]['domain']."')=1";
         if(strlen($ret[$k]['domain'])<=28) {
            $wheredomain_inp.=" OR DMDOMAIN(input.inpname, '".$ret[$k]['domain'].".[*]')=1";
            $wheredomain_ana.=" OR DMDOMAIN(analog.ananame, '".$ret[$k]['domain'].".[*]')=1";
         }
         elseif(strlen($ret[$k]['domain'])<=30) {
            $wheredomain_inp.=" OR DMDOMAIN(input.inpname, '".$ret[$k]['domain'].".*')=1";
            $wheredomain_ana.=" OR DMDOMAIN(analog.ananame, '".$ret[$k]['domain'].".*')=1";
         }
         $wheredomain_inp.=")";
         $wheredomain_ana.=")";
252
         $ret[$k]['inputs'] = getPanelIO('input', $selector, "", "*", $activeonly,
253
                              $wheredomain_inp, "id", $limit);
254
         $ret[$k]['analogs'] = getPanelIO('analog', $selector, "", "*", $activeonly,
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
                              $wheredomain_ana, "id", $limit);

      }
   }
   //print_r($ret);
   return $ret;
}


function getPanelActions($table, $content, $websection, $activeonly=true)
{
   $sqlquery="SELECT $table.*,'$table' as devtype,$add FROM $table";
}

function getPanelButtons($user, $content, $sections="*", $websection="*", $selector='dmdomain',$activeonly=true, $limit="")
{
   $buts=array();
   if($sections=='*') {
      $sectar=array('relay','input','analog','output','actions');
   } else {
      $sectar=explode(",",str_replace(" ","",$sections));
   }
277
   foreach($sectar as $sect)
278
   {
279 280
      $res=array();
      switch($sect)
281
      {
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
         case "relay":
            $res=getPanelIO("relay", $selector, $content, $websection, $activeonly, "", "", $limit);
            $buts=$buts+$res;
            break;
         case "input":
            $res=getPanelIO("input", $selector, $content, $websection, $activeonly, "", "", $limit);
            $buts=$buts+$res;
            break;
         case "analog":
            $res=getPanelIO("analog",$selector, $content, $websection, $activeonly, "", "", $limit);
            $buts=$buts+$res;
            break;
         case "output":
            $res=getPanelIO("output",$selector, $content, $websection, $activeonly, "", "", $limit);
            $buts=$buts+$res;
            break;
         case "actions":
            $res=getPanelIO("actions",$selector ,$content, $websection, $activeonly, "", "", $limit);
            $buts=$buts+$res;
            break;
302 303 304

      }
   }
305

306 307 308 309
   return $buts;
}


nextime's avatar
nextime committed
310
?>