Beispiel #1
0
 /**
  * Implement json_encode for PHP that does not support it
  *
  * @param	mixed	$elements		PHP Object to json encode
  * @return 	string					Json encoded string
  */
 function json_encode($elements)
 {
     return dol_json_encode($elements);
 }
            $id = 1;
        }
        $listofdata[$id] = array('id' => $id, 'id_product' => $id_product, 'qty' => $qty, 'id_sw' => $id_sw, 'id_tw' => $id_tw);
        $_SESSION['massstockmove'] = dol_json_encode($listofdata);
        unset($id_product);
        //unset($id_sw);
        //unset($id_tw);
        unset($qty);
    }
}
if ($action == 'delline' && $idline != '') {
    if (!empty($listofdata[$idline])) {
        unset($listofdata[$idline]);
    }
    if (count($listofdata) > 0) {
        $_SESSION['massstockmove'] = dol_json_encode($listofdata);
    } else {
        unset($_SESSION['massstockmove']);
    }
}
if ($action == 'createmovements') {
    $error = 0;
    if (!GETPOST("label")) {
        $error++;
        setEventMessage($langs->trans("ErrorFieldRequired"), $langs->transnoentitiesnoconv("LabelMovement"));
    }
    $db->begin();
    if (!$error) {
        $product = new Product($db);
        foreach ($listofdata as $key => $val) {
            $id = $val['id'];
Beispiel #3
0
 /**
  * Return amount of elements by month for several years
  *
  * @param int $endyear
  * @param int $startyear
  * @param int $cachedelay accept for cache file (0=No read, no save of cache, -1=No read but save)
  * @return array of values
  */
 function getTransformRateByMonthWithPrevYear($endyear, $startyear, $cachedelay = 0)
 {
     global $conf, $user, $langs;
     if ($startyear > $endyear) {
         return -1;
     }
     $datay = array();
     // Search into cache
     if (!empty($cachedelay)) {
         include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
         include_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php';
     }
     $newpathofdestfile = $conf->user->dir_temp . '/' . get_class($this) . '_' . __FUNCTION__ . '_' . (empty($this->cachefilesuffix) ? '' : $this->cachefilesuffix . '_') . $langs->defaultlang . '_user' . $user->id . '.cache';
     $newmask = '0644';
     $nowgmt = dol_now();
     $foundintocache = 0;
     if ($cachedelay > 0) {
         $filedate = dol_filemtime($newpathofdestfile);
         if ($filedate >= $nowgmt - $cachedelay) {
             $foundintocache = 1;
             $this->_lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $filedate;
         } else {
             dol_syslog(get_class($this) . '::' . __FUNCTION__ . " cache file " . $newpathofdestfile . " is not found or older than now - cachedelay (" . $nowgmt . " - " . $cachedelay . ") so we can't use it.");
         }
     }
     // Load file into $data
     if ($foundintocache) {
         dol_syslog(get_class($this) . '::' . __FUNCTION__ . " read data from cache file " . $newpathofdestfile . " " . $filedate . ".");
         $data = dol_json_decode(file_get_contents($newpathofdestfile), true);
     } else {
         $year = $startyear;
         while ($year <= $endyear) {
             $datay[$year] = $this->getTransformRateByMonth($year);
             $year++;
         }
         $data = array();
         // $data = array('xval'=>array(0=>xlabel,1=>yval1,2=>yval2...),...)
         for ($i = 0; $i < 12; $i++) {
             $data[$i][] = $datay[$endyear][$i][0];
             // set label
             $year = $startyear;
             while ($year <= $endyear) {
                 $data[$i][] = $datay[$year][$i][1];
                 // set yval for x=i
                 $year++;
             }
         }
     }
     // Save cache file
     if (empty($foundintocache) && ($cachedelay > 0 || $cachedelay == -1)) {
         dol_syslog(get_class($this) . '::' . __FUNCTION__ . " save cache file " . $newpathofdestfile . " onto disk.");
         if (!dol_is_dir($conf->user->dir_temp)) {
             dol_mkdir($conf->user->dir_temp);
         }
         $fp = fopen($newpathofdestfile, 'w');
         fwrite($fp, dol_json_encode($data));
         fclose($fp);
         if (!empty($conf->global->MAIN_UMASK)) {
             $newmask = $conf->global->MAIN_UMASK;
         }
         @chmod($newpathofdestfile, octdec($newmask));
         $this->_lastfetchdate[get_class($this) . '_' . __FUNCTION__] = $nowgmt;
     }
     return $data;
 }
            $currentAmount += $remainAmount;
            // curAmount put to curRemain
        } else {
            $currentAmount = $currentRemain;
            $result += $currentRemain;
        }
    } else {
        // Reset the substraction for this amount
        $result += price2num($currentAmount);
        $currentAmount = 0;
        if ($result >= 0) {
            $amountToBreakdown = $result - $currentRemain >= 0 ? $currentRemain : $currentRemain + ($result - $currentRemain);
            // Remain can only partially be paid
            $currentAmount = $amountToBreakdown;
            // In both cases, amount will take breakdown value
            $result -= $amountToBreakdown;
            // And canceled substraction has been replaced by breakdown
        }
        // else there's no need to calc anything, just reset the field (result is still < 0)
    }
    $toJsonArray['amount_' . $currentInvId] = price2num($currentAmount) . "";
    // Param will exist only if an img has been clicked
}
$toJsonArray['makeRed'] = $totalRemaining < price2num($result) || price2num($result) < 0 ? true : false;
$toJsonArray['result'] = price($result);
// Return value to user format
$toJsonArray['resultnum'] = price2num($result);
// Return value to numeric format
// Encode to JSON to return
echo dol_json_encode($toJsonArray);
// Printing the call's result
Beispiel #5
0
    /**
     * testJsonencode
     *
     * @return	void
     */
    public function testJsonEncode()
    {
        //$this->sharedFixture
        global $conf,$user,$langs,$db;
        $this->savconf=$conf;
        $this->savuser=$user;
        $this->savlangs=$langs;
        $this->savdb=$db;

        $arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "'));

        $encoded=json_encode($arraytotest);
        //var_dump($encoded);
        $this->assertEquals('[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]',$encoded);
        $decoded=json_decode($encoded,true);
        //var_dump($decoded);
        $this->assertEquals($arraytotest,$decoded);

        $encoded=dol_json_encode($arraytotest);
        //var_dump($encoded);
        $this->assertEquals('[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]',$encoded);
        $decoded=dol_json_decode($encoded,true);
        //var_dump($decoded);
        $this->assertEquals($arraytotest,$decoded);
    }
