get_parms( "sbas_id" , "period" , 'field1' , 'fieldDs1' , 'fieldDv1' , 'field2' , 'fieldDs2' , 'fieldDv2' , 'status0' , 'coll0' , 'status1' , 'coll1' , 'status2' , 'coll2' ); $dom = new DOMDocument(); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; if ($dom->loadXML($oldxml)) { $xmlchanged = false; // foreach($parm2 as $pname=>$pvalue) foreach (array( "str:sbas_id", "str:period", 'str:field1', 'str:fieldDs1', 'str:fieldDv1', 'str:field2', 'str:fieldDs2', 'str:fieldDv2', 'str:status0', 'str:coll0', 'str:status1', 'str:coll1', 'str:status2', 'str:coll2' ) as $pname) { $ptype = substr($pname, 0, 3); $pname = substr($pname, 4); $pvalue = $parm2[$pname]; if (($ns = $dom->getElementsByTagName($pname)->item(0)) != NULL) { // le champ existait dans le xml, on supprime son ancienne valeur (tout le contenu) while (($n = $ns->firstChild)) { $ns->removeChild($n); } } else { // le champ n'existait pas dans le xml, on le cree $dom->documentElement->appendChild($dom->createTextNode("\t")); $ns = $dom->documentElement->appendChild($dom->createElement($pname)); $dom->documentElement->appendChild($dom->createTextNode("\n")); } // on fixe sa valeur switch ($ptype) { case "str": $ns->appendChild($dom->createTextNode($pvalue)); break; case "boo": $ns->appendChild($dom->createTextNode($pvalue ? '1' : '0')); break; } $xmlchanged = true; } } return($dom->saveXML()); } // ==================================================================== // xml2graphic : must fill the graphic form (using js) from xml // ==================================================================== public function xml2graphic($xml, $form) { if (($sxml = simplexml_load_string($xml)) != FALSE) { // in fact XML IS always valid here... // ... but we could check for safe values if ((int) ($sxml->period) < 10) { $sxml->period = 10; } elseif ((int) ($sxml->period) > 1440) { // 1 jour $sxml->period = 1440; } if ((string) ($sxml->delay) == '') { $sxml->delay = 0; } ?> dependencyContainer['phraseanet.user']->ACL()->get_granted_sbas(array('bas_manage')); ?>
 :   

 : 

   
 
 
 
 
 :
 :

cmd
array("set"=>false, "values"=>array(), "usage"=>" : usage du truc") ); // ====================================================================================================== // ===== help() : text displayed if --help (optional) // ====================================================================================================== public function help() { return(_("task::outofdate:deplacement de docs suivant valeurs de champs 'date'")); } // ====================================================================================================== // ===== run() : le code d'execution de la tache proprement dite // ====================================================================================================== protected $sxTaskSettings = null; // les settings de la tache en simplexml private $connbas = null; // cnx a la base private $msg = ""; private $sbas_id; protected function run2() { $ret = ''; $conn = connection::getPDOConnection($this->dependencyContainer); $this->sxTaskSettings = simplexml_load_string($this->settings); $this->sbas_id = (int) ($this->sxTaskSettings->sbas_id); $this->connbas = connection::getPDOConnection($this->dependencyContainer, $this->sbas_id); $this->running = true; $this->tmask = array(); $this->tmaskgrp = array(); $this->period = 60; // ici la tache tourne tant qu'elle est active $loop = 0; while ($this->running) { if ( ! $conn->ping()) { $this->log(("Warning : abox connection lost, restarting in 10 min.")); for ($i = 0; $i < 60 * 10; $i ++ ) { sleep(1); } $this->running = false; return(self::STATUS_TORESTART); } try { $connbas = connection::getPDOConnection($this->dependencyContainer, $this->sbas_id); if ( ! $connbas->ping()) { throw new Exception('Mysql has gone away'); } } catch (Exception $e) { $this->log(("dbox connection lost, restarting in 10 min.")); for ($i = 0; $i < 60 * 10; $i ++ ) { sleep(1); } $this->running = false; return(self::STATUS_TORESTART); } $this->setLastExecTime(); $sql = "SELECT * FROM task2 WHERE task_id = :task_id"; $stmt = $conn->prepare($sql); $stmt->execute(array(':task_id' => $this->getID())); $row = $stmt->fetch(PDO::FETCH_ASSOC); $stmt->closeCursor(); if ($row) { if ($row['status'] == 'tostop') { $ret = self::STATUS_STOPPED; $this->running = false; } else { if (($this->sxTaskSettings = simplexml_load_string($row['settings'])) != FALSE) { $period = (int) ($this->sxTaskSettings->period); if ($period <= 0 || $period >= 24 * 60) { $period = 60; } } else { $period = 60; } $this->connbas = connection::getPDOConnection($this->dependencyContainer, $this->sbas_id); $duration = time(); $r = $this->doRecords(); switch ($r) { case 'WAIT': $this->setState(self::STATE_STOPPED); $this->running = false; break; case 'BAD': $this->setState(self::STATE_STOPPED); $this->running = false; break; case 'NORECSTODO': $duration = time() - $duration; if ($duration < $period) { sleep($period - $duration); $conn = connection::getPDOConnection($this->dependencyContainer); } break; case 'MAXRECSDONE': case 'MAXMEMORY': case 'MAXLOOP': if ($row['status'] == self::STATE_STARTED && $this->getRunner() !== self::RUNNER_MANUAL) { $this->setState(self::STATE_TORESTART); $this->running = false; } break; default: if ($row['status'] == self::STATE_STARTED) { $this->setState(self::STATE_STOPPED); $this->running = false; } break; } } } else { $this->setState(self::STATE_STOPPED); $this->running = false; } $loop ++; } } public function doRecords() { $ndone = 0; $ret = 'NORECSTODO'; $tsql = $this->calcSQL($this->sxTaskSettings); $nchanged = 0; foreach ($tsql as $xsql) { try { $stmt = $this->connbas->prepare($xsql['sql']); if ($stmt->execute($xsql['params'])) { $n = $stmt->rowCount(); $stmt->closeCursor(); $nchanged += $n; if ($n > 0) { $this->log(sprintf("SQL='%s' ; parms=%s - %s changes", $xsql['sql'], var_export($xsql['params']), $n)); } } else { $this->log(sprintf("ERROR SQL='%s' ; parms=%s", $xsql['sql'], var_export($xsql['params'], true))); } } catch (ErrorException $e) { $this->log(sprintf("ERROR SQL='%s' ; parms=%s", $xsql['sql'], var_export($xsql['params'], true))); } } $ret = ($nchanged > 0 ? $nchanged : 'NORECSTODO'); return($ret); } private function calcSQL($sxTaskSettings) { $ret = array(); $this->sxTaskSettings = $sxTaskSettings; $date1 = $date2 = NULL; $field1 = $field2 = ''; // test : DATE 1 if (($field1 = trim($this->sxTaskSettings->field1)) != '') { $date1 = time(); if (($delta = (int) ($this->sxTaskSettings->fieldDv1)) > 0) { if ($this->sxTaskSettings->fieldDs1 == '-') { $date1 += 86400 * $delta; } else { $date1 -= 86400 * $delta; } } $date1 = date("YmdHis", $date1); } // test : DATE 2 if (($field2 = trim($this->sxTaskSettings->field2)) != '') { $date2 = time(); if (($delta = (int) ($this->sxTaskSettings->fieldDv2)) > 0) { if ($this->sxTaskSettings->fieldDs2 == '-') { $date2 += 86400 * $delta; } else { $date2 -= 86400 * $delta; } } $date2 = date("YmdHis", $date2); } $sqlset = $params = $tmp_params = array(); $sqlwhere = array(); for ($i = 0; $i <= 2; $i ++ ) { $sqlwhere[$i] = ''; $sqlset[$i] = ''; $x = 'status' . $i; @list($tostat, $statval) = explode('_', (string) ($this->sxTaskSettings->{$x})); if ($tostat >= 4 && $tostat <= 63) { if ($statval == '0') { $sqlset[$i] = 'status=status & ~(1<<' . $tostat . ')'; $sqlwhere[$i] .= '(status & (1<<' . $tostat . ') = 0)'; } elseif ($statval == '1') { $sqlset[$i] = 'status=status|(1<<' . $tostat . ')'; $sqlwhere[$i] .= '(status & (1<<' . $tostat . ') != 0)'; } } $x = 'coll' . $i; if (($tocoll = (string) ($this->sxTaskSettings->{$x})) != '') { $sqlset[$i] .= ( $sqlset[$i] ? ', ' : '') . ('coll_id = :coll_id_set' . $i); $sqlwhere[$i] .= ( $sqlwhere[$i] ? ' AND ' : '') . '(coll_id = :coll_id_where' . $i . ')'; $tmp_params[':coll_id_set' . $i] = $tocoll; $tmp_params[':coll_id_where' . $i] = $tocoll; } } if ($date1 && $sqlset[0]) { $params = array(); $params[':name1'] = $field1; $params[':date1'] = $date1; $params[':coll_id_set0'] = $tmp_params[':coll_id_set0']; $w = 'p1.name = :name1 AND :date1 <= p1.value'; if ($sqlwhere[1] && $sqlwhere[2]) { $w .= ' AND ((' . $sqlwhere[1] . ') OR (' . $sqlwhere[2] . '))'; $params[':coll_id_where1'] = $tmp_params[':coll_id_where1']; $params[':coll_id_where2'] = $tmp_params[':coll_id_where2']; } else { if ($sqlwhere[1]) { $w .= ' AND ' . $sqlwhere[1]; $params[':coll_id_where1'] = $tmp_params[':coll_id_where1']; } elseif ($sqlwhere[2]) { $w .= ' AND ' . $sqlwhere[2]; $params[':coll_id_where2'] = $tmp_params[':coll_id_where2']; } } $sql = "UPDATE prop AS p1 INNER JOIN record USING(record_id)" . " SET " . $sqlset[0] . " WHERE " . $w; $ret[] = array('sql' => $sql, 'params' => $params); } if ($date1 && $date2) { $params = array(); $params[':name1'] = $field1; $params[':name2'] = $field2; $params[':date1'] = $date1; $params[':date2'] = $date2; $params[':coll_id_set1'] = $tmp_params[':coll_id_set1']; $w = 'p1.name = :name1 AND p2.name = :name2 AND :date1 > p1.value AND :date2 <= p2.value'; if ($sqlwhere[0] && $sqlwhere[2]) { $w .= ' AND ((' . $sqlwhere[0] . ') OR (' . $sqlwhere[2] . '))'; $params[':coll_id_where0'] = $tmp_params[':coll_id_where0']; $params[':coll_id_where2'] = $tmp_params[':coll_id_where2']; } else { if ($sqlwhere[0]) { $w .= ' AND ' . $sqlwhere[0]; $params[':coll_id_where0'] = $tmp_params[':coll_id_where0']; } elseif ($sqlwhere[2]) { $w .= ' AND ' . $sqlwhere[2]; $params[':coll_id_where2'] = $tmp_params[':coll_id_where2']; } } $sql = "UPDATE (prop AS p1 INNER JOIN prop AS p2 USING(record_id))" . " INNER JOIN record USING(record_id)" . " SET " . $sqlset[1] . " WHERE " . $w; $ret[] = array('sql' => $sql, 'params' => $params); } if ($date2 && $sqlset[2]) { $params = array(); $params[':name2'] = $field2; $params[':date2'] = $date2; $params[':coll_id_set2'] = $tmp_params[':coll_id_set2']; $w = 'p2.name = :name2 AND :date2 > p2.value'; if ($sqlwhere[0] && $sqlwhere[1]) { $w .= ' AND ((' . $sqlwhere[0] . ') OR (' . $sqlwhere[1] . '))'; $params[':coll_id_where0'] = $tmp_params[':coll_id_where0']; $params[':coll_id_where1'] = $tmp_params[':coll_id_where1']; } else { if ($sqlwhere[0]) { $w .= ' AND ' . $sqlwhere[0]; $params[':coll_id_where0'] = $tmp_params[':coll_id_where0']; } elseif ($sqlwhere[1]) { $w .= ' AND ' . $sqlwhere[1]; $params[':coll_id_where1'] = $tmp_params[':coll_id_where1']; } } $sql = "UPDATE prop AS p2 INNER JOIN record USING(record_id)" . " SET " . $sqlset[2] . " WHERE " . $w; $ret[] = array('sql' => $sql, 'params' => $params); } return($ret); } public function facility() { $ret = NULL; $request = http_request::getInstance(); $parm2 = $request->get_parms( 'ACT', 'bid' ); phrasea::headers(200, true, 'application/json', 'UTF-8', false); $ret = NULL; switch ($parm2['ACT']) { case 'CALCSQL': $xml = $this->graphic2xml(''); $sxml = simplexml_load_string($xml); $ret = $this->calcSQL($sxml); break; case 'GETBASE': $ret = array('date_fields' => array(), 'status_bits' => array(), 'collections' => array()); if ($parm2['bid'] != '') { $sbas_id = (int) $parm2['bid']; try { $databox = $this->dependencyContainer['phraseanet.appbox']->get_databox($sbas_id); $meta_struct = $databox->get_meta_structure(); foreach ($meta_struct as $meta) { if (mb_strtolower($meta->get_type()) == 'date') { $ret['date_fields'][] = $meta->get_name(); } } $status = $databox->get_statusbits(); foreach ($status as $n => $stat) { $labelon = $stat['labelon'] ? $stat['labelon'] : ($n . '-ON'); $labeloff = $stat['labeloff'] ? $stat['labeloff'] : ($n . '-OFF'); $ret['status_bits'][] = array('n' => $n, 'value' => 0, 'label' => $labeloff); $ret['status_bits'][] = array('n' => $n, 'value' => 1, 'label' => $labelon); } foreach ($databox->get_collections() as $collection) { $ret['collections'][] = array('id' => $collection->get_coll_id(), 'name' => $collection->get_name()); } } catch (Exception $e) { } } break; } print(json_encode($ret)); } }