示例#1
0
 /**
  * Modify a config file by clearing some data
  * Config file is searched in APP_DIR/files forder. If config server url is defined
  * the filename is searched on config server
  * @method clearOnServer
  * @static
  * @param {string} $filename The name of the config file. If config server is defined, file is changed there
  * @param {string|array} [$args=null] OA key or an array of keys for traversing the tree.
  *	If keys are not supplied the file is cleared
  *	If all-but-last keys point to plain array, last key is interpreted as a member
  *	of that array and only this array member is removed
  *	If all-but-last keys point to associative array (A) and last key is plain array (B)
  *	all keys from array A which are in array B are unset
  * @param {boolean} [$noSave=false] Weather result shall be returned or saved. Shall be of type boolean
  * @return {boolean} Wheather data was successfuly cleared. If some key does not exist still true
  * @throws {Q_Exception}
  */
 static function clearOnServer($filename, $args = null, $noSave = false)
 {
     if (!isset($args) || $args === 'null') {
         $args = array();
     }
     if (is_string($args)) {
         $args = array($args);
     }
     if (is_string($noSave)) {
         $noSave = json_decode($noSave);
     }
     $noSave = !!$noSave;
     if ($cs = self::serverInfo()) {
         // request config server
         if (!empty($cs['url'])) {
             if (!empty($cs['internal'])) {
                 // query "internal" Qbix server
                 return Q_Utils::queryInternal('Q/Config', array('Q/method' => 'clear', 'filename' => $filename, 'args' => $args, 'noSave' => $noSave), $cs['url']);
             } else {
                 // query "external" Qbix server
                 return Q_Utils::queryExternal('Q/Config', array('Q/method' => 'clear', 'filename' => $filename, 'args' => $args, 'noSave' => $noSave), $cs['url']);
             }
         }
     }
     // modify local file
     if (defined('APP_DIR')) {
         $filename = Q::realPath(APP_DIR . DS . 'files' . DS . $filename);
     } else {
         throw new Q_Exception("'APP_DIR' is not defined");
     }
     if (!$filename) {
         return true;
     }
     $tree = new Q_Tree();
     if (count($args)) {
         $tree->load($filename);
         // if not loaded we consider three empty
         if (count($args) > 1) {
             $last = call_user_func_array(array($tree, "get"), $args);
         } else {
             $last = $tree->getAll();
         }
         if (is_array($last)) {
             if (array_keys($last) === range(0, count($last) - 1)) {
                 // it's plain array and we remove it's member
                 $search = array_pop($args);
                 if (!is_array($search)) {
                     $search = array($search);
                 }
                 foreach ($search as $value) {
                     $keys = array_keys($last, $value);
                     for ($deleted = 0, $i = 0; $i < count($keys); $i++) {
                         array_splice($last, $keys[$i] - $deleted++, 1);
                     }
                 }
                 call_user_func_array(array($tree, "clear"), $args);
                 if (count($last)) {
                     array_push($args, $last);
                     call_user_func_array(array($tree, "set"), $args);
                 }
             } else {
                 // $last is associative array
                 $search = array_pop($args);
                 if (!is_array($search)) {
                     $search = array($search);
                 }
                 foreach ($search as $value) {
                     call_user_func_array(array($tree, "clear"), array_merge($args, array($value)));
                 }
             }
         }
     } else {
         $tree = new Q_Tree();
     }
     return $noSave ? $tree->getAll() : $tree->save($filename);
 }