Beispiel #6
0
 /**
  * testJsonEncode
  *
  * @return	void
  */
 public function testJsonEncode()
 {
     //$this->sharedFixture
     global $conf, $user, $langs, $db;
     $this->savconf = $conf;
     $this->savuser = $user;
     $this->savlangs = $langs;
     $this->savdb = $db;
     // Do a test with an array starting with 0
     $arraytotest = array(0 => array('key' => 1, 'value' => 'PRODREF', 'label' => 'Product ref with é and special chars \\ \' "'));
     $arrayencodedexpected = '[{"key":1,"value":"PRODREF","label":"Product ref with \\u00e9 and special chars \\\\ \' \\""}]';
     $encoded = json_encode($arraytotest);
     $this->assertEquals($arrayencodedexpected, $encoded);
     $decoded = json_decode($encoded, true);
     $this->assertEquals($arraytotest, $decoded, 'test for json_xxx');
     $encoded = dol_json_encode($arraytotest);
     $this->assertEquals($arrayencodedexpected, $encoded);
     $decoded = dol_json_decode($encoded, true);
     $this->assertEquals($arraytotest, $decoded, 'test for dol_json_xxx');
     // Same test but array start with 2 instead of 0
     $arraytotest = array(2 => array('key' => 1, 'value' => 'PRODREF', 'label' => 'Product ref with é and special chars \\ \' "'));
     $arrayencodedexpected = '{"2":{"key":1,"value":"PRODREF","label":"Product ref with \\u00e9 and special chars \\\\ \' \\""}}';
     $encoded = json_encode($arraytotest);
     $this->assertEquals($arrayencodedexpected, $encoded);
     $decoded = json_decode($encoded, true);
     $this->assertEquals($arraytotest, $decoded, 'test for json_xxx');
     $encoded = dol_json_encode($arraytotest);
     $this->assertEquals($arrayencodedexpected, $encoded);
     $decoded = dol_json_decode($encoded, true);
     $this->assertEquals($arraytotest, $decoded, 'test for dol_json_xxx');
     // Test with object
     $now = gmmktime(12, 0, 0, 1, 1, 1970);
     $objecttotest = new stdClass();
     $objecttotest->property1 = 'abc';
     $objecttotest->property2 = 1234;
     $objecttotest->property3 = $now;
     $encoded = dol_json_encode($objecttotest);
     $this->assertEquals('{"property1":"abc","property2":1234,"property3":43200}', $encoded);
 }
require_once 'filefunc.inc.php';
// If there is a POST parameter to tell to save automatically some POST parameters into a cookies, we do it
if (!empty($_POST["DOL_AUTOSET_COOKIE"])) {
    require_once DOL_DOCUMENT_ROOT . '/core/lib/json.lib.php';
    $tmpautoset = explode(':', $_POST["DOL_AUTOSET_COOKIE"], 2);
    $tmplist = explode(',', $tmpautoset[1]);
    $cookiearrayvalue = '';
    foreach ($tmplist as $tmpkey) {
        $postkey = $tmpautoset[0] . '_' . $tmpkey;
        //var_dump('tmpkey='.$tmpkey.' postkey='.$postkey.' value='.$_POST[$postkey]);
        if (!empty($_POST[$postkey])) {
            $cookiearrayvalue[$tmpkey] = $_POST[$postkey];
        }
    }
    $cookiename = $tmpautoset[0];
    $cookievalue = dol_json_encode($cookiearrayvalue);
    //var_dump('setcookie cookiename='.$cookiename.' cookievalue='.$cookievalue);
    setcookie($cookiename, empty($cookievalue) ? '' : $cookievalue, empty($cookievalue) ? 0 : time() + 86400 * 354, '/');
    // keep cookie 1 year
    if (empty($cookievalue)) {
        unset($_COOKIE[$cookiename]);
    }
}
// Init session. Name of session is specific to Dolibarr instance.
$prefix = dol_getprefix();
$sessionname = 'DOLSESSID_' . $prefix;
$sessiontimeout = 'DOLSESSTIMEOUT_' . $prefix;
if (!empty($_COOKIE[$sessiontimeout])) {
    ini_set('session.gc_maxlifetime', $_COOKIE[$sessiontimeout]);
}
session_name($sessionname);
Beispiel #8
0
 if (empty($donotclearsession)) {
     if ($object->userownerid > 0) {
         $listofuserid[$object->userownerid] = array('id' => $object->userownerid, 'transparency' => $object->transparency);
     }
     // Owner first
     if (!empty($object->userassigned)) {
         // Restore array with key with same value than param 'id'
         $tmplist1 = $object->userassigned;
         $tmplist2 = array();
         foreach ($tmplist1 as $key => $val) {
             if ($val['id'] && $val['id'] != $object->userownerid) {
                 $listofuserid[$val['id']] = $val;
             }
         }
     }
     $_SESSION['assignedtouser'] = dol_json_encode($listofuserid);
 } else {
     if (!empty($_SESSION['assignedtouser'])) {
         $listofuserid = dol_json_decode($_SESSION['assignedtouser'], true);
     }
 }
 print $form->select_dolusers_forevent('view', 'assignedtouser', 1);
 if (in_array($user->id, array_keys($listofuserid))) {
     print $langs->trans("MyAvailability") . ': ' . ($object->userassigned[$user->id]['transparency'] > 0 ? $langs->trans("Busy") : $langs->trans("Available"));
 }
 // We show nothing if event is assigned to nobody
 print '	</td></tr>';
 print '</table><br><br><table class="border" width="100%">';
 // Third party - Contact
 print '<tr><td width="30%">' . $langs->trans("ActionOnCompany") . '</td><td>' . ($object->thirdparty->id ? $object->thirdparty->getNomUrl(1) : $langs->trans("None"));
 if (is_object($object->thirdparty) && $object->thirdparty->id > 0 && $object->type_code == 'AC_TEL') {