@@ -369,235 +369,235 @@ discard block |
||
369 | 369 | // We save tabtype for a future use, to remember what kind of payment it is |
370 | 370 | $tabpay[$obj->rowid]['type'] = $links[$key]['type']; |
371 | 371 | $tabtype[$obj->rowid] = $links[$key]['type']; |
372 | - /* phpcs:disable -- Code does nothing at this moment -> commented |
|
372 | + /* phpcs:disable -- Code does nothing at this moment -> commented |
|
373 | 373 | } elseif (in_array($links[$key]['type'], array('company', 'user'))) { |
374 | 374 | if ($tabpay[$obj->rowid]['type'] == 'unknown') { |
375 | 375 | // We can guess here it is a bank record for a thirdparty company or a user. |
376 | 376 | // But we won't be able to record somewhere else than into a waiting account, because there is no other journal to record the contreparty. |
377 | 377 | } |
378 | 378 | */ // phpcs::enable |
379 | - } |
|
380 | - |
|
381 | - // Special case to ask later to add more request to get information for old links without company link. |
|
382 | - if ($links[$key]['type'] == 'withdraw') { |
|
383 | - $tabmoreinfo[$obj->rowid]['withdraw'] = 1; |
|
384 | - } |
|
379 | + } |
|
385 | 380 | |
386 | - if ($links[$key]['type'] == 'payment') { |
|
387 | - $paymentstatic->id = $links[$key]['url_id']; |
|
388 | - $paymentstatic->ref = $links[$key]['url_id']; |
|
389 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentstatic->getNomUrl(2, '', ''); // TODO Do not include list of invoice in tooltip, the dol_string_nohtmltag is ko with this |
|
390 | - $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id; |
|
391 | - } elseif ($links[$key]['type'] == 'payment_supplier') { |
|
392 | - $paymentsupplierstatic->id = $links[$key]['url_id']; |
|
393 | - $paymentsupplierstatic->ref = $links[$key]['url_id']; |
|
394 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsupplierstatic->getNomUrl(2); |
|
395 | - $tabpay[$obj->rowid]["paymentsupplierid"] = $paymentsupplierstatic->id; |
|
396 | - } elseif ($links[$key]['type'] == 'company') { |
|
397 | - $societestatic->id = $links[$key]['url_id']; |
|
398 | - $societestatic->name = $links[$key]['label']; |
|
399 | - $societestatic->email = $tabcompany[$obj->rowid]['email']; |
|
400 | - $tabpay[$obj->rowid]["soclib"] = $societestatic->getNomUrl(1, '', 30); |
|
401 | - if ($compta_soc) { |
|
402 | - if (empty($tabtp[$obj->rowid][$compta_soc])) { |
|
403 | - $tabtp[$obj->rowid][$compta_soc] = $amounttouse; |
|
404 | - } else { |
|
405 | - $tabtp[$obj->rowid][$compta_soc] += $amounttouse; |
|
406 | - } |
|
407 | - } |
|
408 | - } elseif ($links[$key]['type'] == 'user') { |
|
409 | - $userstatic->id = $links[$key]['url_id']; |
|
410 | - $userstatic->name = $links[$key]['label']; |
|
411 | - $userstatic->email = $tabuser[$obj->rowid]['email']; |
|
412 | - $userstatic->firstname = $tabuser[$obj->rowid]['firstname']; |
|
413 | - $userstatic->lastname = $tabuser[$obj->rowid]['lastname']; |
|
414 | - $userstatic->status = $tabuser[$obj->rowid]['status']; |
|
415 | - $userstatic->accountancy_code = $tabuser[$obj->rowid]['accountancy_code']; |
|
416 | - if ($userstatic->id > 0) { |
|
417 | - $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, 'accountancy', 0); |
|
418 | - } else { |
|
419 | - $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen, but happens with old data when id of user was not saved on expense report payment. |
|
420 | - } |
|
421 | - if ($compta_user) { |
|
422 | - $tabtp[$obj->rowid][$compta_user] += $amounttouse; |
|
423 | - } |
|
424 | - } elseif ($links[$key]['type'] == 'sc') { |
|
425 | - $chargestatic->id = $links[$key]['url_id']; |
|
426 | - $chargestatic->ref = $links[$key]['url_id']; |
|
427 | - |
|
428 | - $tabpay[$obj->rowid]["lib"] .= ' '.$chargestatic->getNomUrl(2); |
|
429 | - $reg = array(); |
|
430 | - if (preg_match('/^\((.*)\)$/i', $links[$key]['label'], $reg)) { |
|
431 | - if ($reg[1] == 'socialcontribution') { |
|
432 | - $reg[1] = 'SocialContribution'; |
|
433 | - } |
|
434 | - $chargestatic->label = $langs->trans($reg[1]); |
|
435 | - } else { |
|
436 | - $chargestatic->label = $links[$key]['label']; |
|
437 | - } |
|
438 | - $chargestatic->ref = $chargestatic->label; |
|
439 | - $tabpay[$obj->rowid]["soclib"] = $chargestatic->getNomUrl(1, 30); |
|
440 | - $tabpay[$obj->rowid]["paymentscid"] = $chargestatic->id; |
|
441 | - |
|
442 | - // Retrieve the accounting code of the social contribution of the payment from link of payment. |
|
443 | - // Note: We have the social contribution id, it can be faster to get accounting code from social contribution id. |
|
444 | - $sqlmid = "SELECT cchgsoc.accountancy_code"; |
|
445 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."c_chargesociales cchgsoc"; |
|
446 | - $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."chargesociales as chgsoc ON chgsoc.fk_type = cchgsoc.id"; |
|
447 | - $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."paiementcharge as paycharg ON paycharg.fk_charge = chgsoc.rowid"; |
|
448 | - $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."bank_url as bkurl ON bkurl.url_id=paycharg.rowid AND bkurl.type = 'payment_sc'"; |
|
449 | - $sqlmid .= " WHERE bkurl.fk_bank = ".((int) $obj->rowid); |
|
450 | - |
|
451 | - dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG); |
|
452 | - $resultmid = $db->query($sqlmid); |
|
453 | - if ($resultmid) { |
|
454 | - $objmid = $db->fetch_object($resultmid); |
|
455 | - $tabtp[$obj->rowid][$objmid->accountancy_code] = isset($tabtp[$obj->rowid][$objmid->accountancy_code]) ? $tabtp[$obj->rowid][$objmid->accountancy_code] + $amounttouse : $amounttouse; |
|
456 | - } |
|
457 | - } elseif ($links[$key]['type'] == 'payment_donation') { |
|
458 | - $paymentdonstatic->id = $links[$key]['url_id']; |
|
459 | - $paymentdonstatic->ref = $links[$key]['url_id']; |
|
460 | - $paymentdonstatic->fk_donation = $links[$key]['url_id']; |
|
461 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentdonstatic->getNomUrl(2); |
|
462 | - $tabpay[$obj->rowid]["paymentdonationid"] = $paymentdonstatic->id; |
|
463 | - $tabtp[$obj->rowid][$account_pay_donation] = isset($tabtp[$obj->rowid][$account_pay_donation]) ? $tabtp[$obj->rowid][$account_pay_donation] + $amounttouse : $amounttouse; |
|
464 | - } elseif ($links[$key]['type'] == 'member') { |
|
465 | - $paymentsubscriptionstatic->id = $links[$key]['url_id']; |
|
466 | - $paymentsubscriptionstatic->ref = $links[$key]['url_id']; |
|
467 | - $paymentsubscriptionstatic->label = $links[$key]['label']; |
|
468 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsubscriptionstatic->getNomUrl(2); |
|
469 | - $tabpay[$obj->rowid]["paymentsubscriptionid"] = $paymentsubscriptionstatic->id; |
|
470 | - $paymentsubscriptionstatic->fetch($paymentsubscriptionstatic->id); |
|
471 | - $tabtp[$obj->rowid][$account_pay_subscription] = isset($tabtp[$obj->rowid][$account_pay_subscription]) ? $tabtp[$obj->rowid][$account_pay_subscription] + $amounttouse : $amounttouse; |
|
472 | - } elseif ($links[$key]['type'] == 'payment_vat') { // Payment VAT |
|
473 | - $paymentvatstatic->id = $links[$key]['url_id']; |
|
474 | - $paymentvatstatic->ref = $links[$key]['url_id']; |
|
475 | - $paymentvatstatic->label = $links[$key]['label']; |
|
476 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvatstatic->getNomUrl(2); |
|
477 | - $tabpay[$obj->rowid]["paymentvatid"] = $paymentvatstatic->id; |
|
478 | - $tabtp[$obj->rowid][$account_pay_vat] = isset($tabtp[$obj->rowid][$account_pay_vat]) ? $tabtp[$obj->rowid][$account_pay_vat] + $amounttouse : $amounttouse; |
|
479 | - } elseif ($links[$key]['type'] == 'payment_salary') { |
|
480 | - $paymentsalstatic->id = $links[$key]['url_id']; |
|
481 | - $paymentsalstatic->ref = $links[$key]['url_id']; |
|
482 | - $paymentsalstatic->label = $links[$key]['label']; |
|
483 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsalstatic->getNomUrl(2); |
|
484 | - $tabpay[$obj->rowid]["paymentsalid"] = $paymentsalstatic->id; |
|
485 | - |
|
486 | - // This part of code is no more required. it is here to solve case where a link were missing (with v14.0.0) and keep writing in accountancy complete. |
|
487 | - // Note: A better way to fix this is to delete payment of salary and recreate it, or to fix the bookkeeping table manually after. |
|
488 | - if (getDolGlobalString('ACCOUNTANCY_AUTOFIX_MISSING_LINK_TO_USER_ON_SALARY_BANK_PAYMENT')) { |
|
489 | - $tmpsalary = new Salary($db); |
|
490 | - $tmpsalary->fetch($paymentsalstatic->id); |
|
491 | - $tmpsalary->fetch_user($tmpsalary->fk_user); |
|
492 | - |
|
493 | - $userstatic->id = $tmpsalary->user->id; |
|
494 | - $userstatic->name = $tmpsalary->user->name; |
|
495 | - $userstatic->email = $tmpsalary->user->email; |
|
496 | - $userstatic->firstname = $tmpsalary->user->firstname; |
|
497 | - $userstatic->lastname = $tmpsalary->user->lastname; |
|
498 | - $userstatic->status = $tmpsalary->user->status; |
|
499 | - $userstatic->accountancy_code = $tmpsalary->user->accountancy_code; |
|
500 | - |
|
501 | - if ($userstatic->id > 0) { |
|
502 | - $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, 'accountancy', 0); |
|
503 | - } else { |
|
504 | - $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen |
|
505 | - } |
|
381 | + // Special case to ask later to add more request to get information for old links without company link. |
|
382 | + if ($links[$key]['type'] == 'withdraw') { |
|
383 | + $tabmoreinfo[$obj->rowid]['withdraw'] = 1; |
|
384 | + } |
|
506 | 385 | |
507 | - if (empty($obj->typeop_user)) { // Add test to avoid to add amount twice if a link already exists also on user. |
|
508 | - $compta_user = $userstatic->accountancy_code; |
|
509 | - if ($compta_user) { |
|
510 | - $tabtp[$obj->rowid][$compta_user] += $amounttouse; |
|
511 | - $tabuser[$obj->rowid] = array( |
|
512 | - 'id' => $userstatic->id, |
|
513 | - 'name' => dolGetFirstLastname($userstatic->firstname, $userstatic->lastname), |
|
514 | - 'lastname' => $userstatic->lastname, |
|
515 | - 'firstname' => $userstatic->firstname, |
|
516 | - 'email' => $userstatic->email, |
|
517 | - 'accountancy_code' => $compta_user, |
|
518 | - 'status' => $userstatic->status |
|
519 | - ); |
|
520 | - } |
|
521 | - } |
|
522 | - } |
|
523 | - } elseif ($links[$key]['type'] == 'payment_expensereport') { |
|
524 | - $paymentexpensereportstatic->id = $links[$key]['url_id']; |
|
525 | - $tabpay[$obj->rowid]["lib"] .= $paymentexpensereportstatic->getNomUrl(2); |
|
526 | - $tabpay[$obj->rowid]["paymentexpensereport"] = $paymentexpensereportstatic->id; |
|
527 | - } elseif ($links[$key]['type'] == 'payment_various') { |
|
528 | - $paymentvariousstatic->id = $links[$key]['url_id']; |
|
529 | - $paymentvariousstatic->ref = $links[$key]['url_id']; |
|
530 | - $paymentvariousstatic->label = $links[$key]['label']; |
|
531 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvariousstatic->getNomUrl(2); |
|
532 | - $tabpay[$obj->rowid]["paymentvariousid"] = $paymentvariousstatic->id; |
|
533 | - $paymentvariousstatic->fetch($paymentvariousstatic->id); |
|
534 | - $account_various = (!empty($paymentvariousstatic->accountancy_code) ? $paymentvariousstatic->accountancy_code : 'NotDefined'); // NotDefined is a reserved word |
|
535 | - $account_subledger = (!empty($paymentvariousstatic->subledger_account) ? $paymentvariousstatic->subledger_account : ''); // NotDefined is a reserved word |
|
536 | - $tabpay[$obj->rowid]["account_various"] = $account_various; |
|
537 | - $tabtp[$obj->rowid][$account_subledger] = isset($tabtp[$obj->rowid][$account_subledger]) ? $tabtp[$obj->rowid][$account_subledger] + $amounttouse : $amounttouse; |
|
538 | - } elseif ($links[$key]['type'] == 'payment_loan') { |
|
539 | - $paymentloanstatic->id = $links[$key]['url_id']; |
|
540 | - $paymentloanstatic->ref = $links[$key]['url_id']; |
|
541 | - $paymentloanstatic->fk_loan = $links[$key]['url_id']; |
|
542 | - $tabpay[$obj->rowid]["lib"] .= ' '.$paymentloanstatic->getNomUrl(2); |
|
543 | - $tabpay[$obj->rowid]["paymentloanid"] = $paymentloanstatic->id; |
|
544 | - //$tabtp[$obj->rowid][$account_pay_loan] += $amounttouse; |
|
545 | - $sqlmid = 'SELECT pl.amount_capital, pl.amount_insurance, pl.amount_interest, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest'; |
|
546 | - $sqlmid .= ' FROM '.MAIN_DB_PREFIX.'payment_loan as pl, '.MAIN_DB_PREFIX.'loan as l'; |
|
547 | - $sqlmid .= ' WHERE l.rowid = pl.fk_loan AND pl.fk_bank = '.((int) $obj->rowid); |
|
548 | - |
|
549 | - dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG); |
|
550 | - $resultmid = $db->query($sqlmid); |
|
551 | - if ($resultmid) { |
|
552 | - $objmid = $db->fetch_object($resultmid); |
|
553 | - $tabtp[$obj->rowid][$objmid->accountancy_account_capital] = isset($tabtp[$obj->rowid][$objmid->accountancy_account_capital]) ? $tabtp[$obj->rowid][$objmid->accountancy_account_capital] - $objmid->amount_capital : $amounttouse; |
|
554 | - $tabtp[$obj->rowid][$objmid->accountancy_account_insurance] = isset($tabtp[$obj->rowid][$objmid->accountancy_account_insurance]) ? $tabtp[$obj->rowid][$objmid->accountancy_account_insurance] - $objmid->amount_insurance : $amounttouse; |
|
555 | - $tabtp[$obj->rowid][$objmid->accountancy_account_interest] = isset($tabtp[$obj->rowid][$objmid->accountancy_account_interest]) ? $tabtp[$obj->rowid][$objmid->accountancy_account_interest] - $objmid->amount_interes : $amounttouse; |
|
556 | - } |
|
557 | - } elseif ($links[$key]['type'] == 'banktransfert') { |
|
558 | - $accountLinestatic->fetch($links[$key]['url_id']); |
|
559 | - $tabpay[$obj->rowid]["lib"] .= ' '.$langs->trans("BankTransfer").'- '.$accountLinestatic ->getNomUrl(1); |
|
560 | - $tabtp[$obj->rowid][$account_transfer] = isset($tabtp[$obj->rowid][$account_transfer]) ? $tabtp[$obj->rowid][$account_transfer] + $amounttouse : $amounttouse; |
|
561 | - $bankaccountstatic->fetch($tabpay[$obj->rowid]['fk_bank_account']); |
|
562 | - $tabpay[$obj->rowid]["soclib"] = $bankaccountstatic->getNomUrl(2); |
|
563 | - } |
|
564 | - } |
|
565 | - } |
|
386 | + if ($links[$key]['type'] == 'payment') { |
|
387 | + $paymentstatic->id = $links[$key]['url_id']; |
|
388 | + $paymentstatic->ref = $links[$key]['url_id']; |
|
389 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentstatic->getNomUrl(2, '', ''); // TODO Do not include list of invoice in tooltip, the dol_string_nohtmltag is ko with this |
|
390 | + $tabpay[$obj->rowid]["paymentid"] = $paymentstatic->id; |
|
391 | + } elseif ($links[$key]['type'] == 'payment_supplier') { |
|
392 | + $paymentsupplierstatic->id = $links[$key]['url_id']; |
|
393 | + $paymentsupplierstatic->ref = $links[$key]['url_id']; |
|
394 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsupplierstatic->getNomUrl(2); |
|
395 | + $tabpay[$obj->rowid]["paymentsupplierid"] = $paymentsupplierstatic->id; |
|
396 | + } elseif ($links[$key]['type'] == 'company') { |
|
397 | + $societestatic->id = $links[$key]['url_id']; |
|
398 | + $societestatic->name = $links[$key]['label']; |
|
399 | + $societestatic->email = $tabcompany[$obj->rowid]['email']; |
|
400 | + $tabpay[$obj->rowid]["soclib"] = $societestatic->getNomUrl(1, '', 30); |
|
401 | + if ($compta_soc) { |
|
402 | + if (empty($tabtp[$obj->rowid][$compta_soc])) { |
|
403 | + $tabtp[$obj->rowid][$compta_soc] = $amounttouse; |
|
404 | + } else { |
|
405 | + $tabtp[$obj->rowid][$compta_soc] += $amounttouse; |
|
406 | + } |
|
407 | + } |
|
408 | + } elseif ($links[$key]['type'] == 'user') { |
|
409 | + $userstatic->id = $links[$key]['url_id']; |
|
410 | + $userstatic->name = $links[$key]['label']; |
|
411 | + $userstatic->email = $tabuser[$obj->rowid]['email']; |
|
412 | + $userstatic->firstname = $tabuser[$obj->rowid]['firstname']; |
|
413 | + $userstatic->lastname = $tabuser[$obj->rowid]['lastname']; |
|
414 | + $userstatic->status = $tabuser[$obj->rowid]['status']; |
|
415 | + $userstatic->accountancy_code = $tabuser[$obj->rowid]['accountancy_code']; |
|
416 | + if ($userstatic->id > 0) { |
|
417 | + $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, 'accountancy', 0); |
|
418 | + } else { |
|
419 | + $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen, but happens with old data when id of user was not saved on expense report payment. |
|
420 | + } |
|
421 | + if ($compta_user) { |
|
422 | + $tabtp[$obj->rowid][$compta_user] += $amounttouse; |
|
423 | + } |
|
424 | + } elseif ($links[$key]['type'] == 'sc') { |
|
425 | + $chargestatic->id = $links[$key]['url_id']; |
|
426 | + $chargestatic->ref = $links[$key]['url_id']; |
|
427 | + |
|
428 | + $tabpay[$obj->rowid]["lib"] .= ' '.$chargestatic->getNomUrl(2); |
|
429 | + $reg = array(); |
|
430 | + if (preg_match('/^\((.*)\)$/i', $links[$key]['label'], $reg)) { |
|
431 | + if ($reg[1] == 'socialcontribution') { |
|
432 | + $reg[1] = 'SocialContribution'; |
|
433 | + } |
|
434 | + $chargestatic->label = $langs->trans($reg[1]); |
|
435 | + } else { |
|
436 | + $chargestatic->label = $links[$key]['label']; |
|
437 | + } |
|
438 | + $chargestatic->ref = $chargestatic->label; |
|
439 | + $tabpay[$obj->rowid]["soclib"] = $chargestatic->getNomUrl(1, 30); |
|
440 | + $tabpay[$obj->rowid]["paymentscid"] = $chargestatic->id; |
|
441 | + |
|
442 | + // Retrieve the accounting code of the social contribution of the payment from link of payment. |
|
443 | + // Note: We have the social contribution id, it can be faster to get accounting code from social contribution id. |
|
444 | + $sqlmid = "SELECT cchgsoc.accountancy_code"; |
|
445 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."c_chargesociales cchgsoc"; |
|
446 | + $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."chargesociales as chgsoc ON chgsoc.fk_type = cchgsoc.id"; |
|
447 | + $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."paiementcharge as paycharg ON paycharg.fk_charge = chgsoc.rowid"; |
|
448 | + $sqlmid .= " INNER JOIN ".MAIN_DB_PREFIX."bank_url as bkurl ON bkurl.url_id=paycharg.rowid AND bkurl.type = 'payment_sc'"; |
|
449 | + $sqlmid .= " WHERE bkurl.fk_bank = ".((int) $obj->rowid); |
|
450 | + |
|
451 | + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG); |
|
452 | + $resultmid = $db->query($sqlmid); |
|
453 | + if ($resultmid) { |
|
454 | + $objmid = $db->fetch_object($resultmid); |
|
455 | + $tabtp[$obj->rowid][$objmid->accountancy_code] = isset($tabtp[$obj->rowid][$objmid->accountancy_code]) ? $tabtp[$obj->rowid][$objmid->accountancy_code] + $amounttouse : $amounttouse; |
|
456 | + } |
|
457 | + } elseif ($links[$key]['type'] == 'payment_donation') { |
|
458 | + $paymentdonstatic->id = $links[$key]['url_id']; |
|
459 | + $paymentdonstatic->ref = $links[$key]['url_id']; |
|
460 | + $paymentdonstatic->fk_donation = $links[$key]['url_id']; |
|
461 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentdonstatic->getNomUrl(2); |
|
462 | + $tabpay[$obj->rowid]["paymentdonationid"] = $paymentdonstatic->id; |
|
463 | + $tabtp[$obj->rowid][$account_pay_donation] = isset($tabtp[$obj->rowid][$account_pay_donation]) ? $tabtp[$obj->rowid][$account_pay_donation] + $amounttouse : $amounttouse; |
|
464 | + } elseif ($links[$key]['type'] == 'member') { |
|
465 | + $paymentsubscriptionstatic->id = $links[$key]['url_id']; |
|
466 | + $paymentsubscriptionstatic->ref = $links[$key]['url_id']; |
|
467 | + $paymentsubscriptionstatic->label = $links[$key]['label']; |
|
468 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsubscriptionstatic->getNomUrl(2); |
|
469 | + $tabpay[$obj->rowid]["paymentsubscriptionid"] = $paymentsubscriptionstatic->id; |
|
470 | + $paymentsubscriptionstatic->fetch($paymentsubscriptionstatic->id); |
|
471 | + $tabtp[$obj->rowid][$account_pay_subscription] = isset($tabtp[$obj->rowid][$account_pay_subscription]) ? $tabtp[$obj->rowid][$account_pay_subscription] + $amounttouse : $amounttouse; |
|
472 | + } elseif ($links[$key]['type'] == 'payment_vat') { // Payment VAT |
|
473 | + $paymentvatstatic->id = $links[$key]['url_id']; |
|
474 | + $paymentvatstatic->ref = $links[$key]['url_id']; |
|
475 | + $paymentvatstatic->label = $links[$key]['label']; |
|
476 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvatstatic->getNomUrl(2); |
|
477 | + $tabpay[$obj->rowid]["paymentvatid"] = $paymentvatstatic->id; |
|
478 | + $tabtp[$obj->rowid][$account_pay_vat] = isset($tabtp[$obj->rowid][$account_pay_vat]) ? $tabtp[$obj->rowid][$account_pay_vat] + $amounttouse : $amounttouse; |
|
479 | + } elseif ($links[$key]['type'] == 'payment_salary') { |
|
480 | + $paymentsalstatic->id = $links[$key]['url_id']; |
|
481 | + $paymentsalstatic->ref = $links[$key]['url_id']; |
|
482 | + $paymentsalstatic->label = $links[$key]['label']; |
|
483 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentsalstatic->getNomUrl(2); |
|
484 | + $tabpay[$obj->rowid]["paymentsalid"] = $paymentsalstatic->id; |
|
485 | + |
|
486 | + // This part of code is no more required. it is here to solve case where a link were missing (with v14.0.0) and keep writing in accountancy complete. |
|
487 | + // Note: A better way to fix this is to delete payment of salary and recreate it, or to fix the bookkeeping table manually after. |
|
488 | + if (getDolGlobalString('ACCOUNTANCY_AUTOFIX_MISSING_LINK_TO_USER_ON_SALARY_BANK_PAYMENT')) { |
|
489 | + $tmpsalary = new Salary($db); |
|
490 | + $tmpsalary->fetch($paymentsalstatic->id); |
|
491 | + $tmpsalary->fetch_user($tmpsalary->fk_user); |
|
492 | + |
|
493 | + $userstatic->id = $tmpsalary->user->id; |
|
494 | + $userstatic->name = $tmpsalary->user->name; |
|
495 | + $userstatic->email = $tmpsalary->user->email; |
|
496 | + $userstatic->firstname = $tmpsalary->user->firstname; |
|
497 | + $userstatic->lastname = $tmpsalary->user->lastname; |
|
498 | + $userstatic->status = $tmpsalary->user->status; |
|
499 | + $userstatic->accountancy_code = $tmpsalary->user->accountancy_code; |
|
500 | + |
|
501 | + if ($userstatic->id > 0) { |
|
502 | + $tabpay[$obj->rowid]["soclib"] = $userstatic->getNomUrl(1, 'accountancy', 0); |
|
503 | + } else { |
|
504 | + $tabpay[$obj->rowid]["soclib"] = '???'; // Should not happen |
|
505 | + } |
|
506 | + |
|
507 | + if (empty($obj->typeop_user)) { // Add test to avoid to add amount twice if a link already exists also on user. |
|
508 | + $compta_user = $userstatic->accountancy_code; |
|
509 | + if ($compta_user) { |
|
510 | + $tabtp[$obj->rowid][$compta_user] += $amounttouse; |
|
511 | + $tabuser[$obj->rowid] = array( |
|
512 | + 'id' => $userstatic->id, |
|
513 | + 'name' => dolGetFirstLastname($userstatic->firstname, $userstatic->lastname), |
|
514 | + 'lastname' => $userstatic->lastname, |
|
515 | + 'firstname' => $userstatic->firstname, |
|
516 | + 'email' => $userstatic->email, |
|
517 | + 'accountancy_code' => $compta_user, |
|
518 | + 'status' => $userstatic->status |
|
519 | + ); |
|
520 | + } |
|
521 | + } |
|
522 | + } |
|
523 | + } elseif ($links[$key]['type'] == 'payment_expensereport') { |
|
524 | + $paymentexpensereportstatic->id = $links[$key]['url_id']; |
|
525 | + $tabpay[$obj->rowid]["lib"] .= $paymentexpensereportstatic->getNomUrl(2); |
|
526 | + $tabpay[$obj->rowid]["paymentexpensereport"] = $paymentexpensereportstatic->id; |
|
527 | + } elseif ($links[$key]['type'] == 'payment_various') { |
|
528 | + $paymentvariousstatic->id = $links[$key]['url_id']; |
|
529 | + $paymentvariousstatic->ref = $links[$key]['url_id']; |
|
530 | + $paymentvariousstatic->label = $links[$key]['label']; |
|
531 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentvariousstatic->getNomUrl(2); |
|
532 | + $tabpay[$obj->rowid]["paymentvariousid"] = $paymentvariousstatic->id; |
|
533 | + $paymentvariousstatic->fetch($paymentvariousstatic->id); |
|
534 | + $account_various = (!empty($paymentvariousstatic->accountancy_code) ? $paymentvariousstatic->accountancy_code : 'NotDefined'); // NotDefined is a reserved word |
|
535 | + $account_subledger = (!empty($paymentvariousstatic->subledger_account) ? $paymentvariousstatic->subledger_account : ''); // NotDefined is a reserved word |
|
536 | + $tabpay[$obj->rowid]["account_various"] = $account_various; |
|
537 | + $tabtp[$obj->rowid][$account_subledger] = isset($tabtp[$obj->rowid][$account_subledger]) ? $tabtp[$obj->rowid][$account_subledger] + $amounttouse : $amounttouse; |
|
538 | + } elseif ($links[$key]['type'] == 'payment_loan') { |
|
539 | + $paymentloanstatic->id = $links[$key]['url_id']; |
|
540 | + $paymentloanstatic->ref = $links[$key]['url_id']; |
|
541 | + $paymentloanstatic->fk_loan = $links[$key]['url_id']; |
|
542 | + $tabpay[$obj->rowid]["lib"] .= ' '.$paymentloanstatic->getNomUrl(2); |
|
543 | + $tabpay[$obj->rowid]["paymentloanid"] = $paymentloanstatic->id; |
|
544 | + //$tabtp[$obj->rowid][$account_pay_loan] += $amounttouse; |
|
545 | + $sqlmid = 'SELECT pl.amount_capital, pl.amount_insurance, pl.amount_interest, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest'; |
|
546 | + $sqlmid .= ' FROM '.MAIN_DB_PREFIX.'payment_loan as pl, '.MAIN_DB_PREFIX.'loan as l'; |
|
547 | + $sqlmid .= ' WHERE l.rowid = pl.fk_loan AND pl.fk_bank = '.((int) $obj->rowid); |
|
548 | + |
|
549 | + dol_syslog("accountancy/journal/bankjournal.php:: sqlmid=".$sqlmid, LOG_DEBUG); |
|
550 | + $resultmid = $db->query($sqlmid); |
|
551 | + if ($resultmid) { |
|
552 | + $objmid = $db->fetch_object($resultmid); |
|
553 | + $tabtp[$obj->rowid][$objmid->accountancy_account_capital] = isset($tabtp[$obj->rowid][$objmid->accountancy_account_capital]) ? $tabtp[$obj->rowid][$objmid->accountancy_account_capital] - $objmid->amount_capital : $amounttouse; |
|
554 | + $tabtp[$obj->rowid][$objmid->accountancy_account_insurance] = isset($tabtp[$obj->rowid][$objmid->accountancy_account_insurance]) ? $tabtp[$obj->rowid][$objmid->accountancy_account_insurance] - $objmid->amount_insurance : $amounttouse; |
|
555 | + $tabtp[$obj->rowid][$objmid->accountancy_account_interest] = isset($tabtp[$obj->rowid][$objmid->accountancy_account_interest]) ? $tabtp[$obj->rowid][$objmid->accountancy_account_interest] - $objmid->amount_interes : $amounttouse; |
|
556 | + } |
|
557 | + } elseif ($links[$key]['type'] == 'banktransfert') { |
|
558 | + $accountLinestatic->fetch($links[$key]['url_id']); |
|
559 | + $tabpay[$obj->rowid]["lib"] .= ' '.$langs->trans("BankTransfer").'- '.$accountLinestatic ->getNomUrl(1); |
|
560 | + $tabtp[$obj->rowid][$account_transfer] = isset($tabtp[$obj->rowid][$account_transfer]) ? $tabtp[$obj->rowid][$account_transfer] + $amounttouse : $amounttouse; |
|
561 | + $bankaccountstatic->fetch($tabpay[$obj->rowid]['fk_bank_account']); |
|
562 | + $tabpay[$obj->rowid]["soclib"] = $bankaccountstatic->getNomUrl(2); |
|
563 | + } |
|
564 | + } |
|
565 | + } |
|
566 | 566 | |
567 | - if (empty($tabbq[$obj->rowid][$compta_bank])) { |
|
568 | - $tabbq[$obj->rowid][$compta_bank] = $amounttouse; |
|
569 | - } else { |
|
570 | - $tabbq[$obj->rowid][$compta_bank] += $amounttouse; |
|
571 | - } |
|
567 | + if (empty($tabbq[$obj->rowid][$compta_bank])) { |
|
568 | + $tabbq[$obj->rowid][$compta_bank] = $amounttouse; |
|
569 | + } else { |
|
570 | + $tabbq[$obj->rowid][$compta_bank] += $amounttouse; |
|
571 | + } |
|
572 | 572 | |
573 | - // If no links were found to know the amount on thirdparty, we try to guess it. |
|
574 | - // This may happens on bank entries without the links lines to 'company'. |
|
575 | - if (empty($tabtp[$obj->rowid]) && !empty($tabmoreinfo[$obj->rowid]['withdraw'])) { // If we don't find 'company' link because it is an old 'withdraw' record |
|
576 | - foreach ($links as $key => $val) { |
|
577 | - if ($links[$key]['type'] == 'payment') { |
|
578 | - // Get thirdparty |
|
579 | - $tmppayment->fetch($links[$key]['url_id']); |
|
580 | - $arrayofamounts = $tmppayment->getAmountsArray(); |
|
581 | - if (is_array($arrayofamounts)) { |
|
582 | - foreach ($arrayofamounts as $invoiceid => $amount) { |
|
583 | - $tmpinvoice->fetch($invoiceid); |
|
584 | - $tmpinvoice->fetch_thirdparty(); |
|
585 | - if ($tmpinvoice->thirdparty->code_compta_client) { |
|
586 | - $tabtp[$obj->rowid][$tmpinvoice->thirdparty->code_compta_client] += $amount; |
|
587 | - } |
|
588 | - } |
|
589 | - } |
|
590 | - } |
|
591 | - } |
|
592 | - } |
|
573 | + // If no links were found to know the amount on thirdparty, we try to guess it. |
|
574 | + // This may happens on bank entries without the links lines to 'company'. |
|
575 | + if (empty($tabtp[$obj->rowid]) && !empty($tabmoreinfo[$obj->rowid]['withdraw'])) { // If we don't find 'company' link because it is an old 'withdraw' record |
|
576 | + foreach ($links as $key => $val) { |
|
577 | + if ($links[$key]['type'] == 'payment') { |
|
578 | + // Get thirdparty |
|
579 | + $tmppayment->fetch($links[$key]['url_id']); |
|
580 | + $arrayofamounts = $tmppayment->getAmountsArray(); |
|
581 | + if (is_array($arrayofamounts)) { |
|
582 | + foreach ($arrayofamounts as $invoiceid => $amount) { |
|
583 | + $tmpinvoice->fetch($invoiceid); |
|
584 | + $tmpinvoice->fetch_thirdparty(); |
|
585 | + if ($tmpinvoice->thirdparty->code_compta_client) { |
|
586 | + $tabtp[$obj->rowid][$tmpinvoice->thirdparty->code_compta_client] += $amount; |
|
587 | + } |
|
588 | + } |
|
589 | + } |
|
590 | + } |
|
591 | + } |
|
592 | + } |
|
593 | 593 | |
594 | - // If no links were found to know the amount on thirdparty/user, we init it to account 'NotDefined'. |
|
595 | - if (empty($tabtp[$obj->rowid])) { |
|
596 | - $tabtp[$obj->rowid]['NotDefined'] = $tabbq[$obj->rowid][$compta_bank]; |
|
597 | - } |
|
594 | + // If no links were found to know the amount on thirdparty/user, we init it to account 'NotDefined'. |
|
595 | + if (empty($tabtp[$obj->rowid])) { |
|
596 | + $tabtp[$obj->rowid]['NotDefined'] = $tabbq[$obj->rowid][$compta_bank]; |
|
597 | + } |
|
598 | 598 | |
599 | - // Check account number is ok |
|
600 | - /*if ($action == 'writebookkeeping') // Make test now in such a case |
|
599 | + // Check account number is ok |
|
600 | + /*if ($action == 'writebookkeeping') // Make test now in such a case |
|
601 | 601 | { |
602 | 602 | reset($tabbq[$obj->rowid]); |
603 | 603 | $first_key_tabbq = key($tabbq[$obj->rowid]); |
@@ -615,12 +615,12 @@ discard block |
||
615 | 615 | } |
616 | 616 | }*/ |
617 | 617 | |
618 | - // if($obj->socid)$tabtp[$obj->rowid][$compta_soc] += $amounttouse; |
|
618 | + // if($obj->socid)$tabtp[$obj->rowid][$compta_soc] += $amounttouse; |
|
619 | 619 | |
620 | - $i++; |
|
621 | - } |
|
620 | + $i++; |
|
621 | + } |
|
622 | 622 | } else { |
623 | - dol_print_error($db); |
|
623 | + dol_print_error($db); |
|
624 | 624 | } |
625 | 625 | |
626 | 626 | |
@@ -632,454 +632,454 @@ discard block |
||
632 | 632 | |
633 | 633 | // Write bookkeeping |
634 | 634 | if (!$error && $action == 'writebookkeeping') { |
635 | - $now = dol_now(); |
|
636 | - |
|
637 | - $accountingaccountcustomer = new AccountingAccount($db); |
|
638 | - $accountingaccountcustomer->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'), true); |
|
639 | - |
|
640 | - $accountingaccountsupplier = new AccountingAccount($db); |
|
641 | - $accountingaccountsupplier->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'), true); |
|
642 | - |
|
643 | - $accountingaccountpayment = new AccountingAccount($db); |
|
644 | - $accountingaccountpayment->fetch(null, getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'), true); |
|
645 | - |
|
646 | - $accountingaccountexpensereport = new AccountingAccount($db); |
|
647 | - $accountingaccountexpensereport->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_EXPENSEREPORT, true); |
|
648 | - |
|
649 | - $accountingaccountsuspense = new AccountingAccount($db); |
|
650 | - $accountingaccountsuspense->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'), true); |
|
651 | - |
|
652 | - $error = 0; |
|
653 | - foreach ($tabpay as $key => $val) { // $key is rowid into llx_bank |
|
654 | - $date = dol_print_date($val["date"], 'day'); |
|
655 | - |
|
656 | - $ref = getSourceDocRef($val, $tabtype[$key]); |
|
657 | - |
|
658 | - $errorforline = 0; |
|
659 | - |
|
660 | - $totalcredit = 0; |
|
661 | - $totaldebit = 0; |
|
662 | - |
|
663 | - $db->begin(); |
|
664 | - |
|
665 | - // Introduce a protection. Total of tabtp must be total of tabbq |
|
666 | - //var_dump($tabpay); |
|
667 | - //var_dump($tabtp); |
|
668 | - //var_dump($tabbq);exit; |
|
669 | - |
|
670 | - // Bank |
|
671 | - if (!$errorforline && is_array($tabbq[$key])) { |
|
672 | - // Line into bank account |
|
673 | - foreach ($tabbq[$key] as $k => $mt) { |
|
674 | - if ($mt) { |
|
675 | - if (empty($conf->cache['accountingaccountincurrententity'][$k])) { |
|
676 | - $accountingaccount = new AccountingAccount($db); |
|
677 | - $accountingaccount->fetch(0, $k, true); // $k is accounting account of the bank. |
|
678 | - $conf->cache['accountingaccountincurrententity'][$k] = $accountingaccount; |
|
679 | - } else { |
|
680 | - $accountingaccount = $conf->cache['accountingaccountincurrententity'][$k]; |
|
681 | - } |
|
682 | - |
|
683 | - $account_label = $accountingaccount->label; |
|
684 | - |
|
685 | - $reflabel = ''; |
|
686 | - if (!empty($val['lib'])) { |
|
687 | - $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
688 | - } |
|
689 | - $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']); |
|
690 | - if (!empty($val['soclib'])) { |
|
691 | - $reflabel .= " - ".dol_string_nohtmltag($val['soclib']); |
|
692 | - } |
|
693 | - |
|
694 | - $bookkeeping = new BookKeeping($db); |
|
695 | - $bookkeeping->doc_date = $val["date"]; |
|
696 | - $bookkeeping->doc_ref = $ref; |
|
697 | - $bookkeeping->doc_type = 'bank'; |
|
698 | - $bookkeeping->fk_doc = $key; |
|
699 | - $bookkeeping->fk_docdet = $val["fk_bank"]; |
|
700 | - |
|
701 | - $bookkeeping->numero_compte = $k; |
|
702 | - $bookkeeping->label_compte = $account_label; |
|
703 | - |
|
704 | - $bookkeeping->label_operation = $reflabel; |
|
705 | - $bookkeeping->montant = $mt; |
|
706 | - $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C'; |
|
707 | - $bookkeeping->debit = ($mt >= 0 ? $mt : 0); |
|
708 | - $bookkeeping->credit = ($mt < 0 ? -$mt : 0); |
|
709 | - $bookkeeping->code_journal = $journal; |
|
710 | - $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
|
711 | - $bookkeeping->fk_user_author = $user->id; |
|
712 | - $bookkeeping->date_creation = $now; |
|
713 | - |
|
714 | - // No subledger_account value for the bank line but add a specific label_operation |
|
715 | - $bookkeeping->subledger_account = ''; |
|
716 | - $bookkeeping->label_operation = $reflabel; |
|
717 | - $bookkeeping->entity = $conf->entity; |
|
718 | - |
|
719 | - $totaldebit += $bookkeeping->debit; |
|
720 | - $totalcredit += $bookkeeping->credit; |
|
721 | - |
|
722 | - $result = $bookkeeping->create($user); |
|
723 | - if ($result < 0) { |
|
724 | - if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
|
725 | - $error++; |
|
726 | - $errorforline++; |
|
727 | - setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
|
728 | - } else { |
|
729 | - $error++; |
|
730 | - $errorforline++; |
|
731 | - setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
|
732 | - } |
|
733 | - } |
|
734 | - } |
|
735 | - } |
|
736 | - } |
|
737 | - |
|
738 | - // Third party |
|
739 | - if (!$errorforline) { |
|
740 | - if (is_array($tabtp[$key])) { |
|
741 | - // Line into thirdparty account |
|
742 | - foreach ($tabtp[$key] as $k => $mt) { |
|
743 | - if ($mt) { |
|
744 | - $lettering = false; |
|
745 | - |
|
746 | - $reflabel = ''; |
|
747 | - if (!empty($val['lib'])) { |
|
748 | - $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : ""); |
|
749 | - } |
|
750 | - if ($tabtype[$key] == 'banktransfert') { |
|
751 | - $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer); |
|
752 | - } else { |
|
753 | - $reflabel .= dol_string_nohtmltag($val['soclib']); |
|
754 | - } |
|
635 | + $now = dol_now(); |
|
636 | + |
|
637 | + $accountingaccountcustomer = new AccountingAccount($db); |
|
638 | + $accountingaccountcustomer->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'), true); |
|
639 | + |
|
640 | + $accountingaccountsupplier = new AccountingAccount($db); |
|
641 | + $accountingaccountsupplier->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'), true); |
|
642 | + |
|
643 | + $accountingaccountpayment = new AccountingAccount($db); |
|
644 | + $accountingaccountpayment->fetch(null, getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'), true); |
|
645 | + |
|
646 | + $accountingaccountexpensereport = new AccountingAccount($db); |
|
647 | + $accountingaccountexpensereport->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_EXPENSEREPORT, true); |
|
648 | + |
|
649 | + $accountingaccountsuspense = new AccountingAccount($db); |
|
650 | + $accountingaccountsuspense->fetch(null, getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'), true); |
|
651 | + |
|
652 | + $error = 0; |
|
653 | + foreach ($tabpay as $key => $val) { // $key is rowid into llx_bank |
|
654 | + $date = dol_print_date($val["date"], 'day'); |
|
655 | + |
|
656 | + $ref = getSourceDocRef($val, $tabtype[$key]); |
|
657 | + |
|
658 | + $errorforline = 0; |
|
659 | + |
|
660 | + $totalcredit = 0; |
|
661 | + $totaldebit = 0; |
|
662 | + |
|
663 | + $db->begin(); |
|
664 | + |
|
665 | + // Introduce a protection. Total of tabtp must be total of tabbq |
|
666 | + //var_dump($tabpay); |
|
667 | + //var_dump($tabtp); |
|
668 | + //var_dump($tabbq);exit; |
|
669 | + |
|
670 | + // Bank |
|
671 | + if (!$errorforline && is_array($tabbq[$key])) { |
|
672 | + // Line into bank account |
|
673 | + foreach ($tabbq[$key] as $k => $mt) { |
|
674 | + if ($mt) { |
|
675 | + if (empty($conf->cache['accountingaccountincurrententity'][$k])) { |
|
676 | + $accountingaccount = new AccountingAccount($db); |
|
677 | + $accountingaccount->fetch(0, $k, true); // $k is accounting account of the bank. |
|
678 | + $conf->cache['accountingaccountincurrententity'][$k] = $accountingaccount; |
|
679 | + } else { |
|
680 | + $accountingaccount = $conf->cache['accountingaccountincurrententity'][$k]; |
|
681 | + } |
|
682 | + |
|
683 | + $account_label = $accountingaccount->label; |
|
684 | + |
|
685 | + $reflabel = ''; |
|
686 | + if (!empty($val['lib'])) { |
|
687 | + $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
688 | + } |
|
689 | + $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']); |
|
690 | + if (!empty($val['soclib'])) { |
|
691 | + $reflabel .= " - ".dol_string_nohtmltag($val['soclib']); |
|
692 | + } |
|
693 | + |
|
694 | + $bookkeeping = new BookKeeping($db); |
|
695 | + $bookkeeping->doc_date = $val["date"]; |
|
696 | + $bookkeeping->doc_ref = $ref; |
|
697 | + $bookkeeping->doc_type = 'bank'; |
|
698 | + $bookkeeping->fk_doc = $key; |
|
699 | + $bookkeeping->fk_docdet = $val["fk_bank"]; |
|
700 | + |
|
701 | + $bookkeeping->numero_compte = $k; |
|
702 | + $bookkeeping->label_compte = $account_label; |
|
703 | + |
|
704 | + $bookkeeping->label_operation = $reflabel; |
|
705 | + $bookkeeping->montant = $mt; |
|
706 | + $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C'; |
|
707 | + $bookkeeping->debit = ($mt >= 0 ? $mt : 0); |
|
708 | + $bookkeeping->credit = ($mt < 0 ? -$mt : 0); |
|
709 | + $bookkeeping->code_journal = $journal; |
|
710 | + $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
|
711 | + $bookkeeping->fk_user_author = $user->id; |
|
712 | + $bookkeeping->date_creation = $now; |
|
713 | + |
|
714 | + // No subledger_account value for the bank line but add a specific label_operation |
|
715 | + $bookkeeping->subledger_account = ''; |
|
716 | + $bookkeeping->label_operation = $reflabel; |
|
717 | + $bookkeeping->entity = $conf->entity; |
|
718 | + |
|
719 | + $totaldebit += $bookkeeping->debit; |
|
720 | + $totalcredit += $bookkeeping->credit; |
|
721 | + |
|
722 | + $result = $bookkeeping->create($user); |
|
723 | + if ($result < 0) { |
|
724 | + if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
|
725 | + $error++; |
|
726 | + $errorforline++; |
|
727 | + setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
|
728 | + } else { |
|
729 | + $error++; |
|
730 | + $errorforline++; |
|
731 | + setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
|
732 | + } |
|
733 | + } |
|
734 | + } |
|
735 | + } |
|
736 | + } |
|
755 | 737 | |
756 | - $bookkeeping = new BookKeeping($db); |
|
757 | - $bookkeeping->doc_date = $val["date"]; |
|
758 | - $bookkeeping->doc_ref = $ref; |
|
759 | - $bookkeeping->doc_type = 'bank'; |
|
760 | - $bookkeeping->fk_doc = $key; |
|
761 | - $bookkeeping->fk_docdet = $val["fk_bank"]; |
|
762 | - |
|
763 | - $bookkeeping->label_operation = $reflabel; |
|
764 | - $bookkeeping->montant = $mt; |
|
765 | - $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
|
766 | - $bookkeeping->debit = ($mt < 0 ? -$mt : 0); |
|
767 | - $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
|
768 | - $bookkeeping->code_journal = $journal; |
|
769 | - $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
|
770 | - $bookkeeping->fk_user_author = $user->id; |
|
771 | - $bookkeeping->date_creation = $now; |
|
772 | - |
|
773 | - if ($tabtype[$key] == 'payment') { // If payment is payment of customer invoice, we get ref of invoice |
|
774 | - $lettering = true; |
|
775 | - $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp |
|
776 | - $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction |
|
777 | - $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'); |
|
778 | - $bookkeeping->label_compte = $accountingaccountcustomer->label; |
|
779 | - } elseif ($tabtype[$key] == 'payment_supplier') { // If payment is payment of supplier invoice, we get ref of invoice |
|
780 | - $lettering = true; |
|
781 | - $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp |
|
782 | - $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction |
|
783 | - $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'); |
|
784 | - $bookkeeping->label_compte = $accountingaccountsupplier->label; |
|
785 | - } elseif ($tabtype[$key] == 'payment_expensereport') { |
|
786 | - $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code']; |
|
787 | - $bookkeeping->subledger_label = $tabuser[$key]['name']; |
|
788 | - $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT'); |
|
789 | - $bookkeeping->label_compte = $accountingaccountexpensereport->label; |
|
790 | - } elseif ($tabtype[$key] == 'payment_salary') { |
|
791 | - $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code']; |
|
792 | - $bookkeeping->subledger_label = $tabuser[$key]['name']; |
|
793 | - $bookkeeping->numero_compte = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
|
794 | - $bookkeeping->label_compte = $accountingaccountpayment->label; |
|
795 | - } elseif (in_array($tabtype[$key], array('sc', 'payment_sc'))) { // If payment is payment of social contribution |
|
796 | - $bookkeeping->subledger_account = ''; |
|
797 | - $bookkeeping->subledger_label = ''; |
|
798 | - $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
799 | - $bookkeeping->numero_compte = $k; |
|
800 | - $bookkeeping->label_compte = $accountingaccount->label; |
|
801 | - } elseif ($tabtype[$key] == 'payment_vat') { |
|
802 | - $bookkeeping->subledger_account = ''; |
|
803 | - $bookkeeping->subledger_label = ''; |
|
804 | - $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
805 | - $bookkeeping->numero_compte = $k; |
|
806 | - $bookkeeping->label_compte = $accountingaccount->label; |
|
807 | - } elseif ($tabtype[$key] == 'payment_donation') { |
|
808 | - $bookkeeping->subledger_account = ''; |
|
809 | - $bookkeeping->subledger_label = ''; |
|
810 | - $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
811 | - $bookkeeping->numero_compte = $k; |
|
812 | - $bookkeeping->label_compte = $accountingaccount->label; |
|
813 | - } elseif ($tabtype[$key] == 'member') { |
|
814 | - $bookkeeping->subledger_account = ''; |
|
815 | - $bookkeeping->subledger_label = ''; |
|
816 | - $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
817 | - $bookkeeping->numero_compte = $k; |
|
818 | - $bookkeeping->label_compte = $accountingaccount->label; |
|
819 | - } elseif ($tabtype[$key] == 'payment_loan') { |
|
820 | - $bookkeeping->subledger_account = ''; |
|
821 | - $bookkeeping->subledger_label = ''; |
|
822 | - $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
823 | - $bookkeeping->numero_compte = $k; |
|
824 | - $bookkeeping->label_compte = $accountingaccount->label; |
|
825 | - } elseif ($tabtype[$key] == 'payment_various') { |
|
826 | - $bookkeeping->subledger_account = $k; |
|
827 | - $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
|
828 | - $accountingaccount->fetch(null, $tabpay[$key]["account_various"], true); // TODO Use a cache |
|
829 | - $bookkeeping->numero_compte = $tabpay[$key]["account_various"]; |
|
830 | - $bookkeeping->label_compte = $accountingaccount->label; |
|
831 | - } elseif ($tabtype[$key] == 'banktransfert') { |
|
832 | - $bookkeeping->subledger_account = ''; |
|
833 | - $bookkeeping->subledger_label = ''; |
|
834 | - $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
835 | - $bookkeeping->numero_compte = $k; |
|
836 | - $bookkeeping->label_compte = $accountingaccount->label; |
|
837 | - } else { |
|
838 | - if ($tabtype[$key] == 'unknown') { // Unknown transaction, we will use a waiting account for thirdparty. |
|
839 | - // Temporary account |
|
840 | - $bookkeeping->subledger_account = ''; |
|
841 | - $bookkeeping->subledger_label = ''; |
|
842 | - $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'); |
|
843 | - $bookkeeping->label_compte = $accountingaccountsuspense->label; |
|
844 | - } |
|
845 | - } |
|
846 | - $bookkeeping->label_operation = $reflabel; |
|
847 | - $bookkeeping->entity = $conf->entity; |
|
848 | - |
|
849 | - $totaldebit += $bookkeeping->debit; |
|
850 | - $totalcredit += $bookkeeping->credit; |
|
851 | - |
|
852 | - $result = $bookkeeping->create($user); |
|
853 | - if ($result < 0) { |
|
854 | - if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
|
855 | - $error++; |
|
856 | - $errorforline++; |
|
857 | - setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
|
858 | - } else { |
|
859 | - $error++; |
|
860 | - $errorforline++; |
|
861 | - setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
|
862 | - } |
|
863 | - } else { |
|
864 | - if ($lettering && getDolGlobalInt('ACCOUNTING_ENABLE_LETTERING') && getDolGlobalInt('ACCOUNTING_ENABLE_AUTOLETTERING')) { |
|
865 | - $lettering_static = new Lettering($db); |
|
866 | - $nb_lettering = $lettering_static->bookkeepingLetteringAll(array($bookkeeping->id)); |
|
867 | - } |
|
868 | - } |
|
869 | - } |
|
870 | - } |
|
871 | - } else { // If thirdparty unknown, output the waiting account |
|
872 | - foreach ($tabbq[$key] as $k => $mt) { |
|
873 | - if ($mt) { |
|
874 | - $reflabel = ''; |
|
875 | - if (!empty($val['lib'])) { |
|
876 | - $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
877 | - } |
|
878 | - $reflabel .= dol_string_nohtmltag('WaitingAccount'); |
|
879 | - |
|
880 | - $bookkeeping = new BookKeeping($db); |
|
881 | - $bookkeeping->doc_date = $val["date"]; |
|
882 | - $bookkeeping->doc_ref = $ref; |
|
883 | - $bookkeeping->doc_type = 'bank'; |
|
884 | - $bookkeeping->fk_doc = $key; |
|
885 | - $bookkeeping->fk_docdet = $val["fk_bank"]; |
|
886 | - $bookkeeping->montant = $mt; |
|
887 | - $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
|
888 | - $bookkeeping->debit = ($mt < 0 ? -$mt : 0); |
|
889 | - $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
|
890 | - $bookkeeping->code_journal = $journal; |
|
891 | - $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
|
892 | - $bookkeeping->fk_user_author = $user->id; |
|
893 | - $bookkeeping->date_creation = $now; |
|
894 | - $bookkeeping->label_compte = ''; |
|
895 | - $bookkeeping->label_operation = $reflabel; |
|
896 | - $bookkeeping->entity = $conf->entity; |
|
897 | - |
|
898 | - $totaldebit += $bookkeeping->debit; |
|
899 | - $totalcredit += $bookkeeping->credit; |
|
900 | - |
|
901 | - $result = $bookkeeping->create($user); |
|
902 | - |
|
903 | - if ($result < 0) { |
|
904 | - if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
|
905 | - $error++; |
|
906 | - $errorforline++; |
|
907 | - setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
|
908 | - } else { |
|
909 | - $error++; |
|
910 | - $errorforline++; |
|
911 | - setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
|
912 | - } |
|
913 | - } |
|
914 | - } |
|
915 | - } |
|
916 | - } |
|
917 | - } |
|
738 | + // Third party |
|
739 | + if (!$errorforline) { |
|
740 | + if (is_array($tabtp[$key])) { |
|
741 | + // Line into thirdparty account |
|
742 | + foreach ($tabtp[$key] as $k => $mt) { |
|
743 | + if ($mt) { |
|
744 | + $lettering = false; |
|
745 | + |
|
746 | + $reflabel = ''; |
|
747 | + if (!empty($val['lib'])) { |
|
748 | + $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : ""); |
|
749 | + } |
|
750 | + if ($tabtype[$key] == 'banktransfert') { |
|
751 | + $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer); |
|
752 | + } else { |
|
753 | + $reflabel .= dol_string_nohtmltag($val['soclib']); |
|
754 | + } |
|
755 | + |
|
756 | + $bookkeeping = new BookKeeping($db); |
|
757 | + $bookkeeping->doc_date = $val["date"]; |
|
758 | + $bookkeeping->doc_ref = $ref; |
|
759 | + $bookkeeping->doc_type = 'bank'; |
|
760 | + $bookkeeping->fk_doc = $key; |
|
761 | + $bookkeeping->fk_docdet = $val["fk_bank"]; |
|
762 | + |
|
763 | + $bookkeeping->label_operation = $reflabel; |
|
764 | + $bookkeeping->montant = $mt; |
|
765 | + $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
|
766 | + $bookkeeping->debit = ($mt < 0 ? -$mt : 0); |
|
767 | + $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
|
768 | + $bookkeeping->code_journal = $journal; |
|
769 | + $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
|
770 | + $bookkeeping->fk_user_author = $user->id; |
|
771 | + $bookkeeping->date_creation = $now; |
|
772 | + |
|
773 | + if ($tabtype[$key] == 'payment') { // If payment is payment of customer invoice, we get ref of invoice |
|
774 | + $lettering = true; |
|
775 | + $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp |
|
776 | + $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction |
|
777 | + $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'); |
|
778 | + $bookkeeping->label_compte = $accountingaccountcustomer->label; |
|
779 | + } elseif ($tabtype[$key] == 'payment_supplier') { // If payment is payment of supplier invoice, we get ref of invoice |
|
780 | + $lettering = true; |
|
781 | + $bookkeeping->subledger_account = $k; // For payment, the subledger account is stored as $key of $tabtp |
|
782 | + $bookkeeping->subledger_label = $tabcompany[$key]['name']; // $tabcompany is defined only if we are sure there is 1 thirdparty for the bank transaction |
|
783 | + $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'); |
|
784 | + $bookkeeping->label_compte = $accountingaccountsupplier->label; |
|
785 | + } elseif ($tabtype[$key] == 'payment_expensereport') { |
|
786 | + $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code']; |
|
787 | + $bookkeeping->subledger_label = $tabuser[$key]['name']; |
|
788 | + $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT'); |
|
789 | + $bookkeeping->label_compte = $accountingaccountexpensereport->label; |
|
790 | + } elseif ($tabtype[$key] == 'payment_salary') { |
|
791 | + $bookkeeping->subledger_account = $tabuser[$key]['accountancy_code']; |
|
792 | + $bookkeeping->subledger_label = $tabuser[$key]['name']; |
|
793 | + $bookkeeping->numero_compte = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
|
794 | + $bookkeeping->label_compte = $accountingaccountpayment->label; |
|
795 | + } elseif (in_array($tabtype[$key], array('sc', 'payment_sc'))) { // If payment is payment of social contribution |
|
796 | + $bookkeeping->subledger_account = ''; |
|
797 | + $bookkeeping->subledger_label = ''; |
|
798 | + $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
799 | + $bookkeeping->numero_compte = $k; |
|
800 | + $bookkeeping->label_compte = $accountingaccount->label; |
|
801 | + } elseif ($tabtype[$key] == 'payment_vat') { |
|
802 | + $bookkeeping->subledger_account = ''; |
|
803 | + $bookkeeping->subledger_label = ''; |
|
804 | + $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
805 | + $bookkeeping->numero_compte = $k; |
|
806 | + $bookkeeping->label_compte = $accountingaccount->label; |
|
807 | + } elseif ($tabtype[$key] == 'payment_donation') { |
|
808 | + $bookkeeping->subledger_account = ''; |
|
809 | + $bookkeeping->subledger_label = ''; |
|
810 | + $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
811 | + $bookkeeping->numero_compte = $k; |
|
812 | + $bookkeeping->label_compte = $accountingaccount->label; |
|
813 | + } elseif ($tabtype[$key] == 'member') { |
|
814 | + $bookkeeping->subledger_account = ''; |
|
815 | + $bookkeeping->subledger_label = ''; |
|
816 | + $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
817 | + $bookkeeping->numero_compte = $k; |
|
818 | + $bookkeeping->label_compte = $accountingaccount->label; |
|
819 | + } elseif ($tabtype[$key] == 'payment_loan') { |
|
820 | + $bookkeeping->subledger_account = ''; |
|
821 | + $bookkeeping->subledger_label = ''; |
|
822 | + $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
823 | + $bookkeeping->numero_compte = $k; |
|
824 | + $bookkeeping->label_compte = $accountingaccount->label; |
|
825 | + } elseif ($tabtype[$key] == 'payment_various') { |
|
826 | + $bookkeeping->subledger_account = $k; |
|
827 | + $bookkeeping->subledger_label = $tabcompany[$key]['name']; |
|
828 | + $accountingaccount->fetch(null, $tabpay[$key]["account_various"], true); // TODO Use a cache |
|
829 | + $bookkeeping->numero_compte = $tabpay[$key]["account_various"]; |
|
830 | + $bookkeeping->label_compte = $accountingaccount->label; |
|
831 | + } elseif ($tabtype[$key] == 'banktransfert') { |
|
832 | + $bookkeeping->subledger_account = ''; |
|
833 | + $bookkeeping->subledger_label = ''; |
|
834 | + $accountingaccount->fetch(null, $k, true); // TODO Use a cache |
|
835 | + $bookkeeping->numero_compte = $k; |
|
836 | + $bookkeeping->label_compte = $accountingaccount->label; |
|
837 | + } else { |
|
838 | + if ($tabtype[$key] == 'unknown') { // Unknown transaction, we will use a waiting account for thirdparty. |
|
839 | + // Temporary account |
|
840 | + $bookkeeping->subledger_account = ''; |
|
841 | + $bookkeeping->subledger_label = ''; |
|
842 | + $bookkeeping->numero_compte = getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'); |
|
843 | + $bookkeeping->label_compte = $accountingaccountsuspense->label; |
|
844 | + } |
|
845 | + } |
|
846 | + $bookkeeping->label_operation = $reflabel; |
|
847 | + $bookkeeping->entity = $conf->entity; |
|
848 | + |
|
849 | + $totaldebit += $bookkeeping->debit; |
|
850 | + $totalcredit += $bookkeeping->credit; |
|
851 | + |
|
852 | + $result = $bookkeeping->create($user); |
|
853 | + if ($result < 0) { |
|
854 | + if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
|
855 | + $error++; |
|
856 | + $errorforline++; |
|
857 | + setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
|
858 | + } else { |
|
859 | + $error++; |
|
860 | + $errorforline++; |
|
861 | + setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
|
862 | + } |
|
863 | + } else { |
|
864 | + if ($lettering && getDolGlobalInt('ACCOUNTING_ENABLE_LETTERING') && getDolGlobalInt('ACCOUNTING_ENABLE_AUTOLETTERING')) { |
|
865 | + $lettering_static = new Lettering($db); |
|
866 | + $nb_lettering = $lettering_static->bookkeepingLetteringAll(array($bookkeeping->id)); |
|
867 | + } |
|
868 | + } |
|
869 | + } |
|
870 | + } |
|
871 | + } else { // If thirdparty unknown, output the waiting account |
|
872 | + foreach ($tabbq[$key] as $k => $mt) { |
|
873 | + if ($mt) { |
|
874 | + $reflabel = ''; |
|
875 | + if (!empty($val['lib'])) { |
|
876 | + $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
877 | + } |
|
878 | + $reflabel .= dol_string_nohtmltag('WaitingAccount'); |
|
879 | + |
|
880 | + $bookkeeping = new BookKeeping($db); |
|
881 | + $bookkeeping->doc_date = $val["date"]; |
|
882 | + $bookkeeping->doc_ref = $ref; |
|
883 | + $bookkeeping->doc_type = 'bank'; |
|
884 | + $bookkeeping->fk_doc = $key; |
|
885 | + $bookkeeping->fk_docdet = $val["fk_bank"]; |
|
886 | + $bookkeeping->montant = $mt; |
|
887 | + $bookkeeping->sens = ($mt < 0) ? 'D' : 'C'; |
|
888 | + $bookkeeping->debit = ($mt < 0 ? -$mt : 0); |
|
889 | + $bookkeeping->credit = ($mt >= 0) ? $mt : 0; |
|
890 | + $bookkeeping->code_journal = $journal; |
|
891 | + $bookkeeping->journal_label = $langs->transnoentities($journal_label); |
|
892 | + $bookkeeping->fk_user_author = $user->id; |
|
893 | + $bookkeeping->date_creation = $now; |
|
894 | + $bookkeeping->label_compte = ''; |
|
895 | + $bookkeeping->label_operation = $reflabel; |
|
896 | + $bookkeeping->entity = $conf->entity; |
|
897 | + |
|
898 | + $totaldebit += $bookkeeping->debit; |
|
899 | + $totalcredit += $bookkeeping->credit; |
|
900 | + |
|
901 | + $result = $bookkeeping->create($user); |
|
902 | + |
|
903 | + if ($result < 0) { |
|
904 | + if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') { // Already exists |
|
905 | + $error++; |
|
906 | + $errorforline++; |
|
907 | + setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings'); |
|
908 | + } else { |
|
909 | + $error++; |
|
910 | + $errorforline++; |
|
911 | + setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors'); |
|
912 | + } |
|
913 | + } |
|
914 | + } |
|
915 | + } |
|
916 | + } |
|
917 | + } |
|
918 | 918 | |
919 | - if (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT')) { |
|
920 | - $error++; |
|
921 | - $errorforline++; |
|
922 | - setEventMessages('We tried to insert a non balanced transaction in book for '.$ref.'. Canceled. Surely a bug.', null, 'errors'); |
|
923 | - } |
|
919 | + if (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT')) { |
|
920 | + $error++; |
|
921 | + $errorforline++; |
|
922 | + setEventMessages('We tried to insert a non balanced transaction in book for '.$ref.'. Canceled. Surely a bug.', null, 'errors'); |
|
923 | + } |
|
924 | 924 | |
925 | - if (!$errorforline) { |
|
926 | - $db->commit(); |
|
927 | - } else { |
|
928 | - //print 'KO for line '.$key.' '.$error.'<br>'; |
|
929 | - $db->rollback(); |
|
925 | + if (!$errorforline) { |
|
926 | + $db->commit(); |
|
927 | + } else { |
|
928 | + //print 'KO for line '.$key.' '.$error.'<br>'; |
|
929 | + $db->rollback(); |
|
930 | 930 | |
931 | - $MAXNBERRORS = 5; |
|
932 | - if ($error >= $MAXNBERRORS) { |
|
933 | - setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped").' (>'.$MAXNBERRORS.')', null, 'errors'); |
|
934 | - break; // Break in the foreach |
|
935 | - } |
|
936 | - } |
|
937 | - } |
|
938 | - |
|
939 | - if (empty($error) && count($tabpay) > 0) { |
|
940 | - setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); |
|
941 | - } elseif (count($tabpay) == $error) { |
|
942 | - setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings'); |
|
943 | - } else { |
|
944 | - setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); |
|
945 | - } |
|
946 | - |
|
947 | - $action = ''; |
|
948 | - |
|
949 | - // Must reload data, so we make a redirect |
|
950 | - if (count($tabpay) != $error) { |
|
951 | - $param = 'id_journal='.$id_journal; |
|
952 | - $param .= '&date_startday='.$date_startday; |
|
953 | - $param .= '&date_startmonth='.$date_startmonth; |
|
954 | - $param .= '&date_startyear='.$date_startyear; |
|
955 | - $param .= '&date_endday='.$date_endday; |
|
956 | - $param .= '&date_endmonth='.$date_endmonth; |
|
957 | - $param .= '&date_endyear='.$date_endyear; |
|
958 | - $param .= '&in_bookkeeping='.$in_bookkeeping; |
|
959 | - header("Location: ".$_SERVER['PHP_SELF'].($param ? '?'.$param : '')); |
|
960 | - exit; |
|
961 | - } |
|
931 | + $MAXNBERRORS = 5; |
|
932 | + if ($error >= $MAXNBERRORS) { |
|
933 | + setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped").' (>'.$MAXNBERRORS.')', null, 'errors'); |
|
934 | + break; // Break in the foreach |
|
935 | + } |
|
936 | + } |
|
937 | + } |
|
938 | + |
|
939 | + if (empty($error) && count($tabpay) > 0) { |
|
940 | + setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs'); |
|
941 | + } elseif (count($tabpay) == $error) { |
|
942 | + setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings'); |
|
943 | + } else { |
|
944 | + setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings'); |
|
945 | + } |
|
946 | + |
|
947 | + $action = ''; |
|
948 | + |
|
949 | + // Must reload data, so we make a redirect |
|
950 | + if (count($tabpay) != $error) { |
|
951 | + $param = 'id_journal='.$id_journal; |
|
952 | + $param .= '&date_startday='.$date_startday; |
|
953 | + $param .= '&date_startmonth='.$date_startmonth; |
|
954 | + $param .= '&date_startyear='.$date_startyear; |
|
955 | + $param .= '&date_endday='.$date_endday; |
|
956 | + $param .= '&date_endmonth='.$date_endmonth; |
|
957 | + $param .= '&date_endyear='.$date_endyear; |
|
958 | + $param .= '&in_bookkeeping='.$in_bookkeeping; |
|
959 | + header("Location: ".$_SERVER['PHP_SELF'].($param ? '?'.$param : '')); |
|
960 | + exit; |
|
961 | + } |
|
962 | 962 | } |
963 | 963 | |
964 | 964 | |
965 | 965 | |
966 | 966 | // Export |
967 | 967 | if ($action == 'exportcsv') { // ISO and not UTF8 ! |
968 | - $sep = getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV'); |
|
969 | - |
|
970 | - $filename = 'journal'; |
|
971 | - $type_export = 'journal'; |
|
972 | - include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php'; |
|
973 | - |
|
974 | - // CSV header line |
|
975 | - print '"'.$langs->transnoentitiesnoconv("BankId").'"'.$sep; |
|
976 | - print '"'.$langs->transnoentitiesnoconv("Date").'"'.$sep; |
|
977 | - print '"'.$langs->transnoentitiesnoconv("PaymentMode").'"'.$sep; |
|
978 | - print '"'.$langs->transnoentitiesnoconv("AccountAccounting").'"'.$sep; |
|
979 | - print '"'.$langs->transnoentitiesnoconv("LedgerAccount").'"'.$sep; |
|
980 | - print '"'.$langs->transnoentitiesnoconv("SubledgerAccount").'"'.$sep; |
|
981 | - print '"'.$langs->transnoentitiesnoconv("Label").'"'.$sep; |
|
982 | - print '"'.$langs->transnoentitiesnoconv("AccountingDebit").'"'.$sep; |
|
983 | - print '"'.$langs->transnoentitiesnoconv("AccountingCredit").'"'.$sep; |
|
984 | - print '"'.$langs->transnoentitiesnoconv("Journal").'"'.$sep; |
|
985 | - print '"'.$langs->transnoentitiesnoconv("Note").'"'.$sep; |
|
986 | - print "\n"; |
|
987 | - |
|
988 | - foreach ($tabpay as $key => $val) { |
|
989 | - $date = dol_print_date($val["date"], 'day'); |
|
990 | - |
|
991 | - $ref = getSourceDocRef($val, $tabtype[$key]); |
|
992 | - |
|
993 | - // Bank |
|
994 | - foreach ($tabbq[$key] as $k => $mt) { |
|
995 | - if ($mt) { |
|
996 | - $reflabel = ''; |
|
997 | - if (!empty($val['lib'])) { |
|
998 | - $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
999 | - } |
|
1000 | - $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']); |
|
1001 | - if (!empty($val['soclib'])) { |
|
1002 | - $reflabel .= " - ".dol_string_nohtmltag($val['soclib']); |
|
1003 | - } |
|
1004 | - |
|
1005 | - print '"'.$key.'"'.$sep; |
|
1006 | - print '"'.$date.'"'.$sep; |
|
1007 | - print '"'.$val["type_payment"].'"'.$sep; |
|
1008 | - print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
|
1009 | - print '"'.length_accounta(html_entity_decode($k)).'"'.$sep; |
|
1010 | - print " ".$sep; |
|
1011 | - print '"'.$reflabel.'"'.$sep; |
|
1012 | - print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
|
1013 | - print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
|
1014 | - print '"'.$journal.'"'.$sep; |
|
1015 | - print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
|
1016 | - print "\n"; |
|
1017 | - } |
|
1018 | - } |
|
968 | + $sep = getDolGlobalString('ACCOUNTING_EXPORT_SEPARATORCSV'); |
|
969 | + |
|
970 | + $filename = 'journal'; |
|
971 | + $type_export = 'journal'; |
|
972 | + include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php'; |
|
973 | + |
|
974 | + // CSV header line |
|
975 | + print '"'.$langs->transnoentitiesnoconv("BankId").'"'.$sep; |
|
976 | + print '"'.$langs->transnoentitiesnoconv("Date").'"'.$sep; |
|
977 | + print '"'.$langs->transnoentitiesnoconv("PaymentMode").'"'.$sep; |
|
978 | + print '"'.$langs->transnoentitiesnoconv("AccountAccounting").'"'.$sep; |
|
979 | + print '"'.$langs->transnoentitiesnoconv("LedgerAccount").'"'.$sep; |
|
980 | + print '"'.$langs->transnoentitiesnoconv("SubledgerAccount").'"'.$sep; |
|
981 | + print '"'.$langs->transnoentitiesnoconv("Label").'"'.$sep; |
|
982 | + print '"'.$langs->transnoentitiesnoconv("AccountingDebit").'"'.$sep; |
|
983 | + print '"'.$langs->transnoentitiesnoconv("AccountingCredit").'"'.$sep; |
|
984 | + print '"'.$langs->transnoentitiesnoconv("Journal").'"'.$sep; |
|
985 | + print '"'.$langs->transnoentitiesnoconv("Note").'"'.$sep; |
|
986 | + print "\n"; |
|
987 | + |
|
988 | + foreach ($tabpay as $key => $val) { |
|
989 | + $date = dol_print_date($val["date"], 'day'); |
|
990 | + |
|
991 | + $ref = getSourceDocRef($val, $tabtype[$key]); |
|
992 | + |
|
993 | + // Bank |
|
994 | + foreach ($tabbq[$key] as $k => $mt) { |
|
995 | + if ($mt) { |
|
996 | + $reflabel = ''; |
|
997 | + if (!empty($val['lib'])) { |
|
998 | + $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
999 | + } |
|
1000 | + $reflabel .= $langs->trans("Bank").' '.dol_string_nohtmltag($val['bank_account_ref']); |
|
1001 | + if (!empty($val['soclib'])) { |
|
1002 | + $reflabel .= " - ".dol_string_nohtmltag($val['soclib']); |
|
1003 | + } |
|
1019 | 1004 | |
1020 | - // Third party |
|
1021 | - if (is_array($tabtp[$key])) { |
|
1022 | - foreach ($tabtp[$key] as $k => $mt) { |
|
1023 | - if ($mt) { |
|
1024 | - $reflabel = ''; |
|
1025 | - if (!empty($val['lib'])) { |
|
1026 | - $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : ""); |
|
1027 | - } |
|
1028 | - if ($tabtype[$key] == 'banktransfert') { |
|
1029 | - $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer); |
|
1030 | - } else { |
|
1031 | - $reflabel .= dol_string_nohtmltag($val['soclib']); |
|
1032 | - } |
|
1005 | + print '"'.$key.'"'.$sep; |
|
1006 | + print '"'.$date.'"'.$sep; |
|
1007 | + print '"'.$val["type_payment"].'"'.$sep; |
|
1008 | + print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
|
1009 | + print '"'.length_accounta(html_entity_decode($k)).'"'.$sep; |
|
1010 | + print " ".$sep; |
|
1011 | + print '"'.$reflabel.'"'.$sep; |
|
1012 | + print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
|
1013 | + print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
|
1014 | + print '"'.$journal.'"'.$sep; |
|
1015 | + print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
|
1016 | + print "\n"; |
|
1017 | + } |
|
1018 | + } |
|
1033 | 1019 | |
1034 | - print '"'.$key.'"'.$sep; |
|
1035 | - print '"'.$date.'"'.$sep; |
|
1036 | - print '"'.$val["type_payment"].'"'.$sep; |
|
1037 | - print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
|
1038 | - if ($tabtype[$key] == 'payment_supplier') { |
|
1039 | - print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER').'"'.$sep; |
|
1040 | - } elseif ($tabtype[$key] == 'payment') { |
|
1041 | - print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER').'"'.$sep; |
|
1042 | - } elseif ($tabtype[$key] == 'payment_expensereport') { |
|
1043 | - print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT').'"'.$sep; |
|
1044 | - } elseif ($tabtype[$key] == 'payment_salary') { |
|
1045 | - print '"'.getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT').'"'.$sep; |
|
1046 | - } else { |
|
1047 | - print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
|
1048 | - } |
|
1049 | - print '"'.length_accounta(html_entity_decode($k)).'"'.$sep; |
|
1050 | - print '"'.$reflabel.'"'.$sep; |
|
1051 | - print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
|
1052 | - print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
|
1053 | - print '"'.$journal.'"'.$sep; |
|
1054 | - print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
|
1055 | - print "\n"; |
|
1056 | - } |
|
1057 | - } |
|
1058 | - } else { // If thirdparty unknown, output the waiting account |
|
1059 | - foreach ($tabbq[$key] as $k => $mt) { |
|
1060 | - if ($mt) { |
|
1061 | - $reflabel = ''; |
|
1062 | - if (!empty($val['lib'])) { |
|
1063 | - $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
1064 | - } |
|
1065 | - $reflabel .= dol_string_nohtmltag('WaitingAccount'); |
|
1066 | - |
|
1067 | - print '"'.$key.'"'.$sep; |
|
1068 | - print '"'.$date.'"'.$sep; |
|
1069 | - print '"'.$val["type_payment"].'"'.$sep; |
|
1070 | - print '"'.length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')).'"'.$sep; |
|
1071 | - print '"'.length_accounta(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')).'"'.$sep; |
|
1072 | - print $sep; |
|
1073 | - print '"'.$reflabel.'"'.$sep; |
|
1074 | - print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
|
1075 | - print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
|
1076 | - print '"'.$journal.'"'.$sep; |
|
1077 | - print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
|
1078 | - print "\n"; |
|
1079 | - } |
|
1080 | - } |
|
1081 | - } |
|
1082 | - } |
|
1020 | + // Third party |
|
1021 | + if (is_array($tabtp[$key])) { |
|
1022 | + foreach ($tabtp[$key] as $k => $mt) { |
|
1023 | + if ($mt) { |
|
1024 | + $reflabel = ''; |
|
1025 | + if (!empty($val['lib'])) { |
|
1026 | + $reflabel .= dol_string_nohtmltag($val['lib']).($val['soclib'] ? " - " : ""); |
|
1027 | + } |
|
1028 | + if ($tabtype[$key] == 'banktransfert') { |
|
1029 | + $reflabel .= dol_string_nohtmltag($langs->transnoentitiesnoconv('TransitionalAccount').' '.$account_transfer); |
|
1030 | + } else { |
|
1031 | + $reflabel .= dol_string_nohtmltag($val['soclib']); |
|
1032 | + } |
|
1033 | + |
|
1034 | + print '"'.$key.'"'.$sep; |
|
1035 | + print '"'.$date.'"'.$sep; |
|
1036 | + print '"'.$val["type_payment"].'"'.$sep; |
|
1037 | + print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
|
1038 | + if ($tabtype[$key] == 'payment_supplier') { |
|
1039 | + print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER').'"'.$sep; |
|
1040 | + } elseif ($tabtype[$key] == 'payment') { |
|
1041 | + print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER').'"'.$sep; |
|
1042 | + } elseif ($tabtype[$key] == 'payment_expensereport') { |
|
1043 | + print '"'.getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT').'"'.$sep; |
|
1044 | + } elseif ($tabtype[$key] == 'payment_salary') { |
|
1045 | + print '"'.getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT').'"'.$sep; |
|
1046 | + } else { |
|
1047 | + print '"'.length_accountg(html_entity_decode($k)).'"'.$sep; |
|
1048 | + } |
|
1049 | + print '"'.length_accounta(html_entity_decode($k)).'"'.$sep; |
|
1050 | + print '"'.$reflabel.'"'.$sep; |
|
1051 | + print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
|
1052 | + print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
|
1053 | + print '"'.$journal.'"'.$sep; |
|
1054 | + print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
|
1055 | + print "\n"; |
|
1056 | + } |
|
1057 | + } |
|
1058 | + } else { // If thirdparty unknown, output the waiting account |
|
1059 | + foreach ($tabbq[$key] as $k => $mt) { |
|
1060 | + if ($mt) { |
|
1061 | + $reflabel = ''; |
|
1062 | + if (!empty($val['lib'])) { |
|
1063 | + $reflabel .= dol_string_nohtmltag($val['lib'])." - "; |
|
1064 | + } |
|
1065 | + $reflabel .= dol_string_nohtmltag('WaitingAccount'); |
|
1066 | + |
|
1067 | + print '"'.$key.'"'.$sep; |
|
1068 | + print '"'.$date.'"'.$sep; |
|
1069 | + print '"'.$val["type_payment"].'"'.$sep; |
|
1070 | + print '"'.length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')).'"'.$sep; |
|
1071 | + print '"'.length_accounta(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')).'"'.$sep; |
|
1072 | + print $sep; |
|
1073 | + print '"'.$reflabel.'"'.$sep; |
|
1074 | + print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep; |
|
1075 | + print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep; |
|
1076 | + print '"'.$journal.'"'.$sep; |
|
1077 | + print '"'.dol_string_nohtmltag($ref).'"'.$sep; |
|
1078 | + print "\n"; |
|
1079 | + } |
|
1080 | + } |
|
1081 | + } |
|
1082 | + } |
|
1083 | 1083 | } |
1084 | 1084 | |
1085 | 1085 | |
@@ -1090,117 +1090,117 @@ discard block |
||
1090 | 1090 | $form = new Form($db); |
1091 | 1091 | |
1092 | 1092 | if (empty($action) || $action == 'view') { |
1093 | - $invoicestatic = new Facture($db); |
|
1094 | - $invoicesupplierstatic = new FactureFournisseur($db); |
|
1095 | - $expensereportstatic = new ExpenseReport($db); |
|
1096 | - $vatstatic = new Tva($db); |
|
1097 | - $donationstatic = new Don($db); |
|
1098 | - $loanstatic = new Loan($db); |
|
1099 | - $salarystatic = new Salary($db); |
|
1100 | - $variousstatic = new PaymentVarious($db); |
|
1101 | - |
|
1102 | - $title = $langs->trans("GenerationOfAccountingEntries").' - '.$accountingjournalstatic->getNomUrl(0, 2, 1, '', 1); |
|
1103 | - $help_url ='EN:Module_Double_Entry_Accounting|FR:Module_Comptabilité_en_Partie_Double#Génération_des_écritures_en_comptabilité'; |
|
1104 | - llxHeader('', dol_string_nohtmltag($title), $help_url, '', 0, 0, '', '', '', 'mod-accountancy accountancy-generation page-bankjournal'); |
|
1105 | - |
|
1106 | - $nom = $title; |
|
1107 | - $builddate = dol_now(); |
|
1108 | - //$description = $langs->trans("DescFinanceJournal") . '<br>'; |
|
1109 | - $description = $langs->trans("DescJournalOnlyBindedVisible").'<br>'; |
|
1110 | - |
|
1111 | - $listofchoices = array( |
|
1112 | - 'notyet' => $langs->trans("NotYetInGeneralLedger"), |
|
1113 | - 'already' => $langs->trans("AlreadyInGeneralLedger") |
|
1114 | - ); |
|
1115 | - $period = $form->selectDate($date_start ? $date_start : -1, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end ? $date_end : -1, 'date_end', 0, 0, 0, '', 1, 0); |
|
1116 | - $period .= ' - '.$langs->trans("JournalizationInLedgerStatus").' '.$form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1); |
|
1117 | - |
|
1118 | - $varlink = 'id_journal='.$id_journal; |
|
1119 | - $periodlink = ''; |
|
1120 | - $exportlink = ''; |
|
1121 | - |
|
1122 | - $listofchoices = array( |
|
1123 | - 1 => $langs->trans("TransfertAllBankLines"), |
|
1124 | - 2 => $langs->trans("TransfertOnlyConciliatedBankLine") |
|
1125 | - ); |
|
1126 | - $moreoptions = [ "BankLineConciliated" => $form->selectarray('only_rappro', $listofchoices, $only_rappro)]; |
|
1127 | - |
|
1128 | - journalHead($nom, '', $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink, $moreoptions); |
|
1129 | - |
|
1130 | - $desc = ''; |
|
1131 | - |
|
1132 | - if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') { |
|
1133 | - // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) |
|
1134 | - // Fiscal period test |
|
1135 | - $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE entity = ".((int) $conf->entity); |
|
1136 | - $resql = $db->query($sql); |
|
1137 | - if ($resql) { |
|
1138 | - $obj = $db->fetch_object($resql); |
|
1139 | - if ($obj->nb == 0) { |
|
1140 | - print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheFiscalPeriodIsNotDefined"); |
|
1141 | - $desc = ' : '.$langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}'); |
|
1142 | - $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("FiscalPeriod").'</strong>', $desc); |
|
1143 | - print $desc; |
|
1144 | - print '</div>'; |
|
1145 | - } |
|
1146 | - } else { |
|
1147 | - dol_print_error($db); |
|
1148 | - } |
|
1149 | - } |
|
1150 | - |
|
1151 | - // Bank test |
|
1152 | - $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."bank_account WHERE entity = ".((int) $conf->entity)." AND fk_accountancy_journal IS NULL AND clos=0"; |
|
1153 | - $resql = $db->query($sql); |
|
1154 | - if ($resql) { |
|
1155 | - $obj = $db->fetch_object($resql); |
|
1156 | - if ($obj->nb > 0) { |
|
1157 | - print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount"); |
|
1158 | - $desc = ' : '.$langs->trans("AccountancyAreaDescBank", 6, '{link}'); |
|
1159 | - $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("BankAccounts").'</strong>', $desc); |
|
1160 | - print $desc; |
|
1161 | - print '</div>'; |
|
1162 | - } |
|
1163 | - } else { |
|
1164 | - dol_print_error($db); |
|
1165 | - } |
|
1093 | + $invoicestatic = new Facture($db); |
|
1094 | + $invoicesupplierstatic = new FactureFournisseur($db); |
|
1095 | + $expensereportstatic = new ExpenseReport($db); |
|
1096 | + $vatstatic = new Tva($db); |
|
1097 | + $donationstatic = new Don($db); |
|
1098 | + $loanstatic = new Loan($db); |
|
1099 | + $salarystatic = new Salary($db); |
|
1100 | + $variousstatic = new PaymentVarious($db); |
|
1101 | + |
|
1102 | + $title = $langs->trans("GenerationOfAccountingEntries").' - '.$accountingjournalstatic->getNomUrl(0, 2, 1, '', 1); |
|
1103 | + $help_url ='EN:Module_Double_Entry_Accounting|FR:Module_Comptabilité_en_Partie_Double#Génération_des_écritures_en_comptabilité'; |
|
1104 | + llxHeader('', dol_string_nohtmltag($title), $help_url, '', 0, 0, '', '', '', 'mod-accountancy accountancy-generation page-bankjournal'); |
|
1105 | + |
|
1106 | + $nom = $title; |
|
1107 | + $builddate = dol_now(); |
|
1108 | + //$description = $langs->trans("DescFinanceJournal") . '<br>'; |
|
1109 | + $description = $langs->trans("DescJournalOnlyBindedVisible").'<br>'; |
|
1110 | + |
|
1111 | + $listofchoices = array( |
|
1112 | + 'notyet' => $langs->trans("NotYetInGeneralLedger"), |
|
1113 | + 'already' => $langs->trans("AlreadyInGeneralLedger") |
|
1114 | + ); |
|
1115 | + $period = $form->selectDate($date_start ? $date_start : -1, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end ? $date_end : -1, 'date_end', 0, 0, 0, '', 1, 0); |
|
1116 | + $period .= ' - '.$langs->trans("JournalizationInLedgerStatus").' '.$form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1); |
|
1117 | + |
|
1118 | + $varlink = 'id_journal='.$id_journal; |
|
1119 | + $periodlink = ''; |
|
1120 | + $exportlink = ''; |
|
1121 | + |
|
1122 | + $listofchoices = array( |
|
1123 | + 1 => $langs->trans("TransfertAllBankLines"), |
|
1124 | + 2 => $langs->trans("TransfertOnlyConciliatedBankLine") |
|
1125 | + ); |
|
1126 | + $moreoptions = [ "BankLineConciliated" => $form->selectarray('only_rappro', $listofchoices, $only_rappro)]; |
|
1127 | + |
|
1128 | + journalHead($nom, '', $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink, $moreoptions); |
|
1129 | + |
|
1130 | + $desc = ''; |
|
1131 | + |
|
1132 | + if (getDolGlobalString('ACCOUNTANCY_FISCAL_PERIOD_MODE') != 'blockedonclosed') { |
|
1133 | + // Test that setup is complete (we are in accounting, so test on entity is always on $conf->entity only, no sharing allowed) |
|
1134 | + // Fiscal period test |
|
1135 | + $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."accounting_fiscalyear WHERE entity = ".((int) $conf->entity); |
|
1136 | + $resql = $db->query($sql); |
|
1137 | + if ($resql) { |
|
1138 | + $obj = $db->fetch_object($resql); |
|
1139 | + if ($obj->nb == 0) { |
|
1140 | + print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheFiscalPeriodIsNotDefined"); |
|
1141 | + $desc = ' : '.$langs->trans("AccountancyAreaDescFiscalPeriod", 4, '{link}'); |
|
1142 | + $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("FiscalPeriod").'</strong>', $desc); |
|
1143 | + print $desc; |
|
1144 | + print '</div>'; |
|
1145 | + } |
|
1146 | + } else { |
|
1147 | + dol_print_error($db); |
|
1148 | + } |
|
1149 | + } |
|
1150 | + |
|
1151 | + // Bank test |
|
1152 | + $sql = "SELECT COUNT(rowid) as nb FROM ".MAIN_DB_PREFIX."bank_account WHERE entity = ".((int) $conf->entity)." AND fk_accountancy_journal IS NULL AND clos=0"; |
|
1153 | + $resql = $db->query($sql); |
|
1154 | + if ($resql) { |
|
1155 | + $obj = $db->fetch_object($resql); |
|
1156 | + if ($obj->nb > 0) { |
|
1157 | + print '<br><div class="warning">'.img_warning().' '.$langs->trans("TheJournalCodeIsNotDefinedOnSomeBankAccount"); |
|
1158 | + $desc = ' : '.$langs->trans("AccountancyAreaDescBank", 6, '{link}'); |
|
1159 | + $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("BankAccounts").'</strong>', $desc); |
|
1160 | + print $desc; |
|
1161 | + print '</div>'; |
|
1162 | + } |
|
1163 | + } else { |
|
1164 | + dol_print_error($db); |
|
1165 | + } |
|
1166 | 1166 | |
1167 | 1167 | |
1168 | - // Button to write into Ledger |
|
1169 | - if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' |
|
1170 | - || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1' |
|
1171 | - || (isModEnabled("salaries") && (getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == "" || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1')) |
|
1172 | - || (isModEnabled("expensereport") && (getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT') == '-1'))) { |
|
1168 | + // Button to write into Ledger |
|
1169 | + if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' |
|
1170 | + || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1' |
|
1171 | + || (isModEnabled("salaries") && (getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == "" || getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT') == '-1')) |
|
1172 | + || (isModEnabled("expensereport") && (getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT') == '-1'))) { |
|
1173 | 1173 | |
1174 | 1174 | |
1175 | - print($desc ? '' : '<br>').'<div class="warning">'.img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone"); |
|
1176 | - $desc = ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '{link}'); |
|
1177 | - $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>', $desc); |
|
1178 | - print $desc; |
|
1179 | - print '</div>'; |
|
1180 | - } |
|
1175 | + print($desc ? '' : '<br>').'<div class="warning">'.img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone"); |
|
1176 | + $desc = ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '{link}'); |
|
1177 | + $desc = str_replace('{link}', '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>', $desc); |
|
1178 | + print $desc; |
|
1179 | + print '</div>'; |
|
1180 | + } |
|
1181 | 1181 | |
1182 | 1182 | |
1183 | - print '<br><div class="tabsAction tabsActionNoBottom centerimp">'; |
|
1183 | + print '<br><div class="tabsAction tabsActionNoBottom centerimp">'; |
|
1184 | 1184 | |
1185 | - if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') { |
|
1186 | - print '<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans("ExportDraftJournal").'" onclick="launch_export();" />'; |
|
1187 | - } |
|
1185 | + if (getDolGlobalString('ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping == 'notyet') { |
|
1186 | + print '<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans("ExportDraftJournal").'" onclick="launch_export();" />'; |
|
1187 | + } |
|
1188 | 1188 | |
1189 | - if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' |
|
1190 | - || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1') { |
|
1191 | - print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="'.$langs->trans("WriteBookKeeping").'" />'; |
|
1192 | - } else { |
|
1193 | - if ($in_bookkeeping == 'notyet') { |
|
1194 | - print '<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans("WriteBookKeeping").'" onclick="writebookkeeping();" />'; |
|
1195 | - } else { |
|
1196 | - print '<a class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans("WriteBookKeeping").'</a>'; |
|
1197 | - } |
|
1198 | - } |
|
1189 | + if (getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER') == '-1' |
|
1190 | + || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == "" || getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER') == '-1') { |
|
1191 | + print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="'.$langs->trans("WriteBookKeeping").'" />'; |
|
1192 | + } else { |
|
1193 | + if ($in_bookkeeping == 'notyet') { |
|
1194 | + print '<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans("WriteBookKeeping").'" onclick="writebookkeeping();" />'; |
|
1195 | + } else { |
|
1196 | + print '<a class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans("WriteBookKeeping").'</a>'; |
|
1197 | + } |
|
1198 | + } |
|
1199 | 1199 | |
1200 | - print '</div>'; |
|
1200 | + print '</div>'; |
|
1201 | 1201 | |
1202 | - // TODO Avoid using js. We can use a direct link with $param |
|
1203 | - print ' |
|
1202 | + // TODO Avoid using js. We can use a direct link with $param |
|
1203 | + print ' |
|
1204 | 1204 | <script type="text/javascript"> |
1205 | 1205 | function launch_export() { |
1206 | 1206 | console.log("Set value into form and submit"); |
@@ -1216,259 +1216,259 @@ discard block |
||
1216 | 1216 | } |
1217 | 1217 | </script>'; |
1218 | 1218 | |
1219 | - /* |
|
1219 | + /* |
|
1220 | 1220 | * Show result array |
1221 | 1221 | */ |
1222 | - print '<br>'; |
|
1223 | - |
|
1224 | - $i = 0; |
|
1225 | - print '<div class="div-table-responsive">'; |
|
1226 | - print '<table class="noborder centpercent">'; |
|
1227 | - print '<tr class="liste_titre">'; |
|
1228 | - print "<td>".$langs->trans("Date")."</td>"; |
|
1229 | - print "<td>".$langs->trans("Piece").' ('.$langs->trans("ObjectsRef").")</td>"; |
|
1230 | - print "<td>".$langs->trans("AccountAccounting")."</td>"; |
|
1231 | - print "<td>".$langs->trans("SubledgerAccount")."</td>"; |
|
1232 | - print "<td>".$langs->trans("LabelOperation")."</td>"; |
|
1233 | - print '<td class="center">'.$langs->trans("PaymentMode")."</td>"; |
|
1234 | - print '<td class="right">'.$langs->trans("AccountingDebit")."</td>"; |
|
1235 | - print '<td class="right">'.$langs->trans("AccountingCredit")."</td>"; |
|
1236 | - print "</tr>\n"; |
|
1237 | - |
|
1238 | - $r = ''; |
|
1239 | - |
|
1240 | - foreach ($tabpay as $key => $val) { // $key is rowid in llx_bank |
|
1241 | - $date = dol_print_date($val["date"], 'day'); |
|
1242 | - |
|
1243 | - $ref = getSourceDocRef($val, $tabtype[$key]); |
|
1244 | - |
|
1245 | - // Bank |
|
1246 | - foreach ($tabbq[$key] as $k => $mt) { |
|
1247 | - if ($mt) { |
|
1248 | - $reflabel = ''; |
|
1249 | - if (!empty($val['lib'])) { |
|
1250 | - $reflabel .= $val['lib']." - "; |
|
1251 | - } |
|
1252 | - $reflabel .= $langs->trans("Bank").' '.$val['bank_account_ref']; |
|
1253 | - if (!empty($val['soclib'])) { |
|
1254 | - $reflabel .= " - ".$val['soclib']; |
|
1255 | - } |
|
1222 | + print '<br>'; |
|
1256 | 1223 | |
1257 | - //var_dump($tabpay[$key]); |
|
1258 | - print '<!-- Bank bank.rowid='.$key.' type='.$tabpay[$key]['type'].' ref='.$tabpay[$key]['ref'].'-->'; |
|
1259 | - print '<tr class="oddeven">'; |
|
1224 | + $i = 0; |
|
1225 | + print '<div class="div-table-responsive">'; |
|
1226 | + print '<table class="noborder centpercent">'; |
|
1227 | + print '<tr class="liste_titre">'; |
|
1228 | + print "<td>".$langs->trans("Date")."</td>"; |
|
1229 | + print "<td>".$langs->trans("Piece").' ('.$langs->trans("ObjectsRef").")</td>"; |
|
1230 | + print "<td>".$langs->trans("AccountAccounting")."</td>"; |
|
1231 | + print "<td>".$langs->trans("SubledgerAccount")."</td>"; |
|
1232 | + print "<td>".$langs->trans("LabelOperation")."</td>"; |
|
1233 | + print '<td class="center">'.$langs->trans("PaymentMode")."</td>"; |
|
1234 | + print '<td class="right">'.$langs->trans("AccountingDebit")."</td>"; |
|
1235 | + print '<td class="right">'.$langs->trans("AccountingCredit")."</td>"; |
|
1236 | + print "</tr>\n"; |
|
1237 | + |
|
1238 | + $r = ''; |
|
1239 | + |
|
1240 | + foreach ($tabpay as $key => $val) { // $key is rowid in llx_bank |
|
1241 | + $date = dol_print_date($val["date"], 'day'); |
|
1242 | + |
|
1243 | + $ref = getSourceDocRef($val, $tabtype[$key]); |
|
1244 | + |
|
1245 | + // Bank |
|
1246 | + foreach ($tabbq[$key] as $k => $mt) { |
|
1247 | + if ($mt) { |
|
1248 | + $reflabel = ''; |
|
1249 | + if (!empty($val['lib'])) { |
|
1250 | + $reflabel .= $val['lib']." - "; |
|
1251 | + } |
|
1252 | + $reflabel .= $langs->trans("Bank").' '.$val['bank_account_ref']; |
|
1253 | + if (!empty($val['soclib'])) { |
|
1254 | + $reflabel .= " - ".$val['soclib']; |
|
1255 | + } |
|
1260 | 1256 | |
1261 | - // Date |
|
1262 | - print "<td>".$date."</td>"; |
|
1257 | + //var_dump($tabpay[$key]); |
|
1258 | + print '<!-- Bank bank.rowid='.$key.' type='.$tabpay[$key]['type'].' ref='.$tabpay[$key]['ref'].'-->'; |
|
1259 | + print '<tr class="oddeven">'; |
|
1263 | 1260 | |
1264 | - // Ref |
|
1265 | - print "<td>".dol_escape_htmltag($ref)."</td>"; |
|
1261 | + // Date |
|
1262 | + print "<td>".$date."</td>"; |
|
1266 | 1263 | |
1267 | - // Ledger account |
|
1268 | - $accounttoshow = length_accountg($k); |
|
1269 | - if (empty($accounttoshow) || $accounttoshow == 'NotDefined') { |
|
1270 | - $accounttoshow = '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>'; |
|
1271 | - } |
|
1272 | - print '<td class="maxwidth300" title="'.dol_escape_htmltag(dol_string_nohtmltag($accounttoshow)).'">'; |
|
1273 | - print $accounttoshow; |
|
1274 | - print "</td>"; |
|
1264 | + // Ref |
|
1265 | + print "<td>".dol_escape_htmltag($ref)."</td>"; |
|
1275 | 1266 | |
1276 | - // Subledger account |
|
1277 | - print '<td class="maxwidth300">'; |
|
1278 | - /*$accounttoshow = length_accountg($k); |
|
1267 | + // Ledger account |
|
1268 | + $accounttoshow = length_accountg($k); |
|
1269 | + if (empty($accounttoshow) || $accounttoshow == 'NotDefined') { |
|
1270 | + $accounttoshow = '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>'; |
|
1271 | + } |
|
1272 | + print '<td class="maxwidth300" title="'.dol_escape_htmltag(dol_string_nohtmltag($accounttoshow)).'">'; |
|
1273 | + print $accounttoshow; |
|
1274 | + print "</td>"; |
|
1275 | + |
|
1276 | + // Subledger account |
|
1277 | + print '<td class="maxwidth300">'; |
|
1278 | + /*$accounttoshow = length_accountg($k); |
|
1279 | 1279 | if (empty($accounttoshow) || $accounttoshow == 'NotDefined') |
1280 | 1280 | { |
1281 | 1281 | print '<span class="error">'.$langs->trans("BankAccountNotDefined").'</span>'; |
1282 | 1282 | } |
1283 | 1283 | else print $accounttoshow;*/ |
1284 | - print "</td>"; |
|
1285 | - |
|
1286 | - // Label operation |
|
1287 | - print '<td>'; |
|
1288 | - print $reflabel; // This is already html escaped content |
|
1289 | - print "</td>"; |
|
1290 | - |
|
1291 | - print '<td class="center">'.$val["type_payment"]."</td>"; |
|
1292 | - print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
|
1293 | - print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
|
1294 | - print "</tr>"; |
|
1295 | - |
|
1296 | - $i++; |
|
1297 | - } |
|
1298 | - } |
|
1299 | - |
|
1300 | - // Third party |
|
1301 | - if (is_array($tabtp[$key])) { |
|
1302 | - foreach ($tabtp[$key] as $k => $mt) { |
|
1303 | - if ($mt) { |
|
1304 | - $reflabel = ''; |
|
1305 | - if (!empty($val['lib'])) { |
|
1306 | - $reflabel .= $val['lib'].(isset($val['soclib']) ? " - " : ""); |
|
1307 | - } |
|
1308 | - if ($tabtype[$key] == 'banktransfert') { |
|
1309 | - $reflabel .= $langs->trans('TransitionalAccount').' '.$account_transfer; |
|
1310 | - } else { |
|
1311 | - $reflabel .= isset($val['soclib']) ? $val['soclib'] : ""; |
|
1312 | - } |
|
1313 | - |
|
1314 | - print '<!-- Thirdparty bank.rowid='.$key.' -->'; |
|
1315 | - print '<tr class="oddeven">'; |
|
1316 | - |
|
1317 | - // Date |
|
1318 | - print "<td>".$date."</td>"; |
|
1319 | - |
|
1320 | - // Ref |
|
1321 | - print "<td>".dol_escape_htmltag($ref)."</td>"; |
|
1322 | - |
|
1323 | - // Ledger account |
|
1324 | - $account_ledger = $k; |
|
1325 | - // Try to force general ledger account depending on type |
|
1326 | - if ($tabtype[$key] == 'payment') { |
|
1327 | - $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'); |
|
1328 | - } |
|
1329 | - if ($tabtype[$key] == 'payment_supplier') { |
|
1330 | - $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'); |
|
1331 | - } |
|
1332 | - if ($tabtype[$key] == 'payment_expensereport') { |
|
1333 | - $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT'); |
|
1334 | - } |
|
1335 | - if ($tabtype[$key] == 'payment_salary') { |
|
1336 | - $account_ledger = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
|
1337 | - } |
|
1338 | - if ($tabtype[$key] == 'payment_vat') { |
|
1339 | - $account_ledger = getDolGlobalString('ACCOUNTING_VAT_PAY_ACCOUNT'); |
|
1340 | - } |
|
1341 | - if ($tabtype[$key] == 'member') { |
|
1342 | - $account_ledger = getDolGlobalString('ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT'); |
|
1343 | - } |
|
1344 | - if ($tabtype[$key] == 'payment_various') { |
|
1345 | - $account_ledger = $tabpay[$key]["account_various"]; |
|
1346 | - } |
|
1347 | - $accounttoshow = length_accountg($account_ledger); |
|
1348 | - if (empty($accounttoshow) || $accounttoshow == 'NotDefined') { |
|
1349 | - if ($tabtype[$key] == 'unknown') { |
|
1350 | - // We will accept writing, but into a waiting account |
|
1351 | - if (!getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE') || getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE') == '-1') { |
|
1352 | - $accounttoshow = '<span class="error small">'.$langs->trans('UnknownAccountForThirdpartyAndWaitingAccountNotDefinedBlocking').'</span>'; |
|
1353 | - } else { |
|
1354 | - $accounttoshow = '<span class="warning small">'.$langs->trans('UnknownAccountForThirdparty', length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'))).'</span>'; // We will use a waiting account |
|
1355 | - } |
|
1356 | - } else { |
|
1357 | - // We will refuse writing |
|
1358 | - $errorstring = 'UnknownAccountForThirdpartyBlocking'; |
|
1359 | - if ($tabtype[$key] == 'payment') { |
|
1360 | - $errorstring = 'MainAccountForCustomersNotDefined'; |
|
1361 | - } |
|
1362 | - if ($tabtype[$key] == 'payment_supplier') { |
|
1363 | - $errorstring = 'MainAccountForSuppliersNotDefined'; |
|
1364 | - } |
|
1365 | - if ($tabtype[$key] == 'payment_expensereport') { |
|
1366 | - $errorstring = 'MainAccountForUsersNotDefined'; |
|
1367 | - } |
|
1368 | - if ($tabtype[$key] == 'payment_salary') { |
|
1369 | - $errorstring = 'MainAccountForUsersNotDefined'; |
|
1370 | - } |
|
1371 | - if ($tabtype[$key] == 'payment_vat') { |
|
1372 | - $errorstring = 'MainAccountForVatPaymentNotDefined'; |
|
1373 | - } |
|
1374 | - if ($tabtype[$key] == 'member') { |
|
1375 | - $errorstring = 'MainAccountForSubscriptionPaymentNotDefined'; |
|
1376 | - } |
|
1377 | - $accounttoshow = '<span class="error small">'.$langs->trans($errorstring).'</span>'; |
|
1378 | - } |
|
1379 | - } |
|
1380 | - print '<td class="maxwidth300" title="'.dol_escape_htmltag(dol_string_nohtmltag($accounttoshow)).'">'; |
|
1381 | - print $accounttoshow; // This is a HTML string |
|
1382 | - print "</td>"; |
|
1383 | - |
|
1384 | - // Subledger account |
|
1385 | - $accounttoshowsubledger = ''; |
|
1386 | - if (in_array($tabtype[$key], array('payment', 'payment_supplier', 'payment_expensereport', 'payment_salary', 'payment_various'))) { // Type of payments that uses a subledger |
|
1387 | - $accounttoshowsubledger = length_accounta($k); |
|
1388 | - if ($accounttoshow != $accounttoshowsubledger) { |
|
1389 | - if (empty($accounttoshowsubledger) || $accounttoshowsubledger == 'NotDefined') { |
|
1390 | - //var_dump($tabpay[$key]); |
|
1391 | - //var_dump($tabtype[$key]); |
|
1392 | - //var_dump($tabbq[$key]); |
|
1393 | - //print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>'; |
|
1394 | - if (!empty($tabcompany[$key]['code_compta'])) { |
|
1395 | - if (in_array($tabtype[$key], array('payment_various', 'payment_salary'))) { |
|
1396 | - // For such case, if subledger is not defined, we won't use subledger accounts. |
|
1397 | - $accounttoshowsubledger = '<span class="warning small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownSubledgerIgnored").'</span>'; |
|
1398 | - } else { |
|
1399 | - $accounttoshowsubledger = '<span class="warning small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknown", $tabcompany[$key]['code_compta']).'</span>'; |
|
1400 | - } |
|
1401 | - } else { |
|
1402 | - $accounttoshowsubledger = '<span class="error small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownBlocking").'</span>'; |
|
1403 | - } |
|
1404 | - } |
|
1405 | - } else { |
|
1406 | - $accounttoshowsubledger = ''; |
|
1407 | - } |
|
1408 | - } |
|
1409 | - print '<td class="maxwidth300">'; |
|
1410 | - print $accounttoshowsubledger; // This is a html string |
|
1411 | - print "</td>"; |
|
1412 | - |
|
1413 | - print "<td>".$reflabel."</td>"; |
|
1414 | - |
|
1415 | - print '<td class="center">'.$val["type_payment"]."</td>"; |
|
1284 | + print "</td>"; |
|
1416 | 1285 | |
1417 | - print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
|
1286 | + // Label operation |
|
1287 | + print '<td>'; |
|
1288 | + print $reflabel; // This is already html escaped content |
|
1289 | + print "</td>"; |
|
1418 | 1290 | |
1419 | - print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
|
1291 | + print '<td class="center">'.$val["type_payment"]."</td>"; |
|
1292 | + print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
|
1293 | + print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
|
1294 | + print "</tr>"; |
|
1420 | 1295 | |
1421 | - print "</tr>"; |
|
1296 | + $i++; |
|
1297 | + } |
|
1298 | + } |
|
1422 | 1299 | |
1423 | - $i++; |
|
1424 | - } |
|
1425 | - } |
|
1426 | - } else { // Waiting account |
|
1427 | - foreach ($tabbq[$key] as $k => $mt) { |
|
1428 | - if ($mt) { |
|
1429 | - $reflabel = ''; |
|
1430 | - if (!empty($val['lib'])) { |
|
1431 | - $reflabel .= $val['lib']." - "; |
|
1432 | - } |
|
1433 | - $reflabel .= 'WaitingAccount'; |
|
1434 | - |
|
1435 | - print '<!-- Wait bank.rowid='.$key.' -->'; |
|
1436 | - print '<tr class="oddeven">'; |
|
1437 | - print "<td>".$date."</td>"; |
|
1438 | - print "<td>".$ref."</td>"; |
|
1439 | - // Ledger account |
|
1440 | - print "<td>"; |
|
1441 | - /*if (empty($accounttoshow) || $accounttoshow == 'NotDefined') |
|
1300 | + // Third party |
|
1301 | + if (is_array($tabtp[$key])) { |
|
1302 | + foreach ($tabtp[$key] as $k => $mt) { |
|
1303 | + if ($mt) { |
|
1304 | + $reflabel = ''; |
|
1305 | + if (!empty($val['lib'])) { |
|
1306 | + $reflabel .= $val['lib'].(isset($val['soclib']) ? " - " : ""); |
|
1307 | + } |
|
1308 | + if ($tabtype[$key] == 'banktransfert') { |
|
1309 | + $reflabel .= $langs->trans('TransitionalAccount').' '.$account_transfer; |
|
1310 | + } else { |
|
1311 | + $reflabel .= isset($val['soclib']) ? $val['soclib'] : ""; |
|
1312 | + } |
|
1313 | + |
|
1314 | + print '<!-- Thirdparty bank.rowid='.$key.' -->'; |
|
1315 | + print '<tr class="oddeven">'; |
|
1316 | + |
|
1317 | + // Date |
|
1318 | + print "<td>".$date."</td>"; |
|
1319 | + |
|
1320 | + // Ref |
|
1321 | + print "<td>".dol_escape_htmltag($ref)."</td>"; |
|
1322 | + |
|
1323 | + // Ledger account |
|
1324 | + $account_ledger = $k; |
|
1325 | + // Try to force general ledger account depending on type |
|
1326 | + if ($tabtype[$key] == 'payment') { |
|
1327 | + $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_CUSTOMER'); |
|
1328 | + } |
|
1329 | + if ($tabtype[$key] == 'payment_supplier') { |
|
1330 | + $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_SUPPLIER'); |
|
1331 | + } |
|
1332 | + if ($tabtype[$key] == 'payment_expensereport') { |
|
1333 | + $account_ledger = getDolGlobalString('ACCOUNTING_ACCOUNT_EXPENSEREPORT'); |
|
1334 | + } |
|
1335 | + if ($tabtype[$key] == 'payment_salary') { |
|
1336 | + $account_ledger = getDolGlobalString('SALARIES_ACCOUNTING_ACCOUNT_PAYMENT'); |
|
1337 | + } |
|
1338 | + if ($tabtype[$key] == 'payment_vat') { |
|
1339 | + $account_ledger = getDolGlobalString('ACCOUNTING_VAT_PAY_ACCOUNT'); |
|
1340 | + } |
|
1341 | + if ($tabtype[$key] == 'member') { |
|
1342 | + $account_ledger = getDolGlobalString('ADHERENT_SUBSCRIPTION_ACCOUNTINGACCOUNT'); |
|
1343 | + } |
|
1344 | + if ($tabtype[$key] == 'payment_various') { |
|
1345 | + $account_ledger = $tabpay[$key]["account_various"]; |
|
1346 | + } |
|
1347 | + $accounttoshow = length_accountg($account_ledger); |
|
1348 | + if (empty($accounttoshow) || $accounttoshow == 'NotDefined') { |
|
1349 | + if ($tabtype[$key] == 'unknown') { |
|
1350 | + // We will accept writing, but into a waiting account |
|
1351 | + if (!getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE') || getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE') == '-1') { |
|
1352 | + $accounttoshow = '<span class="error small">'.$langs->trans('UnknownAccountForThirdpartyAndWaitingAccountNotDefinedBlocking').'</span>'; |
|
1353 | + } else { |
|
1354 | + $accounttoshow = '<span class="warning small">'.$langs->trans('UnknownAccountForThirdparty', length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE'))).'</span>'; // We will use a waiting account |
|
1355 | + } |
|
1356 | + } else { |
|
1357 | + // We will refuse writing |
|
1358 | + $errorstring = 'UnknownAccountForThirdpartyBlocking'; |
|
1359 | + if ($tabtype[$key] == 'payment') { |
|
1360 | + $errorstring = 'MainAccountForCustomersNotDefined'; |
|
1361 | + } |
|
1362 | + if ($tabtype[$key] == 'payment_supplier') { |
|
1363 | + $errorstring = 'MainAccountForSuppliersNotDefined'; |
|
1364 | + } |
|
1365 | + if ($tabtype[$key] == 'payment_expensereport') { |
|
1366 | + $errorstring = 'MainAccountForUsersNotDefined'; |
|
1367 | + } |
|
1368 | + if ($tabtype[$key] == 'payment_salary') { |
|
1369 | + $errorstring = 'MainAccountForUsersNotDefined'; |
|
1370 | + } |
|
1371 | + if ($tabtype[$key] == 'payment_vat') { |
|
1372 | + $errorstring = 'MainAccountForVatPaymentNotDefined'; |
|
1373 | + } |
|
1374 | + if ($tabtype[$key] == 'member') { |
|
1375 | + $errorstring = 'MainAccountForSubscriptionPaymentNotDefined'; |
|
1376 | + } |
|
1377 | + $accounttoshow = '<span class="error small">'.$langs->trans($errorstring).'</span>'; |
|
1378 | + } |
|
1379 | + } |
|
1380 | + print '<td class="maxwidth300" title="'.dol_escape_htmltag(dol_string_nohtmltag($accounttoshow)).'">'; |
|
1381 | + print $accounttoshow; // This is a HTML string |
|
1382 | + print "</td>"; |
|
1383 | + |
|
1384 | + // Subledger account |
|
1385 | + $accounttoshowsubledger = ''; |
|
1386 | + if (in_array($tabtype[$key], array('payment', 'payment_supplier', 'payment_expensereport', 'payment_salary', 'payment_various'))) { // Type of payments that uses a subledger |
|
1387 | + $accounttoshowsubledger = length_accounta($k); |
|
1388 | + if ($accounttoshow != $accounttoshowsubledger) { |
|
1389 | + if (empty($accounttoshowsubledger) || $accounttoshowsubledger == 'NotDefined') { |
|
1390 | + //var_dump($tabpay[$key]); |
|
1391 | + //var_dump($tabtype[$key]); |
|
1392 | + //var_dump($tabbq[$key]); |
|
1393 | + //print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>'; |
|
1394 | + if (!empty($tabcompany[$key]['code_compta'])) { |
|
1395 | + if (in_array($tabtype[$key], array('payment_various', 'payment_salary'))) { |
|
1396 | + // For such case, if subledger is not defined, we won't use subledger accounts. |
|
1397 | + $accounttoshowsubledger = '<span class="warning small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownSubledgerIgnored").'</span>'; |
|
1398 | + } else { |
|
1399 | + $accounttoshowsubledger = '<span class="warning small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknown", $tabcompany[$key]['code_compta']).'</span>'; |
|
1400 | + } |
|
1401 | + } else { |
|
1402 | + $accounttoshowsubledger = '<span class="error small">'.$langs->trans("ThirdpartyAccountNotDefinedOrThirdPartyUnknownBlocking").'</span>'; |
|
1403 | + } |
|
1404 | + } |
|
1405 | + } else { |
|
1406 | + $accounttoshowsubledger = ''; |
|
1407 | + } |
|
1408 | + } |
|
1409 | + print '<td class="maxwidth300">'; |
|
1410 | + print $accounttoshowsubledger; // This is a html string |
|
1411 | + print "</td>"; |
|
1412 | + |
|
1413 | + print "<td>".$reflabel."</td>"; |
|
1414 | + |
|
1415 | + print '<td class="center">'.$val["type_payment"]."</td>"; |
|
1416 | + |
|
1417 | + print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
|
1418 | + |
|
1419 | + print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
|
1420 | + |
|
1421 | + print "</tr>"; |
|
1422 | + |
|
1423 | + $i++; |
|
1424 | + } |
|
1425 | + } |
|
1426 | + } else { // Waiting account |
|
1427 | + foreach ($tabbq[$key] as $k => $mt) { |
|
1428 | + if ($mt) { |
|
1429 | + $reflabel = ''; |
|
1430 | + if (!empty($val['lib'])) { |
|
1431 | + $reflabel .= $val['lib']." - "; |
|
1432 | + } |
|
1433 | + $reflabel .= 'WaitingAccount'; |
|
1434 | + |
|
1435 | + print '<!-- Wait bank.rowid='.$key.' -->'; |
|
1436 | + print '<tr class="oddeven">'; |
|
1437 | + print "<td>".$date."</td>"; |
|
1438 | + print "<td>".$ref."</td>"; |
|
1439 | + // Ledger account |
|
1440 | + print "<td>"; |
|
1441 | + /*if (empty($accounttoshow) || $accounttoshow == 'NotDefined') |
|
1442 | 1442 | { |
1443 | 1443 | print '<span class="error">'.$langs->trans("WaitAccountNotDefined").'</span>'; |
1444 | 1444 | } |
1445 | 1445 | else */ |
1446 | - print length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')); |
|
1447 | - print "</td>"; |
|
1448 | - // Subledger account |
|
1449 | - print "<td>"; |
|
1450 | - print "</td>"; |
|
1451 | - print "<td>".dol_escape_htmltag($reflabel)."</td>"; |
|
1452 | - print '<td class="center">'.$val["type_payment"]."</td>"; |
|
1453 | - print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
|
1454 | - print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
|
1455 | - print "</tr>"; |
|
1456 | - |
|
1457 | - $i++; |
|
1458 | - } |
|
1459 | - } |
|
1460 | - } |
|
1461 | - } |
|
1446 | + print length_accountg(getDolGlobalString('ACCOUNTING_ACCOUNT_SUSPENSE')); |
|
1447 | + print "</td>"; |
|
1448 | + // Subledger account |
|
1449 | + print "<td>"; |
|
1450 | + print "</td>"; |
|
1451 | + print "<td>".dol_escape_htmltag($reflabel)."</td>"; |
|
1452 | + print '<td class="center">'.$val["type_payment"]."</td>"; |
|
1453 | + print '<td class="right nowraponall amount">'.($mt < 0 ? price(-$mt) : '')."</td>"; |
|
1454 | + print '<td class="right nowraponall amount">'.($mt >= 0 ? price($mt) : '')."</td>"; |
|
1455 | + print "</tr>"; |
|
1456 | + |
|
1457 | + $i++; |
|
1458 | + } |
|
1459 | + } |
|
1460 | + } |
|
1461 | + } |
|
1462 | 1462 | |
1463 | - if (!$i) { |
|
1464 | - $colspan = 8; |
|
1465 | - print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>'; |
|
1466 | - } |
|
1463 | + if (!$i) { |
|
1464 | + $colspan = 8; |
|
1465 | + print '<tr class="oddeven"><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>'; |
|
1466 | + } |
|
1467 | 1467 | |
1468 | - print "</table>"; |
|
1469 | - print '</div>'; |
|
1468 | + print "</table>"; |
|
1469 | + print '</div>'; |
|
1470 | 1470 | |
1471 | - llxFooter(); |
|
1471 | + llxFooter(); |
|
1472 | 1472 | } |
1473 | 1473 | |
1474 | 1474 | $db->close(); |
@@ -1484,110 +1484,110 @@ discard block |
||
1484 | 1484 | */ |
1485 | 1485 | function getSourceDocRef($val, $typerecord) |
1486 | 1486 | { |
1487 | - global $db, $langs; |
|
1488 | - |
|
1489 | - // Defined the docref into $ref (We start with $val['ref'] by default and we complete according to other data) |
|
1490 | - // WE MUST HAVE SAME REF FOR ALL LINES WE WILL RECORD INTO THE BOOKKEEPING |
|
1491 | - $ref = $val['ref']; |
|
1492 | - if ($ref == '(SupplierInvoicePayment)' || $ref == '(SupplierInvoicePaymentBack)') { |
|
1493 | - $ref = $langs->transnoentitiesnoconv('Supplier'); |
|
1494 | - } |
|
1495 | - if ($ref == '(CustomerInvoicePayment)' || $ref == '(CustomerInvoicePaymentBack)') { |
|
1496 | - $ref = $langs->transnoentitiesnoconv('Customer'); |
|
1497 | - } |
|
1498 | - if ($ref == '(SocialContributionPayment)') { |
|
1499 | - $ref = $langs->transnoentitiesnoconv('SocialContribution'); |
|
1500 | - } |
|
1501 | - if ($ref == '(DonationPayment)') { |
|
1502 | - $ref = $langs->transnoentitiesnoconv('Donation'); |
|
1503 | - } |
|
1504 | - if ($ref == '(SubscriptionPayment)') { |
|
1505 | - $ref = $langs->transnoentitiesnoconv('Subscription'); |
|
1506 | - } |
|
1507 | - if ($ref == '(ExpenseReportPayment)') { |
|
1508 | - $ref = $langs->transnoentitiesnoconv('Employee'); |
|
1509 | - } |
|
1510 | - if ($ref == '(LoanPayment)') { |
|
1511 | - $ref = $langs->transnoentitiesnoconv('Loan'); |
|
1512 | - } |
|
1513 | - if ($ref == '(payment_salary)') { |
|
1514 | - $ref = $langs->transnoentitiesnoconv('Employee'); |
|
1515 | - } |
|
1516 | - |
|
1517 | - $sqlmid = ''; |
|
1518 | - if ($typerecord == 'payment') { |
|
1519 | - if (getDolGlobalInt('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) { |
|
1520 | - $sqlmid = "SELECT payfac.fk_facture as id, ".$db->ifsql('f1.rowid IS NULL', 'f.ref', 'f1.ref')." as ref"; |
|
1521 | - $sqlmid .= " FROM ".$db->prefix()."paiement_facture as payfac"; |
|
1522 | - $sqlmid .= " LEFT JOIN ".$db->prefix()."facture as f ON f.rowid = payfac.fk_facture"; |
|
1523 | - $sqlmid .= " LEFT JOIN ".$db->prefix()."societe_remise_except as sre ON sre.fk_facture_source = payfac.fk_facture"; |
|
1524 | - $sqlmid .= " LEFT JOIN ".$db->prefix()."facture as f1 ON f1.rowid = sre.fk_facture"; |
|
1525 | - $sqlmid .= " WHERE payfac.fk_paiement=".((int) $val['paymentid']); |
|
1526 | - } else { |
|
1527 | - $sqlmid = "SELECT payfac.fk_facture as id, f.ref as ref"; |
|
1528 | - $sqlmid .= " FROM ".$db->prefix()."paiement_facture as payfac"; |
|
1529 | - $sqlmid .= " INNER JOIN ".$db->prefix()."facture as f ON f.rowid = payfac.fk_facture"; |
|
1530 | - $sqlmid .= " WHERE payfac.fk_paiement=".((int) $val['paymentid']); |
|
1531 | - } |
|
1532 | - $ref = $langs->transnoentitiesnoconv("Invoice"); |
|
1533 | - } elseif ($typerecord == 'payment_supplier') { |
|
1534 | - $sqlmid = 'SELECT payfac.fk_facturefourn as id, f.ref'; |
|
1535 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementfourn_facturefourn as payfac, ".MAIN_DB_PREFIX."facture_fourn as f"; |
|
1536 | - $sqlmid .= " WHERE payfac.fk_facturefourn = f.rowid AND payfac.fk_paiementfourn=".((int) $val["paymentsupplierid"]); |
|
1537 | - $ref = $langs->transnoentitiesnoconv("SupplierInvoice"); |
|
1538 | - } elseif ($typerecord == 'payment_expensereport') { |
|
1539 | - $sqlmid = 'SELECT e.rowid as id, e.ref'; |
|
1540 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as pe, ".MAIN_DB_PREFIX."expensereport as e"; |
|
1541 | - $sqlmid .= " WHERE pe.rowid=".((int) $val["paymentexpensereport"])." AND pe.fk_expensereport = e.rowid"; |
|
1542 | - $ref = $langs->transnoentitiesnoconv("ExpenseReport"); |
|
1543 | - } elseif ($typerecord == 'payment_salary') { |
|
1544 | - $sqlmid = 'SELECT s.rowid as ref'; |
|
1545 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_salary as s"; |
|
1546 | - $sqlmid .= " WHERE s.rowid=".((int) $val["paymentsalid"]); |
|
1547 | - $ref = $langs->transnoentitiesnoconv("SalaryPayment"); |
|
1548 | - } elseif ($typerecord == 'sc') { |
|
1549 | - $sqlmid = 'SELECT sc.rowid as ref'; |
|
1550 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementcharge as sc"; |
|
1551 | - $sqlmid .= " WHERE sc.rowid=".((int) $val["paymentscid"]); |
|
1552 | - $ref = $langs->transnoentitiesnoconv("SocialContribution"); |
|
1553 | - } elseif ($typerecord == 'payment_vat') { |
|
1554 | - $sqlmid = 'SELECT v.rowid as ref'; |
|
1555 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."tva as v"; |
|
1556 | - $sqlmid .= " WHERE v.rowid=".((int) $val["paymentvatid"]); |
|
1557 | - $ref = $langs->transnoentitiesnoconv("PaymentVat"); |
|
1558 | - } elseif ($typerecord == 'payment_donation') { |
|
1559 | - $sqlmid = 'SELECT payd.fk_donation as ref'; |
|
1560 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_donation as payd"; |
|
1561 | - $sqlmid .= " WHERE payd.fk_donation=".((int) $val["paymentdonationid"]); |
|
1562 | - $ref = $langs->transnoentitiesnoconv("Donation"); |
|
1563 | - } elseif ($typerecord == 'payment_loan') { |
|
1564 | - $sqlmid = 'SELECT l.rowid as ref'; |
|
1565 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_loan as l"; |
|
1566 | - $sqlmid .= " WHERE l.rowid=".((int) $val["paymentloanid"]); |
|
1567 | - $ref = $langs->transnoentitiesnoconv("LoanPayment"); |
|
1568 | - } elseif ($typerecord == 'payment_various') { |
|
1569 | - $sqlmid = 'SELECT v.rowid as ref'; |
|
1570 | - $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_various as v"; |
|
1571 | - $sqlmid .= " WHERE v.rowid=".((int) $val["paymentvariousid"]); |
|
1572 | - $ref = $langs->transnoentitiesnoconv("VariousPayment"); |
|
1573 | - } |
|
1574 | - // Add warning |
|
1575 | - if (empty($sqlmid)) { |
|
1576 | - dol_syslog("Found a typerecord=".$typerecord." not supported", LOG_WARNING); |
|
1577 | - } |
|
1578 | - |
|
1579 | - if ($sqlmid) { |
|
1580 | - dol_syslog("accountancy/journal/bankjournal.php::sqlmid=".$sqlmid, LOG_DEBUG); |
|
1581 | - $resultmid = $db->query($sqlmid); |
|
1582 | - if ($resultmid) { |
|
1583 | - while ($objmid = $db->fetch_object($resultmid)) { |
|
1584 | - $ref .= ' '.$objmid->ref; |
|
1585 | - } |
|
1586 | - } else { |
|
1587 | - dol_print_error($db); |
|
1588 | - } |
|
1589 | - } |
|
1487 | + global $db, $langs; |
|
1488 | + |
|
1489 | + // Defined the docref into $ref (We start with $val['ref'] by default and we complete according to other data) |
|
1490 | + // WE MUST HAVE SAME REF FOR ALL LINES WE WILL RECORD INTO THE BOOKKEEPING |
|
1491 | + $ref = $val['ref']; |
|
1492 | + if ($ref == '(SupplierInvoicePayment)' || $ref == '(SupplierInvoicePaymentBack)') { |
|
1493 | + $ref = $langs->transnoentitiesnoconv('Supplier'); |
|
1494 | + } |
|
1495 | + if ($ref == '(CustomerInvoicePayment)' || $ref == '(CustomerInvoicePaymentBack)') { |
|
1496 | + $ref = $langs->transnoentitiesnoconv('Customer'); |
|
1497 | + } |
|
1498 | + if ($ref == '(SocialContributionPayment)') { |
|
1499 | + $ref = $langs->transnoentitiesnoconv('SocialContribution'); |
|
1500 | + } |
|
1501 | + if ($ref == '(DonationPayment)') { |
|
1502 | + $ref = $langs->transnoentitiesnoconv('Donation'); |
|
1503 | + } |
|
1504 | + if ($ref == '(SubscriptionPayment)') { |
|
1505 | + $ref = $langs->transnoentitiesnoconv('Subscription'); |
|
1506 | + } |
|
1507 | + if ($ref == '(ExpenseReportPayment)') { |
|
1508 | + $ref = $langs->transnoentitiesnoconv('Employee'); |
|
1509 | + } |
|
1510 | + if ($ref == '(LoanPayment)') { |
|
1511 | + $ref = $langs->transnoentitiesnoconv('Loan'); |
|
1512 | + } |
|
1513 | + if ($ref == '(payment_salary)') { |
|
1514 | + $ref = $langs->transnoentitiesnoconv('Employee'); |
|
1515 | + } |
|
1516 | + |
|
1517 | + $sqlmid = ''; |
|
1518 | + if ($typerecord == 'payment') { |
|
1519 | + if (getDolGlobalInt('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) { |
|
1520 | + $sqlmid = "SELECT payfac.fk_facture as id, ".$db->ifsql('f1.rowid IS NULL', 'f.ref', 'f1.ref')." as ref"; |
|
1521 | + $sqlmid .= " FROM ".$db->prefix()."paiement_facture as payfac"; |
|
1522 | + $sqlmid .= " LEFT JOIN ".$db->prefix()."facture as f ON f.rowid = payfac.fk_facture"; |
|
1523 | + $sqlmid .= " LEFT JOIN ".$db->prefix()."societe_remise_except as sre ON sre.fk_facture_source = payfac.fk_facture"; |
|
1524 | + $sqlmid .= " LEFT JOIN ".$db->prefix()."facture as f1 ON f1.rowid = sre.fk_facture"; |
|
1525 | + $sqlmid .= " WHERE payfac.fk_paiement=".((int) $val['paymentid']); |
|
1526 | + } else { |
|
1527 | + $sqlmid = "SELECT payfac.fk_facture as id, f.ref as ref"; |
|
1528 | + $sqlmid .= " FROM ".$db->prefix()."paiement_facture as payfac"; |
|
1529 | + $sqlmid .= " INNER JOIN ".$db->prefix()."facture as f ON f.rowid = payfac.fk_facture"; |
|
1530 | + $sqlmid .= " WHERE payfac.fk_paiement=".((int) $val['paymentid']); |
|
1531 | + } |
|
1532 | + $ref = $langs->transnoentitiesnoconv("Invoice"); |
|
1533 | + } elseif ($typerecord == 'payment_supplier') { |
|
1534 | + $sqlmid = 'SELECT payfac.fk_facturefourn as id, f.ref'; |
|
1535 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementfourn_facturefourn as payfac, ".MAIN_DB_PREFIX."facture_fourn as f"; |
|
1536 | + $sqlmid .= " WHERE payfac.fk_facturefourn = f.rowid AND payfac.fk_paiementfourn=".((int) $val["paymentsupplierid"]); |
|
1537 | + $ref = $langs->transnoentitiesnoconv("SupplierInvoice"); |
|
1538 | + } elseif ($typerecord == 'payment_expensereport') { |
|
1539 | + $sqlmid = 'SELECT e.rowid as id, e.ref'; |
|
1540 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_expensereport as pe, ".MAIN_DB_PREFIX."expensereport as e"; |
|
1541 | + $sqlmid .= " WHERE pe.rowid=".((int) $val["paymentexpensereport"])." AND pe.fk_expensereport = e.rowid"; |
|
1542 | + $ref = $langs->transnoentitiesnoconv("ExpenseReport"); |
|
1543 | + } elseif ($typerecord == 'payment_salary') { |
|
1544 | + $sqlmid = 'SELECT s.rowid as ref'; |
|
1545 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_salary as s"; |
|
1546 | + $sqlmid .= " WHERE s.rowid=".((int) $val["paymentsalid"]); |
|
1547 | + $ref = $langs->transnoentitiesnoconv("SalaryPayment"); |
|
1548 | + } elseif ($typerecord == 'sc') { |
|
1549 | + $sqlmid = 'SELECT sc.rowid as ref'; |
|
1550 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."paiementcharge as sc"; |
|
1551 | + $sqlmid .= " WHERE sc.rowid=".((int) $val["paymentscid"]); |
|
1552 | + $ref = $langs->transnoentitiesnoconv("SocialContribution"); |
|
1553 | + } elseif ($typerecord == 'payment_vat') { |
|
1554 | + $sqlmid = 'SELECT v.rowid as ref'; |
|
1555 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."tva as v"; |
|
1556 | + $sqlmid .= " WHERE v.rowid=".((int) $val["paymentvatid"]); |
|
1557 | + $ref = $langs->transnoentitiesnoconv("PaymentVat"); |
|
1558 | + } elseif ($typerecord == 'payment_donation') { |
|
1559 | + $sqlmid = 'SELECT payd.fk_donation as ref'; |
|
1560 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_donation as payd"; |
|
1561 | + $sqlmid .= " WHERE payd.fk_donation=".((int) $val["paymentdonationid"]); |
|
1562 | + $ref = $langs->transnoentitiesnoconv("Donation"); |
|
1563 | + } elseif ($typerecord == 'payment_loan') { |
|
1564 | + $sqlmid = 'SELECT l.rowid as ref'; |
|
1565 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_loan as l"; |
|
1566 | + $sqlmid .= " WHERE l.rowid=".((int) $val["paymentloanid"]); |
|
1567 | + $ref = $langs->transnoentitiesnoconv("LoanPayment"); |
|
1568 | + } elseif ($typerecord == 'payment_various') { |
|
1569 | + $sqlmid = 'SELECT v.rowid as ref'; |
|
1570 | + $sqlmid .= " FROM ".MAIN_DB_PREFIX."payment_various as v"; |
|
1571 | + $sqlmid .= " WHERE v.rowid=".((int) $val["paymentvariousid"]); |
|
1572 | + $ref = $langs->transnoentitiesnoconv("VariousPayment"); |
|
1573 | + } |
|
1574 | + // Add warning |
|
1575 | + if (empty($sqlmid)) { |
|
1576 | + dol_syslog("Found a typerecord=".$typerecord." not supported", LOG_WARNING); |
|
1577 | + } |
|
1578 | + |
|
1579 | + if ($sqlmid) { |
|
1580 | + dol_syslog("accountancy/journal/bankjournal.php::sqlmid=".$sqlmid, LOG_DEBUG); |
|
1581 | + $resultmid = $db->query($sqlmid); |
|
1582 | + if ($resultmid) { |
|
1583 | + while ($objmid = $db->fetch_object($resultmid)) { |
|
1584 | + $ref .= ' '.$objmid->ref; |
|
1585 | + } |
|
1586 | + } else { |
|
1587 | + dol_print_error($db); |
|
1588 | + } |
|
1589 | + } |
|
1590 | 1590 | |
1591 | - $ref = dol_trunc($langs->transnoentitiesnoconv("BankId").' '.$val['fk_bank'].' - '.$ref, 295); // 295 + 3 dots (...) is < than max size of 300 |
|
1592 | - return $ref; |
|
1591 | + $ref = dol_trunc($langs->transnoentitiesnoconv("BankId").' '.$val['fk_bank'].' - '.$ref, 295); // 295 + 3 dots (...) is < than max size of 300 |
|
1592 | + return $ref; |
|
1593 | 1593 | } |
@@ -260,7 +260,7 @@ discard block |
||
260 | 260 | return $this->LibStatut($this->statut, $mode, ((!empty($this->date_end)) ? ($this->date_end < dol_now() ? 1 : 0) : -1)); |
261 | 261 | } |
262 | 262 | |
263 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
263 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
264 | 264 | /** |
265 | 265 | * Return label of a contract line status |
266 | 266 | * |
@@ -273,7 +273,7 @@ discard block |
||
273 | 273 | */ |
274 | 274 | public static function LibStatut($status, $mode, $expired = -1, $moreatt = '', $morelabel = '') |
275 | 275 | { |
276 | - // phpcs:enable |
|
276 | + // phpcs:enable |
|
277 | 277 | global $langs; |
278 | 278 | $langs->load("contracts"); |
279 | 279 | |
@@ -709,7 +709,7 @@ discard block |
||
709 | 709 | } |
710 | 710 | |
711 | 711 | |
712 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
712 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
713 | 713 | /** |
714 | 714 | * Update in database the fields total_xxx of lines |
715 | 715 | * Used by migration process |
@@ -718,7 +718,7 @@ discard block |
||
718 | 718 | */ |
719 | 719 | public function update_total() |
720 | 720 | { |
721 | - // phpcs:enable |
|
721 | + // phpcs:enable |
|
722 | 722 | $this->db->begin(); |
723 | 723 | |
724 | 724 | // Mise a jour ligne en base |
@@ -836,7 +836,7 @@ discard block |
||
836 | 836 | } |
837 | 837 | } |
838 | 838 | |
839 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
839 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
840 | 840 | /** |
841 | 841 | * Activate a contract line |
842 | 842 | * |
@@ -848,7 +848,7 @@ discard block |
||
848 | 848 | */ |
849 | 849 | public function active_line($user, $date, $date_end = '', $comment = '') |
850 | 850 | { |
851 | - // phpcs:enable |
|
851 | + // phpcs:enable |
|
852 | 852 | $error = 0; |
853 | 853 | |
854 | 854 | $this->db->begin(); |
@@ -894,7 +894,7 @@ discard block |
||
894 | 894 | } |
895 | 895 | } |
896 | 896 | |
897 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
897 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
898 | 898 | /** |
899 | 899 | * Close a contract line |
900 | 900 | * |
@@ -906,7 +906,7 @@ discard block |
||
906 | 906 | */ |
907 | 907 | public function close_line($user, $date_end_real, $comment = '', $notrigger = 0) |
908 | 908 | { |
909 | - // phpcs:enable |
|
909 | + // phpcs:enable |
|
910 | 910 | $this->date_cloture = $date_end_real; |
911 | 911 | $this->date_end_real = $date_end_real; |
912 | 912 | $this->user_closing_id = $user->id; |
@@ -364,7 +364,7 @@ discard block |
||
364 | 364 | } |
365 | 365 | } |
366 | 366 | |
367 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
367 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
368 | 368 | /** |
369 | 369 | * Activate a contract line |
370 | 370 | * |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | */ |
378 | 378 | public function active_line($user, $line_id, $date_start, $date_end = '', $comment = '') |
379 | 379 | { |
380 | - // phpcs:enable |
|
380 | + // phpcs:enable |
|
381 | 381 | $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->active_line($user, $date_start, $date_end, $comment); |
382 | 382 | if ($result < 0) { |
383 | 383 | $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error; |
@@ -387,7 +387,7 @@ discard block |
||
387 | 387 | } |
388 | 388 | |
389 | 389 | |
390 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
390 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
391 | 391 | /** |
392 | 392 | * Close a contract line |
393 | 393 | * |
@@ -399,7 +399,7 @@ discard block |
||
399 | 399 | */ |
400 | 400 | public function close_line($user, $line_id, $date_end, $comment = '') |
401 | 401 | { |
402 | - // phpcs:enable |
|
402 | + // phpcs:enable |
|
403 | 403 | $result = $this->lines[$this->lines_id_index_mapper[$line_id]]->close_line($user, $date_end, $comment); |
404 | 404 | if ($result < 0) { |
405 | 405 | $this->error = $this->lines[$this->lines_id_index_mapper[$line_id]]->error; |
@@ -821,7 +821,7 @@ discard block |
||
821 | 821 | } |
822 | 822 | } |
823 | 823 | |
824 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
824 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
825 | 825 | /** |
826 | 826 | * Load lines array into this->lines. |
827 | 827 | * This set also nbofserviceswait, nbofservicesopened, nbofservicesexpired and nbofservicesclosed |
@@ -833,7 +833,7 @@ discard block |
||
833 | 833 | */ |
834 | 834 | public function fetch_lines($only_services = 0, $loadalsotranslation = 0, $noextrafields = 0) |
835 | 835 | { |
836 | - // phpcs:enable |
|
836 | + // phpcs:enable |
|
837 | 837 | $this->nbofservices = 0; |
838 | 838 | $this->nbofserviceswait = 0; |
839 | 839 | $this->nbofservicesopened = 0; |
@@ -1964,7 +1964,7 @@ discard block |
||
1964 | 1964 | } |
1965 | 1965 | |
1966 | 1966 | |
1967 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
1967 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
1968 | 1968 | /** |
1969 | 1969 | * Update statut of contract according to services |
1970 | 1970 | * |
@@ -1974,7 +1974,7 @@ discard block |
||
1974 | 1974 | */ |
1975 | 1975 | public function update_statut($user) |
1976 | 1976 | { |
1977 | - // phpcs:enable |
|
1977 | + // phpcs:enable |
|
1978 | 1978 | dol_syslog(__METHOD__ . " is deprecated", LOG_WARNING); |
1979 | 1979 | |
1980 | 1980 | // If draft, we keep it (should not happen) |
@@ -2006,7 +2006,7 @@ discard block |
||
2006 | 2006 | return $this->LibStatut($this->statut, $mode); |
2007 | 2007 | } |
2008 | 2008 | |
2009 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2009 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2010 | 2010 | /** |
2011 | 2011 | * Return the label of a given contrat status |
2012 | 2012 | * |
@@ -2016,7 +2016,7 @@ discard block |
||
2016 | 2016 | */ |
2017 | 2017 | public function LibStatut($status, $mode) |
2018 | 2018 | { |
2019 | - // phpcs:enable |
|
2019 | + // phpcs:enable |
|
2020 | 2020 | global $langs; |
2021 | 2021 | |
2022 | 2022 | if (empty($this->labelStatus) || empty($this->labelStatusShort)) { |
@@ -2226,7 +2226,7 @@ discard block |
||
2226 | 2226 | } |
2227 | 2227 | } |
2228 | 2228 | |
2229 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2229 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2230 | 2230 | /** |
2231 | 2231 | * Return list of line rowid |
2232 | 2232 | * |
@@ -2235,7 +2235,7 @@ discard block |
||
2235 | 2235 | */ |
2236 | 2236 | public function array_detail($status = -1) |
2237 | 2237 | { |
2238 | - // phpcs:enable |
|
2238 | + // phpcs:enable |
|
2239 | 2239 | $tab = array(); |
2240 | 2240 | |
2241 | 2241 | $sql = "SELECT cd.rowid"; |
@@ -2307,7 +2307,7 @@ discard block |
||
2307 | 2307 | } |
2308 | 2308 | |
2309 | 2309 | |
2310 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2310 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2311 | 2311 | /** |
2312 | 2312 | * Load indicators for dashboard (this->nbtodo and this->nbtodolate) |
2313 | 2313 | * |
@@ -2317,7 +2317,7 @@ discard block |
||
2317 | 2317 | */ |
2318 | 2318 | public function load_board($user, $mode) |
2319 | 2319 | { |
2320 | - // phpcs:enable |
|
2320 | + // phpcs:enable |
|
2321 | 2321 | global $conf, $langs; |
2322 | 2322 | |
2323 | 2323 | $this->from = " FROM " . MAIN_DB_PREFIX . "contrat as c"; |
@@ -61,1650 +61,1650 @@ |
||
61 | 61 | // syntax tree |
62 | 62 | class lessc_parser |
63 | 63 | { |
64 | - protected static $nextBlockId = 0; // used to uniquely identify blocks |
|
65 | - |
|
66 | - protected static $precedence = array( |
|
67 | - '=<' => 0, |
|
68 | - '>=' => 0, |
|
69 | - '=' => 0, |
|
70 | - '<' => 0, |
|
71 | - '>' => 0, |
|
72 | - |
|
73 | - '+' => 1, |
|
74 | - '-' => 1, |
|
75 | - '*' => 2, |
|
76 | - '/' => 2, |
|
77 | - '%' => 2, |
|
78 | - ); |
|
79 | - |
|
80 | - protected static $whitePattern; |
|
81 | - protected static $commentMulti; |
|
82 | - |
|
83 | - protected static $commentSingle = "//"; |
|
84 | - protected static $commentMultiLeft = "/*"; |
|
85 | - protected static $commentMultiRight = "*/"; |
|
86 | - |
|
87 | - // regex string to match any of the operators |
|
88 | - protected static $operatorString; |
|
89 | - |
|
90 | - // these properties will supress division unless it's inside parenthases |
|
91 | - protected static $supressDivisionProps = |
|
92 | - array('/border-radius$/i', '/^font$/i'); |
|
93 | - |
|
94 | - protected $blockDirectives = array("font-face", "keyframes", "page", "-moz-document", "viewport", "-moz-viewport", "-o-viewport", "-ms-viewport"); |
|
95 | - protected $lineDirectives = array("charset"); |
|
96 | - |
|
97 | - /** |
|
98 | - * if we are in parens we can be more liberal with whitespace around |
|
99 | - * operators because it must evaluate to a single value and thus is less |
|
100 | - * ambiguous. |
|
101 | - * |
|
102 | - * Consider: |
|
103 | - * property1: 10 -5; // is two numbers, 10 and -5 |
|
104 | - * property2: (10 -5); // should evaluate to 5 |
|
105 | - */ |
|
106 | - protected $inParens = false; |
|
107 | - |
|
108 | - // caches preg escaped literals |
|
109 | - protected static $literalCache = array(); |
|
110 | - |
|
111 | - public $env; |
|
112 | - public $buffer; |
|
113 | - public $count; |
|
114 | - public $line; |
|
115 | - public $eatWhiteDefault; |
|
116 | - public $lessc; |
|
117 | - public $sourceName; |
|
118 | - public $writeComments; |
|
119 | - public $seenComments; |
|
120 | - public $currentProperty; |
|
121 | - public $inExp; |
|
122 | - |
|
123 | - |
|
124 | - public function __construct($lessc, $sourceName = null) |
|
125 | - { |
|
126 | - $this->eatWhiteDefault = true; |
|
127 | - // reference to less needed for vPrefix, mPrefix, and parentSelector |
|
128 | - $this->lessc = $lessc; |
|
129 | - |
|
130 | - $this->sourceName = $sourceName; // name used for error messages |
|
131 | - |
|
132 | - $this->writeComments = false; |
|
133 | - |
|
134 | - if (!self::$operatorString) { |
|
135 | - self::$operatorString = |
|
136 | - '('.implode('|', array_map( |
|
137 | - array('lessc', 'preg_quote'), |
|
138 | - array_keys(self::$precedence) |
|
139 | - )).')'; |
|
140 | - |
|
141 | - $commentSingle = Lessc::preg_quote(self::$commentSingle); |
|
142 | - $commentMultiLeft = Lessc::preg_quote(self::$commentMultiLeft); |
|
143 | - $commentMultiRight = Lessc::preg_quote(self::$commentMultiRight); |
|
144 | - |
|
145 | - self::$commentMulti = $commentMultiLeft.'.*?'.$commentMultiRight; |
|
146 | - self::$whitePattern = '/'.$commentSingle.'[^\n]*\s*|('.self::$commentMulti.')\s*|\s+/Ais'; |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * Parse a string |
|
152 | - * |
|
153 | - * @param string $buffer String to parse |
|
154 | - * @throws exception |
|
155 | - * @return NULL|stdclass |
|
156 | - */ |
|
157 | - public function parse($buffer) |
|
158 | - { |
|
159 | - $this->count = 0; |
|
160 | - $this->line = 1; |
|
161 | - |
|
162 | - $this->env = null; // block stack |
|
163 | - $this->buffer = $this->writeComments ? $buffer : $this->removeComments($buffer); |
|
164 | - $this->pushSpecialBlock("root"); |
|
165 | - $this->eatWhiteDefault = true; |
|
166 | - $this->seenComments = array(); |
|
167 | - |
|
168 | - // trim whitespace on head |
|
169 | - // if (preg_match('/^\s+/', $this->buffer, $m)) { |
|
170 | - // $this->line += substr_count($m[0], "\n"); |
|
171 | - // $this->buffer = ltrim($this->buffer); |
|
172 | - // } |
|
173 | - $this->whitespace(); |
|
174 | - |
|
175 | - // parse the entire file |
|
176 | - while (false !== $this->parseChunk()); |
|
177 | - |
|
178 | - if ($this->count != strlen($this->buffer)) { |
|
179 | - $this->throwError('parse error count '.$this->count.' != len buffer '.strlen($this->buffer)); |
|
180 | - |
|
181 | - } |
|
182 | - |
|
183 | - // TODO report where the block was opened |
|
184 | - if (!property_exists($this->env, 'parent') || !is_null($this->env->parent)) { |
|
185 | - throw new exception('parse error: unclosed block'); |
|
186 | - } |
|
187 | - |
|
188 | - return $this->env; |
|
189 | - } |
|
190 | - |
|
191 | - /** |
|
192 | - * Parse a single chunk off the head of the buffer and append it to the |
|
193 | - * current parse environment. |
|
194 | - * Returns false when the buffer is empty, or when there is an error. |
|
195 | - * |
|
196 | - * This function is called repeatedly until the entire document is |
|
197 | - * parsed. |
|
198 | - * |
|
199 | - * This parser is most similar to a recursive descent parser. Single |
|
200 | - * functions represent discrete grammatical rules for the language, and |
|
201 | - * they are able to capture the text that represents those rules. |
|
202 | - * |
|
203 | - * Consider the function Lessc::keyword(). (all parse functions are |
|
204 | - * structured the same) |
|
205 | - * |
|
206 | - * The function takes a single reference argument. When calling the |
|
207 | - * function it will attempt to match a keyword on the head of the buffer. |
|
208 | - * If it is successful, it will place the keyword in the referenced |
|
209 | - * argument, advance the position in the buffer, and return true. If it |
|
210 | - * fails then it won't advance the buffer and it will return false. |
|
211 | - * |
|
212 | - * All of these parse functions are powered by Lessc::match(), which behaves |
|
213 | - * the same way, but takes a literal regular expression. Sometimes it is |
|
214 | - * more convenient to use match instead of creating a new function. |
|
215 | - * |
|
216 | - * Because of the format of the functions, to parse an entire string of |
|
217 | - * grammatical rules, you can chain them together using &&. |
|
218 | - * |
|
219 | - * But, if some of the rules in the chain succeed before one fails, then |
|
220 | - * the buffer position will be left at an invalid state. In order to |
|
221 | - * avoid this, Lessc::seek() is used to remember and set buffer positions. |
|
222 | - * |
|
223 | - * Before parsing a chain, use $s = $this->seek() to remember the current |
|
224 | - * position into $s. Then if a chain fails, use $this->seek($s) to |
|
225 | - * go back where we started. |
|
226 | - */ |
|
227 | - protected function parseChunk() |
|
228 | - { |
|
229 | - if (empty($this->buffer)) { |
|
230 | - return false; |
|
231 | - } |
|
232 | - $s = $this->seek(); |
|
233 | - |
|
234 | - if ($this->whitespace()) { |
|
235 | - return true; |
|
236 | - } |
|
237 | - |
|
238 | - $key = null; |
|
239 | - $value = null; |
|
240 | - $mediaQueries = null; |
|
241 | - $dirName = null; |
|
242 | - $dirValue = null; |
|
243 | - $importValue = null; |
|
244 | - $guards = null; |
|
245 | - $tag = null; |
|
246 | - $args = null; |
|
247 | - $isVararg = null; |
|
248 | - $argv = null; |
|
249 | - $suffix = null; |
|
250 | - $var = null; |
|
251 | - $tags = null; |
|
252 | - |
|
253 | - // setting a property |
|
254 | - if ($this->keyword($key) && $this->assign() && |
|
255 | - $this->propertyValue($value, $key) && $this->end() |
|
256 | - ) { |
|
257 | - $this->append(array('assign', $key, $value), $s); |
|
258 | - return true; |
|
259 | - } else { |
|
260 | - $this->seek($s); |
|
261 | - } |
|
262 | - |
|
263 | - |
|
264 | - // look for special css blocks |
|
265 | - if ($this->literal('@', false)) { |
|
266 | - $this->count--; |
|
267 | - |
|
268 | - // media |
|
269 | - if ($this->literal('@media')) { |
|
270 | - if ($this->mediaQueryList($mediaQueries) |
|
271 | - && $this->literal('{') |
|
272 | - ) { |
|
273 | - $media = $this->pushSpecialBlock("media"); |
|
274 | - $media->queries = is_null($mediaQueries) ? array() : $mediaQueries; |
|
275 | - return true; |
|
276 | - } else { |
|
277 | - $this->seek($s); |
|
278 | - return false; |
|
279 | - } |
|
280 | - } |
|
281 | - |
|
282 | - if ($this->literal("@", false) && $this->keyword($dirName)) { |
|
283 | - if ($this->isDirective($dirName, $this->blockDirectives)) { |
|
284 | - if ($this->openString("{", $dirValue, null, array(";")) && |
|
285 | - $this->literal("{") |
|
286 | - ) { |
|
287 | - $dir = $this->pushSpecialBlock("directive"); |
|
288 | - $dir->name = $dirName; |
|
289 | - if (isset($dirValue)) { |
|
290 | - $dir->value = $dirValue; |
|
291 | - } |
|
292 | - return true; |
|
293 | - } |
|
294 | - } elseif ($this->isDirective($dirName, $this->lineDirectives)) { |
|
295 | - if ($this->propertyValue($dirValue) && $this->end()) { |
|
296 | - $this->append(array("directive", $dirName, $dirValue)); |
|
297 | - return true; |
|
298 | - } |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - $this->seek($s); |
|
303 | - } |
|
304 | - |
|
305 | - // setting a variable |
|
306 | - if ($this->variable($var) && $this->assign() && |
|
307 | - $this->propertyValue($value) && $this->end() |
|
308 | - ) { |
|
309 | - $this->append(array('assign', $var, $value), $s); |
|
310 | - return true; |
|
311 | - } else { |
|
312 | - $this->seek($s); |
|
313 | - } |
|
314 | - |
|
315 | - if ($this->import($importValue)) { |
|
316 | - $this->append($importValue, $s); |
|
317 | - return true; |
|
318 | - } |
|
319 | - |
|
320 | - // opening parametric mixin |
|
321 | - if ($this->tag($tag, true) && $this->argumentDef($args, $isVararg) && |
|
322 | - $this->guards($guards) && |
|
323 | - $this->literal('{') |
|
324 | - ) { |
|
325 | - $block = $this->pushBlock($this->fixTags(array($tag))); |
|
326 | - $block->args = $args; |
|
327 | - $block->isVararg = $isVararg; |
|
328 | - if (!empty($guards)) { |
|
329 | - $block->guards = $guards; |
|
330 | - } |
|
331 | - return true; |
|
332 | - } else { |
|
333 | - $this->seek($s); |
|
334 | - } |
|
335 | - |
|
336 | - // opening a simple block |
|
337 | - if ($this->tags($tags) && $this->literal('{', false)) { |
|
338 | - $tags = $this->fixTags($tags); |
|
339 | - $this->pushBlock($tags); |
|
340 | - return true; |
|
341 | - } else { |
|
342 | - $this->seek($s); |
|
343 | - } |
|
344 | - |
|
345 | - // closing a block |
|
346 | - if ($this->literal('}', false)) { |
|
347 | - try { |
|
348 | - $block = $this->pop(); |
|
349 | - } catch (exception $e) { |
|
350 | - $this->seek($s); |
|
351 | - $this->throwError($e->getMessage()); |
|
352 | - } |
|
353 | - |
|
354 | - $hidden = false; |
|
355 | - if (is_null($block->type)) { |
|
356 | - $hidden = true; |
|
357 | - if (!isset($block->args)) { |
|
358 | - foreach ($block->tags as $tag) { |
|
359 | - if (!is_string($tag) || $tag[0] != $this->lessc->mPrefix) { |
|
360 | - $hidden = false; |
|
361 | - break; |
|
362 | - } |
|
363 | - } |
|
364 | - } |
|
365 | - |
|
366 | - foreach ($block->tags as $tag) { |
|
367 | - if (is_string($tag)) { |
|
368 | - $this->env->children[$tag][] = $block; |
|
369 | - } |
|
370 | - } |
|
371 | - } |
|
372 | - |
|
373 | - if (!$hidden) { |
|
374 | - $this->append(array('block', $block), $s); |
|
375 | - } |
|
376 | - |
|
377 | - // this is done here so comments aren't bundled into he block that |
|
378 | - // was just closed |
|
379 | - $this->whitespace(); |
|
380 | - return true; |
|
381 | - } |
|
382 | - |
|
383 | - // mixin |
|
384 | - if ($this->mixinTags($tags) && |
|
385 | - $this->argumentDef($argv, $isVararg) && |
|
386 | - $this->keyword($suffix) && $this->end() |
|
387 | - ) { |
|
388 | - $tags = $this->fixTags($tags); |
|
389 | - $this->append(array('mixin', $tags, $argv, $suffix), $s); |
|
390 | - return true; |
|
391 | - } else { |
|
392 | - $this->seek($s); |
|
393 | - } |
|
394 | - |
|
395 | - // spare ; |
|
396 | - if ($this->literal(';')) { |
|
397 | - return true; |
|
398 | - } |
|
399 | - |
|
400 | - return false; // got nothing, throw error |
|
401 | - } |
|
402 | - |
|
403 | - protected function isDirective($dirname, $directives) |
|
404 | - { |
|
405 | - // TODO: cache pattern in parser |
|
406 | - $pattern = implode( |
|
407 | - "|", |
|
408 | - array_map(array("lessc", "preg_quote"), $directives) |
|
409 | - ); |
|
410 | - $pattern = '/^(-[a-z-]+-)?('.$pattern.')$/i'; |
|
411 | - |
|
412 | - return preg_match($pattern, $dirname); |
|
413 | - } |
|
414 | - |
|
415 | - protected function fixTags($tags) |
|
416 | - { |
|
417 | - // move @ tags out of variable namespace |
|
418 | - foreach ($tags as &$tag) { |
|
419 | - if ($tag[0] == $this->lessc->vPrefix) { |
|
420 | - $tag[0] = $this->lessc->mPrefix; |
|
421 | - } |
|
422 | - } |
|
423 | - return $tags; |
|
424 | - } |
|
425 | - |
|
426 | - // a list of expressions |
|
427 | - protected function expressionList(&$exps) |
|
428 | - { |
|
429 | - $exp = null; |
|
430 | - |
|
431 | - $values = array(); |
|
432 | - |
|
433 | - while ($this->expression($exp)) { |
|
434 | - $values[] = $exp; |
|
435 | - } |
|
436 | - |
|
437 | - if (count($values) == 0) { |
|
438 | - return false; |
|
439 | - } |
|
440 | - |
|
441 | - $exps = Lessc::compressList($values, ' '); |
|
442 | - return true; |
|
443 | - } |
|
444 | - |
|
445 | - /** |
|
446 | - * Attempt to consume an expression. |
|
447 | - * @link http://en.wikipedia.org/wiki/Operator-precedence_parser#Pseudo-code |
|
448 | - */ |
|
449 | - protected function expression(&$out) |
|
450 | - { |
|
451 | - $lhs = null; |
|
452 | - $rhs = null; |
|
453 | - |
|
454 | - if ($this->value($lhs)) { |
|
455 | - $out = $this->expHelper($lhs, 0); |
|
456 | - |
|
457 | - // look for / shorthand |
|
458 | - if (!empty($this->env->supressedDivision)) { |
|
459 | - unset($this->env->supressedDivision); |
|
460 | - $s = $this->seek(); |
|
461 | - if ($this->literal("/") && $this->value($rhs)) { |
|
462 | - $out = array("list", "", |
|
463 | - array($out, array("keyword", "/"), $rhs)); |
|
464 | - } else { |
|
465 | - $this->seek($s); |
|
466 | - } |
|
467 | - } |
|
468 | - |
|
469 | - return true; |
|
470 | - } |
|
471 | - return false; |
|
472 | - } |
|
473 | - |
|
474 | - /** |
|
475 | - * recursively parse infix equation with $lhs at precedence $minP |
|
476 | - */ |
|
477 | - protected function expHelper($lhs, $minP) |
|
478 | - { |
|
479 | - $next = null; |
|
480 | - $rhs = null; |
|
481 | - |
|
482 | - $this->inExp = true; |
|
483 | - $ss = $this->seek(); |
|
484 | - |
|
485 | - while (true) { |
|
486 | - $whiteBefore = isset($this->buffer[$this->count - 1]) && |
|
487 | - ctype_space($this->buffer[$this->count - 1]); |
|
488 | - |
|
489 | - // If there is whitespace before the operator, then we require |
|
490 | - // whitespace after the operator for it to be an expression |
|
491 | - $needWhite = $whiteBefore && !$this->inParens; |
|
492 | - |
|
493 | - $m = array(); |
|
494 | - if ($this->match(self::$operatorString.($needWhite ? '\s' : ''), $m) && self::$precedence[$m[1]] >= $minP) { |
|
495 | - if (!$this->inParens && isset($this->env->currentProperty) && $m[1] == "/" && empty($this->env->supressedDivision)) { |
|
496 | - foreach (self::$supressDivisionProps as $pattern) { |
|
497 | - if (preg_match($pattern, $this->env->currentProperty)) { |
|
498 | - $this->env->supressedDivision = true; |
|
499 | - break 2; |
|
500 | - } |
|
501 | - } |
|
502 | - } |
|
503 | - |
|
504 | - |
|
505 | - $whiteAfter = isset($this->buffer[$this->count - 1]) && |
|
506 | - ctype_space($this->buffer[$this->count - 1]); |
|
507 | - |
|
508 | - if (!$this->value($rhs)) { |
|
509 | - break; |
|
510 | - } |
|
511 | - |
|
512 | - // peek for next operator to see what to do with rhs |
|
513 | - if ($this->peek(self::$operatorString, $next) && self::$precedence[$next[1]] > self::$precedence[$m[1]]) { |
|
514 | - $rhs = $this->expHelper($rhs, self::$precedence[$next[1]]); |
|
515 | - } |
|
516 | - |
|
517 | - $lhs = array('expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter); |
|
518 | - $ss = $this->seek(); |
|
519 | - |
|
520 | - continue; |
|
521 | - } |
|
522 | - |
|
523 | - break; |
|
524 | - } |
|
525 | - |
|
526 | - $this->seek($ss); |
|
527 | - |
|
528 | - return $lhs; |
|
529 | - } |
|
530 | - |
|
531 | - // consume a list of values for a property |
|
532 | - public function propertyValue(&$value, $keyName = null) |
|
533 | - { |
|
534 | - $v = null; |
|
535 | - $values = array(); |
|
536 | - |
|
537 | - if ($keyName !== null) { |
|
538 | - $this->env->currentProperty = $keyName; |
|
539 | - } |
|
540 | - |
|
541 | - $s = null; |
|
542 | - while ($this->expressionList($v)) { |
|
543 | - $values[] = $v; |
|
544 | - $s = $this->seek(); |
|
545 | - if (!$this->literal(',')) { |
|
546 | - break; |
|
547 | - } |
|
548 | - } |
|
549 | - |
|
550 | - if ($s) { |
|
551 | - $this->seek($s); |
|
552 | - } |
|
553 | - |
|
554 | - if ($keyName !== null) { |
|
555 | - unset($this->env->currentProperty); |
|
556 | - } |
|
557 | - |
|
558 | - if (count($values) == 0) { |
|
559 | - return false; |
|
560 | - } |
|
561 | - |
|
562 | - $value = Lessc::compressList($values, ', '); |
|
563 | - return true; |
|
564 | - } |
|
565 | - |
|
566 | - protected function parenValue(&$out) |
|
567 | - { |
|
568 | - $exp = null; |
|
569 | - |
|
570 | - $s = $this->seek(); |
|
571 | - |
|
572 | - // speed shortcut |
|
573 | - if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] != "(") { |
|
574 | - return false; |
|
575 | - } |
|
576 | - |
|
577 | - $inParens = $this->inParens; |
|
578 | - if ($this->literal("(") && |
|
579 | - ($this->inParens = true) && $this->expression($exp) && |
|
580 | - $this->literal(")") |
|
581 | - ) { |
|
582 | - $out = $exp; |
|
583 | - $this->inParens = $inParens; |
|
584 | - return true; |
|
585 | - } else { |
|
586 | - $this->inParens = $inParens; |
|
587 | - $this->seek($s); |
|
588 | - } |
|
589 | - |
|
590 | - return false; |
|
591 | - } |
|
592 | - |
|
593 | - // a single value |
|
594 | - protected function value(&$value) |
|
595 | - { |
|
596 | - $inner = null; |
|
597 | - $word = null; |
|
598 | - $str = null; |
|
599 | - $var = null; |
|
600 | - |
|
601 | - $s = $this->seek(); |
|
602 | - |
|
603 | - // speed shortcut |
|
604 | - if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] == "-") { |
|
605 | - // negation |
|
606 | - if ($this->literal("-", false) && |
|
607 | - (($this->variable($inner) && $inner = array("variable", $inner)) || |
|
608 | - $this->unit($inner) || |
|
609 | - $this->parenValue($inner)) |
|
610 | - ) { |
|
611 | - $value = array("unary", "-", $inner); |
|
612 | - return true; |
|
613 | - } else { |
|
614 | - $this->seek($s); |
|
615 | - } |
|
616 | - } |
|
617 | - |
|
618 | - if ($this->parenValue($value)) { |
|
619 | - return true; |
|
620 | - } |
|
621 | - if ($this->unit($value)) { |
|
622 | - return true; |
|
623 | - } |
|
624 | - if ($this->color($value)) { |
|
625 | - return true; |
|
626 | - } |
|
627 | - if ($this->func($value)) { |
|
628 | - return true; |
|
629 | - } |
|
630 | - if ($this->string($value)) { |
|
631 | - return true; |
|
632 | - } |
|
633 | - |
|
634 | - if ($this->keyword($word)) { |
|
635 | - $value = array('keyword', $word); |
|
636 | - return true; |
|
637 | - } |
|
638 | - |
|
639 | - // try a variable |
|
640 | - if ($this->variable($var)) { |
|
641 | - $value = array('variable', $var); |
|
642 | - return true; |
|
643 | - } |
|
644 | - |
|
645 | - // unquote string (should this work on any type? |
|
646 | - if ($this->literal("~") && $this->string($str)) { |
|
647 | - $value = array("escape", $str); |
|
648 | - return true; |
|
649 | - } else { |
|
650 | - $this->seek($s); |
|
651 | - } |
|
652 | - |
|
653 | - // css hack: \0 |
|
654 | - $m = array(); |
|
655 | - if ($this->literal('\\') && $this->match('([0-9]+)', $m)) { |
|
656 | - $value = array('keyword', '\\'.$m[1]); |
|
657 | - return true; |
|
658 | - } else { |
|
659 | - $this->seek($s); |
|
660 | - } |
|
661 | - |
|
662 | - return false; |
|
663 | - } |
|
664 | - |
|
665 | - // an import statement |
|
666 | - protected function import(&$out, $value = '') |
|
667 | - { |
|
668 | - if (!$this->literal('@import')) { |
|
669 | - return false; |
|
670 | - } |
|
671 | - |
|
672 | - // @import "something.css" media; |
|
673 | - // @import url("something.css") media; |
|
674 | - // @import url(something.css) media; |
|
675 | - |
|
676 | - if ($this->propertyValue($value)) { |
|
677 | - $out = array("import", $value); |
|
678 | - return true; |
|
679 | - } |
|
680 | - |
|
681 | - return false; |
|
682 | - } |
|
683 | - |
|
684 | - protected function mediaQueryList(&$out) |
|
685 | - { |
|
686 | - $list = null; |
|
687 | - |
|
688 | - if ($this->genericList($list, "mediaQuery", ",", false)) { |
|
689 | - $out = $list[2]; |
|
690 | - return true; |
|
691 | - } |
|
692 | - return false; |
|
693 | - } |
|
694 | - |
|
695 | - protected function mediaQuery(&$out) |
|
696 | - { |
|
697 | - $mediaType = null; |
|
698 | - |
|
699 | - $s = $this->seek(); |
|
700 | - |
|
701 | - $expressions = null; |
|
702 | - $parts = array(); |
|
703 | - |
|
704 | - if ((($this->literal("only") && ($only = true)) || ($this->literal("not") && ($not = true))) && $this->keyword($mediaType)) { |
|
705 | - $prop = array("mediaType"); |
|
706 | - if (isset($only)) { |
|
707 | - $prop[] = "only"; |
|
708 | - } |
|
709 | - if (isset($not)) { |
|
710 | - $prop[] = "not"; |
|
711 | - } |
|
712 | - $prop[] = $mediaType; |
|
713 | - $parts[] = $prop; |
|
714 | - } else { |
|
715 | - $this->seek($s); |
|
716 | - } |
|
717 | - |
|
718 | - |
|
719 | - if (!empty($mediaType) && !$this->literal("and")) { |
|
720 | - // ~ |
|
721 | - } else { |
|
722 | - $this->genericList($expressions, "mediaExpression", "and", false); |
|
723 | - if (is_array($expressions)) { |
|
724 | - $parts = array_merge($parts, $expressions[2]); |
|
725 | - } |
|
726 | - } |
|
727 | - |
|
728 | - if (count($parts) == 0) { |
|
729 | - $this->seek($s); |
|
730 | - return false; |
|
731 | - } |
|
732 | - |
|
733 | - $out = $parts; |
|
734 | - return true; |
|
735 | - } |
|
736 | - |
|
737 | - protected function mediaExpression(&$out) |
|
738 | - { |
|
739 | - $feature = null; |
|
740 | - $variable = null; |
|
741 | - |
|
742 | - $s = $this->seek(); |
|
743 | - $value = null; |
|
744 | - if ($this->literal("(") && |
|
745 | - $this->keyword($feature) && |
|
746 | - ($this->literal(":") && $this->expression($value)) && |
|
747 | - $this->literal(")") |
|
748 | - ) { |
|
749 | - $out = array("mediaExp", $feature); |
|
750 | - if ($value) { |
|
751 | - $out[] = $value; |
|
752 | - } |
|
753 | - return true; |
|
754 | - } elseif ($this->variable($variable)) { |
|
755 | - $out = array('variable', $variable); |
|
756 | - return true; |
|
757 | - } |
|
758 | - |
|
759 | - $this->seek($s); |
|
760 | - return false; |
|
761 | - } |
|
762 | - |
|
763 | - // an unbounded string stopped by $end |
|
764 | - protected function openString($end, &$out, $nestingOpen = null, $rejectStrs = null) |
|
765 | - { |
|
766 | - $str = null; |
|
767 | - $inter = null; |
|
768 | - |
|
769 | - $oldWhite = $this->eatWhiteDefault; |
|
770 | - $this->eatWhiteDefault = false; |
|
771 | - |
|
772 | - $stop = array("'", '"', "@{", $end); |
|
773 | - $stop = array_map(array("lessc", "preg_quote"), $stop); |
|
774 | - // $stop[] = self::$commentMulti; |
|
775 | - |
|
776 | - if (!is_null($rejectStrs)) { |
|
777 | - $stop = array_merge($stop, $rejectStrs); |
|
778 | - } |
|
779 | - |
|
780 | - $patt = '(.*?)('.implode("|", $stop).')'; |
|
781 | - |
|
782 | - $nestingLevel = 0; |
|
783 | - |
|
784 | - $content = array(); |
|
785 | - $m = array(); |
|
786 | - while ($this->match($patt, $m, false)) { |
|
787 | - if (!empty($m[1])) { |
|
788 | - $content[] = $m[1]; |
|
789 | - if ($nestingOpen) { |
|
790 | - $nestingLevel += substr_count($m[1], $nestingOpen); |
|
791 | - } |
|
792 | - } |
|
793 | - |
|
794 | - $tok = $m[2]; |
|
795 | - |
|
796 | - $this->count -= strlen($tok); |
|
797 | - if ($tok == $end) { |
|
798 | - if ($nestingLevel == 0) { |
|
799 | - break; |
|
800 | - } else { |
|
801 | - $nestingLevel--; |
|
802 | - } |
|
803 | - } |
|
804 | - |
|
805 | - if (($tok == "'" || $tok == '"') && $this->string($str)) { |
|
806 | - $content[] = $str; |
|
807 | - continue; |
|
808 | - } |
|
809 | - |
|
810 | - if ($tok == "@{" && $this->interpolation($inter)) { |
|
811 | - $content[] = $inter; |
|
812 | - continue; |
|
813 | - } |
|
814 | - |
|
815 | - if (!empty($rejectStrs) && in_array($tok, $rejectStrs)) { |
|
816 | - break; |
|
817 | - } |
|
818 | - |
|
819 | - $content[] = $tok; |
|
820 | - $this->count += strlen($tok); |
|
821 | - } |
|
822 | - |
|
823 | - $this->eatWhiteDefault = $oldWhite; |
|
824 | - |
|
825 | - if (count($content) == 0) { |
|
826 | - return false; |
|
827 | - } |
|
828 | - |
|
829 | - // trim the end |
|
830 | - if (is_string(end($content))) { |
|
831 | - $content[count($content) - 1] = rtrim(end($content)); |
|
832 | - } |
|
833 | - |
|
834 | - $out = array("string", "", $content); |
|
835 | - return true; |
|
836 | - } |
|
837 | - |
|
838 | - protected function string(&$out) |
|
839 | - { |
|
840 | - $inter = null; |
|
841 | - |
|
842 | - $s = $this->seek(); |
|
843 | - if ($this->literal('"', false)) { |
|
844 | - $delim = '"'; |
|
845 | - } elseif ($this->literal("'", false)) { |
|
846 | - $delim = "'"; |
|
847 | - } else { |
|
848 | - return false; |
|
849 | - } |
|
850 | - |
|
851 | - $content = array(); |
|
852 | - |
|
853 | - // look for either ending delim , escape, or string interpolation |
|
854 | - $patt = '([^\n]*?)(@\{|\\\\|'. |
|
855 | - Lessc::preg_quote($delim).')'; |
|
856 | - |
|
857 | - $oldWhite = $this->eatWhiteDefault; |
|
858 | - $this->eatWhiteDefault = false; |
|
859 | - |
|
860 | - $m = array(); |
|
861 | - while ($this->match($patt, $m, false)) { |
|
862 | - $content[] = $m[1]; |
|
863 | - if ($m[2] == "@{") { |
|
864 | - $this->count -= strlen($m[2]); |
|
865 | - if ($this->interpolation($inter)) { |
|
866 | - $content[] = $inter; |
|
867 | - } else { |
|
868 | - $this->count += strlen($m[2]); |
|
869 | - $content[] = "@{"; // ignore it |
|
870 | - } |
|
871 | - } elseif ($m[2] == '\\') { |
|
872 | - $content[] = $m[2]; |
|
873 | - if ($this->literal($delim, false)) { |
|
874 | - $content[] = $delim; |
|
875 | - } |
|
876 | - } else { |
|
877 | - $this->count -= strlen($delim); |
|
878 | - break; // delim |
|
879 | - } |
|
880 | - } |
|
881 | - |
|
882 | - $this->eatWhiteDefault = $oldWhite; |
|
883 | - |
|
884 | - if ($this->literal($delim)) { |
|
885 | - $out = array("string", $delim, $content); |
|
886 | - return true; |
|
887 | - } |
|
888 | - |
|
889 | - $this->seek($s); |
|
890 | - return false; |
|
891 | - } |
|
892 | - |
|
893 | - protected function interpolation(&$out) |
|
894 | - { |
|
895 | - $interp = array(); |
|
896 | - |
|
897 | - $oldWhite = $this->eatWhiteDefault; |
|
898 | - $this->eatWhiteDefault = true; |
|
899 | - |
|
900 | - $s = $this->seek(); |
|
901 | - if ($this->literal("@{") && |
|
902 | - $this->openString("}", $interp, null, array("'", '"', ";")) && |
|
903 | - $this->literal("}", false) |
|
904 | - ) { |
|
905 | - $out = array("interpolate", $interp); |
|
906 | - $this->eatWhiteDefault = $oldWhite; |
|
907 | - if ($this->eatWhiteDefault) { |
|
908 | - $this->whitespace(); |
|
909 | - } |
|
910 | - return true; |
|
911 | - } |
|
912 | - |
|
913 | - $this->eatWhiteDefault = $oldWhite; |
|
914 | - $this->seek($s); |
|
915 | - return false; |
|
916 | - } |
|
917 | - |
|
918 | - protected function unit(&$unit) |
|
919 | - { |
|
920 | - $m = array(); |
|
921 | - |
|
922 | - // speed shortcut |
|
923 | - if (isset($this->buffer[$this->count])) { |
|
924 | - $char = $this->buffer[$this->count]; |
|
925 | - if (!ctype_digit($char) && $char != ".") { |
|
926 | - return false; |
|
927 | - } |
|
928 | - } |
|
929 | - |
|
930 | - if ($this->match('([0-9]+(?:\.[0-9]*)?|\.[0-9]+)([%a-zA-Z]+)?', $m)) { |
|
931 | - $unit = array("number", $m[1], empty($m[2]) ? "" : $m[2]); |
|
932 | - return true; |
|
933 | - } |
|
934 | - return false; |
|
935 | - } |
|
936 | - |
|
937 | - // a # color |
|
938 | - protected function color(&$out) |
|
939 | - { |
|
940 | - $m = array(); |
|
941 | - |
|
942 | - if ($this->match('(#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3}))', $m)) { |
|
943 | - if (strlen($m[1]) > 7) { |
|
944 | - $out = array("string", "", array($m[1])); |
|
945 | - } else { |
|
946 | - $out = array("raw_color", $m[1]); |
|
947 | - } |
|
948 | - return true; |
|
949 | - } |
|
950 | - |
|
951 | - return false; |
|
952 | - } |
|
953 | - |
|
954 | - // consume an argument definition list surrounded by () |
|
955 | - // each argument is a variable name with optional value |
|
956 | - // or at the end a ... or a variable named followed by ... |
|
957 | - // arguments are separated by , unless a ; is in the list, then ; is the |
|
958 | - // delimiter. |
|
959 | - protected function argumentDef(&$args, &$isVararg) |
|
960 | - { |
|
961 | - $value = array(); |
|
962 | - $rhs = null; |
|
963 | - |
|
964 | - $s = $this->seek(); |
|
965 | - if (!$this->literal('(')) { |
|
966 | - return false; |
|
967 | - } |
|
968 | - |
|
969 | - $values = array(); |
|
970 | - $delim = ","; |
|
971 | - $method = "expressionList"; |
|
972 | - |
|
973 | - $isVararg = false; |
|
974 | - while (true) { |
|
975 | - if ($this->literal("...")) { |
|
976 | - $isVararg = true; |
|
977 | - break; |
|
978 | - } |
|
979 | - |
|
980 | - if ($this->$method($value)) { |
|
981 | - if ($value[0] == "variable") { |
|
982 | - $arg = array("arg", $value[1]); |
|
983 | - $ss = $this->seek(); |
|
984 | - |
|
985 | - if ($this->assign() && $this->$method($rhs)) { |
|
986 | - $arg[] = $rhs; |
|
987 | - } else { |
|
988 | - $this->seek($ss); |
|
989 | - if ($this->literal("...")) { |
|
990 | - $arg[0] = "rest"; |
|
991 | - $isVararg = true; |
|
992 | - } |
|
993 | - } |
|
994 | - |
|
995 | - $values[] = $arg; |
|
996 | - if ($isVararg) { |
|
997 | - break; |
|
998 | - } |
|
999 | - continue; |
|
1000 | - } else { |
|
1001 | - $values[] = array("lit", $value); |
|
1002 | - } |
|
1003 | - } |
|
1004 | - |
|
1005 | - |
|
1006 | - if (!$this->literal($delim)) { |
|
1007 | - if ($delim == "," && $this->literal(";")) { |
|
1008 | - // found new delim, convert existing args |
|
1009 | - $delim = ";"; |
|
1010 | - $method = "propertyValue"; |
|
1011 | - |
|
1012 | - // transform arg list |
|
1013 | - if (isset($values[1])) { // 2 items |
|
1014 | - $newList = array(); |
|
1015 | - foreach ($values as $i => $arg) { |
|
1016 | - switch ($arg[0]) { |
|
1017 | - case "arg": |
|
1018 | - if ($i) { |
|
1019 | - $this->throwError("Cannot mix ; and , as delimiter types"); |
|
1020 | - } |
|
1021 | - $newList[] = $arg[2]; |
|
1022 | - break; |
|
1023 | - case "lit": |
|
1024 | - $newList[] = $arg[1]; |
|
1025 | - break; |
|
1026 | - case "rest": |
|
1027 | - $this->throwError("Unexpected rest before semicolon"); |
|
1028 | - } |
|
1029 | - } |
|
1030 | - |
|
1031 | - $newList = array("list", ", ", $newList); |
|
1032 | - |
|
1033 | - switch ($values[0][0]) { |
|
1034 | - case "arg": |
|
1035 | - $newArg = array("arg", $values[0][1], $newList); |
|
1036 | - break; |
|
1037 | - case "lit": |
|
1038 | - $newArg = array("lit", $newList); |
|
1039 | - break; |
|
1040 | - } |
|
1041 | - |
|
1042 | - } elseif ($values) { // 1 item |
|
1043 | - $newArg = $values[0]; |
|
1044 | - } |
|
1045 | - |
|
1046 | - if ($newArg) { |
|
1047 | - $values = array($newArg); |
|
1048 | - } |
|
1049 | - } else { |
|
1050 | - break; |
|
1051 | - } |
|
1052 | - } |
|
1053 | - } |
|
1054 | - |
|
1055 | - if (!$this->literal(')')) { |
|
1056 | - $this->seek($s); |
|
1057 | - return false; |
|
1058 | - } |
|
1059 | - |
|
1060 | - $args = $values; |
|
1061 | - |
|
1062 | - return true; |
|
1063 | - } |
|
1064 | - |
|
1065 | - // consume a list of tags |
|
1066 | - // this accepts a hanging delimiter |
|
1067 | - protected function tags(&$tags, $simple = false, $delim = ',') |
|
1068 | - { |
|
1069 | - $tt = array(); |
|
1070 | - |
|
1071 | - $tags = array(); |
|
1072 | - while ($this->tag($tt, $simple)) { |
|
1073 | - $tags[] = $tt; |
|
1074 | - if (!$this->literal($delim)) { |
|
1075 | - break; |
|
1076 | - } |
|
1077 | - } |
|
1078 | - if (count($tags) == 0) { |
|
1079 | - return false; |
|
1080 | - } |
|
1081 | - |
|
1082 | - return true; |
|
1083 | - } |
|
1084 | - |
|
1085 | - // list of tags of specifying mixin path |
|
1086 | - // optionally separated by > (lazy, accepts extra >) |
|
1087 | - protected function mixinTags(&$tags) |
|
1088 | - { |
|
1089 | - $tt = array(); |
|
1090 | - |
|
1091 | - $tags = array(); |
|
1092 | - while ($this->tag($tt, true)) { |
|
1093 | - $tags[] = $tt; |
|
1094 | - $this->literal(">"); |
|
1095 | - } |
|
1096 | - |
|
1097 | - if (!$tags) { |
|
1098 | - return false; |
|
1099 | - } |
|
1100 | - |
|
1101 | - return true; |
|
1102 | - } |
|
1103 | - |
|
1104 | - // a bracketed value (contained within in a tag definition) |
|
1105 | - protected function tagBracket(&$parts, &$hasExpression) |
|
1106 | - { |
|
1107 | - $str = null; |
|
1108 | - $inter = null; |
|
1109 | - $word = null; |
|
1110 | - |
|
1111 | - // speed shortcut |
|
1112 | - if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] != "[") { |
|
1113 | - return false; |
|
1114 | - } |
|
1115 | - |
|
1116 | - $s = $this->seek(); |
|
1117 | - |
|
1118 | - $hasInterpolation = false; |
|
1119 | - |
|
1120 | - if ($this->literal("[", false)) { |
|
1121 | - $attrParts = array("["); |
|
1122 | - // keyword, string, operator |
|
1123 | - while (true) { |
|
1124 | - if ($this->literal("]", false)) { |
|
1125 | - $this->count--; |
|
1126 | - break; // get out early |
|
1127 | - } |
|
1128 | - |
|
1129 | - $m = array(); |
|
1130 | - if ($this->match('\s+', $m)) { |
|
1131 | - $attrParts[] = " "; |
|
1132 | - continue; |
|
1133 | - } |
|
1134 | - if ($this->string($str)) { |
|
1135 | - // escape parent selector, (yuck) |
|
1136 | - foreach ($str[2] as &$chunk) { |
|
1137 | - $chunk = str_replace($this->lessc->parentSelector, "$&$", $chunk); |
|
1138 | - } |
|
1139 | - |
|
1140 | - $attrParts[] = $str; |
|
1141 | - $hasInterpolation = true; |
|
1142 | - continue; |
|
1143 | - } |
|
1144 | - |
|
1145 | - if ($this->keyword($word)) { |
|
1146 | - $attrParts[] = $word; |
|
1147 | - continue; |
|
1148 | - } |
|
1149 | - |
|
1150 | - if ($this->interpolation($inter)) { |
|
1151 | - $attrParts[] = $inter; |
|
1152 | - $hasInterpolation = true; |
|
1153 | - continue; |
|
1154 | - } |
|
1155 | - |
|
1156 | - // operator, handles attr namespace too |
|
1157 | - if ($this->match('[|-~\$\*\^=]+', $m)) { |
|
1158 | - $attrParts[] = $m[0]; |
|
1159 | - continue; |
|
1160 | - } |
|
1161 | - |
|
1162 | - break; |
|
1163 | - } |
|
1164 | - |
|
1165 | - if ($this->literal("]", false)) { |
|
1166 | - $attrParts[] = "]"; |
|
1167 | - foreach ($attrParts as $part) { |
|
1168 | - $parts[] = $part; |
|
1169 | - } |
|
1170 | - $hasExpression = $hasExpression || $hasInterpolation; |
|
1171 | - return true; |
|
1172 | - } |
|
1173 | - $this->seek($s); |
|
1174 | - } |
|
1175 | - |
|
1176 | - $this->seek($s); |
|
1177 | - return false; |
|
1178 | - } |
|
1179 | - |
|
1180 | - // a space separated list of selectors |
|
1181 | - protected function tag(&$tag, $simple = false) |
|
1182 | - { |
|
1183 | - $interp = null; |
|
1184 | - $unit = null; |
|
1185 | - |
|
1186 | - if ($simple) { |
|
1187 | - $chars = '^@,:;{}\][>\(\) "\''; |
|
1188 | - } else { |
|
1189 | - $chars = '^@,;{}["\''; |
|
1190 | - } |
|
1191 | - $s = $this->seek(); |
|
1192 | - |
|
1193 | - $hasExpression = false; |
|
1194 | - $parts = array(); |
|
1195 | - while ($this->tagBracket($parts, $hasExpression)); |
|
1196 | - |
|
1197 | - $oldWhite = $this->eatWhiteDefault; |
|
1198 | - $this->eatWhiteDefault = false; |
|
1199 | - |
|
1200 | - while (true) { |
|
1201 | - $m = array(); |
|
1202 | - if ($this->match('(['.$chars.'0-9]['.$chars.']*)', $m)) { |
|
1203 | - $parts[] = $m[1]; |
|
1204 | - if ($simple) { |
|
1205 | - break; |
|
1206 | - } |
|
1207 | - |
|
1208 | - while ($this->tagBracket($parts, $hasExpression)); |
|
1209 | - continue; |
|
1210 | - } |
|
1211 | - |
|
1212 | - if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] == "@") { |
|
1213 | - if ($this->interpolation($interp)) { |
|
1214 | - $hasExpression = true; |
|
1215 | - $interp[2] = true; // don't unescape |
|
1216 | - $parts[] = $interp; |
|
1217 | - continue; |
|
1218 | - } |
|
1219 | - |
|
1220 | - if ($this->literal("@")) { |
|
1221 | - $parts[] = "@"; |
|
1222 | - continue; |
|
1223 | - } |
|
1224 | - } |
|
1225 | - |
|
1226 | - if ($this->unit($unit)) { // for keyframes |
|
1227 | - $parts[] = $unit[1]; |
|
1228 | - $parts[] = $unit[2]; |
|
1229 | - continue; |
|
1230 | - } |
|
1231 | - |
|
1232 | - break; |
|
1233 | - } |
|
1234 | - |
|
1235 | - $this->eatWhiteDefault = $oldWhite; |
|
1236 | - if (!$parts) { |
|
1237 | - $this->seek($s); |
|
1238 | - return false; |
|
1239 | - } |
|
1240 | - |
|
1241 | - if ($hasExpression) { |
|
1242 | - $tag = array("exp", array("string", "", $parts)); |
|
1243 | - } else { |
|
1244 | - $tag = trim(implode($parts)); |
|
1245 | - } |
|
1246 | - |
|
1247 | - $this->whitespace(); |
|
1248 | - return true; |
|
1249 | - } |
|
1250 | - |
|
1251 | - // a css function |
|
1252 | - protected function func(&$func) |
|
1253 | - { |
|
1254 | - $s = $this->seek(); |
|
1255 | - |
|
1256 | - $m = array(); |
|
1257 | - $value = array(); |
|
1258 | - $string = array(); |
|
1259 | - $name = null; |
|
1260 | - |
|
1261 | - if ($this->match('(%|[\w\-_][\w\-_:\.]+|[\w_])', $m) && $this->literal('(')) { |
|
1262 | - $fname = $m[1]; |
|
1263 | - |
|
1264 | - $sPreArgs = $this->seek(); |
|
1265 | - |
|
1266 | - $args = array(); |
|
1267 | - while (true) { |
|
1268 | - $ss = $this->seek(); |
|
1269 | - // this ugly nonsense is for ie filter properties |
|
1270 | - if ($this->keyword($name) && $this->literal('=') && $this->expressionList($value)) { |
|
1271 | - $args[] = array("string", "", array($name, "=", $value)); |
|
1272 | - } else { |
|
1273 | - $this->seek($ss); |
|
1274 | - if ($this->expressionList($value)) { |
|
1275 | - $args[] = $value; |
|
1276 | - } |
|
1277 | - } |
|
1278 | - |
|
1279 | - if (!$this->literal(',')) { |
|
1280 | - break; |
|
1281 | - } |
|
1282 | - } |
|
1283 | - $args = array('list', ',', $args); |
|
1284 | - |
|
1285 | - if ($this->literal(')')) { |
|
1286 | - $func = array('function', $fname, $args); |
|
1287 | - return true; |
|
1288 | - } elseif ($fname == 'url') { |
|
1289 | - // couldn't parse and in url? treat as string |
|
1290 | - $this->seek($sPreArgs); |
|
1291 | - if ($this->openString(")", $string) && $this->literal(")")) { |
|
1292 | - $func = array('function', $fname, $string); |
|
1293 | - return true; |
|
1294 | - } |
|
1295 | - } |
|
1296 | - } |
|
1297 | - |
|
1298 | - $this->seek($s); |
|
1299 | - return false; |
|
1300 | - } |
|
1301 | - |
|
1302 | - // consume a less variable |
|
1303 | - protected function variable(&$name) |
|
1304 | - { |
|
1305 | - $sub = null; |
|
1306 | - $name = null; |
|
1307 | - |
|
1308 | - $s = $this->seek(); |
|
1309 | - if ($this->literal($this->lessc->vPrefix, false) && |
|
1310 | - ($this->variable($sub) || $this->keyword($name)) |
|
1311 | - ) { |
|
1312 | - if (!empty($sub)) { |
|
1313 | - $name = array('variable', $sub); |
|
1314 | - } else { |
|
1315 | - $name = $this->lessc->vPrefix.$name; |
|
1316 | - } |
|
1317 | - return true; |
|
1318 | - } |
|
1319 | - |
|
1320 | - $name = null; |
|
1321 | - $this->seek($s); |
|
1322 | - return false; |
|
1323 | - } |
|
1324 | - |
|
1325 | - /** |
|
1326 | - * Consume an assignment operator |
|
1327 | - * Can optionally take a name that will be set to the current property name |
|
1328 | - */ |
|
1329 | - protected function assign($name = null) |
|
1330 | - { |
|
1331 | - if ($name) { |
|
1332 | - $this->currentProperty = $name; |
|
1333 | - } |
|
1334 | - return $this->literal(':') || $this->literal('='); |
|
1335 | - } |
|
1336 | - |
|
1337 | - // consume a keyword |
|
1338 | - protected function keyword(&$word) |
|
1339 | - { |
|
1340 | - $m = array(); |
|
1341 | - if ($this->match('([\w_\-\*!"][\w\-_"]*)', $m)) { |
|
1342 | - $word = $m[1]; |
|
1343 | - return true; |
|
1344 | - } |
|
1345 | - return false; |
|
1346 | - } |
|
1347 | - |
|
1348 | - // consume an end of statement delimiter |
|
1349 | - protected function end() |
|
1350 | - { |
|
1351 | - if ($this->literal(';', false)) { |
|
1352 | - return true; |
|
1353 | - } elseif ($this->count == strlen($this->buffer) || $this->buffer[$this->count] == '}') { |
|
1354 | - // if there is end of file or a closing block next then we don't need a ; |
|
1355 | - return true; |
|
1356 | - } |
|
1357 | - return false; |
|
1358 | - } |
|
1359 | - |
|
1360 | - protected function guards(&$guards) |
|
1361 | - { |
|
1362 | - $g = null; |
|
1363 | - |
|
1364 | - $s = $this->seek(); |
|
1365 | - |
|
1366 | - if (!$this->literal("when")) { |
|
1367 | - $this->seek($s); |
|
1368 | - return false; |
|
1369 | - } |
|
1370 | - |
|
1371 | - $guards = array(); |
|
1372 | - |
|
1373 | - while ($this->guardGroup($g)) { |
|
1374 | - $guards[] = $g; |
|
1375 | - if (!$this->literal(",")) { |
|
1376 | - break; |
|
1377 | - } |
|
1378 | - } |
|
1379 | - |
|
1380 | - if (count($guards) == 0) { |
|
1381 | - $guards = null; |
|
1382 | - $this->seek($s); |
|
1383 | - return false; |
|
1384 | - } |
|
1385 | - |
|
1386 | - return true; |
|
1387 | - } |
|
1388 | - |
|
1389 | - // a bunch of guards that are and'd together |
|
1390 | - // TODO rename to guardGroup |
|
1391 | - protected function guardGroup(&$guardGroup) |
|
1392 | - { |
|
1393 | - $guard = null; |
|
1394 | - |
|
1395 | - $s = $this->seek(); |
|
1396 | - $guardGroup = array(); |
|
1397 | - while ($this->guard($guard)) { |
|
1398 | - $guardGroup[] = $guard; |
|
1399 | - if (!$this->literal("and")) { |
|
1400 | - break; |
|
1401 | - } |
|
1402 | - } |
|
1403 | - |
|
1404 | - if (count($guardGroup) == 0) { |
|
1405 | - $guardGroup = null; |
|
1406 | - $this->seek($s); |
|
1407 | - return false; |
|
1408 | - } |
|
1409 | - |
|
1410 | - return true; |
|
1411 | - } |
|
1412 | - |
|
1413 | - protected function guard(&$guard) |
|
1414 | - { |
|
1415 | - $exp = null; |
|
1416 | - |
|
1417 | - $s = $this->seek(); |
|
1418 | - $negate = $this->literal("not"); |
|
1419 | - |
|
1420 | - if ($this->literal("(") && $this->expression($exp) && $this->literal(")")) { |
|
1421 | - $guard = $exp; |
|
1422 | - if ($negate) { |
|
1423 | - $guard = array("negate", $guard); |
|
1424 | - } |
|
1425 | - return true; |
|
1426 | - } |
|
1427 | - |
|
1428 | - $this->seek($s); |
|
1429 | - return false; |
|
1430 | - } |
|
1431 | - |
|
1432 | - /* raw parsing functions */ |
|
1433 | - |
|
1434 | - protected function literal($what, $eatWhitespace = null) |
|
1435 | - { |
|
1436 | - if ($eatWhitespace === null) { |
|
1437 | - $eatWhitespace = $this->eatWhiteDefault; |
|
1438 | - } |
|
1439 | - |
|
1440 | - // shortcut on single letter |
|
1441 | - if (!isset($what[1]) && isset($this->buffer[$this->count])) { |
|
1442 | - if ($this->buffer[$this->count] == $what) { |
|
1443 | - if (!$eatWhitespace) { |
|
1444 | - $this->count++; |
|
1445 | - return true; |
|
1446 | - } |
|
1447 | - // goes below... |
|
1448 | - } else { |
|
1449 | - return false; |
|
1450 | - } |
|
1451 | - } |
|
1452 | - |
|
1453 | - if (!isset(self::$literalCache[$what])) { |
|
1454 | - self::$literalCache[$what] = Lessc::preg_quote($what); |
|
1455 | - } |
|
1456 | - |
|
1457 | - $m = array(); |
|
1458 | - return $this->match(self::$literalCache[$what], $m, $eatWhitespace); |
|
1459 | - } |
|
1460 | - |
|
1461 | - protected function genericList(&$out, $parseItem, $delim = "", $flatten = true) |
|
1462 | - { |
|
1463 | - $value = null; |
|
1464 | - |
|
1465 | - $s = $this->seek(); |
|
1466 | - $items = array(); |
|
1467 | - while ($this->$parseItem($value)) { |
|
1468 | - $items[] = $value; |
|
1469 | - if ($delim) { |
|
1470 | - if (!$this->literal($delim)) { |
|
1471 | - break; |
|
1472 | - } |
|
1473 | - } |
|
1474 | - } |
|
1475 | - |
|
1476 | - if (count($items) == 0) { |
|
1477 | - $this->seek($s); |
|
1478 | - return false; |
|
1479 | - } |
|
1480 | - |
|
1481 | - if ($flatten && count($items) == 1) { |
|
1482 | - $out = $items[0]; |
|
1483 | - } else { |
|
1484 | - $out = array("list", $delim, $items); |
|
1485 | - } |
|
1486 | - |
|
1487 | - return true; |
|
1488 | - } |
|
1489 | - |
|
1490 | - |
|
1491 | - // advance counter to next occurrence of $what |
|
1492 | - // $until - don't include $what in advance |
|
1493 | - // $allowNewline, if string, will be used as valid char set |
|
1494 | - protected function to($what, &$out, $until = false, $allowNewline = false) |
|
1495 | - { |
|
1496 | - if (is_string($allowNewline)) { |
|
1497 | - $validChars = $allowNewline; |
|
1498 | - } else { |
|
1499 | - $validChars = $allowNewline ? "." : "[^\n]"; |
|
1500 | - } |
|
1501 | - $m = array(); |
|
1502 | - if (!$this->match('('.$validChars.'*?)'.Lessc::preg_quote($what), $m, !$until)) { |
|
1503 | - return false; |
|
1504 | - } |
|
1505 | - if ($until) { |
|
1506 | - $this->count -= strlen($what); // give back $what |
|
1507 | - } |
|
1508 | - $out = $m[1]; |
|
1509 | - return true; |
|
1510 | - } |
|
1511 | - |
|
1512 | - // try to match something on head of buffer |
|
1513 | - protected function match($regex, &$out, $eatWhitespace = null) |
|
1514 | - { |
|
1515 | - if ($eatWhitespace === null) { |
|
1516 | - $eatWhitespace = $this->eatWhiteDefault; |
|
1517 | - } |
|
1518 | - |
|
1519 | - $r = '/'.$regex.($eatWhitespace && !$this->writeComments ? '\s*' : '').'/Ais'; |
|
1520 | - if (preg_match($r, $this->buffer, $out, 0, $this->count)) { |
|
1521 | - $this->count += strlen($out[0]); |
|
1522 | - if ($eatWhitespace && $this->writeComments) { |
|
1523 | - $this->whitespace(); |
|
1524 | - } |
|
1525 | - return true; |
|
1526 | - } |
|
1527 | - return false; |
|
1528 | - } |
|
1529 | - |
|
1530 | - // match some whitespace |
|
1531 | - protected function whitespace() |
|
1532 | - { |
|
1533 | - if ($this->writeComments) { |
|
1534 | - $gotWhite = false; |
|
1535 | - $m = array(); |
|
1536 | - while (preg_match(self::$whitePattern, $this->buffer, $m, 0, $this->count)) { |
|
1537 | - if (isset($m[1]) && empty($this->seenComments[$this->count])) { |
|
1538 | - $this->append(array("comment", $m[1])); |
|
1539 | - $this->seenComments[$this->count] = true; |
|
1540 | - } |
|
1541 | - $this->count += strlen($m[0]); |
|
1542 | - $gotWhite = true; |
|
1543 | - } |
|
1544 | - return $gotWhite; |
|
1545 | - } else { |
|
1546 | - $this->match("", $m); |
|
1547 | - return strlen($m[0]) > 0; |
|
1548 | - } |
|
1549 | - } |
|
1550 | - |
|
1551 | - // match something without consuming it |
|
1552 | - protected function peek($regex, &$out = null, $from = null) |
|
1553 | - { |
|
1554 | - if (is_null($from)) { |
|
1555 | - $from = $this->count; |
|
1556 | - } |
|
1557 | - $r = '/'.$regex.'/Ais'; |
|
1558 | - $result = preg_match($r, $this->buffer, $out, 0, $from); |
|
1559 | - |
|
1560 | - return $result; |
|
1561 | - } |
|
1562 | - |
|
1563 | - // seek to a spot in the buffer or return where we are on no argument |
|
1564 | - protected function seek($where = null) |
|
1565 | - { |
|
1566 | - if ($where === null) { |
|
1567 | - return $this->count; |
|
1568 | - } else { |
|
1569 | - $this->count = $where; |
|
1570 | - } |
|
1571 | - return true; |
|
1572 | - } |
|
1573 | - |
|
1574 | - /* misc functions */ |
|
1575 | - |
|
1576 | - public function throwError($msg = "parse error", $count = null) |
|
1577 | - { |
|
1578 | - $count = is_null($count) ? $this->count : $count; |
|
1579 | - |
|
1580 | - $line = $this->line + |
|
1581 | - substr_count(substr($this->buffer, 0, $count), "\n"); |
|
1582 | - |
|
1583 | - if (!empty($this->sourceName)) { |
|
1584 | - $loc = "$this->sourceName on line $line"; |
|
1585 | - } else { |
|
1586 | - $loc = "line: $line"; |
|
1587 | - } |
|
1588 | - |
|
1589 | - // TODO this depends on $this->count |
|
1590 | - $m = array(); |
|
1591 | - if ($this->peek("(.*?)(\n|$)", $m, $count)) { |
|
1592 | - throw new exception("$msg: failed at `$m[1]` $loc"); |
|
1593 | - } else { |
|
1594 | - throw new exception("$msg: $loc"); |
|
1595 | - } |
|
1596 | - } |
|
1597 | - |
|
1598 | - protected function pushBlock($selectors = null, $type = null) |
|
1599 | - { |
|
1600 | - $b = new stdclass(); |
|
1601 | - $b->parent = $this->env; |
|
1602 | - |
|
1603 | - $b->type = $type; |
|
1604 | - $b->id = self::$nextBlockId++; |
|
1605 | - |
|
1606 | - $b->isVararg = false; // TODO: kill me from here |
|
1607 | - $b->tags = $selectors; |
|
1608 | - |
|
1609 | - $b->props = array(); |
|
1610 | - $b->children = array(); |
|
1611 | - |
|
1612 | - $this->env = $b; |
|
1613 | - return $b; |
|
1614 | - } |
|
1615 | - |
|
1616 | - // push a block that doesn't multiply tags |
|
1617 | - protected function pushSpecialBlock($type) |
|
1618 | - { |
|
1619 | - return $this->pushBlock(null, $type); |
|
1620 | - } |
|
1621 | - |
|
1622 | - // append a property to the current block |
|
1623 | - protected function append($prop, $pos = null) |
|
1624 | - { |
|
1625 | - if ($pos !== null) { |
|
1626 | - $prop[-1] = $pos; |
|
1627 | - } |
|
1628 | - $this->env->props[] = $prop; |
|
1629 | - } |
|
1630 | - |
|
1631 | - // pop something off the stack |
|
1632 | - protected function pop() |
|
1633 | - { |
|
1634 | - $old = $this->env; |
|
1635 | - $this->env = $this->env->parent; |
|
1636 | - return $old; |
|
1637 | - } |
|
1638 | - |
|
1639 | - // remove comments from $text |
|
1640 | - // todo: make it work for all functions, not just url |
|
1641 | - protected function removeComments($text) |
|
1642 | - { |
|
1643 | - $look = array( |
|
1644 | - 'url(', '//', '/*', '"', "'" |
|
1645 | - ); |
|
1646 | - |
|
1647 | - $out = ''; |
|
1648 | - $min = null; |
|
1649 | - while (true) { |
|
1650 | - // find the next item |
|
1651 | - foreach ($look as $token) { |
|
1652 | - $pos = strpos($text, $token); |
|
1653 | - if ($pos !== false) { |
|
1654 | - if (!isset($min) || $pos < $min[1]) { |
|
1655 | - $min = array($token, $pos); |
|
1656 | - } |
|
1657 | - } |
|
1658 | - } |
|
1659 | - |
|
1660 | - if (is_null($min)) { |
|
1661 | - break; |
|
1662 | - } |
|
1663 | - |
|
1664 | - $count = $min[1]; |
|
1665 | - $skip = 0; |
|
1666 | - $newlines = 0; |
|
1667 | - switch ($min[0]) { |
|
1668 | - case 'url(': |
|
1669 | - $m = array(); |
|
1670 | - if (preg_match('/url\(.*?\)/', $text, $m, 0, $count)) { |
|
1671 | - $count += strlen($m[0]) - strlen($min[0]); |
|
1672 | - } |
|
1673 | - break; |
|
1674 | - case '"': |
|
1675 | - case "'": |
|
1676 | - $m = array(); |
|
1677 | - if (preg_match('/'.$min[0].'.*?(?<!\\\\)'.$min[0].'/', $text, $m, 0, $count)) { |
|
1678 | - $count += strlen($m[0]) - 1; |
|
1679 | - } |
|
1680 | - break; |
|
1681 | - case '//': |
|
1682 | - $skip = strpos($text, "\n", $count); |
|
1683 | - if ($skip === false) { |
|
1684 | - $skip = strlen($text) - $count; |
|
1685 | - } else { |
|
1686 | - $skip -= $count; |
|
1687 | - } |
|
1688 | - break; |
|
1689 | - case '/*': |
|
1690 | - $m = array(); |
|
1691 | - if (preg_match('/\/\*.*?\*\//s', $text, $m, 0, $count)) { |
|
1692 | - $skip = strlen($m[0]); |
|
1693 | - $newlines = substr_count($m[0], "\n"); |
|
1694 | - } |
|
1695 | - break; |
|
1696 | - } |
|
1697 | - |
|
1698 | - if ($skip == 0) { |
|
1699 | - $count += strlen($min[0]); |
|
1700 | - } |
|
1701 | - |
|
1702 | - $out .= substr($text, 0, $count).str_repeat("\n", $newlines); |
|
1703 | - $text = substr($text, $count + $skip); |
|
1704 | - |
|
1705 | - $min = null; |
|
1706 | - } |
|
1707 | - |
|
1708 | - return $out.$text; |
|
1709 | - } |
|
64 | + protected static $nextBlockId = 0; // used to uniquely identify blocks |
|
65 | + |
|
66 | + protected static $precedence = array( |
|
67 | + '=<' => 0, |
|
68 | + '>=' => 0, |
|
69 | + '=' => 0, |
|
70 | + '<' => 0, |
|
71 | + '>' => 0, |
|
72 | + |
|
73 | + '+' => 1, |
|
74 | + '-' => 1, |
|
75 | + '*' => 2, |
|
76 | + '/' => 2, |
|
77 | + '%' => 2, |
|
78 | + ); |
|
79 | + |
|
80 | + protected static $whitePattern; |
|
81 | + protected static $commentMulti; |
|
82 | + |
|
83 | + protected static $commentSingle = "//"; |
|
84 | + protected static $commentMultiLeft = "/*"; |
|
85 | + protected static $commentMultiRight = "*/"; |
|
86 | + |
|
87 | + // regex string to match any of the operators |
|
88 | + protected static $operatorString; |
|
89 | + |
|
90 | + // these properties will supress division unless it's inside parenthases |
|
91 | + protected static $supressDivisionProps = |
|
92 | + array('/border-radius$/i', '/^font$/i'); |
|
93 | + |
|
94 | + protected $blockDirectives = array("font-face", "keyframes", "page", "-moz-document", "viewport", "-moz-viewport", "-o-viewport", "-ms-viewport"); |
|
95 | + protected $lineDirectives = array("charset"); |
|
96 | + |
|
97 | + /** |
|
98 | + * if we are in parens we can be more liberal with whitespace around |
|
99 | + * operators because it must evaluate to a single value and thus is less |
|
100 | + * ambiguous. |
|
101 | + * |
|
102 | + * Consider: |
|
103 | + * property1: 10 -5; // is two numbers, 10 and -5 |
|
104 | + * property2: (10 -5); // should evaluate to 5 |
|
105 | + */ |
|
106 | + protected $inParens = false; |
|
107 | + |
|
108 | + // caches preg escaped literals |
|
109 | + protected static $literalCache = array(); |
|
110 | + |
|
111 | + public $env; |
|
112 | + public $buffer; |
|
113 | + public $count; |
|
114 | + public $line; |
|
115 | + public $eatWhiteDefault; |
|
116 | + public $lessc; |
|
117 | + public $sourceName; |
|
118 | + public $writeComments; |
|
119 | + public $seenComments; |
|
120 | + public $currentProperty; |
|
121 | + public $inExp; |
|
122 | + |
|
123 | + |
|
124 | + public function __construct($lessc, $sourceName = null) |
|
125 | + { |
|
126 | + $this->eatWhiteDefault = true; |
|
127 | + // reference to less needed for vPrefix, mPrefix, and parentSelector |
|
128 | + $this->lessc = $lessc; |
|
129 | + |
|
130 | + $this->sourceName = $sourceName; // name used for error messages |
|
131 | + |
|
132 | + $this->writeComments = false; |
|
133 | + |
|
134 | + if (!self::$operatorString) { |
|
135 | + self::$operatorString = |
|
136 | + '('.implode('|', array_map( |
|
137 | + array('lessc', 'preg_quote'), |
|
138 | + array_keys(self::$precedence) |
|
139 | + )).')'; |
|
140 | + |
|
141 | + $commentSingle = Lessc::preg_quote(self::$commentSingle); |
|
142 | + $commentMultiLeft = Lessc::preg_quote(self::$commentMultiLeft); |
|
143 | + $commentMultiRight = Lessc::preg_quote(self::$commentMultiRight); |
|
144 | + |
|
145 | + self::$commentMulti = $commentMultiLeft.'.*?'.$commentMultiRight; |
|
146 | + self::$whitePattern = '/'.$commentSingle.'[^\n]*\s*|('.self::$commentMulti.')\s*|\s+/Ais'; |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * Parse a string |
|
152 | + * |
|
153 | + * @param string $buffer String to parse |
|
154 | + * @throws exception |
|
155 | + * @return NULL|stdclass |
|
156 | + */ |
|
157 | + public function parse($buffer) |
|
158 | + { |
|
159 | + $this->count = 0; |
|
160 | + $this->line = 1; |
|
161 | + |
|
162 | + $this->env = null; // block stack |
|
163 | + $this->buffer = $this->writeComments ? $buffer : $this->removeComments($buffer); |
|
164 | + $this->pushSpecialBlock("root"); |
|
165 | + $this->eatWhiteDefault = true; |
|
166 | + $this->seenComments = array(); |
|
167 | + |
|
168 | + // trim whitespace on head |
|
169 | + // if (preg_match('/^\s+/', $this->buffer, $m)) { |
|
170 | + // $this->line += substr_count($m[0], "\n"); |
|
171 | + // $this->buffer = ltrim($this->buffer); |
|
172 | + // } |
|
173 | + $this->whitespace(); |
|
174 | + |
|
175 | + // parse the entire file |
|
176 | + while (false !== $this->parseChunk()); |
|
177 | + |
|
178 | + if ($this->count != strlen($this->buffer)) { |
|
179 | + $this->throwError('parse error count '.$this->count.' != len buffer '.strlen($this->buffer)); |
|
180 | + |
|
181 | + } |
|
182 | + |
|
183 | + // TODO report where the block was opened |
|
184 | + if (!property_exists($this->env, 'parent') || !is_null($this->env->parent)) { |
|
185 | + throw new exception('parse error: unclosed block'); |
|
186 | + } |
|
187 | + |
|
188 | + return $this->env; |
|
189 | + } |
|
190 | + |
|
191 | + /** |
|
192 | + * Parse a single chunk off the head of the buffer and append it to the |
|
193 | + * current parse environment. |
|
194 | + * Returns false when the buffer is empty, or when there is an error. |
|
195 | + * |
|
196 | + * This function is called repeatedly until the entire document is |
|
197 | + * parsed. |
|
198 | + * |
|
199 | + * This parser is most similar to a recursive descent parser. Single |
|
200 | + * functions represent discrete grammatical rules for the language, and |
|
201 | + * they are able to capture the text that represents those rules. |
|
202 | + * |
|
203 | + * Consider the function Lessc::keyword(). (all parse functions are |
|
204 | + * structured the same) |
|
205 | + * |
|
206 | + * The function takes a single reference argument. When calling the |
|
207 | + * function it will attempt to match a keyword on the head of the buffer. |
|
208 | + * If it is successful, it will place the keyword in the referenced |
|
209 | + * argument, advance the position in the buffer, and return true. If it |
|
210 | + * fails then it won't advance the buffer and it will return false. |
|
211 | + * |
|
212 | + * All of these parse functions are powered by Lessc::match(), which behaves |
|
213 | + * the same way, but takes a literal regular expression. Sometimes it is |
|
214 | + * more convenient to use match instead of creating a new function. |
|
215 | + * |
|
216 | + * Because of the format of the functions, to parse an entire string of |
|
217 | + * grammatical rules, you can chain them together using &&. |
|
218 | + * |
|
219 | + * But, if some of the rules in the chain succeed before one fails, then |
|
220 | + * the buffer position will be left at an invalid state. In order to |
|
221 | + * avoid this, Lessc::seek() is used to remember and set buffer positions. |
|
222 | + * |
|
223 | + * Before parsing a chain, use $s = $this->seek() to remember the current |
|
224 | + * position into $s. Then if a chain fails, use $this->seek($s) to |
|
225 | + * go back where we started. |
|
226 | + */ |
|
227 | + protected function parseChunk() |
|
228 | + { |
|
229 | + if (empty($this->buffer)) { |
|
230 | + return false; |
|
231 | + } |
|
232 | + $s = $this->seek(); |
|
233 | + |
|
234 | + if ($this->whitespace()) { |
|
235 | + return true; |
|
236 | + } |
|
237 | + |
|
238 | + $key = null; |
|
239 | + $value = null; |
|
240 | + $mediaQueries = null; |
|
241 | + $dirName = null; |
|
242 | + $dirValue = null; |
|
243 | + $importValue = null; |
|
244 | + $guards = null; |
|
245 | + $tag = null; |
|
246 | + $args = null; |
|
247 | + $isVararg = null; |
|
248 | + $argv = null; |
|
249 | + $suffix = null; |
|
250 | + $var = null; |
|
251 | + $tags = null; |
|
252 | + |
|
253 | + // setting a property |
|
254 | + if ($this->keyword($key) && $this->assign() && |
|
255 | + $this->propertyValue($value, $key) && $this->end() |
|
256 | + ) { |
|
257 | + $this->append(array('assign', $key, $value), $s); |
|
258 | + return true; |
|
259 | + } else { |
|
260 | + $this->seek($s); |
|
261 | + } |
|
262 | + |
|
263 | + |
|
264 | + // look for special css blocks |
|
265 | + if ($this->literal('@', false)) { |
|
266 | + $this->count--; |
|
267 | + |
|
268 | + // media |
|
269 | + if ($this->literal('@media')) { |
|
270 | + if ($this->mediaQueryList($mediaQueries) |
|
271 | + && $this->literal('{') |
|
272 | + ) { |
|
273 | + $media = $this->pushSpecialBlock("media"); |
|
274 | + $media->queries = is_null($mediaQueries) ? array() : $mediaQueries; |
|
275 | + return true; |
|
276 | + } else { |
|
277 | + $this->seek($s); |
|
278 | + return false; |
|
279 | + } |
|
280 | + } |
|
281 | + |
|
282 | + if ($this->literal("@", false) && $this->keyword($dirName)) { |
|
283 | + if ($this->isDirective($dirName, $this->blockDirectives)) { |
|
284 | + if ($this->openString("{", $dirValue, null, array(";")) && |
|
285 | + $this->literal("{") |
|
286 | + ) { |
|
287 | + $dir = $this->pushSpecialBlock("directive"); |
|
288 | + $dir->name = $dirName; |
|
289 | + if (isset($dirValue)) { |
|
290 | + $dir->value = $dirValue; |
|
291 | + } |
|
292 | + return true; |
|
293 | + } |
|
294 | + } elseif ($this->isDirective($dirName, $this->lineDirectives)) { |
|
295 | + if ($this->propertyValue($dirValue) && $this->end()) { |
|
296 | + $this->append(array("directive", $dirName, $dirValue)); |
|
297 | + return true; |
|
298 | + } |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + $this->seek($s); |
|
303 | + } |
|
304 | + |
|
305 | + // setting a variable |
|
306 | + if ($this->variable($var) && $this->assign() && |
|
307 | + $this->propertyValue($value) && $this->end() |
|
308 | + ) { |
|
309 | + $this->append(array('assign', $var, $value), $s); |
|
310 | + return true; |
|
311 | + } else { |
|
312 | + $this->seek($s); |
|
313 | + } |
|
314 | + |
|
315 | + if ($this->import($importValue)) { |
|
316 | + $this->append($importValue, $s); |
|
317 | + return true; |
|
318 | + } |
|
319 | + |
|
320 | + // opening parametric mixin |
|
321 | + if ($this->tag($tag, true) && $this->argumentDef($args, $isVararg) && |
|
322 | + $this->guards($guards) && |
|
323 | + $this->literal('{') |
|
324 | + ) { |
|
325 | + $block = $this->pushBlock($this->fixTags(array($tag))); |
|
326 | + $block->args = $args; |
|
327 | + $block->isVararg = $isVararg; |
|
328 | + if (!empty($guards)) { |
|
329 | + $block->guards = $guards; |
|
330 | + } |
|
331 | + return true; |
|
332 | + } else { |
|
333 | + $this->seek($s); |
|
334 | + } |
|
335 | + |
|
336 | + // opening a simple block |
|
337 | + if ($this->tags($tags) && $this->literal('{', false)) { |
|
338 | + $tags = $this->fixTags($tags); |
|
339 | + $this->pushBlock($tags); |
|
340 | + return true; |
|
341 | + } else { |
|
342 | + $this->seek($s); |
|
343 | + } |
|
344 | + |
|
345 | + // closing a block |
|
346 | + if ($this->literal('}', false)) { |
|
347 | + try { |
|
348 | + $block = $this->pop(); |
|
349 | + } catch (exception $e) { |
|
350 | + $this->seek($s); |
|
351 | + $this->throwError($e->getMessage()); |
|
352 | + } |
|
353 | + |
|
354 | + $hidden = false; |
|
355 | + if (is_null($block->type)) { |
|
356 | + $hidden = true; |
|
357 | + if (!isset($block->args)) { |
|
358 | + foreach ($block->tags as $tag) { |
|
359 | + if (!is_string($tag) || $tag[0] != $this->lessc->mPrefix) { |
|
360 | + $hidden = false; |
|
361 | + break; |
|
362 | + } |
|
363 | + } |
|
364 | + } |
|
365 | + |
|
366 | + foreach ($block->tags as $tag) { |
|
367 | + if (is_string($tag)) { |
|
368 | + $this->env->children[$tag][] = $block; |
|
369 | + } |
|
370 | + } |
|
371 | + } |
|
372 | + |
|
373 | + if (!$hidden) { |
|
374 | + $this->append(array('block', $block), $s); |
|
375 | + } |
|
376 | + |
|
377 | + // this is done here so comments aren't bundled into he block that |
|
378 | + // was just closed |
|
379 | + $this->whitespace(); |
|
380 | + return true; |
|
381 | + } |
|
382 | + |
|
383 | + // mixin |
|
384 | + if ($this->mixinTags($tags) && |
|
385 | + $this->argumentDef($argv, $isVararg) && |
|
386 | + $this->keyword($suffix) && $this->end() |
|
387 | + ) { |
|
388 | + $tags = $this->fixTags($tags); |
|
389 | + $this->append(array('mixin', $tags, $argv, $suffix), $s); |
|
390 | + return true; |
|
391 | + } else { |
|
392 | + $this->seek($s); |
|
393 | + } |
|
394 | + |
|
395 | + // spare ; |
|
396 | + if ($this->literal(';')) { |
|
397 | + return true; |
|
398 | + } |
|
399 | + |
|
400 | + return false; // got nothing, throw error |
|
401 | + } |
|
402 | + |
|
403 | + protected function isDirective($dirname, $directives) |
|
404 | + { |
|
405 | + // TODO: cache pattern in parser |
|
406 | + $pattern = implode( |
|
407 | + "|", |
|
408 | + array_map(array("lessc", "preg_quote"), $directives) |
|
409 | + ); |
|
410 | + $pattern = '/^(-[a-z-]+-)?('.$pattern.')$/i'; |
|
411 | + |
|
412 | + return preg_match($pattern, $dirname); |
|
413 | + } |
|
414 | + |
|
415 | + protected function fixTags($tags) |
|
416 | + { |
|
417 | + // move @ tags out of variable namespace |
|
418 | + foreach ($tags as &$tag) { |
|
419 | + if ($tag[0] == $this->lessc->vPrefix) { |
|
420 | + $tag[0] = $this->lessc->mPrefix; |
|
421 | + } |
|
422 | + } |
|
423 | + return $tags; |
|
424 | + } |
|
425 | + |
|
426 | + // a list of expressions |
|
427 | + protected function expressionList(&$exps) |
|
428 | + { |
|
429 | + $exp = null; |
|
430 | + |
|
431 | + $values = array(); |
|
432 | + |
|
433 | + while ($this->expression($exp)) { |
|
434 | + $values[] = $exp; |
|
435 | + } |
|
436 | + |
|
437 | + if (count($values) == 0) { |
|
438 | + return false; |
|
439 | + } |
|
440 | + |
|
441 | + $exps = Lessc::compressList($values, ' '); |
|
442 | + return true; |
|
443 | + } |
|
444 | + |
|
445 | + /** |
|
446 | + * Attempt to consume an expression. |
|
447 | + * @link http://en.wikipedia.org/wiki/Operator-precedence_parser#Pseudo-code |
|
448 | + */ |
|
449 | + protected function expression(&$out) |
|
450 | + { |
|
451 | + $lhs = null; |
|
452 | + $rhs = null; |
|
453 | + |
|
454 | + if ($this->value($lhs)) { |
|
455 | + $out = $this->expHelper($lhs, 0); |
|
456 | + |
|
457 | + // look for / shorthand |
|
458 | + if (!empty($this->env->supressedDivision)) { |
|
459 | + unset($this->env->supressedDivision); |
|
460 | + $s = $this->seek(); |
|
461 | + if ($this->literal("/") && $this->value($rhs)) { |
|
462 | + $out = array("list", "", |
|
463 | + array($out, array("keyword", "/"), $rhs)); |
|
464 | + } else { |
|
465 | + $this->seek($s); |
|
466 | + } |
|
467 | + } |
|
468 | + |
|
469 | + return true; |
|
470 | + } |
|
471 | + return false; |
|
472 | + } |
|
473 | + |
|
474 | + /** |
|
475 | + * recursively parse infix equation with $lhs at precedence $minP |
|
476 | + */ |
|
477 | + protected function expHelper($lhs, $minP) |
|
478 | + { |
|
479 | + $next = null; |
|
480 | + $rhs = null; |
|
481 | + |
|
482 | + $this->inExp = true; |
|
483 | + $ss = $this->seek(); |
|
484 | + |
|
485 | + while (true) { |
|
486 | + $whiteBefore = isset($this->buffer[$this->count - 1]) && |
|
487 | + ctype_space($this->buffer[$this->count - 1]); |
|
488 | + |
|
489 | + // If there is whitespace before the operator, then we require |
|
490 | + // whitespace after the operator for it to be an expression |
|
491 | + $needWhite = $whiteBefore && !$this->inParens; |
|
492 | + |
|
493 | + $m = array(); |
|
494 | + if ($this->match(self::$operatorString.($needWhite ? '\s' : ''), $m) && self::$precedence[$m[1]] >= $minP) { |
|
495 | + if (!$this->inParens && isset($this->env->currentProperty) && $m[1] == "/" && empty($this->env->supressedDivision)) { |
|
496 | + foreach (self::$supressDivisionProps as $pattern) { |
|
497 | + if (preg_match($pattern, $this->env->currentProperty)) { |
|
498 | + $this->env->supressedDivision = true; |
|
499 | + break 2; |
|
500 | + } |
|
501 | + } |
|
502 | + } |
|
503 | + |
|
504 | + |
|
505 | + $whiteAfter = isset($this->buffer[$this->count - 1]) && |
|
506 | + ctype_space($this->buffer[$this->count - 1]); |
|
507 | + |
|
508 | + if (!$this->value($rhs)) { |
|
509 | + break; |
|
510 | + } |
|
511 | + |
|
512 | + // peek for next operator to see what to do with rhs |
|
513 | + if ($this->peek(self::$operatorString, $next) && self::$precedence[$next[1]] > self::$precedence[$m[1]]) { |
|
514 | + $rhs = $this->expHelper($rhs, self::$precedence[$next[1]]); |
|
515 | + } |
|
516 | + |
|
517 | + $lhs = array('expression', $m[1], $lhs, $rhs, $whiteBefore, $whiteAfter); |
|
518 | + $ss = $this->seek(); |
|
519 | + |
|
520 | + continue; |
|
521 | + } |
|
522 | + |
|
523 | + break; |
|
524 | + } |
|
525 | + |
|
526 | + $this->seek($ss); |
|
527 | + |
|
528 | + return $lhs; |
|
529 | + } |
|
530 | + |
|
531 | + // consume a list of values for a property |
|
532 | + public function propertyValue(&$value, $keyName = null) |
|
533 | + { |
|
534 | + $v = null; |
|
535 | + $values = array(); |
|
536 | + |
|
537 | + if ($keyName !== null) { |
|
538 | + $this->env->currentProperty = $keyName; |
|
539 | + } |
|
540 | + |
|
541 | + $s = null; |
|
542 | + while ($this->expressionList($v)) { |
|
543 | + $values[] = $v; |
|
544 | + $s = $this->seek(); |
|
545 | + if (!$this->literal(',')) { |
|
546 | + break; |
|
547 | + } |
|
548 | + } |
|
549 | + |
|
550 | + if ($s) { |
|
551 | + $this->seek($s); |
|
552 | + } |
|
553 | + |
|
554 | + if ($keyName !== null) { |
|
555 | + unset($this->env->currentProperty); |
|
556 | + } |
|
557 | + |
|
558 | + if (count($values) == 0) { |
|
559 | + return false; |
|
560 | + } |
|
561 | + |
|
562 | + $value = Lessc::compressList($values, ', '); |
|
563 | + return true; |
|
564 | + } |
|
565 | + |
|
566 | + protected function parenValue(&$out) |
|
567 | + { |
|
568 | + $exp = null; |
|
569 | + |
|
570 | + $s = $this->seek(); |
|
571 | + |
|
572 | + // speed shortcut |
|
573 | + if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] != "(") { |
|
574 | + return false; |
|
575 | + } |
|
576 | + |
|
577 | + $inParens = $this->inParens; |
|
578 | + if ($this->literal("(") && |
|
579 | + ($this->inParens = true) && $this->expression($exp) && |
|
580 | + $this->literal(")") |
|
581 | + ) { |
|
582 | + $out = $exp; |
|
583 | + $this->inParens = $inParens; |
|
584 | + return true; |
|
585 | + } else { |
|
586 | + $this->inParens = $inParens; |
|
587 | + $this->seek($s); |
|
588 | + } |
|
589 | + |
|
590 | + return false; |
|
591 | + } |
|
592 | + |
|
593 | + // a single value |
|
594 | + protected function value(&$value) |
|
595 | + { |
|
596 | + $inner = null; |
|
597 | + $word = null; |
|
598 | + $str = null; |
|
599 | + $var = null; |
|
600 | + |
|
601 | + $s = $this->seek(); |
|
602 | + |
|
603 | + // speed shortcut |
|
604 | + if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] == "-") { |
|
605 | + // negation |
|
606 | + if ($this->literal("-", false) && |
|
607 | + (($this->variable($inner) && $inner = array("variable", $inner)) || |
|
608 | + $this->unit($inner) || |
|
609 | + $this->parenValue($inner)) |
|
610 | + ) { |
|
611 | + $value = array("unary", "-", $inner); |
|
612 | + return true; |
|
613 | + } else { |
|
614 | + $this->seek($s); |
|
615 | + } |
|
616 | + } |
|
617 | + |
|
618 | + if ($this->parenValue($value)) { |
|
619 | + return true; |
|
620 | + } |
|
621 | + if ($this->unit($value)) { |
|
622 | + return true; |
|
623 | + } |
|
624 | + if ($this->color($value)) { |
|
625 | + return true; |
|
626 | + } |
|
627 | + if ($this->func($value)) { |
|
628 | + return true; |
|
629 | + } |
|
630 | + if ($this->string($value)) { |
|
631 | + return true; |
|
632 | + } |
|
633 | + |
|
634 | + if ($this->keyword($word)) { |
|
635 | + $value = array('keyword', $word); |
|
636 | + return true; |
|
637 | + } |
|
638 | + |
|
639 | + // try a variable |
|
640 | + if ($this->variable($var)) { |
|
641 | + $value = array('variable', $var); |
|
642 | + return true; |
|
643 | + } |
|
644 | + |
|
645 | + // unquote string (should this work on any type? |
|
646 | + if ($this->literal("~") && $this->string($str)) { |
|
647 | + $value = array("escape", $str); |
|
648 | + return true; |
|
649 | + } else { |
|
650 | + $this->seek($s); |
|
651 | + } |
|
652 | + |
|
653 | + // css hack: \0 |
|
654 | + $m = array(); |
|
655 | + if ($this->literal('\\') && $this->match('([0-9]+)', $m)) { |
|
656 | + $value = array('keyword', '\\'.$m[1]); |
|
657 | + return true; |
|
658 | + } else { |
|
659 | + $this->seek($s); |
|
660 | + } |
|
661 | + |
|
662 | + return false; |
|
663 | + } |
|
664 | + |
|
665 | + // an import statement |
|
666 | + protected function import(&$out, $value = '') |
|
667 | + { |
|
668 | + if (!$this->literal('@import')) { |
|
669 | + return false; |
|
670 | + } |
|
671 | + |
|
672 | + // @import "something.css" media; |
|
673 | + // @import url("something.css") media; |
|
674 | + // @import url(something.css) media; |
|
675 | + |
|
676 | + if ($this->propertyValue($value)) { |
|
677 | + $out = array("import", $value); |
|
678 | + return true; |
|
679 | + } |
|
680 | + |
|
681 | + return false; |
|
682 | + } |
|
683 | + |
|
684 | + protected function mediaQueryList(&$out) |
|
685 | + { |
|
686 | + $list = null; |
|
687 | + |
|
688 | + if ($this->genericList($list, "mediaQuery", ",", false)) { |
|
689 | + $out = $list[2]; |
|
690 | + return true; |
|
691 | + } |
|
692 | + return false; |
|
693 | + } |
|
694 | + |
|
695 | + protected function mediaQuery(&$out) |
|
696 | + { |
|
697 | + $mediaType = null; |
|
698 | + |
|
699 | + $s = $this->seek(); |
|
700 | + |
|
701 | + $expressions = null; |
|
702 | + $parts = array(); |
|
703 | + |
|
704 | + if ((($this->literal("only") && ($only = true)) || ($this->literal("not") && ($not = true))) && $this->keyword($mediaType)) { |
|
705 | + $prop = array("mediaType"); |
|
706 | + if (isset($only)) { |
|
707 | + $prop[] = "only"; |
|
708 | + } |
|
709 | + if (isset($not)) { |
|
710 | + $prop[] = "not"; |
|
711 | + } |
|
712 | + $prop[] = $mediaType; |
|
713 | + $parts[] = $prop; |
|
714 | + } else { |
|
715 | + $this->seek($s); |
|
716 | + } |
|
717 | + |
|
718 | + |
|
719 | + if (!empty($mediaType) && !$this->literal("and")) { |
|
720 | + // ~ |
|
721 | + } else { |
|
722 | + $this->genericList($expressions, "mediaExpression", "and", false); |
|
723 | + if (is_array($expressions)) { |
|
724 | + $parts = array_merge($parts, $expressions[2]); |
|
725 | + } |
|
726 | + } |
|
727 | + |
|
728 | + if (count($parts) == 0) { |
|
729 | + $this->seek($s); |
|
730 | + return false; |
|
731 | + } |
|
732 | + |
|
733 | + $out = $parts; |
|
734 | + return true; |
|
735 | + } |
|
736 | + |
|
737 | + protected function mediaExpression(&$out) |
|
738 | + { |
|
739 | + $feature = null; |
|
740 | + $variable = null; |
|
741 | + |
|
742 | + $s = $this->seek(); |
|
743 | + $value = null; |
|
744 | + if ($this->literal("(") && |
|
745 | + $this->keyword($feature) && |
|
746 | + ($this->literal(":") && $this->expression($value)) && |
|
747 | + $this->literal(")") |
|
748 | + ) { |
|
749 | + $out = array("mediaExp", $feature); |
|
750 | + if ($value) { |
|
751 | + $out[] = $value; |
|
752 | + } |
|
753 | + return true; |
|
754 | + } elseif ($this->variable($variable)) { |
|
755 | + $out = array('variable', $variable); |
|
756 | + return true; |
|
757 | + } |
|
758 | + |
|
759 | + $this->seek($s); |
|
760 | + return false; |
|
761 | + } |
|
762 | + |
|
763 | + // an unbounded string stopped by $end |
|
764 | + protected function openString($end, &$out, $nestingOpen = null, $rejectStrs = null) |
|
765 | + { |
|
766 | + $str = null; |
|
767 | + $inter = null; |
|
768 | + |
|
769 | + $oldWhite = $this->eatWhiteDefault; |
|
770 | + $this->eatWhiteDefault = false; |
|
771 | + |
|
772 | + $stop = array("'", '"', "@{", $end); |
|
773 | + $stop = array_map(array("lessc", "preg_quote"), $stop); |
|
774 | + // $stop[] = self::$commentMulti; |
|
775 | + |
|
776 | + if (!is_null($rejectStrs)) { |
|
777 | + $stop = array_merge($stop, $rejectStrs); |
|
778 | + } |
|
779 | + |
|
780 | + $patt = '(.*?)('.implode("|", $stop).')'; |
|
781 | + |
|
782 | + $nestingLevel = 0; |
|
783 | + |
|
784 | + $content = array(); |
|
785 | + $m = array(); |
|
786 | + while ($this->match($patt, $m, false)) { |
|
787 | + if (!empty($m[1])) { |
|
788 | + $content[] = $m[1]; |
|
789 | + if ($nestingOpen) { |
|
790 | + $nestingLevel += substr_count($m[1], $nestingOpen); |
|
791 | + } |
|
792 | + } |
|
793 | + |
|
794 | + $tok = $m[2]; |
|
795 | + |
|
796 | + $this->count -= strlen($tok); |
|
797 | + if ($tok == $end) { |
|
798 | + if ($nestingLevel == 0) { |
|
799 | + break; |
|
800 | + } else { |
|
801 | + $nestingLevel--; |
|
802 | + } |
|
803 | + } |
|
804 | + |
|
805 | + if (($tok == "'" || $tok == '"') && $this->string($str)) { |
|
806 | + $content[] = $str; |
|
807 | + continue; |
|
808 | + } |
|
809 | + |
|
810 | + if ($tok == "@{" && $this->interpolation($inter)) { |
|
811 | + $content[] = $inter; |
|
812 | + continue; |
|
813 | + } |
|
814 | + |
|
815 | + if (!empty($rejectStrs) && in_array($tok, $rejectStrs)) { |
|
816 | + break; |
|
817 | + } |
|
818 | + |
|
819 | + $content[] = $tok; |
|
820 | + $this->count += strlen($tok); |
|
821 | + } |
|
822 | + |
|
823 | + $this->eatWhiteDefault = $oldWhite; |
|
824 | + |
|
825 | + if (count($content) == 0) { |
|
826 | + return false; |
|
827 | + } |
|
828 | + |
|
829 | + // trim the end |
|
830 | + if (is_string(end($content))) { |
|
831 | + $content[count($content) - 1] = rtrim(end($content)); |
|
832 | + } |
|
833 | + |
|
834 | + $out = array("string", "", $content); |
|
835 | + return true; |
|
836 | + } |
|
837 | + |
|
838 | + protected function string(&$out) |
|
839 | + { |
|
840 | + $inter = null; |
|
841 | + |
|
842 | + $s = $this->seek(); |
|
843 | + if ($this->literal('"', false)) { |
|
844 | + $delim = '"'; |
|
845 | + } elseif ($this->literal("'", false)) { |
|
846 | + $delim = "'"; |
|
847 | + } else { |
|
848 | + return false; |
|
849 | + } |
|
850 | + |
|
851 | + $content = array(); |
|
852 | + |
|
853 | + // look for either ending delim , escape, or string interpolation |
|
854 | + $patt = '([^\n]*?)(@\{|\\\\|'. |
|
855 | + Lessc::preg_quote($delim).')'; |
|
856 | + |
|
857 | + $oldWhite = $this->eatWhiteDefault; |
|
858 | + $this->eatWhiteDefault = false; |
|
859 | + |
|
860 | + $m = array(); |
|
861 | + while ($this->match($patt, $m, false)) { |
|
862 | + $content[] = $m[1]; |
|
863 | + if ($m[2] == "@{") { |
|
864 | + $this->count -= strlen($m[2]); |
|
865 | + if ($this->interpolation($inter)) { |
|
866 | + $content[] = $inter; |
|
867 | + } else { |
|
868 | + $this->count += strlen($m[2]); |
|
869 | + $content[] = "@{"; // ignore it |
|
870 | + } |
|
871 | + } elseif ($m[2] == '\\') { |
|
872 | + $content[] = $m[2]; |
|
873 | + if ($this->literal($delim, false)) { |
|
874 | + $content[] = $delim; |
|
875 | + } |
|
876 | + } else { |
|
877 | + $this->count -= strlen($delim); |
|
878 | + break; // delim |
|
879 | + } |
|
880 | + } |
|
881 | + |
|
882 | + $this->eatWhiteDefault = $oldWhite; |
|
883 | + |
|
884 | + if ($this->literal($delim)) { |
|
885 | + $out = array("string", $delim, $content); |
|
886 | + return true; |
|
887 | + } |
|
888 | + |
|
889 | + $this->seek($s); |
|
890 | + return false; |
|
891 | + } |
|
892 | + |
|
893 | + protected function interpolation(&$out) |
|
894 | + { |
|
895 | + $interp = array(); |
|
896 | + |
|
897 | + $oldWhite = $this->eatWhiteDefault; |
|
898 | + $this->eatWhiteDefault = true; |
|
899 | + |
|
900 | + $s = $this->seek(); |
|
901 | + if ($this->literal("@{") && |
|
902 | + $this->openString("}", $interp, null, array("'", '"', ";")) && |
|
903 | + $this->literal("}", false) |
|
904 | + ) { |
|
905 | + $out = array("interpolate", $interp); |
|
906 | + $this->eatWhiteDefault = $oldWhite; |
|
907 | + if ($this->eatWhiteDefault) { |
|
908 | + $this->whitespace(); |
|
909 | + } |
|
910 | + return true; |
|
911 | + } |
|
912 | + |
|
913 | + $this->eatWhiteDefault = $oldWhite; |
|
914 | + $this->seek($s); |
|
915 | + return false; |
|
916 | + } |
|
917 | + |
|
918 | + protected function unit(&$unit) |
|
919 | + { |
|
920 | + $m = array(); |
|
921 | + |
|
922 | + // speed shortcut |
|
923 | + if (isset($this->buffer[$this->count])) { |
|
924 | + $char = $this->buffer[$this->count]; |
|
925 | + if (!ctype_digit($char) && $char != ".") { |
|
926 | + return false; |
|
927 | + } |
|
928 | + } |
|
929 | + |
|
930 | + if ($this->match('([0-9]+(?:\.[0-9]*)?|\.[0-9]+)([%a-zA-Z]+)?', $m)) { |
|
931 | + $unit = array("number", $m[1], empty($m[2]) ? "" : $m[2]); |
|
932 | + return true; |
|
933 | + } |
|
934 | + return false; |
|
935 | + } |
|
936 | + |
|
937 | + // a # color |
|
938 | + protected function color(&$out) |
|
939 | + { |
|
940 | + $m = array(); |
|
941 | + |
|
942 | + if ($this->match('(#(?:[0-9a-f]{8}|[0-9a-f]{6}|[0-9a-f]{3}))', $m)) { |
|
943 | + if (strlen($m[1]) > 7) { |
|
944 | + $out = array("string", "", array($m[1])); |
|
945 | + } else { |
|
946 | + $out = array("raw_color", $m[1]); |
|
947 | + } |
|
948 | + return true; |
|
949 | + } |
|
950 | + |
|
951 | + return false; |
|
952 | + } |
|
953 | + |
|
954 | + // consume an argument definition list surrounded by () |
|
955 | + // each argument is a variable name with optional value |
|
956 | + // or at the end a ... or a variable named followed by ... |
|
957 | + // arguments are separated by , unless a ; is in the list, then ; is the |
|
958 | + // delimiter. |
|
959 | + protected function argumentDef(&$args, &$isVararg) |
|
960 | + { |
|
961 | + $value = array(); |
|
962 | + $rhs = null; |
|
963 | + |
|
964 | + $s = $this->seek(); |
|
965 | + if (!$this->literal('(')) { |
|
966 | + return false; |
|
967 | + } |
|
968 | + |
|
969 | + $values = array(); |
|
970 | + $delim = ","; |
|
971 | + $method = "expressionList"; |
|
972 | + |
|
973 | + $isVararg = false; |
|
974 | + while (true) { |
|
975 | + if ($this->literal("...")) { |
|
976 | + $isVararg = true; |
|
977 | + break; |
|
978 | + } |
|
979 | + |
|
980 | + if ($this->$method($value)) { |
|
981 | + if ($value[0] == "variable") { |
|
982 | + $arg = array("arg", $value[1]); |
|
983 | + $ss = $this->seek(); |
|
984 | + |
|
985 | + if ($this->assign() && $this->$method($rhs)) { |
|
986 | + $arg[] = $rhs; |
|
987 | + } else { |
|
988 | + $this->seek($ss); |
|
989 | + if ($this->literal("...")) { |
|
990 | + $arg[0] = "rest"; |
|
991 | + $isVararg = true; |
|
992 | + } |
|
993 | + } |
|
994 | + |
|
995 | + $values[] = $arg; |
|
996 | + if ($isVararg) { |
|
997 | + break; |
|
998 | + } |
|
999 | + continue; |
|
1000 | + } else { |
|
1001 | + $values[] = array("lit", $value); |
|
1002 | + } |
|
1003 | + } |
|
1004 | + |
|
1005 | + |
|
1006 | + if (!$this->literal($delim)) { |
|
1007 | + if ($delim == "," && $this->literal(";")) { |
|
1008 | + // found new delim, convert existing args |
|
1009 | + $delim = ";"; |
|
1010 | + $method = "propertyValue"; |
|
1011 | + |
|
1012 | + // transform arg list |
|
1013 | + if (isset($values[1])) { // 2 items |
|
1014 | + $newList = array(); |
|
1015 | + foreach ($values as $i => $arg) { |
|
1016 | + switch ($arg[0]) { |
|
1017 | + case "arg": |
|
1018 | + if ($i) { |
|
1019 | + $this->throwError("Cannot mix ; and , as delimiter types"); |
|
1020 | + } |
|
1021 | + $newList[] = $arg[2]; |
|
1022 | + break; |
|
1023 | + case "lit": |
|
1024 | + $newList[] = $arg[1]; |
|
1025 | + break; |
|
1026 | + case "rest": |
|
1027 | + $this->throwError("Unexpected rest before semicolon"); |
|
1028 | + } |
|
1029 | + } |
|
1030 | + |
|
1031 | + $newList = array("list", ", ", $newList); |
|
1032 | + |
|
1033 | + switch ($values[0][0]) { |
|
1034 | + case "arg": |
|
1035 | + $newArg = array("arg", $values[0][1], $newList); |
|
1036 | + break; |
|
1037 | + case "lit": |
|
1038 | + $newArg = array("lit", $newList); |
|
1039 | + break; |
|
1040 | + } |
|
1041 | + |
|
1042 | + } elseif ($values) { // 1 item |
|
1043 | + $newArg = $values[0]; |
|
1044 | + } |
|
1045 | + |
|
1046 | + if ($newArg) { |
|
1047 | + $values = array($newArg); |
|
1048 | + } |
|
1049 | + } else { |
|
1050 | + break; |
|
1051 | + } |
|
1052 | + } |
|
1053 | + } |
|
1054 | + |
|
1055 | + if (!$this->literal(')')) { |
|
1056 | + $this->seek($s); |
|
1057 | + return false; |
|
1058 | + } |
|
1059 | + |
|
1060 | + $args = $values; |
|
1061 | + |
|
1062 | + return true; |
|
1063 | + } |
|
1064 | + |
|
1065 | + // consume a list of tags |
|
1066 | + // this accepts a hanging delimiter |
|
1067 | + protected function tags(&$tags, $simple = false, $delim = ',') |
|
1068 | + { |
|
1069 | + $tt = array(); |
|
1070 | + |
|
1071 | + $tags = array(); |
|
1072 | + while ($this->tag($tt, $simple)) { |
|
1073 | + $tags[] = $tt; |
|
1074 | + if (!$this->literal($delim)) { |
|
1075 | + break; |
|
1076 | + } |
|
1077 | + } |
|
1078 | + if (count($tags) == 0) { |
|
1079 | + return false; |
|
1080 | + } |
|
1081 | + |
|
1082 | + return true; |
|
1083 | + } |
|
1084 | + |
|
1085 | + // list of tags of specifying mixin path |
|
1086 | + // optionally separated by > (lazy, accepts extra >) |
|
1087 | + protected function mixinTags(&$tags) |
|
1088 | + { |
|
1089 | + $tt = array(); |
|
1090 | + |
|
1091 | + $tags = array(); |
|
1092 | + while ($this->tag($tt, true)) { |
|
1093 | + $tags[] = $tt; |
|
1094 | + $this->literal(">"); |
|
1095 | + } |
|
1096 | + |
|
1097 | + if (!$tags) { |
|
1098 | + return false; |
|
1099 | + } |
|
1100 | + |
|
1101 | + return true; |
|
1102 | + } |
|
1103 | + |
|
1104 | + // a bracketed value (contained within in a tag definition) |
|
1105 | + protected function tagBracket(&$parts, &$hasExpression) |
|
1106 | + { |
|
1107 | + $str = null; |
|
1108 | + $inter = null; |
|
1109 | + $word = null; |
|
1110 | + |
|
1111 | + // speed shortcut |
|
1112 | + if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] != "[") { |
|
1113 | + return false; |
|
1114 | + } |
|
1115 | + |
|
1116 | + $s = $this->seek(); |
|
1117 | + |
|
1118 | + $hasInterpolation = false; |
|
1119 | + |
|
1120 | + if ($this->literal("[", false)) { |
|
1121 | + $attrParts = array("["); |
|
1122 | + // keyword, string, operator |
|
1123 | + while (true) { |
|
1124 | + if ($this->literal("]", false)) { |
|
1125 | + $this->count--; |
|
1126 | + break; // get out early |
|
1127 | + } |
|
1128 | + |
|
1129 | + $m = array(); |
|
1130 | + if ($this->match('\s+', $m)) { |
|
1131 | + $attrParts[] = " "; |
|
1132 | + continue; |
|
1133 | + } |
|
1134 | + if ($this->string($str)) { |
|
1135 | + // escape parent selector, (yuck) |
|
1136 | + foreach ($str[2] as &$chunk) { |
|
1137 | + $chunk = str_replace($this->lessc->parentSelector, "$&$", $chunk); |
|
1138 | + } |
|
1139 | + |
|
1140 | + $attrParts[] = $str; |
|
1141 | + $hasInterpolation = true; |
|
1142 | + continue; |
|
1143 | + } |
|
1144 | + |
|
1145 | + if ($this->keyword($word)) { |
|
1146 | + $attrParts[] = $word; |
|
1147 | + continue; |
|
1148 | + } |
|
1149 | + |
|
1150 | + if ($this->interpolation($inter)) { |
|
1151 | + $attrParts[] = $inter; |
|
1152 | + $hasInterpolation = true; |
|
1153 | + continue; |
|
1154 | + } |
|
1155 | + |
|
1156 | + // operator, handles attr namespace too |
|
1157 | + if ($this->match('[|-~\$\*\^=]+', $m)) { |
|
1158 | + $attrParts[] = $m[0]; |
|
1159 | + continue; |
|
1160 | + } |
|
1161 | + |
|
1162 | + break; |
|
1163 | + } |
|
1164 | + |
|
1165 | + if ($this->literal("]", false)) { |
|
1166 | + $attrParts[] = "]"; |
|
1167 | + foreach ($attrParts as $part) { |
|
1168 | + $parts[] = $part; |
|
1169 | + } |
|
1170 | + $hasExpression = $hasExpression || $hasInterpolation; |
|
1171 | + return true; |
|
1172 | + } |
|
1173 | + $this->seek($s); |
|
1174 | + } |
|
1175 | + |
|
1176 | + $this->seek($s); |
|
1177 | + return false; |
|
1178 | + } |
|
1179 | + |
|
1180 | + // a space separated list of selectors |
|
1181 | + protected function tag(&$tag, $simple = false) |
|
1182 | + { |
|
1183 | + $interp = null; |
|
1184 | + $unit = null; |
|
1185 | + |
|
1186 | + if ($simple) { |
|
1187 | + $chars = '^@,:;{}\][>\(\) "\''; |
|
1188 | + } else { |
|
1189 | + $chars = '^@,;{}["\''; |
|
1190 | + } |
|
1191 | + $s = $this->seek(); |
|
1192 | + |
|
1193 | + $hasExpression = false; |
|
1194 | + $parts = array(); |
|
1195 | + while ($this->tagBracket($parts, $hasExpression)); |
|
1196 | + |
|
1197 | + $oldWhite = $this->eatWhiteDefault; |
|
1198 | + $this->eatWhiteDefault = false; |
|
1199 | + |
|
1200 | + while (true) { |
|
1201 | + $m = array(); |
|
1202 | + if ($this->match('(['.$chars.'0-9]['.$chars.']*)', $m)) { |
|
1203 | + $parts[] = $m[1]; |
|
1204 | + if ($simple) { |
|
1205 | + break; |
|
1206 | + } |
|
1207 | + |
|
1208 | + while ($this->tagBracket($parts, $hasExpression)); |
|
1209 | + continue; |
|
1210 | + } |
|
1211 | + |
|
1212 | + if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] == "@") { |
|
1213 | + if ($this->interpolation($interp)) { |
|
1214 | + $hasExpression = true; |
|
1215 | + $interp[2] = true; // don't unescape |
|
1216 | + $parts[] = $interp; |
|
1217 | + continue; |
|
1218 | + } |
|
1219 | + |
|
1220 | + if ($this->literal("@")) { |
|
1221 | + $parts[] = "@"; |
|
1222 | + continue; |
|
1223 | + } |
|
1224 | + } |
|
1225 | + |
|
1226 | + if ($this->unit($unit)) { // for keyframes |
|
1227 | + $parts[] = $unit[1]; |
|
1228 | + $parts[] = $unit[2]; |
|
1229 | + continue; |
|
1230 | + } |
|
1231 | + |
|
1232 | + break; |
|
1233 | + } |
|
1234 | + |
|
1235 | + $this->eatWhiteDefault = $oldWhite; |
|
1236 | + if (!$parts) { |
|
1237 | + $this->seek($s); |
|
1238 | + return false; |
|
1239 | + } |
|
1240 | + |
|
1241 | + if ($hasExpression) { |
|
1242 | + $tag = array("exp", array("string", "", $parts)); |
|
1243 | + } else { |
|
1244 | + $tag = trim(implode($parts)); |
|
1245 | + } |
|
1246 | + |
|
1247 | + $this->whitespace(); |
|
1248 | + return true; |
|
1249 | + } |
|
1250 | + |
|
1251 | + // a css function |
|
1252 | + protected function func(&$func) |
|
1253 | + { |
|
1254 | + $s = $this->seek(); |
|
1255 | + |
|
1256 | + $m = array(); |
|
1257 | + $value = array(); |
|
1258 | + $string = array(); |
|
1259 | + $name = null; |
|
1260 | + |
|
1261 | + if ($this->match('(%|[\w\-_][\w\-_:\.]+|[\w_])', $m) && $this->literal('(')) { |
|
1262 | + $fname = $m[1]; |
|
1263 | + |
|
1264 | + $sPreArgs = $this->seek(); |
|
1265 | + |
|
1266 | + $args = array(); |
|
1267 | + while (true) { |
|
1268 | + $ss = $this->seek(); |
|
1269 | + // this ugly nonsense is for ie filter properties |
|
1270 | + if ($this->keyword($name) && $this->literal('=') && $this->expressionList($value)) { |
|
1271 | + $args[] = array("string", "", array($name, "=", $value)); |
|
1272 | + } else { |
|
1273 | + $this->seek($ss); |
|
1274 | + if ($this->expressionList($value)) { |
|
1275 | + $args[] = $value; |
|
1276 | + } |
|
1277 | + } |
|
1278 | + |
|
1279 | + if (!$this->literal(',')) { |
|
1280 | + break; |
|
1281 | + } |
|
1282 | + } |
|
1283 | + $args = array('list', ',', $args); |
|
1284 | + |
|
1285 | + if ($this->literal(')')) { |
|
1286 | + $func = array('function', $fname, $args); |
|
1287 | + return true; |
|
1288 | + } elseif ($fname == 'url') { |
|
1289 | + // couldn't parse and in url? treat as string |
|
1290 | + $this->seek($sPreArgs); |
|
1291 | + if ($this->openString(")", $string) && $this->literal(")")) { |
|
1292 | + $func = array('function', $fname, $string); |
|
1293 | + return true; |
|
1294 | + } |
|
1295 | + } |
|
1296 | + } |
|
1297 | + |
|
1298 | + $this->seek($s); |
|
1299 | + return false; |
|
1300 | + } |
|
1301 | + |
|
1302 | + // consume a less variable |
|
1303 | + protected function variable(&$name) |
|
1304 | + { |
|
1305 | + $sub = null; |
|
1306 | + $name = null; |
|
1307 | + |
|
1308 | + $s = $this->seek(); |
|
1309 | + if ($this->literal($this->lessc->vPrefix, false) && |
|
1310 | + ($this->variable($sub) || $this->keyword($name)) |
|
1311 | + ) { |
|
1312 | + if (!empty($sub)) { |
|
1313 | + $name = array('variable', $sub); |
|
1314 | + } else { |
|
1315 | + $name = $this->lessc->vPrefix.$name; |
|
1316 | + } |
|
1317 | + return true; |
|
1318 | + } |
|
1319 | + |
|
1320 | + $name = null; |
|
1321 | + $this->seek($s); |
|
1322 | + return false; |
|
1323 | + } |
|
1324 | + |
|
1325 | + /** |
|
1326 | + * Consume an assignment operator |
|
1327 | + * Can optionally take a name that will be set to the current property name |
|
1328 | + */ |
|
1329 | + protected function assign($name = null) |
|
1330 | + { |
|
1331 | + if ($name) { |
|
1332 | + $this->currentProperty = $name; |
|
1333 | + } |
|
1334 | + return $this->literal(':') || $this->literal('='); |
|
1335 | + } |
|
1336 | + |
|
1337 | + // consume a keyword |
|
1338 | + protected function keyword(&$word) |
|
1339 | + { |
|
1340 | + $m = array(); |
|
1341 | + if ($this->match('([\w_\-\*!"][\w\-_"]*)', $m)) { |
|
1342 | + $word = $m[1]; |
|
1343 | + return true; |
|
1344 | + } |
|
1345 | + return false; |
|
1346 | + } |
|
1347 | + |
|
1348 | + // consume an end of statement delimiter |
|
1349 | + protected function end() |
|
1350 | + { |
|
1351 | + if ($this->literal(';', false)) { |
|
1352 | + return true; |
|
1353 | + } elseif ($this->count == strlen($this->buffer) || $this->buffer[$this->count] == '}') { |
|
1354 | + // if there is end of file or a closing block next then we don't need a ; |
|
1355 | + return true; |
|
1356 | + } |
|
1357 | + return false; |
|
1358 | + } |
|
1359 | + |
|
1360 | + protected function guards(&$guards) |
|
1361 | + { |
|
1362 | + $g = null; |
|
1363 | + |
|
1364 | + $s = $this->seek(); |
|
1365 | + |
|
1366 | + if (!$this->literal("when")) { |
|
1367 | + $this->seek($s); |
|
1368 | + return false; |
|
1369 | + } |
|
1370 | + |
|
1371 | + $guards = array(); |
|
1372 | + |
|
1373 | + while ($this->guardGroup($g)) { |
|
1374 | + $guards[] = $g; |
|
1375 | + if (!$this->literal(",")) { |
|
1376 | + break; |
|
1377 | + } |
|
1378 | + } |
|
1379 | + |
|
1380 | + if (count($guards) == 0) { |
|
1381 | + $guards = null; |
|
1382 | + $this->seek($s); |
|
1383 | + return false; |
|
1384 | + } |
|
1385 | + |
|
1386 | + return true; |
|
1387 | + } |
|
1388 | + |
|
1389 | + // a bunch of guards that are and'd together |
|
1390 | + // TODO rename to guardGroup |
|
1391 | + protected function guardGroup(&$guardGroup) |
|
1392 | + { |
|
1393 | + $guard = null; |
|
1394 | + |
|
1395 | + $s = $this->seek(); |
|
1396 | + $guardGroup = array(); |
|
1397 | + while ($this->guard($guard)) { |
|
1398 | + $guardGroup[] = $guard; |
|
1399 | + if (!$this->literal("and")) { |
|
1400 | + break; |
|
1401 | + } |
|
1402 | + } |
|
1403 | + |
|
1404 | + if (count($guardGroup) == 0) { |
|
1405 | + $guardGroup = null; |
|
1406 | + $this->seek($s); |
|
1407 | + return false; |
|
1408 | + } |
|
1409 | + |
|
1410 | + return true; |
|
1411 | + } |
|
1412 | + |
|
1413 | + protected function guard(&$guard) |
|
1414 | + { |
|
1415 | + $exp = null; |
|
1416 | + |
|
1417 | + $s = $this->seek(); |
|
1418 | + $negate = $this->literal("not"); |
|
1419 | + |
|
1420 | + if ($this->literal("(") && $this->expression($exp) && $this->literal(")")) { |
|
1421 | + $guard = $exp; |
|
1422 | + if ($negate) { |
|
1423 | + $guard = array("negate", $guard); |
|
1424 | + } |
|
1425 | + return true; |
|
1426 | + } |
|
1427 | + |
|
1428 | + $this->seek($s); |
|
1429 | + return false; |
|
1430 | + } |
|
1431 | + |
|
1432 | + /* raw parsing functions */ |
|
1433 | + |
|
1434 | + protected function literal($what, $eatWhitespace = null) |
|
1435 | + { |
|
1436 | + if ($eatWhitespace === null) { |
|
1437 | + $eatWhitespace = $this->eatWhiteDefault; |
|
1438 | + } |
|
1439 | + |
|
1440 | + // shortcut on single letter |
|
1441 | + if (!isset($what[1]) && isset($this->buffer[$this->count])) { |
|
1442 | + if ($this->buffer[$this->count] == $what) { |
|
1443 | + if (!$eatWhitespace) { |
|
1444 | + $this->count++; |
|
1445 | + return true; |
|
1446 | + } |
|
1447 | + // goes below... |
|
1448 | + } else { |
|
1449 | + return false; |
|
1450 | + } |
|
1451 | + } |
|
1452 | + |
|
1453 | + if (!isset(self::$literalCache[$what])) { |
|
1454 | + self::$literalCache[$what] = Lessc::preg_quote($what); |
|
1455 | + } |
|
1456 | + |
|
1457 | + $m = array(); |
|
1458 | + return $this->match(self::$literalCache[$what], $m, $eatWhitespace); |
|
1459 | + } |
|
1460 | + |
|
1461 | + protected function genericList(&$out, $parseItem, $delim = "", $flatten = true) |
|
1462 | + { |
|
1463 | + $value = null; |
|
1464 | + |
|
1465 | + $s = $this->seek(); |
|
1466 | + $items = array(); |
|
1467 | + while ($this->$parseItem($value)) { |
|
1468 | + $items[] = $value; |
|
1469 | + if ($delim) { |
|
1470 | + if (!$this->literal($delim)) { |
|
1471 | + break; |
|
1472 | + } |
|
1473 | + } |
|
1474 | + } |
|
1475 | + |
|
1476 | + if (count($items) == 0) { |
|
1477 | + $this->seek($s); |
|
1478 | + return false; |
|
1479 | + } |
|
1480 | + |
|
1481 | + if ($flatten && count($items) == 1) { |
|
1482 | + $out = $items[0]; |
|
1483 | + } else { |
|
1484 | + $out = array("list", $delim, $items); |
|
1485 | + } |
|
1486 | + |
|
1487 | + return true; |
|
1488 | + } |
|
1489 | + |
|
1490 | + |
|
1491 | + // advance counter to next occurrence of $what |
|
1492 | + // $until - don't include $what in advance |
|
1493 | + // $allowNewline, if string, will be used as valid char set |
|
1494 | + protected function to($what, &$out, $until = false, $allowNewline = false) |
|
1495 | + { |
|
1496 | + if (is_string($allowNewline)) { |
|
1497 | + $validChars = $allowNewline; |
|
1498 | + } else { |
|
1499 | + $validChars = $allowNewline ? "." : "[^\n]"; |
|
1500 | + } |
|
1501 | + $m = array(); |
|
1502 | + if (!$this->match('('.$validChars.'*?)'.Lessc::preg_quote($what), $m, !$until)) { |
|
1503 | + return false; |
|
1504 | + } |
|
1505 | + if ($until) { |
|
1506 | + $this->count -= strlen($what); // give back $what |
|
1507 | + } |
|
1508 | + $out = $m[1]; |
|
1509 | + return true; |
|
1510 | + } |
|
1511 | + |
|
1512 | + // try to match something on head of buffer |
|
1513 | + protected function match($regex, &$out, $eatWhitespace = null) |
|
1514 | + { |
|
1515 | + if ($eatWhitespace === null) { |
|
1516 | + $eatWhitespace = $this->eatWhiteDefault; |
|
1517 | + } |
|
1518 | + |
|
1519 | + $r = '/'.$regex.($eatWhitespace && !$this->writeComments ? '\s*' : '').'/Ais'; |
|
1520 | + if (preg_match($r, $this->buffer, $out, 0, $this->count)) { |
|
1521 | + $this->count += strlen($out[0]); |
|
1522 | + if ($eatWhitespace && $this->writeComments) { |
|
1523 | + $this->whitespace(); |
|
1524 | + } |
|
1525 | + return true; |
|
1526 | + } |
|
1527 | + return false; |
|
1528 | + } |
|
1529 | + |
|
1530 | + // match some whitespace |
|
1531 | + protected function whitespace() |
|
1532 | + { |
|
1533 | + if ($this->writeComments) { |
|
1534 | + $gotWhite = false; |
|
1535 | + $m = array(); |
|
1536 | + while (preg_match(self::$whitePattern, $this->buffer, $m, 0, $this->count)) { |
|
1537 | + if (isset($m[1]) && empty($this->seenComments[$this->count])) { |
|
1538 | + $this->append(array("comment", $m[1])); |
|
1539 | + $this->seenComments[$this->count] = true; |
|
1540 | + } |
|
1541 | + $this->count += strlen($m[0]); |
|
1542 | + $gotWhite = true; |
|
1543 | + } |
|
1544 | + return $gotWhite; |
|
1545 | + } else { |
|
1546 | + $this->match("", $m); |
|
1547 | + return strlen($m[0]) > 0; |
|
1548 | + } |
|
1549 | + } |
|
1550 | + |
|
1551 | + // match something without consuming it |
|
1552 | + protected function peek($regex, &$out = null, $from = null) |
|
1553 | + { |
|
1554 | + if (is_null($from)) { |
|
1555 | + $from = $this->count; |
|
1556 | + } |
|
1557 | + $r = '/'.$regex.'/Ais'; |
|
1558 | + $result = preg_match($r, $this->buffer, $out, 0, $from); |
|
1559 | + |
|
1560 | + return $result; |
|
1561 | + } |
|
1562 | + |
|
1563 | + // seek to a spot in the buffer or return where we are on no argument |
|
1564 | + protected function seek($where = null) |
|
1565 | + { |
|
1566 | + if ($where === null) { |
|
1567 | + return $this->count; |
|
1568 | + } else { |
|
1569 | + $this->count = $where; |
|
1570 | + } |
|
1571 | + return true; |
|
1572 | + } |
|
1573 | + |
|
1574 | + /* misc functions */ |
|
1575 | + |
|
1576 | + public function throwError($msg = "parse error", $count = null) |
|
1577 | + { |
|
1578 | + $count = is_null($count) ? $this->count : $count; |
|
1579 | + |
|
1580 | + $line = $this->line + |
|
1581 | + substr_count(substr($this->buffer, 0, $count), "\n"); |
|
1582 | + |
|
1583 | + if (!empty($this->sourceName)) { |
|
1584 | + $loc = "$this->sourceName on line $line"; |
|
1585 | + } else { |
|
1586 | + $loc = "line: $line"; |
|
1587 | + } |
|
1588 | + |
|
1589 | + // TODO this depends on $this->count |
|
1590 | + $m = array(); |
|
1591 | + if ($this->peek("(.*?)(\n|$)", $m, $count)) { |
|
1592 | + throw new exception("$msg: failed at `$m[1]` $loc"); |
|
1593 | + } else { |
|
1594 | + throw new exception("$msg: $loc"); |
|
1595 | + } |
|
1596 | + } |
|
1597 | + |
|
1598 | + protected function pushBlock($selectors = null, $type = null) |
|
1599 | + { |
|
1600 | + $b = new stdclass(); |
|
1601 | + $b->parent = $this->env; |
|
1602 | + |
|
1603 | + $b->type = $type; |
|
1604 | + $b->id = self::$nextBlockId++; |
|
1605 | + |
|
1606 | + $b->isVararg = false; // TODO: kill me from here |
|
1607 | + $b->tags = $selectors; |
|
1608 | + |
|
1609 | + $b->props = array(); |
|
1610 | + $b->children = array(); |
|
1611 | + |
|
1612 | + $this->env = $b; |
|
1613 | + return $b; |
|
1614 | + } |
|
1615 | + |
|
1616 | + // push a block that doesn't multiply tags |
|
1617 | + protected function pushSpecialBlock($type) |
|
1618 | + { |
|
1619 | + return $this->pushBlock(null, $type); |
|
1620 | + } |
|
1621 | + |
|
1622 | + // append a property to the current block |
|
1623 | + protected function append($prop, $pos = null) |
|
1624 | + { |
|
1625 | + if ($pos !== null) { |
|
1626 | + $prop[-1] = $pos; |
|
1627 | + } |
|
1628 | + $this->env->props[] = $prop; |
|
1629 | + } |
|
1630 | + |
|
1631 | + // pop something off the stack |
|
1632 | + protected function pop() |
|
1633 | + { |
|
1634 | + $old = $this->env; |
|
1635 | + $this->env = $this->env->parent; |
|
1636 | + return $old; |
|
1637 | + } |
|
1638 | + |
|
1639 | + // remove comments from $text |
|
1640 | + // todo: make it work for all functions, not just url |
|
1641 | + protected function removeComments($text) |
|
1642 | + { |
|
1643 | + $look = array( |
|
1644 | + 'url(', '//', '/*', '"', "'" |
|
1645 | + ); |
|
1646 | + |
|
1647 | + $out = ''; |
|
1648 | + $min = null; |
|
1649 | + while (true) { |
|
1650 | + // find the next item |
|
1651 | + foreach ($look as $token) { |
|
1652 | + $pos = strpos($text, $token); |
|
1653 | + if ($pos !== false) { |
|
1654 | + if (!isset($min) || $pos < $min[1]) { |
|
1655 | + $min = array($token, $pos); |
|
1656 | + } |
|
1657 | + } |
|
1658 | + } |
|
1659 | + |
|
1660 | + if (is_null($min)) { |
|
1661 | + break; |
|
1662 | + } |
|
1663 | + |
|
1664 | + $count = $min[1]; |
|
1665 | + $skip = 0; |
|
1666 | + $newlines = 0; |
|
1667 | + switch ($min[0]) { |
|
1668 | + case 'url(': |
|
1669 | + $m = array(); |
|
1670 | + if (preg_match('/url\(.*?\)/', $text, $m, 0, $count)) { |
|
1671 | + $count += strlen($m[0]) - strlen($min[0]); |
|
1672 | + } |
|
1673 | + break; |
|
1674 | + case '"': |
|
1675 | + case "'": |
|
1676 | + $m = array(); |
|
1677 | + if (preg_match('/'.$min[0].'.*?(?<!\\\\)'.$min[0].'/', $text, $m, 0, $count)) { |
|
1678 | + $count += strlen($m[0]) - 1; |
|
1679 | + } |
|
1680 | + break; |
|
1681 | + case '//': |
|
1682 | + $skip = strpos($text, "\n", $count); |
|
1683 | + if ($skip === false) { |
|
1684 | + $skip = strlen($text) - $count; |
|
1685 | + } else { |
|
1686 | + $skip -= $count; |
|
1687 | + } |
|
1688 | + break; |
|
1689 | + case '/*': |
|
1690 | + $m = array(); |
|
1691 | + if (preg_match('/\/\*.*?\*\//s', $text, $m, 0, $count)) { |
|
1692 | + $skip = strlen($m[0]); |
|
1693 | + $newlines = substr_count($m[0], "\n"); |
|
1694 | + } |
|
1695 | + break; |
|
1696 | + } |
|
1697 | + |
|
1698 | + if ($skip == 0) { |
|
1699 | + $count += strlen($min[0]); |
|
1700 | + } |
|
1701 | + |
|
1702 | + $out .= substr($text, 0, $count).str_repeat("\n", $newlines); |
|
1703 | + $text = substr($text, $count + $skip); |
|
1704 | + |
|
1705 | + $min = null; |
|
1706 | + } |
|
1707 | + |
|
1708 | + return $out.$text; |
|
1709 | + } |
|
1710 | 1710 | } |
@@ -59,107 +59,107 @@ |
||
59 | 59 | |
60 | 60 | class lessc_formatter_classic |
61 | 61 | { |
62 | - public $indentChar = " "; |
|
63 | - |
|
64 | - public $break = "\n"; |
|
65 | - public $open = " {"; |
|
66 | - public $close = "}"; |
|
67 | - public $selectorSeparator = ", "; |
|
68 | - public $assignSeparator = ":"; |
|
69 | - |
|
70 | - public $openSingle = " { "; |
|
71 | - public $closeSingle = " }"; |
|
72 | - |
|
73 | - public $disableSingle = false; |
|
74 | - public $breakSelectors = false; |
|
75 | - |
|
76 | - public $compressColors = false; |
|
77 | - public $indentLevel; |
|
78 | - |
|
79 | - public function __construct() |
|
80 | - { |
|
81 | - $this->indentLevel = 0; |
|
82 | - } |
|
83 | - |
|
84 | - public function indentStr($n = 0) |
|
85 | - { |
|
86 | - return str_repeat($this->indentChar, max($this->indentLevel + $n, 0)); |
|
87 | - } |
|
88 | - |
|
89 | - public function property($name, $value) |
|
90 | - { |
|
91 | - return $name.$this->assignSeparator.$value.";"; |
|
92 | - } |
|
93 | - |
|
94 | - protected function isEmpty($block) |
|
95 | - { |
|
96 | - if (empty($block->lines)) { |
|
97 | - foreach ($block->children as $child) { |
|
98 | - if (!$this->isEmpty($child)) { |
|
99 | - return false; |
|
100 | - } |
|
101 | - } |
|
102 | - |
|
103 | - return true; |
|
104 | - } |
|
105 | - return false; |
|
106 | - } |
|
107 | - |
|
108 | - public function block($block) |
|
109 | - { |
|
110 | - if ($this->isEmpty($block)) { |
|
111 | - return; |
|
112 | - } |
|
113 | - |
|
114 | - $inner = $pre = $this->indentStr(); |
|
115 | - |
|
116 | - $isSingle = !$this->disableSingle && |
|
117 | - is_null($block->type) && count($block->lines) == 1; |
|
118 | - |
|
119 | - if (!empty($block->selectors)) { |
|
120 | - $this->indentLevel++; |
|
121 | - |
|
122 | - if ($this->breakSelectors) { |
|
123 | - $selectorSeparator = $this->selectorSeparator.$this->break.$pre; |
|
124 | - } else { |
|
125 | - $selectorSeparator = $this->selectorSeparator; |
|
126 | - } |
|
127 | - |
|
128 | - echo $pre. |
|
129 | - implode($selectorSeparator, $block->selectors); |
|
130 | - if ($isSingle) { |
|
131 | - echo $this->openSingle; |
|
132 | - $inner = ""; |
|
133 | - } else { |
|
134 | - echo $this->open.$this->break; |
|
135 | - $inner = $this->indentStr(); |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - if (!empty($block->lines)) { |
|
140 | - $glue = $this->break.$inner; |
|
141 | - echo $inner.implode($glue, $block->lines); |
|
142 | - if (!$isSingle && !empty($block->children)) { |
|
143 | - echo $this->break; |
|
144 | - } |
|
145 | - } |
|
146 | - |
|
147 | - foreach ($block->children as $child) { |
|
148 | - $this->block($child); |
|
149 | - } |
|
150 | - |
|
151 | - if (!empty($block->selectors)) { |
|
152 | - if (!$isSingle && empty($block->children)) { |
|
153 | - echo $this->break; |
|
154 | - } |
|
155 | - |
|
156 | - if ($isSingle) { |
|
157 | - echo $this->closeSingle.$this->break; |
|
158 | - } else { |
|
159 | - echo $pre.$this->close.$this->break; |
|
160 | - } |
|
161 | - |
|
162 | - $this->indentLevel--; |
|
163 | - } |
|
164 | - } |
|
62 | + public $indentChar = " "; |
|
63 | + |
|
64 | + public $break = "\n"; |
|
65 | + public $open = " {"; |
|
66 | + public $close = "}"; |
|
67 | + public $selectorSeparator = ", "; |
|
68 | + public $assignSeparator = ":"; |
|
69 | + |
|
70 | + public $openSingle = " { "; |
|
71 | + public $closeSingle = " }"; |
|
72 | + |
|
73 | + public $disableSingle = false; |
|
74 | + public $breakSelectors = false; |
|
75 | + |
|
76 | + public $compressColors = false; |
|
77 | + public $indentLevel; |
|
78 | + |
|
79 | + public function __construct() |
|
80 | + { |
|
81 | + $this->indentLevel = 0; |
|
82 | + } |
|
83 | + |
|
84 | + public function indentStr($n = 0) |
|
85 | + { |
|
86 | + return str_repeat($this->indentChar, max($this->indentLevel + $n, 0)); |
|
87 | + } |
|
88 | + |
|
89 | + public function property($name, $value) |
|
90 | + { |
|
91 | + return $name.$this->assignSeparator.$value.";"; |
|
92 | + } |
|
93 | + |
|
94 | + protected function isEmpty($block) |
|
95 | + { |
|
96 | + if (empty($block->lines)) { |
|
97 | + foreach ($block->children as $child) { |
|
98 | + if (!$this->isEmpty($child)) { |
|
99 | + return false; |
|
100 | + } |
|
101 | + } |
|
102 | + |
|
103 | + return true; |
|
104 | + } |
|
105 | + return false; |
|
106 | + } |
|
107 | + |
|
108 | + public function block($block) |
|
109 | + { |
|
110 | + if ($this->isEmpty($block)) { |
|
111 | + return; |
|
112 | + } |
|
113 | + |
|
114 | + $inner = $pre = $this->indentStr(); |
|
115 | + |
|
116 | + $isSingle = !$this->disableSingle && |
|
117 | + is_null($block->type) && count($block->lines) == 1; |
|
118 | + |
|
119 | + if (!empty($block->selectors)) { |
|
120 | + $this->indentLevel++; |
|
121 | + |
|
122 | + if ($this->breakSelectors) { |
|
123 | + $selectorSeparator = $this->selectorSeparator.$this->break.$pre; |
|
124 | + } else { |
|
125 | + $selectorSeparator = $this->selectorSeparator; |
|
126 | + } |
|
127 | + |
|
128 | + echo $pre. |
|
129 | + implode($selectorSeparator, $block->selectors); |
|
130 | + if ($isSingle) { |
|
131 | + echo $this->openSingle; |
|
132 | + $inner = ""; |
|
133 | + } else { |
|
134 | + echo $this->open.$this->break; |
|
135 | + $inner = $this->indentStr(); |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + if (!empty($block->lines)) { |
|
140 | + $glue = $this->break.$inner; |
|
141 | + echo $inner.implode($glue, $block->lines); |
|
142 | + if (!$isSingle && !empty($block->children)) { |
|
143 | + echo $this->break; |
|
144 | + } |
|
145 | + } |
|
146 | + |
|
147 | + foreach ($block->children as $child) { |
|
148 | + $this->block($child); |
|
149 | + } |
|
150 | + |
|
151 | + if (!empty($block->selectors)) { |
|
152 | + if (!$isSingle && empty($block->children)) { |
|
153 | + echo $this->break; |
|
154 | + } |
|
155 | + |
|
156 | + if ($isSingle) { |
|
157 | + echo $this->closeSingle.$this->break; |
|
158 | + } else { |
|
159 | + echo $pre.$this->close.$this->break; |
|
160 | + } |
|
161 | + |
|
162 | + $this->indentLevel--; |
|
163 | + } |
|
164 | + } |
|
165 | 165 | } |
@@ -59,2570 +59,2570 @@ |
||
59 | 59 | class Lessc |
60 | 60 | { |
61 | 61 | |
62 | - public static $VERSION = "v0.8.0"; |
|
63 | - |
|
64 | - public static $TRUE = array("keyword", "true"); |
|
65 | - public static $FALSE = array("keyword", "false"); |
|
66 | - |
|
67 | - protected $libFunctions = array(); |
|
68 | - protected $registeredVars = array(); |
|
69 | - protected $preserveComments = false; |
|
70 | - |
|
71 | - public $vPrefix = '@'; // prefix of abstract properties |
|
72 | - public $mPrefix = '$'; // prefix of abstract blocks |
|
73 | - public $parentSelector = '&'; |
|
74 | - |
|
75 | - public $importDisabled = false; |
|
76 | - public $importDir = ''; |
|
77 | - |
|
78 | - public $scope; |
|
79 | - public $formatter; |
|
80 | - public $formatterName; |
|
81 | - public $parser; |
|
82 | - public $_parseFile; |
|
83 | - public $env; |
|
84 | - public $count; |
|
85 | - |
|
86 | - protected $numberPrecision = null; |
|
87 | - |
|
88 | - protected $allParsedFiles = array(); |
|
89 | - |
|
90 | - // set to the parser that generated the current line when compiling |
|
91 | - // so we know how to create error messages |
|
92 | - protected $sourceParser = null; |
|
93 | - protected $sourceLoc = null; |
|
94 | - |
|
95 | - protected static $nextImportId = 0; // uniquely identify imports |
|
96 | - |
|
97 | - // attempts to find the path of an import url, returns null for css files |
|
98 | - protected function findImport($url) |
|
99 | - { |
|
100 | - foreach ((array) $this->importDir as $dir) { |
|
101 | - $full = $dir.(substr($dir, -1) != '/' ? '/' : '').$url; |
|
102 | - if ($this->fileExists($file = $full.'.less') || $this->fileExists($file = $full)) { |
|
103 | - return $file; |
|
104 | - } |
|
105 | - } |
|
106 | - |
|
107 | - return null; |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * fileExists |
|
112 | - * |
|
113 | - * @param string $name Filename |
|
114 | - * @return boolean |
|
115 | - */ |
|
116 | - protected function fileExists($name) |
|
117 | - { |
|
118 | - return is_file($name); |
|
119 | - } |
|
120 | - |
|
121 | - public static function compressList($items, $delim) |
|
122 | - { |
|
123 | - if (!isset($items[1]) && isset($items[0])) { |
|
124 | - return $items[0]; |
|
125 | - } else { |
|
126 | - return array('list', $delim, $items); |
|
127 | - } |
|
128 | - } |
|
129 | - |
|
130 | - public static function preg_quote($what) |
|
131 | - { |
|
132 | - return preg_quote($what, '/'); |
|
133 | - } |
|
134 | - |
|
135 | - protected function tryImport($importPath, $parentBlock, $out) |
|
136 | - { |
|
137 | - if ($importPath[0] == "function" && $importPath[1] == "url") { |
|
138 | - $importPath = $this->flattenList($importPath[2]); |
|
139 | - } |
|
140 | - |
|
141 | - $str = $this->coerceString($importPath); |
|
142 | - if ($str === null) { |
|
143 | - return false; |
|
144 | - } |
|
145 | - |
|
146 | - $url = $this->compileValue($this->lib_e($str)); |
|
147 | - |
|
148 | - // don't import if it ends in css |
|
149 | - if (substr_compare($url, '.css', -4, 4) === 0) { |
|
150 | - return false; |
|
151 | - } |
|
152 | - |
|
153 | - $realPath = $this->findImport($url); |
|
154 | - |
|
155 | - if ($realPath === null) { |
|
156 | - return false; |
|
157 | - } |
|
158 | - |
|
159 | - if ($this->importDisabled) { |
|
160 | - return array(false, "/* import disabled */"); |
|
161 | - } |
|
162 | - |
|
163 | - if (isset($this->allParsedFiles[realpath($realPath)])) { |
|
164 | - return array(false, null); |
|
165 | - } |
|
166 | - |
|
167 | - $this->addParsedFile($realPath); |
|
168 | - $parser = $this->makeParser($realPath); |
|
169 | - $root = $parser->parse(file_get_contents($realPath)); |
|
170 | - |
|
171 | - // set the parents of all the block props |
|
172 | - foreach ($root->props as $prop) { |
|
173 | - if ($prop[0] == "block") { |
|
174 | - $prop[1]->parent = $parentBlock; |
|
175 | - } |
|
176 | - } |
|
177 | - |
|
178 | - // copy mixins into scope, set their parents |
|
179 | - // bring blocks from import into current block |
|
180 | - // TODO: need to mark the source parser these came from this file |
|
181 | - foreach ($root->children as $childName => $child) { |
|
182 | - if (isset($parentBlock->children[$childName])) { |
|
183 | - $parentBlock->children[$childName] = array_merge( |
|
184 | - $parentBlock->children[$childName], |
|
185 | - $child |
|
186 | - ); |
|
187 | - } else { |
|
188 | - $parentBlock->children[$childName] = $child; |
|
189 | - } |
|
190 | - } |
|
191 | - |
|
192 | - $pi = pathinfo($realPath); |
|
193 | - $dir = $pi["dirname"]; |
|
194 | - |
|
195 | - list($top, $bottom) = $this->sortProps($root->props, true); |
|
196 | - $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir); |
|
197 | - |
|
198 | - return array(true, $bottom, $parser, $dir); |
|
199 | - } |
|
200 | - |
|
201 | - protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir) |
|
202 | - { |
|
203 | - $oldSourceParser = $this->sourceParser; |
|
204 | - |
|
205 | - $oldImport = $this->importDir; |
|
206 | - |
|
207 | - // TODO: this is because the importDir api is stupid |
|
208 | - $this->importDir = (array) $this->importDir; |
|
209 | - array_unshift($this->importDir, $importDir); |
|
210 | - |
|
211 | - foreach ($props as $prop) { |
|
212 | - $this->compileProp($prop, $block, $out); |
|
213 | - } |
|
214 | - |
|
215 | - $this->importDir = $oldImport; |
|
216 | - $this->sourceParser = $oldSourceParser; |
|
217 | - } |
|
218 | - |
|
219 | - /** |
|
220 | - * Recursively compiles a block. |
|
221 | - * |
|
222 | - * A block is analogous to a CSS block in most cases. A single LESS document |
|
223 | - * is encapsulated in a block when parsed, but it does not have parent tags |
|
224 | - * so all of it's children appear on the root level when compiled. |
|
225 | - * |
|
226 | - * Blocks are made up of props and children. |
|
227 | - * |
|
228 | - * Props are property instructions, array tuples which describe an action |
|
229 | - * to be taken, eg. write a property, set a variable, mixin a block. |
|
230 | - * |
|
231 | - * The children of a block are just all the blocks that are defined within. |
|
232 | - * This is used to look up mixins when performing a mixin. |
|
233 | - * |
|
234 | - * Compiling the block involves pushing a fresh environment on the stack, |
|
235 | - * and iterating through the props, compiling each one. |
|
236 | - * |
|
237 | - * See Lessc::compileProp() |
|
238 | - * |
|
239 | - */ |
|
240 | - protected function compileBlock($block) |
|
241 | - { |
|
242 | - switch ($block->type) { |
|
243 | - case "root": |
|
244 | - $this->compileRoot($block); |
|
245 | - break; |
|
246 | - case null: |
|
247 | - $this->compileCSSBlock($block); |
|
248 | - break; |
|
249 | - case "media": |
|
250 | - $this->compileMedia($block); |
|
251 | - break; |
|
252 | - case "directive": |
|
253 | - $name = "@".$block->name; |
|
254 | - if (!empty($block->value)) { |
|
255 | - $name .= " ".$this->compileValue($this->reduce($block->value)); |
|
256 | - } |
|
257 | - |
|
258 | - $this->compileNestedBlock($block, array($name)); |
|
259 | - break; |
|
260 | - default: |
|
261 | - $this->throwError("unknown block type: $block->type\n"); |
|
262 | - } |
|
263 | - } |
|
264 | - |
|
265 | - protected function compileCSSBlock($block) |
|
266 | - { |
|
267 | - $env = $this->pushEnv(); |
|
268 | - |
|
269 | - $selectors = $this->compileSelectors($block->tags); |
|
270 | - $env->selectors = $this->multiplySelectors($selectors); |
|
271 | - $out = $this->makeOutputBlock(null, $env->selectors); |
|
272 | - |
|
273 | - $this->scope->children[] = $out; |
|
274 | - $this->compileProps($block, $out); |
|
275 | - |
|
276 | - $block->scope = $env; // mixins carry scope with them! |
|
277 | - $this->popEnv(); |
|
278 | - } |
|
279 | - |
|
280 | - protected function compileMedia($media) |
|
281 | - { |
|
282 | - $env = $this->pushEnv($media); |
|
283 | - $parentScope = $this->mediaParent($this->scope); |
|
284 | - |
|
285 | - $query = $this->compileMediaQuery($this->multiplyMedia($env)); |
|
286 | - |
|
287 | - $this->scope = $this->makeOutputBlock($media->type, array($query)); |
|
288 | - $parentScope->children[] = $this->scope; |
|
289 | - |
|
290 | - $this->compileProps($media, $this->scope); |
|
291 | - |
|
292 | - if (count($this->scope->lines) > 0) { |
|
293 | - $orphanSelelectors = $this->findClosestSelectors(); |
|
294 | - if (!is_null($orphanSelelectors)) { |
|
295 | - $orphan = $this->makeOutputBlock(null, $orphanSelelectors); |
|
296 | - $orphan->lines = $this->scope->lines; |
|
297 | - array_unshift($this->scope->children, $orphan); |
|
298 | - $this->scope->lines = array(); |
|
299 | - } |
|
300 | - } |
|
301 | - |
|
302 | - $this->scope = $this->scope->parent; |
|
303 | - $this->popEnv(); |
|
304 | - } |
|
305 | - |
|
306 | - protected function mediaParent($scope) |
|
307 | - { |
|
308 | - while (!empty($scope->parent)) { |
|
309 | - if (!empty($scope->type) && $scope->type != "media") { |
|
310 | - break; |
|
311 | - } |
|
312 | - $scope = $scope->parent; |
|
313 | - } |
|
314 | - |
|
315 | - return $scope; |
|
316 | - } |
|
317 | - |
|
318 | - protected function compileNestedBlock($block, $selectors) |
|
319 | - { |
|
320 | - $this->pushEnv($block); |
|
321 | - $this->scope = $this->makeOutputBlock($block->type, $selectors); |
|
322 | - $this->scope->parent->children[] = $this->scope; |
|
323 | - |
|
324 | - $this->compileProps($block, $this->scope); |
|
325 | - |
|
326 | - $this->scope = $this->scope->parent; |
|
327 | - $this->popEnv(); |
|
328 | - } |
|
329 | - |
|
330 | - protected function compileRoot($root) |
|
331 | - { |
|
332 | - $this->pushEnv(); |
|
333 | - $this->scope = $this->makeOutputBlock($root->type); |
|
334 | - $this->compileProps($root, $this->scope); |
|
335 | - $this->popEnv(); |
|
336 | - } |
|
337 | - |
|
338 | - protected function compileProps($block, $out) |
|
339 | - { |
|
340 | - foreach ($this->sortProps($block->props) as $prop) { |
|
341 | - $this->compileProp($prop, $block, $out); |
|
342 | - } |
|
343 | - $out->lines = $this->deduplicate($out->lines); |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * Deduplicate lines in a block. Comments are not deduplicated. If a |
|
348 | - * duplicate rule is detected, the comments immediately preceding each |
|
349 | - * occurence are consolidated. |
|
350 | - */ |
|
351 | - protected function deduplicate($lines) |
|
352 | - { |
|
353 | - $unique = array(); |
|
354 | - $comments = array(); |
|
355 | - |
|
356 | - foreach ($lines as $line) { |
|
357 | - if (strpos($line, '/*') === 0) { |
|
358 | - $comments[] = $line; |
|
359 | - continue; |
|
360 | - } |
|
361 | - if (!in_array($line, $unique)) { |
|
362 | - $unique[] = $line; |
|
363 | - } |
|
364 | - array_splice($unique, array_search($line, $unique), 0, $comments); |
|
365 | - $comments = array(); |
|
366 | - } |
|
367 | - return array_merge($unique, $comments); |
|
368 | - } |
|
369 | - |
|
370 | - protected function sortProps($props, $split = false) |
|
371 | - { |
|
372 | - $vars = array(); |
|
373 | - $imports = array(); |
|
374 | - $other = array(); |
|
375 | - $stack = array(); |
|
376 | - |
|
377 | - foreach ($props as $prop) { |
|
378 | - switch ($prop[0]) { |
|
379 | - case "comment": |
|
380 | - $stack[] = $prop; |
|
381 | - break; |
|
382 | - case "assign": |
|
383 | - $stack[] = $prop; |
|
384 | - if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) { |
|
385 | - $vars = array_merge($vars, $stack); |
|
386 | - } else { |
|
387 | - $other = array_merge($other, $stack); |
|
388 | - } |
|
389 | - $stack = array(); |
|
390 | - break; |
|
391 | - case "import": |
|
392 | - $id = self::$nextImportId++; |
|
393 | - $prop[] = $id; |
|
394 | - $stack[] = $prop; |
|
395 | - $imports = array_merge($imports, $stack); |
|
396 | - $other[] = array("import_mixin", $id); |
|
397 | - $stack = array(); |
|
398 | - break; |
|
399 | - default: |
|
400 | - $stack[] = $prop; |
|
401 | - $other = array_merge($other, $stack); |
|
402 | - $stack = array(); |
|
403 | - break; |
|
404 | - } |
|
405 | - } |
|
406 | - $other = array_merge($other, $stack); |
|
407 | - |
|
408 | - if ($split) { |
|
409 | - return array(array_merge($imports, $vars), $other); |
|
410 | - } else { |
|
411 | - return array_merge($imports, $vars, $other); |
|
412 | - } |
|
413 | - } |
|
414 | - |
|
415 | - protected function compileMediaQuery($queries) |
|
416 | - { |
|
417 | - $compiledQueries = array(); |
|
418 | - foreach ($queries as $query) { |
|
419 | - $parts = array(); |
|
420 | - foreach ($query as $q) { |
|
421 | - switch ($q[0]) { |
|
422 | - case "mediaType": |
|
423 | - $parts[] = implode(" ", array_slice($q, 1)); |
|
424 | - break; |
|
425 | - case "mediaExp": |
|
426 | - if (isset($q[2])) { |
|
427 | - $parts[] = "($q[1]: ". |
|
428 | - $this->compileValue($this->reduce($q[2])).")"; |
|
429 | - } else { |
|
430 | - $parts[] = "($q[1])"; |
|
431 | - } |
|
432 | - break; |
|
433 | - case "variable": |
|
434 | - $parts[] = $this->compileValue($this->reduce($q)); |
|
435 | - break; |
|
436 | - } |
|
437 | - } |
|
438 | - |
|
439 | - if (count($parts) > 0) { |
|
440 | - $compiledQueries[] = implode(" and ", $parts); |
|
441 | - } |
|
442 | - } |
|
443 | - |
|
444 | - $out = "@media"; |
|
445 | - if (!empty($parts)) { |
|
446 | - $out .= " ". |
|
447 | - implode($this->formatter->selectorSeparator, $compiledQueries); |
|
448 | - } |
|
449 | - return $out; |
|
450 | - } |
|
451 | - |
|
452 | - protected function multiplyMedia($env, $childQueries = null) |
|
453 | - { |
|
454 | - if (is_null($env) || |
|
455 | - !empty($env->block->type) && $env->block->type != "media" |
|
456 | - ) { |
|
457 | - return $childQueries; |
|
458 | - } |
|
459 | - |
|
460 | - // plain old block, skip |
|
461 | - if (empty($env->block->type)) { |
|
462 | - return $this->multiplyMedia($env->parent, $childQueries); |
|
463 | - } |
|
464 | - |
|
465 | - $out = array(); |
|
466 | - $queries = $env->block->queries; |
|
467 | - if (is_null($childQueries)) { |
|
468 | - $out = $queries; |
|
469 | - } else { |
|
470 | - foreach ($queries as $parent) { |
|
471 | - foreach ($childQueries as $child) { |
|
472 | - $out[] = array_merge($parent, $child); |
|
473 | - } |
|
474 | - } |
|
475 | - } |
|
476 | - |
|
477 | - return $this->multiplyMedia($env->parent, $out); |
|
478 | - } |
|
479 | - |
|
480 | - protected function expandParentSelectors(&$tag, $replace) |
|
481 | - { |
|
482 | - $parts = explode("$&$", $tag); |
|
483 | - $count = 0; |
|
484 | - foreach ($parts as &$part) { |
|
485 | - $c = 0; |
|
486 | - $part = str_replace($this->parentSelector, $replace, $part, $c); |
|
487 | - $count += $c; |
|
488 | - } |
|
489 | - $tag = implode($this->parentSelector, $parts); |
|
490 | - return $count; |
|
491 | - } |
|
492 | - |
|
493 | - protected function findClosestSelectors() |
|
494 | - { |
|
495 | - $env = $this->env; |
|
496 | - $selectors = null; |
|
497 | - while ($env !== null) { |
|
498 | - if (isset($env->selectors)) { |
|
499 | - $selectors = $env->selectors; |
|
500 | - break; |
|
501 | - } |
|
502 | - $env = $env->parent; |
|
503 | - } |
|
504 | - |
|
505 | - return $selectors; |
|
506 | - } |
|
507 | - |
|
508 | - |
|
509 | - // multiply $selectors against the nearest selectors in env |
|
510 | - protected function multiplySelectors($selectors) |
|
511 | - { |
|
512 | - // find parent selectors |
|
513 | - |
|
514 | - $parentSelectors = $this->findClosestSelectors(); |
|
515 | - if (is_null($parentSelectors)) { |
|
516 | - // kill parent reference in top level selector |
|
517 | - foreach ($selectors as &$s) { |
|
518 | - $this->expandParentSelectors($s, ""); |
|
519 | - } |
|
520 | - |
|
521 | - return $selectors; |
|
522 | - } |
|
523 | - |
|
524 | - $out = array(); |
|
525 | - foreach ($parentSelectors as $parent) { |
|
526 | - foreach ($selectors as $child) { |
|
527 | - $count = $this->expandParentSelectors($child, $parent); |
|
528 | - |
|
529 | - // don't prepend the parent tag if & was used |
|
530 | - if ($count > 0) { |
|
531 | - $out[] = trim($child); |
|
532 | - } else { |
|
533 | - $out[] = trim($parent.' '.$child); |
|
534 | - } |
|
535 | - } |
|
536 | - } |
|
537 | - |
|
538 | - return $out; |
|
539 | - } |
|
540 | - |
|
541 | - // reduces selector expressions |
|
542 | - protected function compileSelectors($selectors) |
|
543 | - { |
|
544 | - $out = array(); |
|
545 | - |
|
546 | - foreach ($selectors as $s) { |
|
547 | - if (is_array($s)) { |
|
548 | - list(, $value) = $s; |
|
549 | - $out[] = trim($this->compileValue($this->reduce($value))); |
|
550 | - } else { |
|
551 | - $out[] = $s; |
|
552 | - } |
|
553 | - } |
|
554 | - |
|
555 | - return $out; |
|
556 | - } |
|
557 | - |
|
558 | - protected function eq($left, $right) |
|
559 | - { |
|
560 | - return $left == $right; |
|
561 | - } |
|
562 | - |
|
563 | - protected function patternMatch($block, $orderedArgs, $keywordArgs) |
|
564 | - { |
|
565 | - // match the guards if it has them |
|
566 | - // any one of the groups must have all its guards pass for a match |
|
567 | - if (!empty($block->guards)) { |
|
568 | - $groupPassed = false; |
|
569 | - foreach ($block->guards as $guardGroup) { |
|
570 | - foreach ($guardGroup as $guard) { |
|
571 | - $this->pushEnv(); |
|
572 | - $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs); |
|
573 | - |
|
574 | - $negate = false; |
|
575 | - if ($guard[0] == "negate") { |
|
576 | - $guard = $guard[1]; |
|
577 | - $negate = true; |
|
578 | - } |
|
579 | - |
|
580 | - $passed = $this->reduce($guard) == self::$TRUE; |
|
581 | - if ($negate) { |
|
582 | - $passed = !$passed; |
|
583 | - } |
|
584 | - |
|
585 | - $this->popEnv(); |
|
586 | - |
|
587 | - if ($passed) { |
|
588 | - $groupPassed = true; |
|
589 | - } else { |
|
590 | - $groupPassed = false; |
|
591 | - break; |
|
592 | - } |
|
593 | - } |
|
594 | - |
|
595 | - if ($groupPassed) { |
|
596 | - break; |
|
597 | - } |
|
598 | - } |
|
599 | - |
|
600 | - if (!$groupPassed) { |
|
601 | - return false; |
|
602 | - } |
|
603 | - } |
|
604 | - |
|
605 | - if (empty($block->args)) { |
|
606 | - return $block->isVararg || empty($orderedArgs) && empty($keywordArgs); |
|
607 | - } |
|
608 | - |
|
609 | - $remainingArgs = $block->args; |
|
610 | - if ($keywordArgs) { |
|
611 | - $remainingArgs = array(); |
|
612 | - foreach ($block->args as $arg) { |
|
613 | - if ($arg[0] == "arg" && isset($keywordArgs[$arg[1]])) { |
|
614 | - continue; |
|
615 | - } |
|
616 | - |
|
617 | - $remainingArgs[] = $arg; |
|
618 | - } |
|
619 | - } |
|
620 | - |
|
621 | - $i = -1; // no args |
|
622 | - // try to match by arity or by argument literal |
|
623 | - foreach ($remainingArgs as $i => $arg) { |
|
624 | - switch ($arg[0]) { |
|
625 | - case "lit": |
|
626 | - if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) { |
|
627 | - return false; |
|
628 | - } |
|
629 | - break; |
|
630 | - case "arg": |
|
631 | - // no arg and no default value |
|
632 | - if (!isset($orderedArgs[$i]) && !isset($arg[2])) { |
|
633 | - return false; |
|
634 | - } |
|
635 | - break; |
|
636 | - case "rest": |
|
637 | - $i--; // rest can be empty |
|
638 | - break 2; |
|
639 | - } |
|
640 | - } |
|
641 | - |
|
642 | - if ($block->isVararg) { |
|
643 | - return true; // not having enough is handled above |
|
644 | - } else { |
|
645 | - $numMatched = $i + 1; |
|
646 | - // greater than because default values always match |
|
647 | - return $numMatched >= count($orderedArgs); |
|
648 | - } |
|
649 | - } |
|
650 | - |
|
651 | - protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array()) |
|
652 | - { |
|
653 | - $matches = null; |
|
654 | - foreach ($blocks as $block) { |
|
655 | - // skip seen blocks that don't have arguments |
|
656 | - if (isset($skip[$block->id]) && !isset($block->args)) { |
|
657 | - continue; |
|
658 | - } |
|
659 | - |
|
660 | - if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) { |
|
661 | - $matches[] = $block; |
|
662 | - } |
|
663 | - } |
|
664 | - |
|
665 | - return $matches; |
|
666 | - } |
|
667 | - |
|
668 | - // attempt to find blocks matched by path and args |
|
669 | - protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array()) |
|
670 | - { |
|
671 | - if ($searchIn == null) { |
|
672 | - return null; |
|
673 | - } |
|
674 | - if (isset($seen[$searchIn->id])) { |
|
675 | - return null; |
|
676 | - } |
|
677 | - $seen[$searchIn->id] = true; |
|
678 | - |
|
679 | - $name = $path[0]; |
|
680 | - |
|
681 | - if (isset($searchIn->children[$name])) { |
|
682 | - $blocks = $searchIn->children[$name]; |
|
683 | - if (count($path) == 1) { |
|
684 | - $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen); |
|
685 | - if (!empty($matches)) { |
|
686 | - // This will return all blocks that match in the closest |
|
687 | - // scope that has any matching block, like lessjs |
|
688 | - return $matches; |
|
689 | - } |
|
690 | - } else { |
|
691 | - $matches = array(); |
|
692 | - foreach ($blocks as $subBlock) { |
|
693 | - $subMatches = $this->findBlocks( |
|
694 | - $subBlock, |
|
695 | - array_slice($path, 1), |
|
696 | - $orderedArgs, |
|
697 | - $keywordArgs, |
|
698 | - $seen |
|
699 | - ); |
|
700 | - |
|
701 | - if (!is_null($subMatches)) { |
|
702 | - foreach ($subMatches as $sm) { |
|
703 | - $matches[] = $sm; |
|
704 | - } |
|
705 | - } |
|
706 | - } |
|
707 | - |
|
708 | - return count($matches) > 0 ? $matches : null; |
|
709 | - } |
|
710 | - } |
|
711 | - if ($searchIn->parent === $searchIn) { |
|
712 | - return null; |
|
713 | - } |
|
714 | - return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen); |
|
715 | - } |
|
716 | - |
|
717 | - // sets all argument names in $args to either the default value |
|
718 | - // or the one passed in through $values |
|
719 | - protected function zipSetArgs($args, $orderedValues, $keywordValues) |
|
720 | - { |
|
721 | - $assignedValues = array(); |
|
722 | - |
|
723 | - $i = 0; |
|
724 | - foreach ($args as $a) { |
|
725 | - if ($a[0] == "arg") { |
|
726 | - if (isset($keywordValues[$a[1]])) { |
|
727 | - // has keyword arg |
|
728 | - $value = $keywordValues[$a[1]]; |
|
729 | - } elseif (isset($orderedValues[$i])) { |
|
730 | - // has ordered arg |
|
731 | - $value = $orderedValues[$i]; |
|
732 | - $i++; |
|
733 | - } elseif (isset($a[2])) { |
|
734 | - // has default value |
|
735 | - $value = $a[2]; |
|
736 | - } else { |
|
737 | - $value = null; // :( |
|
738 | - $this->throwError("Failed to assign arg ".$a[1]); // This ends function by throwing an exception |
|
739 | - } |
|
740 | - |
|
741 | - $value = $this->reduce($value); |
|
742 | - $this->set($a[1], $value); |
|
743 | - $assignedValues[] = $value; |
|
744 | - } else { |
|
745 | - // a lit |
|
746 | - $i++; |
|
747 | - } |
|
748 | - } |
|
749 | - |
|
750 | - // check for a rest |
|
751 | - $last = end($args); |
|
752 | - if ($last && $last[0] == "rest") { |
|
753 | - $rest = array_slice($orderedValues, count($args) - 1); |
|
754 | - $this->set($last[1], $this->reduce(array("list", " ", $rest))); |
|
755 | - } |
|
756 | - |
|
757 | - // wow is this the only true use of PHP's + operator for arrays? |
|
758 | - $this->env->arguments = $assignedValues + $orderedValues; |
|
759 | - } |
|
760 | - |
|
761 | - // compile a prop and update $lines or $blocks appropriately |
|
762 | - protected function compileProp($prop, $block, $out) |
|
763 | - { |
|
764 | - // set error position context |
|
765 | - $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1; |
|
766 | - |
|
767 | - switch ($prop[0]) { |
|
768 | - case 'assign': |
|
769 | - list(, $name, $value) = $prop; |
|
770 | - if ($name[0] == $this->vPrefix) { |
|
771 | - $this->set($name, $value); |
|
772 | - } else { |
|
773 | - $out->lines[] = $this->formatter->property( |
|
774 | - $name, |
|
775 | - $this->compileValue($this->reduce($value)) |
|
776 | - ); |
|
777 | - } |
|
778 | - break; |
|
779 | - case 'block': |
|
780 | - list(, $child) = $prop; |
|
781 | - $this->compileBlock($child); |
|
782 | - break; |
|
783 | - case 'mixin': |
|
784 | - list(, $path, $args, $suffix) = $prop; |
|
785 | - |
|
786 | - $orderedArgs = array(); |
|
787 | - $keywordArgs = array(); |
|
788 | - foreach ((array) $args as $arg) { |
|
789 | - $argval = null; |
|
790 | - switch ($arg[0]) { |
|
791 | - case "arg": |
|
792 | - if (!isset($arg[2])) { |
|
793 | - $orderedArgs[] = $this->reduce(array("variable", $arg[1])); |
|
794 | - } else { |
|
795 | - $keywordArgs[$arg[1]] = $this->reduce($arg[2]); |
|
796 | - } |
|
797 | - break; |
|
798 | - |
|
799 | - case "lit": |
|
800 | - $orderedArgs[] = $this->reduce($arg[1]); |
|
801 | - break; |
|
802 | - default: |
|
803 | - $this->throwError("Unknown arg type: ".$arg[0]); |
|
804 | - } |
|
805 | - } |
|
806 | - |
|
807 | - $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs); |
|
808 | - |
|
809 | - if ($mixins === null) { |
|
810 | - $this->throwError("{$prop[1][0]} is undefined"); |
|
811 | - } |
|
812 | - |
|
813 | - foreach ($mixins as $mixin) { |
|
814 | - if ($mixin === $block && !$orderedArgs) { |
|
815 | - continue; |
|
816 | - } |
|
817 | - |
|
818 | - $haveScope = false; |
|
819 | - if (isset($mixin->parent->scope)) { |
|
820 | - $haveScope = true; |
|
821 | - $mixinParentEnv = $this->pushEnv(); |
|
822 | - $mixinParentEnv->storeParent = $mixin->parent->scope; |
|
823 | - } |
|
824 | - |
|
825 | - $haveArgs = false; |
|
826 | - if (isset($mixin->args)) { |
|
827 | - $haveArgs = true; |
|
828 | - $this->pushEnv(); |
|
829 | - $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs); |
|
830 | - } |
|
831 | - |
|
832 | - $oldParent = $mixin->parent; |
|
833 | - if ($mixin != $block) { |
|
834 | - $mixin->parent = $block; |
|
835 | - } |
|
836 | - |
|
837 | - foreach ($this->sortProps($mixin->props) as $subProp) { |
|
838 | - if ($suffix !== null && |
|
839 | - $subProp[0] == "assign" && |
|
840 | - is_string($subProp[1]) && |
|
841 | - $subProp[1][0] != $this->vPrefix |
|
842 | - ) { |
|
843 | - $subProp[2] = array( |
|
844 | - 'list', ' ', |
|
845 | - array($subProp[2], array('keyword', $suffix)) |
|
846 | - ); |
|
847 | - } |
|
848 | - |
|
849 | - $this->compileProp($subProp, $mixin, $out); |
|
850 | - } |
|
851 | - |
|
852 | - $mixin->parent = $oldParent; |
|
853 | - |
|
854 | - if ($haveArgs) { |
|
855 | - $this->popEnv(); |
|
856 | - } |
|
857 | - if ($haveScope) { |
|
858 | - $this->popEnv(); |
|
859 | - } |
|
860 | - } |
|
861 | - |
|
862 | - break; |
|
863 | - case 'raw': |
|
864 | - $out->lines[] = $prop[1]; |
|
865 | - break; |
|
866 | - case "directive": |
|
867 | - list(, $name, $value) = $prop; |
|
868 | - $out->lines[] = "@$name ".$this->compileValue($this->reduce($value)).';'; |
|
869 | - break; |
|
870 | - case "comment": |
|
871 | - $out->lines[] = $prop[1]; |
|
872 | - break; |
|
873 | - case "import": |
|
874 | - list(, $importPath, $importId) = $prop; |
|
875 | - $importPath = $this->reduce($importPath); |
|
876 | - |
|
877 | - if (!isset($this->env->imports)) { |
|
878 | - $this->env->imports = array(); |
|
879 | - } |
|
880 | - |
|
881 | - $result = $this->tryImport($importPath, $block, $out); |
|
882 | - |
|
883 | - $this->env->imports[$importId] = $result === false ? |
|
884 | - array(false, "@import ".$this->compileValue($importPath).";") : $result; |
|
885 | - |
|
886 | - break; |
|
887 | - case "import_mixin": |
|
888 | - list(, $importId) = $prop; |
|
889 | - $import = $this->env->imports[$importId]; |
|
890 | - if ($import[0] === false) { |
|
891 | - if (isset($import[1])) { |
|
892 | - $out->lines[] = $import[1]; |
|
893 | - } |
|
894 | - } else { |
|
895 | - list(, $bottom, $parser, $importDir) = $import; |
|
896 | - $this->compileImportedProps($bottom, $block, $out, $parser, $importDir); |
|
897 | - } |
|
898 | - |
|
899 | - break; |
|
900 | - default: |
|
901 | - $this->throwError("unknown op: {$prop[0]}\n"); |
|
902 | - } |
|
903 | - } |
|
904 | - |
|
905 | - |
|
906 | - /** |
|
907 | - * Compiles a primitive value into a CSS property value. |
|
908 | - * |
|
909 | - * Values in lessphp are typed by being wrapped in arrays, their format is |
|
910 | - * typically: |
|
911 | - * |
|
912 | - * array(type, contents [, additional_contents]*) |
|
913 | - * |
|
914 | - * The input is expected to be reduced. This function will not work on |
|
915 | - * things like expressions and variables. |
|
916 | - */ |
|
917 | - public function compileValue($value) |
|
918 | - { |
|
919 | - switch ($value[0]) { |
|
920 | - case 'list': |
|
921 | - // [1] - delimiter |
|
922 | - // [2] - array of values |
|
923 | - return implode($value[1], array_map(array($this, 'compileValue'), $value[2])); |
|
924 | - case 'raw_color': |
|
925 | - if (!empty($this->formatter->compressColors)) { |
|
926 | - return $this->compileValue($this->coerceColor($value)); |
|
927 | - } |
|
928 | - return $value[1]; |
|
929 | - case 'keyword': |
|
930 | - // [1] - the keyword |
|
931 | - return $value[1]; |
|
932 | - case 'number': |
|
933 | - list(, $num, $unit) = $value; |
|
934 | - // [1] - the number |
|
935 | - // [2] - the unit |
|
936 | - if ($this->numberPrecision !== null) { |
|
937 | - $num = round($num, $this->numberPrecision); |
|
938 | - } |
|
939 | - return $num.$unit; |
|
940 | - case 'string': |
|
941 | - // [1] - contents of string (includes quotes) |
|
942 | - list(, $delim, $content) = $value; |
|
943 | - foreach ($content as &$part) { |
|
944 | - if (is_array($part)) { |
|
945 | - $part = $this->compileValue($part); |
|
946 | - } |
|
947 | - } |
|
948 | - return $delim.implode($content).$delim; |
|
949 | - case 'color': |
|
950 | - // [1] - red component (either number or a %) |
|
951 | - // [2] - green component |
|
952 | - // [3] - blue component |
|
953 | - // [4] - optional alpha component |
|
954 | - list(, $r, $g, $b) = $value; |
|
955 | - $r = round($r); |
|
956 | - $g = round($g); |
|
957 | - $b = round($b); |
|
958 | - |
|
959 | - if (count($value) == 5 && $value[4] != 1) { // rgba |
|
960 | - return 'rgba('.$r.','.$g.','.$b.','.$value[4].')'; |
|
961 | - } |
|
962 | - |
|
963 | - $h = sprintf("#%02x%02x%02x", $r, $g, $b); |
|
964 | - |
|
965 | - if (!empty($this->formatter->compressColors)) { |
|
966 | - // Converting hex color to short notation (e.g. #003399 to #039) |
|
967 | - if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { |
|
968 | - $h = '#'.$h[1].$h[3].$h[5]; |
|
969 | - } |
|
970 | - } |
|
971 | - |
|
972 | - return $h; |
|
973 | - |
|
974 | - case 'function': |
|
975 | - list(, $name, $args) = $value; |
|
976 | - return $name.'('.$this->compileValue($args).')'; |
|
977 | - default: // assumed to be unit |
|
978 | - $this->throwError("unknown value type: $value[0]"); |
|
979 | - } |
|
980 | - } |
|
981 | - |
|
982 | - protected function lib_pow($args) |
|
983 | - { |
|
984 | - list($base, $exp) = $this->assertArgs($args, 2, "pow"); |
|
985 | - return pow($this->assertNumber($base), $this->assertNumber($exp)); |
|
986 | - } |
|
987 | - |
|
988 | - protected function lib_pi() |
|
989 | - { |
|
990 | - return pi(); |
|
991 | - } |
|
992 | - |
|
993 | - protected function lib_mod($args) |
|
994 | - { |
|
995 | - list($a, $b) = $this->assertArgs($args, 2, "mod"); |
|
996 | - return $this->assertNumber($a) % $this->assertNumber($b); |
|
997 | - } |
|
998 | - |
|
999 | - protected function lib_tan($num) |
|
1000 | - { |
|
1001 | - return tan($this->assertNumber($num)); |
|
1002 | - } |
|
1003 | - |
|
1004 | - protected function lib_sin($num) |
|
1005 | - { |
|
1006 | - return sin($this->assertNumber($num)); |
|
1007 | - } |
|
1008 | - |
|
1009 | - protected function lib_cos($num) |
|
1010 | - { |
|
1011 | - return cos($this->assertNumber($num)); |
|
1012 | - } |
|
1013 | - |
|
1014 | - protected function lib_atan($num) |
|
1015 | - { |
|
1016 | - $num = atan($this->assertNumber($num)); |
|
1017 | - return array("number", $num, "rad"); |
|
1018 | - } |
|
1019 | - |
|
1020 | - protected function lib_asin($num) |
|
1021 | - { |
|
1022 | - $num = asin($this->assertNumber($num)); |
|
1023 | - return array("number", $num, "rad"); |
|
1024 | - } |
|
1025 | - |
|
1026 | - protected function lib_acos($num) |
|
1027 | - { |
|
1028 | - $num = acos($this->assertNumber($num)); |
|
1029 | - return array("number", $num, "rad"); |
|
1030 | - } |
|
1031 | - |
|
1032 | - protected function lib_sqrt($num) |
|
1033 | - { |
|
1034 | - return sqrt($this->assertNumber($num)); |
|
1035 | - } |
|
1036 | - |
|
1037 | - protected function lib_extract($value) |
|
1038 | - { |
|
1039 | - list($list, $idx) = $this->assertArgs($value, 2, "extract"); |
|
1040 | - $idx = $this->assertNumber($idx); |
|
1041 | - // 1 indexed |
|
1042 | - if ($list[0] == "list" && isset($list[2][$idx - 1])) { |
|
1043 | - return $list[2][$idx - 1]; |
|
1044 | - } |
|
1045 | - return ''; |
|
1046 | - } |
|
1047 | - |
|
1048 | - protected function lib_isnumber($value) |
|
1049 | - { |
|
1050 | - return $this->toBool($value[0] == "number"); |
|
1051 | - } |
|
1052 | - |
|
1053 | - protected function lib_isstring($value) |
|
1054 | - { |
|
1055 | - return $this->toBool($value[0] == "string"); |
|
1056 | - } |
|
1057 | - |
|
1058 | - protected function lib_iscolor($value) |
|
1059 | - { |
|
1060 | - return $this->toBool($this->coerceColor($value)); |
|
1061 | - } |
|
1062 | - |
|
1063 | - protected function lib_iskeyword($value) |
|
1064 | - { |
|
1065 | - return $this->toBool($value[0] == "keyword"); |
|
1066 | - } |
|
1067 | - |
|
1068 | - protected function lib_ispixel($value) |
|
1069 | - { |
|
1070 | - return $this->toBool($value[0] == "number" && $value[2] == "px"); |
|
1071 | - } |
|
1072 | - |
|
1073 | - protected function lib_ispercentage($value) |
|
1074 | - { |
|
1075 | - return $this->toBool($value[0] == "number" && $value[2] == "%"); |
|
1076 | - } |
|
1077 | - |
|
1078 | - protected function lib_isem($value) |
|
1079 | - { |
|
1080 | - return $this->toBool($value[0] == "number" && $value[2] == "em"); |
|
1081 | - } |
|
1082 | - |
|
1083 | - protected function lib_isrem($value) |
|
1084 | - { |
|
1085 | - return $this->toBool($value[0] == "number" && $value[2] == "rem"); |
|
1086 | - } |
|
1087 | - |
|
1088 | - protected function lib_rgbahex($color) |
|
1089 | - { |
|
1090 | - $color = $this->coerceColor($color); |
|
1091 | - if (is_null($color)) { |
|
1092 | - $this->throwError("color expected for rgbahex"); |
|
1093 | - } |
|
1094 | - |
|
1095 | - return sprintf( |
|
1096 | - "#%02x%02x%02x%02x", |
|
1097 | - isset($color[4]) ? $color[4] * 255 : 255, |
|
1098 | - $color[1], |
|
1099 | - $color[2], |
|
1100 | - $color[3] |
|
1101 | - ); |
|
1102 | - } |
|
1103 | - |
|
1104 | - protected function lib_argb($color) |
|
1105 | - { |
|
1106 | - return $this->lib_rgbahex($color); |
|
1107 | - } |
|
1108 | - |
|
1109 | - /** |
|
1110 | - * Given an url, decide whether to output a regular link or the base64-encoded contents of the file |
|
1111 | - * |
|
1112 | - * @param array $value either an argument list (two strings) or a single string |
|
1113 | - * @return string formatted url(), either as a link or base64-encoded |
|
1114 | - */ |
|
1115 | - protected function lib_data_uri($value) |
|
1116 | - { |
|
1117 | - $mime = ($value[0] === 'list') ? $value[2][0][2] : null; |
|
1118 | - $url = ($value[0] === 'list') ? $value[2][1][2][0] : $value[2][0]; |
|
1119 | - |
|
1120 | - $fullpath = $this->findImport($url); |
|
1121 | - |
|
1122 | - if ($fullpath && ($fsize = filesize($fullpath)) !== false) { |
|
1123 | - // IE8 can't handle data uris larger than 32KB |
|
1124 | - if ($fsize / 1024 < 32) { |
|
1125 | - if (is_null($mime)) { |
|
1126 | - if (class_exists('finfo')) { // php 5.3+ |
|
1127 | - $finfo = new finfo(FILEINFO_MIME); |
|
1128 | - $mime = explode('; ', $finfo->file($fullpath)); |
|
1129 | - $mime = $mime[0]; |
|
1130 | - } elseif (function_exists('mime_content_type')) { // PHP 5.2 |
|
1131 | - $mime = mime_content_type($fullpath); |
|
1132 | - } |
|
1133 | - } |
|
1134 | - |
|
1135 | - if (!is_null($mime)) { // fallback if the mime type is still unknown |
|
1136 | - $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath))); |
|
1137 | - } |
|
1138 | - } |
|
1139 | - } |
|
1140 | - |
|
1141 | - return 'url("'.$url.'")'; |
|
1142 | - } |
|
1143 | - |
|
1144 | - // utility func to unquote a string |
|
1145 | - protected function lib_e($arg) |
|
1146 | - { |
|
1147 | - switch ($arg[0]) { |
|
1148 | - case "list": |
|
1149 | - $items = $arg[2]; |
|
1150 | - if (isset($items[0])) { |
|
1151 | - return $this->lib_e($items[0]); |
|
1152 | - } |
|
1153 | - $this->throwError("unrecognised input"); // This ends function by throwing an exception |
|
1154 | - // no break |
|
1155 | - case "string": |
|
1156 | - $arg[1] = ""; |
|
1157 | - return $arg; |
|
1158 | - case "keyword": |
|
1159 | - return $arg; |
|
1160 | - default: |
|
1161 | - return array("keyword", $this->compileValue($arg)); |
|
1162 | - } |
|
1163 | - } |
|
1164 | - |
|
1165 | - protected function lib__sprintf($args) |
|
1166 | - { |
|
1167 | - if ($args[0] != "list") { |
|
1168 | - return $args; |
|
1169 | - } |
|
1170 | - $values = $args[2]; |
|
1171 | - $string = array_shift($values); |
|
1172 | - $template = $this->compileValue($this->lib_e($string)); |
|
1173 | - |
|
1174 | - $i = 0; |
|
1175 | - $m = array(); |
|
1176 | - if (preg_match_all('/%[dsa]/', $template, $m)) { |
|
1177 | - foreach ($m[0] as $match) { |
|
1178 | - $val = isset($values[$i]) ? |
|
1179 | - $this->reduce($values[$i]) : array('keyword', ''); |
|
1180 | - |
|
1181 | - // lessjs compat, renders fully expanded color, not raw color |
|
1182 | - if ($color = $this->coerceColor($val)) { |
|
1183 | - $val = $color; |
|
1184 | - } |
|
1185 | - |
|
1186 | - $i++; |
|
1187 | - $rep = $this->compileValue($this->lib_e($val)); |
|
1188 | - $template = preg_replace( |
|
1189 | - '/'.self::preg_quote($match).'/', |
|
1190 | - $rep, |
|
1191 | - $template, |
|
1192 | - 1 |
|
1193 | - ); |
|
1194 | - } |
|
1195 | - } |
|
1196 | - |
|
1197 | - $d = $string[0] == "string" ? $string[1] : '"'; |
|
1198 | - return array("string", $d, array($template)); |
|
1199 | - } |
|
1200 | - |
|
1201 | - protected function lib_floor($arg) |
|
1202 | - { |
|
1203 | - $value = $this->assertNumber($arg); |
|
1204 | - return array("number", floor($value), $arg[2]); |
|
1205 | - } |
|
1206 | - |
|
1207 | - protected function lib_ceil($arg) |
|
1208 | - { |
|
1209 | - $value = $this->assertNumber($arg); |
|
1210 | - return array("number", ceil($value), $arg[2]); |
|
1211 | - } |
|
1212 | - |
|
1213 | - protected function lib_round($arg) |
|
1214 | - { |
|
1215 | - if ($arg[0] != "list") { |
|
1216 | - $value = $this->assertNumber($arg); |
|
1217 | - return array("number", round($value), $arg[2]); |
|
1218 | - } else { |
|
1219 | - $value = $this->assertNumber($arg[2][0]); |
|
1220 | - $precision = $this->assertNumber($arg[2][1]); |
|
1221 | - return array("number", round($value, $precision), $arg[2][0][2]); |
|
1222 | - } |
|
1223 | - } |
|
1224 | - |
|
1225 | - protected function lib_unit($arg) |
|
1226 | - { |
|
1227 | - if ($arg[0] == "list") { |
|
1228 | - list($number, $newUnit) = $arg[2]; |
|
1229 | - return array("number", $this->assertNumber($number), |
|
1230 | - $this->compileValue($this->lib_e($newUnit))); |
|
1231 | - } else { |
|
1232 | - return array("number", $this->assertNumber($arg), ""); |
|
1233 | - } |
|
1234 | - } |
|
1235 | - |
|
1236 | - /** |
|
1237 | - * Helper function to get arguments for color manipulation functions. |
|
1238 | - * takes a list that contains a color like thing and a percentage |
|
1239 | - */ |
|
1240 | - public function colorArgs($args) |
|
1241 | - { |
|
1242 | - if ($args[0] != 'list' || count($args[2]) < 2) { |
|
1243 | - return array(array('color', 0, 0, 0), 0); |
|
1244 | - } |
|
1245 | - list($color, $delta) = $args[2]; |
|
1246 | - $color = $this->assertColor($color); |
|
1247 | - $delta = (float) $delta[1]; |
|
1248 | - |
|
1249 | - return array($color, $delta); |
|
1250 | - } |
|
1251 | - |
|
1252 | - protected function lib_darken($args) |
|
1253 | - { |
|
1254 | - list($color, $delta) = $this->colorArgs($args); |
|
1255 | - |
|
1256 | - $hsl = $this->toHSL($color); |
|
1257 | - $hsl[3] = $this->clamp($hsl[3] - $delta, 100); |
|
1258 | - return $this->toRGB($hsl); |
|
1259 | - } |
|
1260 | - |
|
1261 | - protected function lib_lighten($args) |
|
1262 | - { |
|
1263 | - list($color, $delta) = $this->colorArgs($args); |
|
1264 | - |
|
1265 | - $hsl = $this->toHSL($color); |
|
1266 | - $hsl[3] = $this->clamp($hsl[3] + $delta, 100); |
|
1267 | - return $this->toRGB($hsl); |
|
1268 | - } |
|
1269 | - |
|
1270 | - protected function lib_saturate($args) |
|
1271 | - { |
|
1272 | - list($color, $delta) = $this->colorArgs($args); |
|
1273 | - |
|
1274 | - $hsl = $this->toHSL($color); |
|
1275 | - $hsl[2] = $this->clamp($hsl[2] + $delta, 100); |
|
1276 | - return $this->toRGB($hsl); |
|
1277 | - } |
|
1278 | - |
|
1279 | - protected function lib_desaturate($args) |
|
1280 | - { |
|
1281 | - list($color, $delta) = $this->colorArgs($args); |
|
1282 | - |
|
1283 | - $hsl = $this->toHSL($color); |
|
1284 | - $hsl[2] = $this->clamp($hsl[2] - $delta, 100); |
|
1285 | - return $this->toRGB($hsl); |
|
1286 | - } |
|
1287 | - |
|
1288 | - protected function lib_spin($args) |
|
1289 | - { |
|
1290 | - list($color, $delta) = $this->colorArgs($args); |
|
1291 | - |
|
1292 | - $hsl = $this->toHSL($color); |
|
1293 | - |
|
1294 | - $hsl[1] = $hsl[1] + $delta % 360; |
|
1295 | - if ($hsl[1] < 0) { |
|
1296 | - $hsl[1] += 360; |
|
1297 | - } |
|
1298 | - |
|
1299 | - return $this->toRGB($hsl); |
|
1300 | - } |
|
1301 | - |
|
1302 | - protected function lib_fadeout($args) |
|
1303 | - { |
|
1304 | - list($color, $delta) = $this->colorArgs($args); |
|
1305 | - $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100); |
|
1306 | - return $color; |
|
1307 | - } |
|
1308 | - |
|
1309 | - protected function lib_fadein($args) |
|
1310 | - { |
|
1311 | - list($color, $delta) = $this->colorArgs($args); |
|
1312 | - $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100); |
|
1313 | - return $color; |
|
1314 | - } |
|
1315 | - |
|
1316 | - protected function lib_hue($color) |
|
1317 | - { |
|
1318 | - $hsl = $this->toHSL($this->assertColor($color)); |
|
1319 | - return round($hsl[1]); |
|
1320 | - } |
|
1321 | - |
|
1322 | - protected function lib_saturation($color) |
|
1323 | - { |
|
1324 | - $hsl = $this->toHSL($this->assertColor($color)); |
|
1325 | - return round($hsl[2]); |
|
1326 | - } |
|
1327 | - |
|
1328 | - protected function lib_lightness($color) |
|
1329 | - { |
|
1330 | - $hsl = $this->toHSL($this->assertColor($color)); |
|
1331 | - return round($hsl[3]); |
|
1332 | - } |
|
1333 | - |
|
1334 | - // get the alpha of a color |
|
1335 | - // defaults to 1 for non-colors or colors without an alpha |
|
1336 | - protected function lib_alpha($value) |
|
1337 | - { |
|
1338 | - if (!is_null($color = $this->coerceColor($value))) { |
|
1339 | - return isset($color[4]) ? $color[4] : 1; |
|
1340 | - } |
|
1341 | - return ''; |
|
1342 | - } |
|
1343 | - |
|
1344 | - // set the alpha of the color |
|
1345 | - protected function lib_fade($args) |
|
1346 | - { |
|
1347 | - list($color, $alpha) = $this->colorArgs($args); |
|
1348 | - $color[4] = $this->clamp($alpha / 100.0); |
|
1349 | - return $color; |
|
1350 | - } |
|
1351 | - |
|
1352 | - protected function lib_percentage($arg) |
|
1353 | - { |
|
1354 | - $num = $this->assertNumber($arg); |
|
1355 | - return array("number", $num * 100, "%"); |
|
1356 | - } |
|
1357 | - |
|
1358 | - /** |
|
1359 | - * Mix color with white in variable proportion. |
|
1360 | - * |
|
1361 | - * It is the same as calling `mix(#ffffff, @color, @weight)`. |
|
1362 | - * |
|
1363 | - * tint(@color, [@weight: 50%]); |
|
1364 | - * |
|
1365 | - * http://lesscss.org/functions/#color-operations-tint |
|
1366 | - * |
|
1367 | - * @return array Color |
|
1368 | - */ |
|
1369 | - protected function lib_tint($args) |
|
1370 | - { |
|
1371 | - $white = ['color', 255, 255, 255]; |
|
1372 | - if ($args[0] == 'color') { |
|
1373 | - return $this->lib_mix(['list', ',', [$white, $args]]); |
|
1374 | - } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
1375 | - return $this->lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]); |
|
1376 | - } else { |
|
1377 | - $this->throwError("tint expects (color, weight)"); |
|
1378 | - } |
|
1379 | - return array(); |
|
1380 | - } |
|
1381 | - |
|
1382 | - /** |
|
1383 | - * Mix color with black in variable proportion. |
|
1384 | - * |
|
1385 | - * It is the same as calling `mix(#000000, @color, @weight)` |
|
1386 | - * |
|
1387 | - * shade(@color, [@weight: 50%]); |
|
1388 | - * |
|
1389 | - * http://lesscss.org/functions/#color-operations-shade |
|
1390 | - * |
|
1391 | - * @return array Color |
|
1392 | - */ |
|
1393 | - protected function lib_shade($args) |
|
1394 | - { |
|
1395 | - $black = ['color', 0, 0, 0]; |
|
1396 | - if ($args[0] == 'color') { |
|
1397 | - return $this->lib_mix(['list', ',', [$black, $args]]); |
|
1398 | - } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
1399 | - return $this->lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]); |
|
1400 | - } else { |
|
1401 | - $this->throwError("shade expects (color, weight)"); |
|
1402 | - } |
|
1403 | - return array(); |
|
1404 | - } |
|
1405 | - |
|
1406 | - /** |
|
1407 | - * lib_mix |
|
1408 | - * mixes two colors by weight |
|
1409 | - * mix(@color1, @color2, [@weight: 50%]); |
|
1410 | - * http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method |
|
1411 | - * |
|
1412 | - * @param array $args Args |
|
1413 | - * @return array |
|
1414 | - */ |
|
1415 | - protected function lib_mix($args) |
|
1416 | - { |
|
1417 | - if ($args[0] != "list" || count($args[2]) < 2) { |
|
1418 | - $this->throwError("mix expects (color1, color2, weight)"); |
|
1419 | - } |
|
1420 | - |
|
1421 | - list($first, $second) = $args[2]; |
|
1422 | - $first = $this->assertColor($first); |
|
1423 | - $second = $this->assertColor($second); |
|
1424 | - |
|
1425 | - $first_a = $this->lib_alpha($first); |
|
1426 | - $second_a = $this->lib_alpha($second); |
|
1427 | - |
|
1428 | - if (isset($args[2][2])) { |
|
1429 | - $weight = $args[2][2][1] / 100.0; |
|
1430 | - } else { |
|
1431 | - $weight = 0.5; |
|
1432 | - } |
|
1433 | - |
|
1434 | - $w = $weight * 2 - 1; |
|
1435 | - $a = $first_a - $second_a; |
|
1436 | - |
|
1437 | - $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0; |
|
1438 | - $w2 = 1.0 - $w1; |
|
1439 | - |
|
1440 | - $new = array('color', |
|
1441 | - $w1 * $first[1] + $w2 * $second[1], |
|
1442 | - $w1 * $first[2] + $w2 * $second[2], |
|
1443 | - $w1 * $first[3] + $w2 * $second[3], |
|
1444 | - ); |
|
1445 | - |
|
1446 | - if ($first_a != 1.0 || $second_a != 1.0) { |
|
1447 | - $new[] = $first_a * $weight + $second_a * ($weight - 1); |
|
1448 | - } |
|
1449 | - |
|
1450 | - return $this->fixColor($new); |
|
1451 | - } |
|
1452 | - |
|
1453 | - /** |
|
1454 | - * lib_contrast |
|
1455 | - * |
|
1456 | - * @param array $args Args |
|
1457 | - * @return array |
|
1458 | - */ |
|
1459 | - protected function lib_contrast($args) |
|
1460 | - { |
|
1461 | - $darkColor = array('color', 0, 0, 0); |
|
1462 | - $lightColor = array('color', 255, 255, 255); |
|
1463 | - $threshold = 0.43; |
|
1464 | - |
|
1465 | - if ($args[0] == 'list') { |
|
1466 | - $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor; |
|
1467 | - $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor; |
|
1468 | - $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor; |
|
1469 | - $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold; |
|
1470 | - } else { |
|
1471 | - $inputColor = $this->assertColor($args); |
|
1472 | - } |
|
1473 | - |
|
1474 | - $inputColor = $this->coerceColor($inputColor); |
|
1475 | - $darkColor = $this->coerceColor($darkColor); |
|
1476 | - $lightColor = $this->coerceColor($lightColor); |
|
1477 | - |
|
1478 | - //Figure out which is actually light and dark! |
|
1479 | - if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) { |
|
1480 | - $t = $lightColor; |
|
1481 | - $lightColor = $darkColor; |
|
1482 | - $darkColor = $t; |
|
1483 | - } |
|
1484 | - |
|
1485 | - $inputColor_alpha = $this->lib_alpha($inputColor); |
|
1486 | - if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) { |
|
1487 | - return $lightColor; |
|
1488 | - } |
|
1489 | - return $darkColor; |
|
1490 | - } |
|
1491 | - |
|
1492 | - private function toLuma($color) |
|
1493 | - { |
|
1494 | - list(, $r, $g, $b) = $this->coerceColor($color); |
|
1495 | - |
|
1496 | - $r = $r / 255; |
|
1497 | - $g = $g / 255; |
|
1498 | - $b = $b / 255; |
|
1499 | - |
|
1500 | - $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4); |
|
1501 | - $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4); |
|
1502 | - $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4); |
|
1503 | - |
|
1504 | - return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b); |
|
1505 | - } |
|
1506 | - |
|
1507 | - protected function lib_luma($color) |
|
1508 | - { |
|
1509 | - return array("number", round($this->toLuma($color) * 100, 8), "%"); |
|
1510 | - } |
|
1511 | - |
|
1512 | - |
|
1513 | - public function assertColor($value, $error = "expected color value") |
|
1514 | - { |
|
1515 | - $color = $this->coerceColor($value); |
|
1516 | - if (is_null($color)) { |
|
1517 | - $this->throwError($error); |
|
1518 | - } |
|
1519 | - return $color; |
|
1520 | - } |
|
1521 | - |
|
1522 | - public function assertNumber($value, $error = "expecting number") |
|
1523 | - { |
|
1524 | - if ($value[0] == "number") { |
|
1525 | - return $value[1]; |
|
1526 | - } |
|
1527 | - $this->throwError($error); |
|
1528 | - } |
|
1529 | - |
|
1530 | - public function assertArgs($value, $expectedArgs, $name = "") |
|
1531 | - { |
|
1532 | - if ($expectedArgs == 1) { |
|
1533 | - return $value; |
|
1534 | - } else { |
|
1535 | - if ($value[0] !== "list" || $value[1] != ",") { |
|
1536 | - $this->throwError("expecting list"); |
|
1537 | - } |
|
1538 | - $values = $value[2]; |
|
1539 | - $numValues = count($values); |
|
1540 | - if ($expectedArgs != $numValues) { |
|
1541 | - if ($name) { |
|
1542 | - $name = $name.": "; |
|
1543 | - } |
|
1544 | - |
|
1545 | - $this->throwError("{$name}expecting $expectedArgs arguments, got $numValues"); |
|
1546 | - } |
|
1547 | - |
|
1548 | - return $values; |
|
1549 | - } |
|
1550 | - } |
|
1551 | - |
|
1552 | - protected function toHSL($color) |
|
1553 | - { |
|
1554 | - if ($color[0] === 'hsl') { |
|
1555 | - return $color; |
|
1556 | - } |
|
1557 | - |
|
1558 | - $r = $color[1] / 255; |
|
1559 | - $g = $color[2] / 255; |
|
1560 | - $b = $color[3] / 255; |
|
1561 | - |
|
1562 | - $min = min($r, $g, $b); |
|
1563 | - $max = max($r, $g, $b); |
|
1564 | - |
|
1565 | - $L = ($min + $max) / 2; |
|
1566 | - if ($min == $max) { |
|
1567 | - $S = $H = 0; |
|
1568 | - } else { |
|
1569 | - if ($L < 0.5) { |
|
1570 | - $S = ($max - $min) / ($max + $min); |
|
1571 | - } else { |
|
1572 | - $S = ($max - $min) / (2.0 - $max - $min); |
|
1573 | - } |
|
1574 | - if ($r == $max) { |
|
1575 | - $H = ($g - $b) / ($max - $min); |
|
1576 | - } elseif ($g == $max) { |
|
1577 | - $H = 2.0 + ($b - $r) / ($max - $min); |
|
1578 | - } elseif ($b == $max) { |
|
1579 | - $H = 4.0 + ($r - $g) / ($max - $min); |
|
1580 | - } |
|
1581 | - } |
|
1582 | - |
|
1583 | - $out = array('hsl', |
|
1584 | - ($H < 0 ? $H + 6 : $H) * 60, |
|
1585 | - $S * 100, |
|
1586 | - $L * 100, |
|
1587 | - ); |
|
1588 | - |
|
1589 | - if (count($color) > 4) { |
|
1590 | - // copy alpha |
|
1591 | - $out[] = $color[4]; |
|
1592 | - } |
|
1593 | - return $out; |
|
1594 | - } |
|
1595 | - |
|
1596 | - protected function toRGB_helper($comp, $temp1, $temp2) |
|
1597 | - { |
|
1598 | - if ($comp < 0) { |
|
1599 | - $comp += 1.0; |
|
1600 | - } elseif ($comp > 1) { |
|
1601 | - $comp -= 1.0; |
|
1602 | - } |
|
1603 | - |
|
1604 | - if (6 * $comp < 1) { |
|
1605 | - return $temp1 + ($temp2 - $temp1) * 6 * $comp; |
|
1606 | - } |
|
1607 | - if (2 * $comp < 1) { |
|
1608 | - return $temp2; |
|
1609 | - } |
|
1610 | - if (3 * $comp < 2) { |
|
1611 | - return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6; |
|
1612 | - } |
|
1613 | - |
|
1614 | - return $temp1; |
|
1615 | - } |
|
1616 | - |
|
1617 | - /** |
|
1618 | - * Converts a hsl array into a color value in rgb. |
|
1619 | - * Expects H to be in range of 0 to 360, S and L in 0 to 100 |
|
1620 | - */ |
|
1621 | - protected function toRGB($color) |
|
1622 | - { |
|
1623 | - if ($color[0] === 'color') { |
|
1624 | - return $color; |
|
1625 | - } |
|
1626 | - |
|
1627 | - $H = $color[1] / 360; |
|
1628 | - $S = $color[2] / 100; |
|
1629 | - $L = $color[3] / 100; |
|
1630 | - |
|
1631 | - if ($S == 0) { |
|
1632 | - $r = $g = $b = $L; |
|
1633 | - } else { |
|
1634 | - $temp2 = $L < 0.5 ? |
|
1635 | - $L * (1.0 + $S) : $L + $S - $L * $S; |
|
1636 | - |
|
1637 | - $temp1 = 2.0 * $L - $temp2; |
|
1638 | - |
|
1639 | - $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2); |
|
1640 | - $g = $this->toRGB_helper($H, $temp1, $temp2); |
|
1641 | - $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2); |
|
1642 | - } |
|
1643 | - |
|
1644 | - // $out = array('color', round($r*255), round($g*255), round($b*255)); |
|
1645 | - $out = array('color', $r * 255, $g * 255, $b * 255); |
|
1646 | - if (count($color) > 4) { |
|
1647 | - // copy alpha |
|
1648 | - $out[] = $color[4]; |
|
1649 | - } |
|
1650 | - return $out; |
|
1651 | - } |
|
1652 | - |
|
1653 | - protected function clamp($v, $max = 1, $min = 0) |
|
1654 | - { |
|
1655 | - return min($max, max($min, $v)); |
|
1656 | - } |
|
1657 | - |
|
1658 | - /** |
|
1659 | - * Convert the rgb, rgba, hsl color literals of function type |
|
1660 | - * as returned by the parser into values of color type. |
|
1661 | - */ |
|
1662 | - protected function funcToColor($func) |
|
1663 | - { |
|
1664 | - $fname = $func[1]; |
|
1665 | - if ($func[2][0] != 'list') { |
|
1666 | - // need a list of arguments |
|
1667 | - return false; |
|
1668 | - } |
|
1669 | - $rawComponents = $func[2][2]; |
|
1670 | - |
|
1671 | - if ($fname == 'hsl' || $fname == 'hsla') { |
|
1672 | - $hsl = array('hsl'); |
|
1673 | - $i = 0; |
|
1674 | - foreach ($rawComponents as $c) { |
|
1675 | - $val = $this->reduce($c); |
|
1676 | - $val = isset($val[1]) ? (float) $val[1] : 0; |
|
1677 | - |
|
1678 | - if ($i == 0) { |
|
1679 | - $clamp = 360; |
|
1680 | - } elseif ($i < 3) { |
|
1681 | - $clamp = 100; |
|
1682 | - } else { |
|
1683 | - $clamp = 1; |
|
1684 | - } |
|
1685 | - |
|
1686 | - $hsl[] = $this->clamp($val, $clamp); |
|
1687 | - $i++; |
|
1688 | - } |
|
1689 | - |
|
1690 | - while (count($hsl) < 4) { |
|
1691 | - $hsl[] = 0; |
|
1692 | - } |
|
1693 | - return $this->toRGB($hsl); |
|
1694 | - |
|
1695 | - } elseif ($fname == 'rgb' || $fname == 'rgba') { |
|
1696 | - $components = array(); |
|
1697 | - $i = 1; |
|
1698 | - foreach ($rawComponents as $c) { |
|
1699 | - $c = $this->reduce($c); |
|
1700 | - if ($i < 4) { |
|
1701 | - if ($c[0] == "number" && $c[2] == "%") { |
|
1702 | - $components[] = 255 * ($c[1] / 100); |
|
1703 | - } else { |
|
1704 | - $components[] = (float) $c[1]; |
|
1705 | - } |
|
1706 | - } elseif ($i == 4) { |
|
1707 | - if ($c[0] == "number" && $c[2] == "%") { |
|
1708 | - $components[] = 1.0 * ($c[1] / 100); |
|
1709 | - } else { |
|
1710 | - $components[] = (float) $c[1]; |
|
1711 | - } |
|
1712 | - } else { |
|
1713 | - break; |
|
1714 | - } |
|
1715 | - |
|
1716 | - $i++; |
|
1717 | - } |
|
1718 | - while (count($components) < 3) { |
|
1719 | - $components[] = 0; |
|
1720 | - } |
|
1721 | - array_unshift($components, 'color'); |
|
1722 | - return $this->fixColor($components); |
|
1723 | - } |
|
1724 | - |
|
1725 | - return false; |
|
1726 | - } |
|
1727 | - |
|
1728 | - protected function reduce($value, $forExpression = false) |
|
1729 | - { |
|
1730 | - switch ($value[0]) { |
|
1731 | - case "interpolate": |
|
1732 | - $reduced = $this->reduce($value[1]); |
|
1733 | - $var = $this->compileValue($reduced); |
|
1734 | - $res = $this->reduce(array("variable", $this->vPrefix.$var)); |
|
1735 | - |
|
1736 | - if ($res[0] == "raw_color") { |
|
1737 | - $res = $this->coerceColor($res); |
|
1738 | - } |
|
1739 | - |
|
1740 | - if (empty($value[2])) { |
|
1741 | - $res = $this->lib_e($res); |
|
1742 | - } |
|
1743 | - |
|
1744 | - return $res; |
|
1745 | - case "variable": |
|
1746 | - $key = $value[1]; |
|
1747 | - if (is_array($key)) { |
|
1748 | - $key = $this->reduce($key); |
|
1749 | - $key = $this->vPrefix.$this->compileValue($this->lib_e($key)); |
|
1750 | - } |
|
1751 | - |
|
1752 | - $seen = & $this->env->seenNames; |
|
1753 | - |
|
1754 | - if (!empty($seen[$key])) { |
|
1755 | - $this->throwError("infinite loop detected: $key"); |
|
1756 | - } |
|
1757 | - |
|
1758 | - $seen[$key] = true; |
|
1759 | - $out = $this->reduce($this->get($key)); |
|
1760 | - $seen[$key] = false; |
|
1761 | - return $out; |
|
1762 | - case "list": |
|
1763 | - foreach ($value[2] as &$item) { |
|
1764 | - $item = $this->reduce($item, $forExpression); |
|
1765 | - } |
|
1766 | - return $value; |
|
1767 | - case "expression": |
|
1768 | - return $this->evaluate($value); |
|
1769 | - case "string": |
|
1770 | - foreach ($value[2] as &$part) { |
|
1771 | - if (is_array($part)) { |
|
1772 | - $strip = $part[0] == "variable"; |
|
1773 | - $part = $this->reduce($part); |
|
1774 | - if ($strip) { |
|
1775 | - $part = $this->lib_e($part); |
|
1776 | - } |
|
1777 | - } |
|
1778 | - } |
|
1779 | - return $value; |
|
1780 | - case "escape": |
|
1781 | - list(, $inner) = $value; |
|
1782 | - return $this->lib_e($this->reduce($inner)); |
|
1783 | - case "function": |
|
1784 | - $color = $this->funcToColor($value); |
|
1785 | - if ($color) { |
|
1786 | - return $color; |
|
1787 | - } |
|
1788 | - |
|
1789 | - list(, $name, $args) = $value; |
|
1790 | - if ($name == "%") { |
|
1791 | - $name = "_sprintf"; |
|
1792 | - } |
|
1793 | - |
|
1794 | - $f = isset($this->libFunctions[$name]) ? |
|
1795 | - $this->libFunctions[$name] : array($this, 'lib_'.str_replace('-', '_', $name)); |
|
1796 | - |
|
1797 | - if (is_callable($f)) { |
|
1798 | - if ($args[0] == 'list') { |
|
1799 | - $args = self::compressList($args[2], $args[1]); |
|
1800 | - } |
|
1801 | - |
|
1802 | - $ret = call_user_func($f, $this->reduce($args, true), $this); |
|
1803 | - |
|
1804 | - if (is_null($ret)) { |
|
1805 | - return array("string", "", array( |
|
1806 | - $name, "(", $args, ")" |
|
1807 | - )); |
|
1808 | - } |
|
1809 | - |
|
1810 | - // convert to a typed value if the result is a php primitive |
|
1811 | - if (is_numeric($ret)) { |
|
1812 | - $ret = array('number', $ret, ""); |
|
1813 | - } elseif (!is_array($ret)) { |
|
1814 | - $ret = array('keyword', $ret); |
|
1815 | - } |
|
1816 | - |
|
1817 | - return $ret; |
|
1818 | - } |
|
1819 | - |
|
1820 | - // plain function, reduce args |
|
1821 | - $value[2] = $this->reduce($value[2]); |
|
1822 | - return $value; |
|
1823 | - case "unary": |
|
1824 | - list(, $op, $exp) = $value; |
|
1825 | - $exp = $this->reduce($exp); |
|
1826 | - |
|
1827 | - if ($exp[0] == "number") { |
|
1828 | - switch ($op) { |
|
1829 | - case "+": |
|
1830 | - return $exp; |
|
1831 | - case "-": |
|
1832 | - $exp[1] *= -1; |
|
1833 | - return $exp; |
|
1834 | - } |
|
1835 | - } |
|
1836 | - return array("string", "", array($op, $exp)); |
|
1837 | - } |
|
1838 | - |
|
1839 | - if ($forExpression) { |
|
1840 | - switch ($value[0]) { |
|
1841 | - case "keyword": |
|
1842 | - if ($color = $this->coerceColor($value)) { |
|
1843 | - return $color; |
|
1844 | - } |
|
1845 | - break; |
|
1846 | - case "raw_color": |
|
1847 | - return $this->coerceColor($value); |
|
1848 | - } |
|
1849 | - } |
|
1850 | - |
|
1851 | - return $value; |
|
1852 | - } |
|
1853 | - |
|
1854 | - |
|
1855 | - // coerce a value for use in color operation |
|
1856 | - protected function coerceColor($value) |
|
1857 | - { |
|
1858 | - switch ($value[0]) { |
|
1859 | - case 'color': |
|
1860 | - return $value; |
|
1861 | - case 'raw_color': |
|
1862 | - $c = array("color", 0, 0, 0); |
|
1863 | - $colorStr = substr($value[1], 1); |
|
1864 | - $num = hexdec($colorStr); |
|
1865 | - $width = strlen($colorStr) == 3 ? 16 : 256; |
|
1866 | - |
|
1867 | - for ($i = 3; $i > 0; $i--) { // 3 2 1 |
|
1868 | - $t = intval($num) % $width; |
|
1869 | - $num /= $width; |
|
1870 | - |
|
1871 | - $c[$i] = $t * (256 / $width) + $t * floor(16/$width); |
|
1872 | - } |
|
1873 | - |
|
1874 | - return $c; |
|
1875 | - case 'keyword': |
|
1876 | - $name = $value[1]; |
|
1877 | - if (isset(self::$cssColors[$name])) { |
|
1878 | - $rgba = explode(',', self::$cssColors[$name]); |
|
1879 | - |
|
1880 | - if (isset($rgba[3])) { |
|
1881 | - return array('color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]); |
|
1882 | - } |
|
1883 | - return array('color', $rgba[0], $rgba[1], $rgba[2]); |
|
1884 | - } |
|
1885 | - return null; |
|
1886 | - } |
|
1887 | - return null; |
|
1888 | - } |
|
1889 | - |
|
1890 | - // make something string like into a string |
|
1891 | - protected function coerceString($value) |
|
1892 | - { |
|
1893 | - switch ($value[0]) { |
|
1894 | - case "string": |
|
1895 | - return $value; |
|
1896 | - case "keyword": |
|
1897 | - return array("string", "", array($value[1])); |
|
1898 | - } |
|
1899 | - return null; |
|
1900 | - } |
|
1901 | - |
|
1902 | - // turn list of length 1 into value type |
|
1903 | - protected function flattenList($value) |
|
1904 | - { |
|
1905 | - if ($value[0] == "list" && count($value[2]) == 1) { |
|
1906 | - return $this->flattenList($value[2][0]); |
|
1907 | - } |
|
1908 | - return $value; |
|
1909 | - } |
|
1910 | - |
|
1911 | - public function toBool($a) |
|
1912 | - { |
|
1913 | - return $a ? self::$TRUE : self::$FALSE; |
|
1914 | - } |
|
1915 | - |
|
1916 | - // evaluate an expression |
|
1917 | - protected function evaluate($exp) |
|
1918 | - { |
|
1919 | - list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp; |
|
1920 | - |
|
1921 | - $left = $this->reduce($left, true); |
|
1922 | - $right = $this->reduce($right, true); |
|
1923 | - |
|
1924 | - if ($leftColor = $this->coerceColor($left)) { |
|
1925 | - $left = $leftColor; |
|
1926 | - } |
|
1927 | - |
|
1928 | - if ($rightColor = $this->coerceColor($right)) { |
|
1929 | - $right = $rightColor; |
|
1930 | - } |
|
1931 | - |
|
1932 | - $ltype = $left[0]; |
|
1933 | - $rtype = $right[0]; |
|
1934 | - |
|
1935 | - // operators that work on all types |
|
1936 | - if ($op == "and") { |
|
1937 | - return $this->toBool($left == self::$TRUE && $right == self::$TRUE); |
|
1938 | - } |
|
1939 | - |
|
1940 | - if ($op == "=") { |
|
1941 | - return $this->toBool($this->eq($left, $right)); |
|
1942 | - } |
|
1943 | - |
|
1944 | - if ($op == "+" && !is_null($str = $this->stringConcatenate($left, $right))) { |
|
1945 | - return $str; |
|
1946 | - } |
|
1947 | - |
|
1948 | - // type based operators |
|
1949 | - $fname = "op_{$ltype}_{$rtype}"; |
|
1950 | - if (is_callable(array($this, $fname))) { |
|
1951 | - $out = $this->$fname($op, $left, $right); |
|
1952 | - if (!is_null($out)) { |
|
1953 | - return $out; |
|
1954 | - } |
|
1955 | - } |
|
1956 | - |
|
1957 | - // make the expression look it did before being parsed |
|
1958 | - $paddedOp = $op; |
|
1959 | - if ($whiteBefore) { |
|
1960 | - $paddedOp = " ".$paddedOp; |
|
1961 | - } |
|
1962 | - if ($whiteAfter) { |
|
1963 | - $paddedOp .= " "; |
|
1964 | - } |
|
1965 | - |
|
1966 | - return array("string", "", array($left, $paddedOp, $right)); |
|
1967 | - } |
|
1968 | - |
|
1969 | - protected function stringConcatenate($left, $right) |
|
1970 | - { |
|
1971 | - if ($strLeft = $this->coerceString($left)) { |
|
1972 | - if ($right[0] == "string") { |
|
1973 | - $right[1] = ""; |
|
1974 | - } |
|
1975 | - $strLeft[2][] = $right; |
|
1976 | - return $strLeft; |
|
1977 | - } |
|
1978 | - |
|
1979 | - if ($strRight = $this->coerceString($right)) { |
|
1980 | - array_unshift($strRight[2], $left); |
|
1981 | - return $strRight; |
|
1982 | - } |
|
1983 | - return ''; |
|
1984 | - } |
|
1985 | - |
|
1986 | - |
|
1987 | - // make sure a color's components don't go out of bounds |
|
1988 | - protected function fixColor($c) |
|
1989 | - { |
|
1990 | - foreach (range(1, 3) as $i) { |
|
1991 | - if ($c[$i] < 0) { |
|
1992 | - $c[$i] = 0; |
|
1993 | - } |
|
1994 | - if ($c[$i] > 255) { |
|
1995 | - $c[$i] = 255; |
|
1996 | - } |
|
1997 | - } |
|
1998 | - |
|
1999 | - return $c; |
|
2000 | - } |
|
2001 | - |
|
2002 | - protected function op_number_color($op, $lft, $rgt) |
|
2003 | - { |
|
2004 | - if ($op == '+' || $op == '*') { |
|
2005 | - return $this->op_color_number($op, $rgt, $lft); |
|
2006 | - } |
|
2007 | - return array(); |
|
2008 | - } |
|
2009 | - |
|
2010 | - protected function op_color_number($op, $lft, $rgt) |
|
2011 | - { |
|
2012 | - if ($rgt[0] == '%') { |
|
2013 | - $rgt[1] /= 100; |
|
2014 | - } |
|
2015 | - |
|
2016 | - return $this->op_color_color( |
|
2017 | - $op, |
|
2018 | - $lft, |
|
2019 | - array_fill(1, count($lft) - 1, $rgt[1]) |
|
2020 | - ); |
|
2021 | - } |
|
2022 | - |
|
2023 | - protected function op_color_color($op, $left, $right) |
|
2024 | - { |
|
2025 | - $out = array('color'); |
|
2026 | - $max = count($left) > count($right) ? count($left) : count($right); |
|
2027 | - foreach (range(1, $max - 1) as $i) { |
|
2028 | - $lval = isset($left[$i]) ? $left[$i] : 0; |
|
2029 | - $rval = isset($right[$i]) ? $right[$i] : 0; |
|
2030 | - switch ($op) { |
|
2031 | - case '+': |
|
2032 | - $out[] = $lval + $rval; |
|
2033 | - break; |
|
2034 | - case '-': |
|
2035 | - $out[] = $lval - $rval; |
|
2036 | - break; |
|
2037 | - case '*': |
|
2038 | - $out[] = $lval * $rval; |
|
2039 | - break; |
|
2040 | - case '%': |
|
2041 | - $out[] = $lval % $rval; |
|
2042 | - break; |
|
2043 | - case '/': |
|
2044 | - if ($rval == 0) { |
|
2045 | - $this->throwError("evaluate error: can't divide by zero"); |
|
2046 | - } |
|
2047 | - $out[] = $lval / $rval; |
|
2048 | - break; |
|
2049 | - default: |
|
2050 | - $this->throwError('evaluate error: color op number failed on op '.$op); |
|
2051 | - } |
|
2052 | - } |
|
2053 | - return $this->fixColor($out); |
|
2054 | - } |
|
2055 | - |
|
2056 | - public function lib_red($color) |
|
2057 | - { |
|
2058 | - $color = $this->coerceColor($color); |
|
2059 | - if (is_null($color)) { |
|
2060 | - $this->throwError('color expected for red()'); |
|
2061 | - } |
|
2062 | - |
|
2063 | - return $color[1]; |
|
2064 | - } |
|
2065 | - |
|
2066 | - public function lib_green($color) |
|
2067 | - { |
|
2068 | - $color = $this->coerceColor($color); |
|
2069 | - if (is_null($color)) { |
|
2070 | - $this->throwError('color expected for green()'); |
|
2071 | - } |
|
2072 | - |
|
2073 | - return $color[2]; |
|
2074 | - } |
|
2075 | - |
|
2076 | - public function lib_blue($color) |
|
2077 | - { |
|
2078 | - $color = $this->coerceColor($color); |
|
2079 | - if (is_null($color)) { |
|
2080 | - $this->throwError('color expected for blue()'); |
|
2081 | - } |
|
2082 | - |
|
2083 | - return $color[3]; |
|
2084 | - } |
|
2085 | - |
|
2086 | - |
|
2087 | - // operator on two numbers |
|
2088 | - protected function op_number_number($op, $left, $right) |
|
2089 | - { |
|
2090 | - $unit = empty($left[2]) ? $right[2] : $left[2]; |
|
2091 | - |
|
2092 | - $value = 0; |
|
2093 | - switch ($op) { |
|
2094 | - case '+': |
|
2095 | - $value = $left[1] + $right[1]; |
|
2096 | - break; |
|
2097 | - case '*': |
|
2098 | - $value = $left[1] * $right[1]; |
|
2099 | - break; |
|
2100 | - case '-': |
|
2101 | - $value = $left[1] - $right[1]; |
|
2102 | - break; |
|
2103 | - case '%': |
|
2104 | - $value = $left[1] % $right[1]; |
|
2105 | - break; |
|
2106 | - case '/': |
|
2107 | - if ($right[1] == 0) { |
|
2108 | - $this->throwError('parse error: divide by zero'); |
|
2109 | - } |
|
2110 | - $value = $left[1] / $right[1]; |
|
2111 | - break; |
|
2112 | - case '<': |
|
2113 | - return $this->toBool($left[1] < $right[1]); |
|
2114 | - case '>': |
|
2115 | - return $this->toBool($left[1] > $right[1]); |
|
2116 | - case '>=': |
|
2117 | - return $this->toBool($left[1] >= $right[1]); |
|
2118 | - case '=<': |
|
2119 | - return $this->toBool($left[1] <= $right[1]); |
|
2120 | - default: |
|
2121 | - $this->throwError('parse error: unknown number operator: '.$op); |
|
2122 | - } |
|
2123 | - |
|
2124 | - return array("number", $value, $unit); |
|
2125 | - } |
|
2126 | - |
|
2127 | - |
|
2128 | - /* environment functions */ |
|
2129 | - |
|
2130 | - protected function makeOutputBlock($type, $selectors = null) |
|
2131 | - { |
|
2132 | - $b = new stdclass(); |
|
2133 | - $b->lines = array(); |
|
2134 | - $b->children = array(); |
|
2135 | - $b->selectors = $selectors; |
|
2136 | - $b->type = $type; |
|
2137 | - $b->parent = $this->scope; |
|
2138 | - return $b; |
|
2139 | - } |
|
2140 | - |
|
2141 | - // the state of execution |
|
2142 | - protected function pushEnv($block = null) |
|
2143 | - { |
|
2144 | - $e = new stdclass(); |
|
2145 | - $e->parent = $this->env; |
|
2146 | - $e->store = array(); |
|
2147 | - $e->block = $block; |
|
2148 | - |
|
2149 | - $this->env = $e; |
|
2150 | - return $e; |
|
2151 | - } |
|
2152 | - |
|
2153 | - // pop something off the stack |
|
2154 | - protected function popEnv() |
|
2155 | - { |
|
2156 | - $old = $this->env; |
|
2157 | - $this->env = $this->env->parent; |
|
2158 | - return $old; |
|
2159 | - } |
|
2160 | - |
|
2161 | - // set something in the current env |
|
2162 | - protected function set($name, $value) |
|
2163 | - { |
|
2164 | - $this->env->store[$name] = $value; |
|
2165 | - } |
|
2166 | - |
|
2167 | - |
|
2168 | - // get the highest occurrence entry for a name |
|
2169 | - protected function get($name) |
|
2170 | - { |
|
2171 | - $current = $this->env; |
|
2172 | - |
|
2173 | - $isArguments = $name == $this->vPrefix.'arguments'; |
|
2174 | - while ($current) { |
|
2175 | - if ($isArguments && isset($current->arguments)) { |
|
2176 | - return array('list', ' ', $current->arguments); |
|
2177 | - } |
|
2178 | - |
|
2179 | - if (isset($current->store[$name])) { |
|
2180 | - return $current->store[$name]; |
|
2181 | - } |
|
2182 | - |
|
2183 | - $current = isset($current->storeParent) ? |
|
2184 | - $current->storeParent : $current->parent; |
|
2185 | - } |
|
2186 | - |
|
2187 | - $this->throwError("variable $name is undefined"); |
|
2188 | - } |
|
2189 | - |
|
2190 | - // inject array of unparsed strings into environment as variables |
|
2191 | - protected function injectVariables($args) |
|
2192 | - { |
|
2193 | - $this->pushEnv(); |
|
2194 | - $parser = new lessc_parser($this, __METHOD__); |
|
2195 | - $value = null; |
|
2196 | - foreach ($args as $name => $strValue) { |
|
2197 | - if ($name[0] !== '@') { |
|
2198 | - $name = '@'.$name; |
|
2199 | - } |
|
2200 | - $parser->count = 0; |
|
2201 | - $parser->buffer = (string) $strValue; |
|
2202 | - if (!$parser->propertyValue($value)) { |
|
2203 | - throw new Exception("failed to parse passed in variable $name: $strValue"); |
|
2204 | - } |
|
2205 | - |
|
2206 | - $this->set($name, $value); |
|
2207 | - } |
|
2208 | - } |
|
2209 | - |
|
2210 | - /** |
|
2211 | - * Initialize any static state, can initialize parser for a file |
|
2212 | - * $opts isn't used yet |
|
2213 | - */ |
|
2214 | - public function __construct($fname = null) |
|
2215 | - { |
|
2216 | - if ($fname !== null) { |
|
2217 | - // used for deprecated parse method |
|
2218 | - $this->_parseFile = $fname; |
|
2219 | - } |
|
2220 | - } |
|
2221 | - |
|
2222 | - public function compile($string, $name = null) |
|
2223 | - { |
|
2224 | - $locale = setlocale(LC_NUMERIC, 0); |
|
2225 | - setlocale(LC_NUMERIC, "C"); |
|
2226 | - |
|
2227 | - $this->parser = $this->makeParser($name); |
|
2228 | - $root = $this->parser->parse($string); |
|
2229 | - |
|
2230 | - $this->env = null; |
|
2231 | - $this->scope = null; |
|
2232 | - |
|
2233 | - $this->formatter = $this->newFormatter(); |
|
2234 | - |
|
2235 | - if (!empty($this->registeredVars)) { |
|
2236 | - $this->injectVariables($this->registeredVars); |
|
2237 | - } |
|
2238 | - |
|
2239 | - $this->sourceParser = $this->parser; // used for error messages |
|
2240 | - $this->compileBlock($root); |
|
2241 | - |
|
2242 | - ob_start(); |
|
2243 | - $this->formatter->block($this->scope); |
|
2244 | - $out = ob_get_clean(); |
|
2245 | - setlocale(LC_NUMERIC, $locale); |
|
2246 | - return $out; |
|
2247 | - } |
|
2248 | - |
|
2249 | - public function compileFile($fname, $outFname = null) |
|
2250 | - { |
|
2251 | - if (!is_readable($fname)) { |
|
2252 | - throw new Exception('load error: failed to find '.$fname); |
|
2253 | - } |
|
2254 | - |
|
2255 | - $pi = pathinfo($fname); |
|
2256 | - |
|
2257 | - $oldImport = $this->importDir; |
|
2258 | - |
|
2259 | - $this->importDir = (array) $this->importDir; |
|
2260 | - $this->importDir[] = $pi['dirname'].'/'; |
|
2261 | - |
|
2262 | - $this->addParsedFile($fname); |
|
2263 | - |
|
2264 | - $out = $this->compile(file_get_contents($fname), $fname); |
|
2265 | - |
|
2266 | - $this->importDir = $oldImport; |
|
2267 | - |
|
2268 | - if ($outFname !== null) { |
|
2269 | - return file_put_contents($outFname, $out); |
|
2270 | - } |
|
2271 | - |
|
2272 | - return $out; |
|
2273 | - } |
|
2274 | - |
|
2275 | - // compile only if changed input has changed or output doesn't exist |
|
2276 | - public function checkedCompile($in, $out) |
|
2277 | - { |
|
2278 | - if (!is_file($out) || filemtime($in) > filemtime($out)) { |
|
2279 | - $this->compileFile($in, $out); |
|
2280 | - return true; |
|
2281 | - } |
|
2282 | - return false; |
|
2283 | - } |
|
2284 | - |
|
2285 | - /** |
|
2286 | - * Execute lessphp on a .less file or a lessphp cache structure |
|
2287 | - * |
|
2288 | - * The lessphp cache structure contains information about a specific |
|
2289 | - * less file having been parsed. It can be used as a hint for future |
|
2290 | - * calls to determine whether or not a rebuild is required. |
|
2291 | - * |
|
2292 | - * The cache structure contains two important keys that may be used |
|
2293 | - * externally: |
|
2294 | - * |
|
2295 | - * compiled: The final compiled CSS |
|
2296 | - * updated: The time (in seconds) the CSS was last compiled |
|
2297 | - * |
|
2298 | - * The cache structure is a plain-ol' PHP associative array and can |
|
2299 | - * be serialized and unserialized without a hitch. |
|
2300 | - * |
|
2301 | - * @param mixed $in Input |
|
2302 | - * @param bool $force Force rebuild? |
|
2303 | - * @return array|null lessphp cache structure |
|
2304 | - */ |
|
2305 | - public function cachedCompile($in, $force = false) |
|
2306 | - { |
|
2307 | - // assume no root |
|
2308 | - $root = null; |
|
2309 | - |
|
2310 | - if (is_string($in)) { |
|
2311 | - $root = $in; |
|
2312 | - } elseif (is_array($in) && isset($in['root'])) { |
|
2313 | - if ($force || !isset($in['files'])) { |
|
2314 | - // If we are forcing a recompile or if for some reason the |
|
2315 | - // structure does not contain any file information we should |
|
2316 | - // specify the root to trigger a rebuild. |
|
2317 | - $root = $in['root']; |
|
2318 | - } elseif (isset($in['files']) && is_array($in['files'])) { |
|
2319 | - foreach ($in['files'] as $fname => $ftime) { |
|
2320 | - if (!file_exists($fname) || filemtime($fname) > $ftime) { |
|
2321 | - // One of the files we knew about previously has changed |
|
2322 | - // so we should look at our incoming root again. |
|
2323 | - $root = $in['root']; |
|
2324 | - break; |
|
2325 | - } |
|
2326 | - } |
|
2327 | - } |
|
2328 | - } else { |
|
2329 | - // TODO: Throw an exception? We got neither a string nor something |
|
2330 | - // that looks like a compatible lessphp cache structure. |
|
2331 | - return null; |
|
2332 | - } |
|
2333 | - |
|
2334 | - if ($root !== null) { |
|
2335 | - // If we have a root value which means we should rebuild. |
|
2336 | - $out = array(); |
|
2337 | - $out['root'] = $root; |
|
2338 | - $out['compiled'] = $this->compileFile($root); |
|
2339 | - $out['files'] = $this->allParsedFiles(); |
|
2340 | - $out['updated'] = time(); |
|
2341 | - return $out; |
|
2342 | - } else { |
|
2343 | - // No changes, pass back the structure |
|
2344 | - // we were given initially. |
|
2345 | - return $in; |
|
2346 | - } |
|
2347 | - } |
|
2348 | - |
|
2349 | - // parse and compile buffer |
|
2350 | - // This is deprecated |
|
2351 | - public function parse($str = null, $initialVariables = null) |
|
2352 | - { |
|
2353 | - if (is_array($str)) { |
|
2354 | - $initialVariables = $str; |
|
2355 | - $str = null; |
|
2356 | - } |
|
2357 | - |
|
2358 | - $oldVars = $this->registeredVars; |
|
2359 | - if ($initialVariables !== null) { |
|
2360 | - $this->setVariables($initialVariables); |
|
2361 | - } |
|
2362 | - |
|
2363 | - if ($str == null) { |
|
2364 | - if (empty($this->_parseFile)) { |
|
2365 | - throw new exception("nothing to parse"); |
|
2366 | - } |
|
2367 | - |
|
2368 | - $out = $this->compileFile($this->_parseFile); |
|
2369 | - } else { |
|
2370 | - $out = $this->compile($str); |
|
2371 | - } |
|
2372 | - |
|
2373 | - $this->registeredVars = $oldVars; |
|
2374 | - return $out; |
|
2375 | - } |
|
2376 | - |
|
2377 | - protected function makeParser($name) |
|
2378 | - { |
|
2379 | - $parser = new lessc_parser($this, $name); |
|
2380 | - $parser->writeComments = $this->preserveComments; |
|
2381 | - |
|
2382 | - return $parser; |
|
2383 | - } |
|
2384 | - |
|
2385 | - public function setFormatter($name) |
|
2386 | - { |
|
2387 | - $this->formatterName = $name; |
|
2388 | - } |
|
2389 | - |
|
2390 | - protected function newFormatter() |
|
2391 | - { |
|
2392 | - $className = "lessc_formatter_lessjs"; |
|
2393 | - if (!empty($this->formatterName)) { |
|
2394 | - if (!is_string($this->formatterName)) { |
|
2395 | - return $this->formatterName; |
|
2396 | - } |
|
2397 | - $className = "lessc_formatter_$this->formatterName"; |
|
2398 | - } |
|
2399 | - |
|
2400 | - return new $className(); |
|
2401 | - } |
|
2402 | - |
|
2403 | - public function setPreserveComments($preserve) |
|
2404 | - { |
|
2405 | - $this->preserveComments = $preserve; |
|
2406 | - } |
|
2407 | - |
|
2408 | - public function registerFunction($name, $func) |
|
2409 | - { |
|
2410 | - $this->libFunctions[$name] = $func; |
|
2411 | - } |
|
2412 | - |
|
2413 | - public function unregisterFunction($name) |
|
2414 | - { |
|
2415 | - unset($this->libFunctions[$name]); |
|
2416 | - } |
|
2417 | - |
|
2418 | - public function setVariables($variables) |
|
2419 | - { |
|
2420 | - $this->registeredVars = array_merge($this->registeredVars, $variables); |
|
2421 | - } |
|
2422 | - |
|
2423 | - public function unsetVariable($name) |
|
2424 | - { |
|
2425 | - unset($this->registeredVars[$name]); |
|
2426 | - } |
|
2427 | - |
|
2428 | - public function setImportDir($dirs) |
|
2429 | - { |
|
2430 | - $this->importDir = (array) $dirs; |
|
2431 | - } |
|
2432 | - |
|
2433 | - public function addImportDir($dir) |
|
2434 | - { |
|
2435 | - $this->importDir = (array) $this->importDir; |
|
2436 | - $this->importDir[] = $dir; |
|
2437 | - } |
|
2438 | - |
|
2439 | - public function allParsedFiles() |
|
2440 | - { |
|
2441 | - return $this->allParsedFiles; |
|
2442 | - } |
|
2443 | - |
|
2444 | - public function addParsedFile($file) |
|
2445 | - { |
|
2446 | - $this->allParsedFiles[realpath($file)] = filemtime($file); |
|
2447 | - } |
|
2448 | - |
|
2449 | - /** |
|
2450 | - * Uses the current value of $this->count to show line and line number |
|
2451 | - */ |
|
2452 | - public function throwError($msg = null) |
|
2453 | - { |
|
2454 | - if ($this->sourceLoc >= 0) { |
|
2455 | - $this->sourceParser->throwError($msg, $this->sourceLoc); |
|
2456 | - } |
|
2457 | - throw new exception($msg); |
|
2458 | - } |
|
2459 | - |
|
2460 | - // compile file $in to file $out if $in is newer than $out |
|
2461 | - // returns true when it compiles, false otherwise |
|
2462 | - public static function ccompile($in, $out, $less = null) |
|
2463 | - { |
|
2464 | - if ($less === null) { |
|
2465 | - $less = new self(); |
|
2466 | - } |
|
2467 | - return $less->checkedCompile($in, $out); |
|
2468 | - } |
|
2469 | - |
|
2470 | - public static function cexecute($in, $force = false, $less = null) |
|
2471 | - { |
|
2472 | - if ($less === null) { |
|
2473 | - $less = new self(); |
|
2474 | - } |
|
2475 | - return $less->cachedCompile($in, $force); |
|
2476 | - } |
|
2477 | - |
|
2478 | - protected static $cssColors = array( |
|
2479 | - 'aliceblue' => '240,248,255', |
|
2480 | - 'antiquewhite' => '250,235,215', |
|
2481 | - 'aqua' => '0,255,255', |
|
2482 | - 'aquamarine' => '127,255,212', |
|
2483 | - 'azure' => '240,255,255', |
|
2484 | - 'beige' => '245,245,220', |
|
2485 | - 'bisque' => '255,228,196', |
|
2486 | - 'black' => '0,0,0', |
|
2487 | - 'blanchedalmond' => '255,235,205', |
|
2488 | - 'blue' => '0,0,255', |
|
2489 | - 'blueviolet' => '138,43,226', |
|
2490 | - 'brown' => '165,42,42', |
|
2491 | - 'burlywood' => '222,184,135', |
|
2492 | - 'cadetblue' => '95,158,160', |
|
2493 | - 'chartreuse' => '127,255,0', |
|
2494 | - 'chocolate' => '210,105,30', |
|
2495 | - 'coral' => '255,127,80', |
|
2496 | - 'cornflowerblue' => '100,149,237', |
|
2497 | - 'cornsilk' => '255,248,220', |
|
2498 | - 'crimson' => '220,20,60', |
|
2499 | - 'cyan' => '0,255,255', |
|
2500 | - 'darkblue' => '0,0,139', |
|
2501 | - 'darkcyan' => '0,139,139', |
|
2502 | - 'darkgoldenrod' => '184,134,11', |
|
2503 | - 'darkgray' => '169,169,169', |
|
2504 | - 'darkgreen' => '0,100,0', |
|
2505 | - 'darkgrey' => '169,169,169', |
|
2506 | - 'darkkhaki' => '189,183,107', |
|
2507 | - 'darkmagenta' => '139,0,139', |
|
2508 | - 'darkolivegreen' => '85,107,47', |
|
2509 | - 'darkorange' => '255,140,0', |
|
2510 | - 'darkorchid' => '153,50,204', |
|
2511 | - 'darkred' => '139,0,0', |
|
2512 | - 'darksalmon' => '233,150,122', |
|
2513 | - 'darkseagreen' => '143,188,143', |
|
2514 | - 'darkslateblue' => '72,61,139', |
|
2515 | - 'darkslategray' => '47,79,79', |
|
2516 | - 'darkslategrey' => '47,79,79', |
|
2517 | - 'darkturquoise' => '0,206,209', |
|
2518 | - 'darkviolet' => '148,0,211', |
|
2519 | - 'deeppink' => '255,20,147', |
|
2520 | - 'deepskyblue' => '0,191,255', |
|
2521 | - 'dimgray' => '105,105,105', |
|
2522 | - 'dimgrey' => '105,105,105', |
|
2523 | - 'dodgerblue' => '30,144,255', |
|
2524 | - 'firebrick' => '178,34,34', |
|
2525 | - 'floralwhite' => '255,250,240', |
|
2526 | - 'forestgreen' => '34,139,34', |
|
2527 | - 'fuchsia' => '255,0,255', |
|
2528 | - 'gainsboro' => '220,220,220', |
|
2529 | - 'ghostwhite' => '248,248,255', |
|
2530 | - 'gold' => '255,215,0', |
|
2531 | - 'goldenrod' => '218,165,32', |
|
2532 | - 'gray' => '128,128,128', |
|
2533 | - 'green' => '0,128,0', |
|
2534 | - 'greenyellow' => '173,255,47', |
|
2535 | - 'grey' => '128,128,128', |
|
2536 | - 'honeydew' => '240,255,240', |
|
2537 | - 'hotpink' => '255,105,180', |
|
2538 | - 'indianred' => '205,92,92', |
|
2539 | - 'indigo' => '75,0,130', |
|
2540 | - 'ivory' => '255,255,240', |
|
2541 | - 'khaki' => '240,230,140', |
|
2542 | - 'lavender' => '230,230,250', |
|
2543 | - 'lavenderblush' => '255,240,245', |
|
2544 | - 'lawngreen' => '124,252,0', |
|
2545 | - 'lemonchiffon' => '255,250,205', |
|
2546 | - 'lightblue' => '173,216,230', |
|
2547 | - 'lightcoral' => '240,128,128', |
|
2548 | - 'lightcyan' => '224,255,255', |
|
2549 | - 'lightgoldenrodyellow' => '250,250,210', |
|
2550 | - 'lightgray' => '211,211,211', |
|
2551 | - 'lightgreen' => '144,238,144', |
|
2552 | - 'lightgrey' => '211,211,211', |
|
2553 | - 'lightpink' => '255,182,193', |
|
2554 | - 'lightsalmon' => '255,160,122', |
|
2555 | - 'lightseagreen' => '32,178,170', |
|
2556 | - 'lightskyblue' => '135,206,250', |
|
2557 | - 'lightslategray' => '119,136,153', |
|
2558 | - 'lightslategrey' => '119,136,153', |
|
2559 | - 'lightsteelblue' => '176,196,222', |
|
2560 | - 'lightyellow' => '255,255,224', |
|
2561 | - 'lime' => '0,255,0', |
|
2562 | - 'limegreen' => '50,205,50', |
|
2563 | - 'linen' => '250,240,230', |
|
2564 | - 'magenta' => '255,0,255', |
|
2565 | - 'maroon' => '128,0,0', |
|
2566 | - 'mediumaquamarine' => '102,205,170', |
|
2567 | - 'mediumblue' => '0,0,205', |
|
2568 | - 'mediumorchid' => '186,85,211', |
|
2569 | - 'mediumpurple' => '147,112,219', |
|
2570 | - 'mediumseagreen' => '60,179,113', |
|
2571 | - 'mediumslateblue' => '123,104,238', |
|
2572 | - 'mediumspringgreen' => '0,250,154', |
|
2573 | - 'mediumturquoise' => '72,209,204', |
|
2574 | - 'mediumvioletred' => '199,21,133', |
|
2575 | - 'midnightblue' => '25,25,112', |
|
2576 | - 'mintcream' => '245,255,250', |
|
2577 | - 'mistyrose' => '255,228,225', |
|
2578 | - 'moccasin' => '255,228,181', |
|
2579 | - 'navajowhite' => '255,222,173', |
|
2580 | - 'navy' => '0,0,128', |
|
2581 | - 'oldlace' => '253,245,230', |
|
2582 | - 'olive' => '128,128,0', |
|
2583 | - 'olivedrab' => '107,142,35', |
|
2584 | - 'orange' => '255,165,0', |
|
2585 | - 'orangered' => '255,69,0', |
|
2586 | - 'orchid' => '218,112,214', |
|
2587 | - 'palegoldenrod' => '238,232,170', |
|
2588 | - 'palegreen' => '152,251,152', |
|
2589 | - 'paleturquoise' => '175,238,238', |
|
2590 | - 'palevioletred' => '219,112,147', |
|
2591 | - 'papayawhip' => '255,239,213', |
|
2592 | - 'peachpuff' => '255,218,185', |
|
2593 | - 'peru' => '205,133,63', |
|
2594 | - 'pink' => '255,192,203', |
|
2595 | - 'plum' => '221,160,221', |
|
2596 | - 'powderblue' => '176,224,230', |
|
2597 | - 'purple' => '128,0,128', |
|
2598 | - 'red' => '255,0,0', |
|
2599 | - 'rosybrown' => '188,143,143', |
|
2600 | - 'royalblue' => '65,105,225', |
|
2601 | - 'saddlebrown' => '139,69,19', |
|
2602 | - 'salmon' => '250,128,114', |
|
2603 | - 'sandybrown' => '244,164,96', |
|
2604 | - 'seagreen' => '46,139,87', |
|
2605 | - 'seashell' => '255,245,238', |
|
2606 | - 'sienna' => '160,82,45', |
|
2607 | - 'silver' => '192,192,192', |
|
2608 | - 'skyblue' => '135,206,235', |
|
2609 | - 'slateblue' => '106,90,205', |
|
2610 | - 'slategray' => '112,128,144', |
|
2611 | - 'slategrey' => '112,128,144', |
|
2612 | - 'snow' => '255,250,250', |
|
2613 | - 'springgreen' => '0,255,127', |
|
2614 | - 'steelblue' => '70,130,180', |
|
2615 | - 'tan' => '210,180,140', |
|
2616 | - 'teal' => '0,128,128', |
|
2617 | - 'thistle' => '216,191,216', |
|
2618 | - 'tomato' => '255,99,71', |
|
2619 | - 'transparent' => '0,0,0,0', |
|
2620 | - 'turquoise' => '64,224,208', |
|
2621 | - 'violet' => '238,130,238', |
|
2622 | - 'wheat' => '245,222,179', |
|
2623 | - 'white' => '255,255,255', |
|
2624 | - 'whitesmoke' => '245,245,245', |
|
2625 | - 'yellow' => '255,255,0', |
|
2626 | - 'yellowgreen' => '154,205,50' |
|
2627 | - ); |
|
62 | + public static $VERSION = "v0.8.0"; |
|
63 | + |
|
64 | + public static $TRUE = array("keyword", "true"); |
|
65 | + public static $FALSE = array("keyword", "false"); |
|
66 | + |
|
67 | + protected $libFunctions = array(); |
|
68 | + protected $registeredVars = array(); |
|
69 | + protected $preserveComments = false; |
|
70 | + |
|
71 | + public $vPrefix = '@'; // prefix of abstract properties |
|
72 | + public $mPrefix = '$'; // prefix of abstract blocks |
|
73 | + public $parentSelector = '&'; |
|
74 | + |
|
75 | + public $importDisabled = false; |
|
76 | + public $importDir = ''; |
|
77 | + |
|
78 | + public $scope; |
|
79 | + public $formatter; |
|
80 | + public $formatterName; |
|
81 | + public $parser; |
|
82 | + public $_parseFile; |
|
83 | + public $env; |
|
84 | + public $count; |
|
85 | + |
|
86 | + protected $numberPrecision = null; |
|
87 | + |
|
88 | + protected $allParsedFiles = array(); |
|
89 | + |
|
90 | + // set to the parser that generated the current line when compiling |
|
91 | + // so we know how to create error messages |
|
92 | + protected $sourceParser = null; |
|
93 | + protected $sourceLoc = null; |
|
94 | + |
|
95 | + protected static $nextImportId = 0; // uniquely identify imports |
|
96 | + |
|
97 | + // attempts to find the path of an import url, returns null for css files |
|
98 | + protected function findImport($url) |
|
99 | + { |
|
100 | + foreach ((array) $this->importDir as $dir) { |
|
101 | + $full = $dir.(substr($dir, -1) != '/' ? '/' : '').$url; |
|
102 | + if ($this->fileExists($file = $full.'.less') || $this->fileExists($file = $full)) { |
|
103 | + return $file; |
|
104 | + } |
|
105 | + } |
|
106 | + |
|
107 | + return null; |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * fileExists |
|
112 | + * |
|
113 | + * @param string $name Filename |
|
114 | + * @return boolean |
|
115 | + */ |
|
116 | + protected function fileExists($name) |
|
117 | + { |
|
118 | + return is_file($name); |
|
119 | + } |
|
120 | + |
|
121 | + public static function compressList($items, $delim) |
|
122 | + { |
|
123 | + if (!isset($items[1]) && isset($items[0])) { |
|
124 | + return $items[0]; |
|
125 | + } else { |
|
126 | + return array('list', $delim, $items); |
|
127 | + } |
|
128 | + } |
|
129 | + |
|
130 | + public static function preg_quote($what) |
|
131 | + { |
|
132 | + return preg_quote($what, '/'); |
|
133 | + } |
|
134 | + |
|
135 | + protected function tryImport($importPath, $parentBlock, $out) |
|
136 | + { |
|
137 | + if ($importPath[0] == "function" && $importPath[1] == "url") { |
|
138 | + $importPath = $this->flattenList($importPath[2]); |
|
139 | + } |
|
140 | + |
|
141 | + $str = $this->coerceString($importPath); |
|
142 | + if ($str === null) { |
|
143 | + return false; |
|
144 | + } |
|
145 | + |
|
146 | + $url = $this->compileValue($this->lib_e($str)); |
|
147 | + |
|
148 | + // don't import if it ends in css |
|
149 | + if (substr_compare($url, '.css', -4, 4) === 0) { |
|
150 | + return false; |
|
151 | + } |
|
152 | + |
|
153 | + $realPath = $this->findImport($url); |
|
154 | + |
|
155 | + if ($realPath === null) { |
|
156 | + return false; |
|
157 | + } |
|
158 | + |
|
159 | + if ($this->importDisabled) { |
|
160 | + return array(false, "/* import disabled */"); |
|
161 | + } |
|
162 | + |
|
163 | + if (isset($this->allParsedFiles[realpath($realPath)])) { |
|
164 | + return array(false, null); |
|
165 | + } |
|
166 | + |
|
167 | + $this->addParsedFile($realPath); |
|
168 | + $parser = $this->makeParser($realPath); |
|
169 | + $root = $parser->parse(file_get_contents($realPath)); |
|
170 | + |
|
171 | + // set the parents of all the block props |
|
172 | + foreach ($root->props as $prop) { |
|
173 | + if ($prop[0] == "block") { |
|
174 | + $prop[1]->parent = $parentBlock; |
|
175 | + } |
|
176 | + } |
|
177 | + |
|
178 | + // copy mixins into scope, set their parents |
|
179 | + // bring blocks from import into current block |
|
180 | + // TODO: need to mark the source parser these came from this file |
|
181 | + foreach ($root->children as $childName => $child) { |
|
182 | + if (isset($parentBlock->children[$childName])) { |
|
183 | + $parentBlock->children[$childName] = array_merge( |
|
184 | + $parentBlock->children[$childName], |
|
185 | + $child |
|
186 | + ); |
|
187 | + } else { |
|
188 | + $parentBlock->children[$childName] = $child; |
|
189 | + } |
|
190 | + } |
|
191 | + |
|
192 | + $pi = pathinfo($realPath); |
|
193 | + $dir = $pi["dirname"]; |
|
194 | + |
|
195 | + list($top, $bottom) = $this->sortProps($root->props, true); |
|
196 | + $this->compileImportedProps($top, $parentBlock, $out, $parser, $dir); |
|
197 | + |
|
198 | + return array(true, $bottom, $parser, $dir); |
|
199 | + } |
|
200 | + |
|
201 | + protected function compileImportedProps($props, $block, $out, $sourceParser, $importDir) |
|
202 | + { |
|
203 | + $oldSourceParser = $this->sourceParser; |
|
204 | + |
|
205 | + $oldImport = $this->importDir; |
|
206 | + |
|
207 | + // TODO: this is because the importDir api is stupid |
|
208 | + $this->importDir = (array) $this->importDir; |
|
209 | + array_unshift($this->importDir, $importDir); |
|
210 | + |
|
211 | + foreach ($props as $prop) { |
|
212 | + $this->compileProp($prop, $block, $out); |
|
213 | + } |
|
214 | + |
|
215 | + $this->importDir = $oldImport; |
|
216 | + $this->sourceParser = $oldSourceParser; |
|
217 | + } |
|
218 | + |
|
219 | + /** |
|
220 | + * Recursively compiles a block. |
|
221 | + * |
|
222 | + * A block is analogous to a CSS block in most cases. A single LESS document |
|
223 | + * is encapsulated in a block when parsed, but it does not have parent tags |
|
224 | + * so all of it's children appear on the root level when compiled. |
|
225 | + * |
|
226 | + * Blocks are made up of props and children. |
|
227 | + * |
|
228 | + * Props are property instructions, array tuples which describe an action |
|
229 | + * to be taken, eg. write a property, set a variable, mixin a block. |
|
230 | + * |
|
231 | + * The children of a block are just all the blocks that are defined within. |
|
232 | + * This is used to look up mixins when performing a mixin. |
|
233 | + * |
|
234 | + * Compiling the block involves pushing a fresh environment on the stack, |
|
235 | + * and iterating through the props, compiling each one. |
|
236 | + * |
|
237 | + * See Lessc::compileProp() |
|
238 | + * |
|
239 | + */ |
|
240 | + protected function compileBlock($block) |
|
241 | + { |
|
242 | + switch ($block->type) { |
|
243 | + case "root": |
|
244 | + $this->compileRoot($block); |
|
245 | + break; |
|
246 | + case null: |
|
247 | + $this->compileCSSBlock($block); |
|
248 | + break; |
|
249 | + case "media": |
|
250 | + $this->compileMedia($block); |
|
251 | + break; |
|
252 | + case "directive": |
|
253 | + $name = "@".$block->name; |
|
254 | + if (!empty($block->value)) { |
|
255 | + $name .= " ".$this->compileValue($this->reduce($block->value)); |
|
256 | + } |
|
257 | + |
|
258 | + $this->compileNestedBlock($block, array($name)); |
|
259 | + break; |
|
260 | + default: |
|
261 | + $this->throwError("unknown block type: $block->type\n"); |
|
262 | + } |
|
263 | + } |
|
264 | + |
|
265 | + protected function compileCSSBlock($block) |
|
266 | + { |
|
267 | + $env = $this->pushEnv(); |
|
268 | + |
|
269 | + $selectors = $this->compileSelectors($block->tags); |
|
270 | + $env->selectors = $this->multiplySelectors($selectors); |
|
271 | + $out = $this->makeOutputBlock(null, $env->selectors); |
|
272 | + |
|
273 | + $this->scope->children[] = $out; |
|
274 | + $this->compileProps($block, $out); |
|
275 | + |
|
276 | + $block->scope = $env; // mixins carry scope with them! |
|
277 | + $this->popEnv(); |
|
278 | + } |
|
279 | + |
|
280 | + protected function compileMedia($media) |
|
281 | + { |
|
282 | + $env = $this->pushEnv($media); |
|
283 | + $parentScope = $this->mediaParent($this->scope); |
|
284 | + |
|
285 | + $query = $this->compileMediaQuery($this->multiplyMedia($env)); |
|
286 | + |
|
287 | + $this->scope = $this->makeOutputBlock($media->type, array($query)); |
|
288 | + $parentScope->children[] = $this->scope; |
|
289 | + |
|
290 | + $this->compileProps($media, $this->scope); |
|
291 | + |
|
292 | + if (count($this->scope->lines) > 0) { |
|
293 | + $orphanSelelectors = $this->findClosestSelectors(); |
|
294 | + if (!is_null($orphanSelelectors)) { |
|
295 | + $orphan = $this->makeOutputBlock(null, $orphanSelelectors); |
|
296 | + $orphan->lines = $this->scope->lines; |
|
297 | + array_unshift($this->scope->children, $orphan); |
|
298 | + $this->scope->lines = array(); |
|
299 | + } |
|
300 | + } |
|
301 | + |
|
302 | + $this->scope = $this->scope->parent; |
|
303 | + $this->popEnv(); |
|
304 | + } |
|
305 | + |
|
306 | + protected function mediaParent($scope) |
|
307 | + { |
|
308 | + while (!empty($scope->parent)) { |
|
309 | + if (!empty($scope->type) && $scope->type != "media") { |
|
310 | + break; |
|
311 | + } |
|
312 | + $scope = $scope->parent; |
|
313 | + } |
|
314 | + |
|
315 | + return $scope; |
|
316 | + } |
|
317 | + |
|
318 | + protected function compileNestedBlock($block, $selectors) |
|
319 | + { |
|
320 | + $this->pushEnv($block); |
|
321 | + $this->scope = $this->makeOutputBlock($block->type, $selectors); |
|
322 | + $this->scope->parent->children[] = $this->scope; |
|
323 | + |
|
324 | + $this->compileProps($block, $this->scope); |
|
325 | + |
|
326 | + $this->scope = $this->scope->parent; |
|
327 | + $this->popEnv(); |
|
328 | + } |
|
329 | + |
|
330 | + protected function compileRoot($root) |
|
331 | + { |
|
332 | + $this->pushEnv(); |
|
333 | + $this->scope = $this->makeOutputBlock($root->type); |
|
334 | + $this->compileProps($root, $this->scope); |
|
335 | + $this->popEnv(); |
|
336 | + } |
|
337 | + |
|
338 | + protected function compileProps($block, $out) |
|
339 | + { |
|
340 | + foreach ($this->sortProps($block->props) as $prop) { |
|
341 | + $this->compileProp($prop, $block, $out); |
|
342 | + } |
|
343 | + $out->lines = $this->deduplicate($out->lines); |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * Deduplicate lines in a block. Comments are not deduplicated. If a |
|
348 | + * duplicate rule is detected, the comments immediately preceding each |
|
349 | + * occurence are consolidated. |
|
350 | + */ |
|
351 | + protected function deduplicate($lines) |
|
352 | + { |
|
353 | + $unique = array(); |
|
354 | + $comments = array(); |
|
355 | + |
|
356 | + foreach ($lines as $line) { |
|
357 | + if (strpos($line, '/*') === 0) { |
|
358 | + $comments[] = $line; |
|
359 | + continue; |
|
360 | + } |
|
361 | + if (!in_array($line, $unique)) { |
|
362 | + $unique[] = $line; |
|
363 | + } |
|
364 | + array_splice($unique, array_search($line, $unique), 0, $comments); |
|
365 | + $comments = array(); |
|
366 | + } |
|
367 | + return array_merge($unique, $comments); |
|
368 | + } |
|
369 | + |
|
370 | + protected function sortProps($props, $split = false) |
|
371 | + { |
|
372 | + $vars = array(); |
|
373 | + $imports = array(); |
|
374 | + $other = array(); |
|
375 | + $stack = array(); |
|
376 | + |
|
377 | + foreach ($props as $prop) { |
|
378 | + switch ($prop[0]) { |
|
379 | + case "comment": |
|
380 | + $stack[] = $prop; |
|
381 | + break; |
|
382 | + case "assign": |
|
383 | + $stack[] = $prop; |
|
384 | + if (isset($prop[1][0]) && $prop[1][0] == $this->vPrefix) { |
|
385 | + $vars = array_merge($vars, $stack); |
|
386 | + } else { |
|
387 | + $other = array_merge($other, $stack); |
|
388 | + } |
|
389 | + $stack = array(); |
|
390 | + break; |
|
391 | + case "import": |
|
392 | + $id = self::$nextImportId++; |
|
393 | + $prop[] = $id; |
|
394 | + $stack[] = $prop; |
|
395 | + $imports = array_merge($imports, $stack); |
|
396 | + $other[] = array("import_mixin", $id); |
|
397 | + $stack = array(); |
|
398 | + break; |
|
399 | + default: |
|
400 | + $stack[] = $prop; |
|
401 | + $other = array_merge($other, $stack); |
|
402 | + $stack = array(); |
|
403 | + break; |
|
404 | + } |
|
405 | + } |
|
406 | + $other = array_merge($other, $stack); |
|
407 | + |
|
408 | + if ($split) { |
|
409 | + return array(array_merge($imports, $vars), $other); |
|
410 | + } else { |
|
411 | + return array_merge($imports, $vars, $other); |
|
412 | + } |
|
413 | + } |
|
414 | + |
|
415 | + protected function compileMediaQuery($queries) |
|
416 | + { |
|
417 | + $compiledQueries = array(); |
|
418 | + foreach ($queries as $query) { |
|
419 | + $parts = array(); |
|
420 | + foreach ($query as $q) { |
|
421 | + switch ($q[0]) { |
|
422 | + case "mediaType": |
|
423 | + $parts[] = implode(" ", array_slice($q, 1)); |
|
424 | + break; |
|
425 | + case "mediaExp": |
|
426 | + if (isset($q[2])) { |
|
427 | + $parts[] = "($q[1]: ". |
|
428 | + $this->compileValue($this->reduce($q[2])).")"; |
|
429 | + } else { |
|
430 | + $parts[] = "($q[1])"; |
|
431 | + } |
|
432 | + break; |
|
433 | + case "variable": |
|
434 | + $parts[] = $this->compileValue($this->reduce($q)); |
|
435 | + break; |
|
436 | + } |
|
437 | + } |
|
438 | + |
|
439 | + if (count($parts) > 0) { |
|
440 | + $compiledQueries[] = implode(" and ", $parts); |
|
441 | + } |
|
442 | + } |
|
443 | + |
|
444 | + $out = "@media"; |
|
445 | + if (!empty($parts)) { |
|
446 | + $out .= " ". |
|
447 | + implode($this->formatter->selectorSeparator, $compiledQueries); |
|
448 | + } |
|
449 | + return $out; |
|
450 | + } |
|
451 | + |
|
452 | + protected function multiplyMedia($env, $childQueries = null) |
|
453 | + { |
|
454 | + if (is_null($env) || |
|
455 | + !empty($env->block->type) && $env->block->type != "media" |
|
456 | + ) { |
|
457 | + return $childQueries; |
|
458 | + } |
|
459 | + |
|
460 | + // plain old block, skip |
|
461 | + if (empty($env->block->type)) { |
|
462 | + return $this->multiplyMedia($env->parent, $childQueries); |
|
463 | + } |
|
464 | + |
|
465 | + $out = array(); |
|
466 | + $queries = $env->block->queries; |
|
467 | + if (is_null($childQueries)) { |
|
468 | + $out = $queries; |
|
469 | + } else { |
|
470 | + foreach ($queries as $parent) { |
|
471 | + foreach ($childQueries as $child) { |
|
472 | + $out[] = array_merge($parent, $child); |
|
473 | + } |
|
474 | + } |
|
475 | + } |
|
476 | + |
|
477 | + return $this->multiplyMedia($env->parent, $out); |
|
478 | + } |
|
479 | + |
|
480 | + protected function expandParentSelectors(&$tag, $replace) |
|
481 | + { |
|
482 | + $parts = explode("$&$", $tag); |
|
483 | + $count = 0; |
|
484 | + foreach ($parts as &$part) { |
|
485 | + $c = 0; |
|
486 | + $part = str_replace($this->parentSelector, $replace, $part, $c); |
|
487 | + $count += $c; |
|
488 | + } |
|
489 | + $tag = implode($this->parentSelector, $parts); |
|
490 | + return $count; |
|
491 | + } |
|
492 | + |
|
493 | + protected function findClosestSelectors() |
|
494 | + { |
|
495 | + $env = $this->env; |
|
496 | + $selectors = null; |
|
497 | + while ($env !== null) { |
|
498 | + if (isset($env->selectors)) { |
|
499 | + $selectors = $env->selectors; |
|
500 | + break; |
|
501 | + } |
|
502 | + $env = $env->parent; |
|
503 | + } |
|
504 | + |
|
505 | + return $selectors; |
|
506 | + } |
|
507 | + |
|
508 | + |
|
509 | + // multiply $selectors against the nearest selectors in env |
|
510 | + protected function multiplySelectors($selectors) |
|
511 | + { |
|
512 | + // find parent selectors |
|
513 | + |
|
514 | + $parentSelectors = $this->findClosestSelectors(); |
|
515 | + if (is_null($parentSelectors)) { |
|
516 | + // kill parent reference in top level selector |
|
517 | + foreach ($selectors as &$s) { |
|
518 | + $this->expandParentSelectors($s, ""); |
|
519 | + } |
|
520 | + |
|
521 | + return $selectors; |
|
522 | + } |
|
523 | + |
|
524 | + $out = array(); |
|
525 | + foreach ($parentSelectors as $parent) { |
|
526 | + foreach ($selectors as $child) { |
|
527 | + $count = $this->expandParentSelectors($child, $parent); |
|
528 | + |
|
529 | + // don't prepend the parent tag if & was used |
|
530 | + if ($count > 0) { |
|
531 | + $out[] = trim($child); |
|
532 | + } else { |
|
533 | + $out[] = trim($parent.' '.$child); |
|
534 | + } |
|
535 | + } |
|
536 | + } |
|
537 | + |
|
538 | + return $out; |
|
539 | + } |
|
540 | + |
|
541 | + // reduces selector expressions |
|
542 | + protected function compileSelectors($selectors) |
|
543 | + { |
|
544 | + $out = array(); |
|
545 | + |
|
546 | + foreach ($selectors as $s) { |
|
547 | + if (is_array($s)) { |
|
548 | + list(, $value) = $s; |
|
549 | + $out[] = trim($this->compileValue($this->reduce($value))); |
|
550 | + } else { |
|
551 | + $out[] = $s; |
|
552 | + } |
|
553 | + } |
|
554 | + |
|
555 | + return $out; |
|
556 | + } |
|
557 | + |
|
558 | + protected function eq($left, $right) |
|
559 | + { |
|
560 | + return $left == $right; |
|
561 | + } |
|
562 | + |
|
563 | + protected function patternMatch($block, $orderedArgs, $keywordArgs) |
|
564 | + { |
|
565 | + // match the guards if it has them |
|
566 | + // any one of the groups must have all its guards pass for a match |
|
567 | + if (!empty($block->guards)) { |
|
568 | + $groupPassed = false; |
|
569 | + foreach ($block->guards as $guardGroup) { |
|
570 | + foreach ($guardGroup as $guard) { |
|
571 | + $this->pushEnv(); |
|
572 | + $this->zipSetArgs($block->args, $orderedArgs, $keywordArgs); |
|
573 | + |
|
574 | + $negate = false; |
|
575 | + if ($guard[0] == "negate") { |
|
576 | + $guard = $guard[1]; |
|
577 | + $negate = true; |
|
578 | + } |
|
579 | + |
|
580 | + $passed = $this->reduce($guard) == self::$TRUE; |
|
581 | + if ($negate) { |
|
582 | + $passed = !$passed; |
|
583 | + } |
|
584 | + |
|
585 | + $this->popEnv(); |
|
586 | + |
|
587 | + if ($passed) { |
|
588 | + $groupPassed = true; |
|
589 | + } else { |
|
590 | + $groupPassed = false; |
|
591 | + break; |
|
592 | + } |
|
593 | + } |
|
594 | + |
|
595 | + if ($groupPassed) { |
|
596 | + break; |
|
597 | + } |
|
598 | + } |
|
599 | + |
|
600 | + if (!$groupPassed) { |
|
601 | + return false; |
|
602 | + } |
|
603 | + } |
|
604 | + |
|
605 | + if (empty($block->args)) { |
|
606 | + return $block->isVararg || empty($orderedArgs) && empty($keywordArgs); |
|
607 | + } |
|
608 | + |
|
609 | + $remainingArgs = $block->args; |
|
610 | + if ($keywordArgs) { |
|
611 | + $remainingArgs = array(); |
|
612 | + foreach ($block->args as $arg) { |
|
613 | + if ($arg[0] == "arg" && isset($keywordArgs[$arg[1]])) { |
|
614 | + continue; |
|
615 | + } |
|
616 | + |
|
617 | + $remainingArgs[] = $arg; |
|
618 | + } |
|
619 | + } |
|
620 | + |
|
621 | + $i = -1; // no args |
|
622 | + // try to match by arity or by argument literal |
|
623 | + foreach ($remainingArgs as $i => $arg) { |
|
624 | + switch ($arg[0]) { |
|
625 | + case "lit": |
|
626 | + if (empty($orderedArgs[$i]) || !$this->eq($arg[1], $orderedArgs[$i])) { |
|
627 | + return false; |
|
628 | + } |
|
629 | + break; |
|
630 | + case "arg": |
|
631 | + // no arg and no default value |
|
632 | + if (!isset($orderedArgs[$i]) && !isset($arg[2])) { |
|
633 | + return false; |
|
634 | + } |
|
635 | + break; |
|
636 | + case "rest": |
|
637 | + $i--; // rest can be empty |
|
638 | + break 2; |
|
639 | + } |
|
640 | + } |
|
641 | + |
|
642 | + if ($block->isVararg) { |
|
643 | + return true; // not having enough is handled above |
|
644 | + } else { |
|
645 | + $numMatched = $i + 1; |
|
646 | + // greater than because default values always match |
|
647 | + return $numMatched >= count($orderedArgs); |
|
648 | + } |
|
649 | + } |
|
650 | + |
|
651 | + protected function patternMatchAll($blocks, $orderedArgs, $keywordArgs, $skip = array()) |
|
652 | + { |
|
653 | + $matches = null; |
|
654 | + foreach ($blocks as $block) { |
|
655 | + // skip seen blocks that don't have arguments |
|
656 | + if (isset($skip[$block->id]) && !isset($block->args)) { |
|
657 | + continue; |
|
658 | + } |
|
659 | + |
|
660 | + if ($this->patternMatch($block, $orderedArgs, $keywordArgs)) { |
|
661 | + $matches[] = $block; |
|
662 | + } |
|
663 | + } |
|
664 | + |
|
665 | + return $matches; |
|
666 | + } |
|
667 | + |
|
668 | + // attempt to find blocks matched by path and args |
|
669 | + protected function findBlocks($searchIn, $path, $orderedArgs, $keywordArgs, $seen = array()) |
|
670 | + { |
|
671 | + if ($searchIn == null) { |
|
672 | + return null; |
|
673 | + } |
|
674 | + if (isset($seen[$searchIn->id])) { |
|
675 | + return null; |
|
676 | + } |
|
677 | + $seen[$searchIn->id] = true; |
|
678 | + |
|
679 | + $name = $path[0]; |
|
680 | + |
|
681 | + if (isset($searchIn->children[$name])) { |
|
682 | + $blocks = $searchIn->children[$name]; |
|
683 | + if (count($path) == 1) { |
|
684 | + $matches = $this->patternMatchAll($blocks, $orderedArgs, $keywordArgs, $seen); |
|
685 | + if (!empty($matches)) { |
|
686 | + // This will return all blocks that match in the closest |
|
687 | + // scope that has any matching block, like lessjs |
|
688 | + return $matches; |
|
689 | + } |
|
690 | + } else { |
|
691 | + $matches = array(); |
|
692 | + foreach ($blocks as $subBlock) { |
|
693 | + $subMatches = $this->findBlocks( |
|
694 | + $subBlock, |
|
695 | + array_slice($path, 1), |
|
696 | + $orderedArgs, |
|
697 | + $keywordArgs, |
|
698 | + $seen |
|
699 | + ); |
|
700 | + |
|
701 | + if (!is_null($subMatches)) { |
|
702 | + foreach ($subMatches as $sm) { |
|
703 | + $matches[] = $sm; |
|
704 | + } |
|
705 | + } |
|
706 | + } |
|
707 | + |
|
708 | + return count($matches) > 0 ? $matches : null; |
|
709 | + } |
|
710 | + } |
|
711 | + if ($searchIn->parent === $searchIn) { |
|
712 | + return null; |
|
713 | + } |
|
714 | + return $this->findBlocks($searchIn->parent, $path, $orderedArgs, $keywordArgs, $seen); |
|
715 | + } |
|
716 | + |
|
717 | + // sets all argument names in $args to either the default value |
|
718 | + // or the one passed in through $values |
|
719 | + protected function zipSetArgs($args, $orderedValues, $keywordValues) |
|
720 | + { |
|
721 | + $assignedValues = array(); |
|
722 | + |
|
723 | + $i = 0; |
|
724 | + foreach ($args as $a) { |
|
725 | + if ($a[0] == "arg") { |
|
726 | + if (isset($keywordValues[$a[1]])) { |
|
727 | + // has keyword arg |
|
728 | + $value = $keywordValues[$a[1]]; |
|
729 | + } elseif (isset($orderedValues[$i])) { |
|
730 | + // has ordered arg |
|
731 | + $value = $orderedValues[$i]; |
|
732 | + $i++; |
|
733 | + } elseif (isset($a[2])) { |
|
734 | + // has default value |
|
735 | + $value = $a[2]; |
|
736 | + } else { |
|
737 | + $value = null; // :( |
|
738 | + $this->throwError("Failed to assign arg ".$a[1]); // This ends function by throwing an exception |
|
739 | + } |
|
740 | + |
|
741 | + $value = $this->reduce($value); |
|
742 | + $this->set($a[1], $value); |
|
743 | + $assignedValues[] = $value; |
|
744 | + } else { |
|
745 | + // a lit |
|
746 | + $i++; |
|
747 | + } |
|
748 | + } |
|
749 | + |
|
750 | + // check for a rest |
|
751 | + $last = end($args); |
|
752 | + if ($last && $last[0] == "rest") { |
|
753 | + $rest = array_slice($orderedValues, count($args) - 1); |
|
754 | + $this->set($last[1], $this->reduce(array("list", " ", $rest))); |
|
755 | + } |
|
756 | + |
|
757 | + // wow is this the only true use of PHP's + operator for arrays? |
|
758 | + $this->env->arguments = $assignedValues + $orderedValues; |
|
759 | + } |
|
760 | + |
|
761 | + // compile a prop and update $lines or $blocks appropriately |
|
762 | + protected function compileProp($prop, $block, $out) |
|
763 | + { |
|
764 | + // set error position context |
|
765 | + $this->sourceLoc = isset($prop[-1]) ? $prop[-1] : -1; |
|
766 | + |
|
767 | + switch ($prop[0]) { |
|
768 | + case 'assign': |
|
769 | + list(, $name, $value) = $prop; |
|
770 | + if ($name[0] == $this->vPrefix) { |
|
771 | + $this->set($name, $value); |
|
772 | + } else { |
|
773 | + $out->lines[] = $this->formatter->property( |
|
774 | + $name, |
|
775 | + $this->compileValue($this->reduce($value)) |
|
776 | + ); |
|
777 | + } |
|
778 | + break; |
|
779 | + case 'block': |
|
780 | + list(, $child) = $prop; |
|
781 | + $this->compileBlock($child); |
|
782 | + break; |
|
783 | + case 'mixin': |
|
784 | + list(, $path, $args, $suffix) = $prop; |
|
785 | + |
|
786 | + $orderedArgs = array(); |
|
787 | + $keywordArgs = array(); |
|
788 | + foreach ((array) $args as $arg) { |
|
789 | + $argval = null; |
|
790 | + switch ($arg[0]) { |
|
791 | + case "arg": |
|
792 | + if (!isset($arg[2])) { |
|
793 | + $orderedArgs[] = $this->reduce(array("variable", $arg[1])); |
|
794 | + } else { |
|
795 | + $keywordArgs[$arg[1]] = $this->reduce($arg[2]); |
|
796 | + } |
|
797 | + break; |
|
798 | + |
|
799 | + case "lit": |
|
800 | + $orderedArgs[] = $this->reduce($arg[1]); |
|
801 | + break; |
|
802 | + default: |
|
803 | + $this->throwError("Unknown arg type: ".$arg[0]); |
|
804 | + } |
|
805 | + } |
|
806 | + |
|
807 | + $mixins = $this->findBlocks($block, $path, $orderedArgs, $keywordArgs); |
|
808 | + |
|
809 | + if ($mixins === null) { |
|
810 | + $this->throwError("{$prop[1][0]} is undefined"); |
|
811 | + } |
|
812 | + |
|
813 | + foreach ($mixins as $mixin) { |
|
814 | + if ($mixin === $block && !$orderedArgs) { |
|
815 | + continue; |
|
816 | + } |
|
817 | + |
|
818 | + $haveScope = false; |
|
819 | + if (isset($mixin->parent->scope)) { |
|
820 | + $haveScope = true; |
|
821 | + $mixinParentEnv = $this->pushEnv(); |
|
822 | + $mixinParentEnv->storeParent = $mixin->parent->scope; |
|
823 | + } |
|
824 | + |
|
825 | + $haveArgs = false; |
|
826 | + if (isset($mixin->args)) { |
|
827 | + $haveArgs = true; |
|
828 | + $this->pushEnv(); |
|
829 | + $this->zipSetArgs($mixin->args, $orderedArgs, $keywordArgs); |
|
830 | + } |
|
831 | + |
|
832 | + $oldParent = $mixin->parent; |
|
833 | + if ($mixin != $block) { |
|
834 | + $mixin->parent = $block; |
|
835 | + } |
|
836 | + |
|
837 | + foreach ($this->sortProps($mixin->props) as $subProp) { |
|
838 | + if ($suffix !== null && |
|
839 | + $subProp[0] == "assign" && |
|
840 | + is_string($subProp[1]) && |
|
841 | + $subProp[1][0] != $this->vPrefix |
|
842 | + ) { |
|
843 | + $subProp[2] = array( |
|
844 | + 'list', ' ', |
|
845 | + array($subProp[2], array('keyword', $suffix)) |
|
846 | + ); |
|
847 | + } |
|
848 | + |
|
849 | + $this->compileProp($subProp, $mixin, $out); |
|
850 | + } |
|
851 | + |
|
852 | + $mixin->parent = $oldParent; |
|
853 | + |
|
854 | + if ($haveArgs) { |
|
855 | + $this->popEnv(); |
|
856 | + } |
|
857 | + if ($haveScope) { |
|
858 | + $this->popEnv(); |
|
859 | + } |
|
860 | + } |
|
861 | + |
|
862 | + break; |
|
863 | + case 'raw': |
|
864 | + $out->lines[] = $prop[1]; |
|
865 | + break; |
|
866 | + case "directive": |
|
867 | + list(, $name, $value) = $prop; |
|
868 | + $out->lines[] = "@$name ".$this->compileValue($this->reduce($value)).';'; |
|
869 | + break; |
|
870 | + case "comment": |
|
871 | + $out->lines[] = $prop[1]; |
|
872 | + break; |
|
873 | + case "import": |
|
874 | + list(, $importPath, $importId) = $prop; |
|
875 | + $importPath = $this->reduce($importPath); |
|
876 | + |
|
877 | + if (!isset($this->env->imports)) { |
|
878 | + $this->env->imports = array(); |
|
879 | + } |
|
880 | + |
|
881 | + $result = $this->tryImport($importPath, $block, $out); |
|
882 | + |
|
883 | + $this->env->imports[$importId] = $result === false ? |
|
884 | + array(false, "@import ".$this->compileValue($importPath).";") : $result; |
|
885 | + |
|
886 | + break; |
|
887 | + case "import_mixin": |
|
888 | + list(, $importId) = $prop; |
|
889 | + $import = $this->env->imports[$importId]; |
|
890 | + if ($import[0] === false) { |
|
891 | + if (isset($import[1])) { |
|
892 | + $out->lines[] = $import[1]; |
|
893 | + } |
|
894 | + } else { |
|
895 | + list(, $bottom, $parser, $importDir) = $import; |
|
896 | + $this->compileImportedProps($bottom, $block, $out, $parser, $importDir); |
|
897 | + } |
|
898 | + |
|
899 | + break; |
|
900 | + default: |
|
901 | + $this->throwError("unknown op: {$prop[0]}\n"); |
|
902 | + } |
|
903 | + } |
|
904 | + |
|
905 | + |
|
906 | + /** |
|
907 | + * Compiles a primitive value into a CSS property value. |
|
908 | + * |
|
909 | + * Values in lessphp are typed by being wrapped in arrays, their format is |
|
910 | + * typically: |
|
911 | + * |
|
912 | + * array(type, contents [, additional_contents]*) |
|
913 | + * |
|
914 | + * The input is expected to be reduced. This function will not work on |
|
915 | + * things like expressions and variables. |
|
916 | + */ |
|
917 | + public function compileValue($value) |
|
918 | + { |
|
919 | + switch ($value[0]) { |
|
920 | + case 'list': |
|
921 | + // [1] - delimiter |
|
922 | + // [2] - array of values |
|
923 | + return implode($value[1], array_map(array($this, 'compileValue'), $value[2])); |
|
924 | + case 'raw_color': |
|
925 | + if (!empty($this->formatter->compressColors)) { |
|
926 | + return $this->compileValue($this->coerceColor($value)); |
|
927 | + } |
|
928 | + return $value[1]; |
|
929 | + case 'keyword': |
|
930 | + // [1] - the keyword |
|
931 | + return $value[1]; |
|
932 | + case 'number': |
|
933 | + list(, $num, $unit) = $value; |
|
934 | + // [1] - the number |
|
935 | + // [2] - the unit |
|
936 | + if ($this->numberPrecision !== null) { |
|
937 | + $num = round($num, $this->numberPrecision); |
|
938 | + } |
|
939 | + return $num.$unit; |
|
940 | + case 'string': |
|
941 | + // [1] - contents of string (includes quotes) |
|
942 | + list(, $delim, $content) = $value; |
|
943 | + foreach ($content as &$part) { |
|
944 | + if (is_array($part)) { |
|
945 | + $part = $this->compileValue($part); |
|
946 | + } |
|
947 | + } |
|
948 | + return $delim.implode($content).$delim; |
|
949 | + case 'color': |
|
950 | + // [1] - red component (either number or a %) |
|
951 | + // [2] - green component |
|
952 | + // [3] - blue component |
|
953 | + // [4] - optional alpha component |
|
954 | + list(, $r, $g, $b) = $value; |
|
955 | + $r = round($r); |
|
956 | + $g = round($g); |
|
957 | + $b = round($b); |
|
958 | + |
|
959 | + if (count($value) == 5 && $value[4] != 1) { // rgba |
|
960 | + return 'rgba('.$r.','.$g.','.$b.','.$value[4].')'; |
|
961 | + } |
|
962 | + |
|
963 | + $h = sprintf("#%02x%02x%02x", $r, $g, $b); |
|
964 | + |
|
965 | + if (!empty($this->formatter->compressColors)) { |
|
966 | + // Converting hex color to short notation (e.g. #003399 to #039) |
|
967 | + if ($h[1] === $h[2] && $h[3] === $h[4] && $h[5] === $h[6]) { |
|
968 | + $h = '#'.$h[1].$h[3].$h[5]; |
|
969 | + } |
|
970 | + } |
|
971 | + |
|
972 | + return $h; |
|
973 | + |
|
974 | + case 'function': |
|
975 | + list(, $name, $args) = $value; |
|
976 | + return $name.'('.$this->compileValue($args).')'; |
|
977 | + default: // assumed to be unit |
|
978 | + $this->throwError("unknown value type: $value[0]"); |
|
979 | + } |
|
980 | + } |
|
981 | + |
|
982 | + protected function lib_pow($args) |
|
983 | + { |
|
984 | + list($base, $exp) = $this->assertArgs($args, 2, "pow"); |
|
985 | + return pow($this->assertNumber($base), $this->assertNumber($exp)); |
|
986 | + } |
|
987 | + |
|
988 | + protected function lib_pi() |
|
989 | + { |
|
990 | + return pi(); |
|
991 | + } |
|
992 | + |
|
993 | + protected function lib_mod($args) |
|
994 | + { |
|
995 | + list($a, $b) = $this->assertArgs($args, 2, "mod"); |
|
996 | + return $this->assertNumber($a) % $this->assertNumber($b); |
|
997 | + } |
|
998 | + |
|
999 | + protected function lib_tan($num) |
|
1000 | + { |
|
1001 | + return tan($this->assertNumber($num)); |
|
1002 | + } |
|
1003 | + |
|
1004 | + protected function lib_sin($num) |
|
1005 | + { |
|
1006 | + return sin($this->assertNumber($num)); |
|
1007 | + } |
|
1008 | + |
|
1009 | + protected function lib_cos($num) |
|
1010 | + { |
|
1011 | + return cos($this->assertNumber($num)); |
|
1012 | + } |
|
1013 | + |
|
1014 | + protected function lib_atan($num) |
|
1015 | + { |
|
1016 | + $num = atan($this->assertNumber($num)); |
|
1017 | + return array("number", $num, "rad"); |
|
1018 | + } |
|
1019 | + |
|
1020 | + protected function lib_asin($num) |
|
1021 | + { |
|
1022 | + $num = asin($this->assertNumber($num)); |
|
1023 | + return array("number", $num, "rad"); |
|
1024 | + } |
|
1025 | + |
|
1026 | + protected function lib_acos($num) |
|
1027 | + { |
|
1028 | + $num = acos($this->assertNumber($num)); |
|
1029 | + return array("number", $num, "rad"); |
|
1030 | + } |
|
1031 | + |
|
1032 | + protected function lib_sqrt($num) |
|
1033 | + { |
|
1034 | + return sqrt($this->assertNumber($num)); |
|
1035 | + } |
|
1036 | + |
|
1037 | + protected function lib_extract($value) |
|
1038 | + { |
|
1039 | + list($list, $idx) = $this->assertArgs($value, 2, "extract"); |
|
1040 | + $idx = $this->assertNumber($idx); |
|
1041 | + // 1 indexed |
|
1042 | + if ($list[0] == "list" && isset($list[2][$idx - 1])) { |
|
1043 | + return $list[2][$idx - 1]; |
|
1044 | + } |
|
1045 | + return ''; |
|
1046 | + } |
|
1047 | + |
|
1048 | + protected function lib_isnumber($value) |
|
1049 | + { |
|
1050 | + return $this->toBool($value[0] == "number"); |
|
1051 | + } |
|
1052 | + |
|
1053 | + protected function lib_isstring($value) |
|
1054 | + { |
|
1055 | + return $this->toBool($value[0] == "string"); |
|
1056 | + } |
|
1057 | + |
|
1058 | + protected function lib_iscolor($value) |
|
1059 | + { |
|
1060 | + return $this->toBool($this->coerceColor($value)); |
|
1061 | + } |
|
1062 | + |
|
1063 | + protected function lib_iskeyword($value) |
|
1064 | + { |
|
1065 | + return $this->toBool($value[0] == "keyword"); |
|
1066 | + } |
|
1067 | + |
|
1068 | + protected function lib_ispixel($value) |
|
1069 | + { |
|
1070 | + return $this->toBool($value[0] == "number" && $value[2] == "px"); |
|
1071 | + } |
|
1072 | + |
|
1073 | + protected function lib_ispercentage($value) |
|
1074 | + { |
|
1075 | + return $this->toBool($value[0] == "number" && $value[2] == "%"); |
|
1076 | + } |
|
1077 | + |
|
1078 | + protected function lib_isem($value) |
|
1079 | + { |
|
1080 | + return $this->toBool($value[0] == "number" && $value[2] == "em"); |
|
1081 | + } |
|
1082 | + |
|
1083 | + protected function lib_isrem($value) |
|
1084 | + { |
|
1085 | + return $this->toBool($value[0] == "number" && $value[2] == "rem"); |
|
1086 | + } |
|
1087 | + |
|
1088 | + protected function lib_rgbahex($color) |
|
1089 | + { |
|
1090 | + $color = $this->coerceColor($color); |
|
1091 | + if (is_null($color)) { |
|
1092 | + $this->throwError("color expected for rgbahex"); |
|
1093 | + } |
|
1094 | + |
|
1095 | + return sprintf( |
|
1096 | + "#%02x%02x%02x%02x", |
|
1097 | + isset($color[4]) ? $color[4] * 255 : 255, |
|
1098 | + $color[1], |
|
1099 | + $color[2], |
|
1100 | + $color[3] |
|
1101 | + ); |
|
1102 | + } |
|
1103 | + |
|
1104 | + protected function lib_argb($color) |
|
1105 | + { |
|
1106 | + return $this->lib_rgbahex($color); |
|
1107 | + } |
|
1108 | + |
|
1109 | + /** |
|
1110 | + * Given an url, decide whether to output a regular link or the base64-encoded contents of the file |
|
1111 | + * |
|
1112 | + * @param array $value either an argument list (two strings) or a single string |
|
1113 | + * @return string formatted url(), either as a link or base64-encoded |
|
1114 | + */ |
|
1115 | + protected function lib_data_uri($value) |
|
1116 | + { |
|
1117 | + $mime = ($value[0] === 'list') ? $value[2][0][2] : null; |
|
1118 | + $url = ($value[0] === 'list') ? $value[2][1][2][0] : $value[2][0]; |
|
1119 | + |
|
1120 | + $fullpath = $this->findImport($url); |
|
1121 | + |
|
1122 | + if ($fullpath && ($fsize = filesize($fullpath)) !== false) { |
|
1123 | + // IE8 can't handle data uris larger than 32KB |
|
1124 | + if ($fsize / 1024 < 32) { |
|
1125 | + if (is_null($mime)) { |
|
1126 | + if (class_exists('finfo')) { // php 5.3+ |
|
1127 | + $finfo = new finfo(FILEINFO_MIME); |
|
1128 | + $mime = explode('; ', $finfo->file($fullpath)); |
|
1129 | + $mime = $mime[0]; |
|
1130 | + } elseif (function_exists('mime_content_type')) { // PHP 5.2 |
|
1131 | + $mime = mime_content_type($fullpath); |
|
1132 | + } |
|
1133 | + } |
|
1134 | + |
|
1135 | + if (!is_null($mime)) { // fallback if the mime type is still unknown |
|
1136 | + $url = sprintf('data:%s;base64,%s', $mime, base64_encode(file_get_contents($fullpath))); |
|
1137 | + } |
|
1138 | + } |
|
1139 | + } |
|
1140 | + |
|
1141 | + return 'url("'.$url.'")'; |
|
1142 | + } |
|
1143 | + |
|
1144 | + // utility func to unquote a string |
|
1145 | + protected function lib_e($arg) |
|
1146 | + { |
|
1147 | + switch ($arg[0]) { |
|
1148 | + case "list": |
|
1149 | + $items = $arg[2]; |
|
1150 | + if (isset($items[0])) { |
|
1151 | + return $this->lib_e($items[0]); |
|
1152 | + } |
|
1153 | + $this->throwError("unrecognised input"); // This ends function by throwing an exception |
|
1154 | + // no break |
|
1155 | + case "string": |
|
1156 | + $arg[1] = ""; |
|
1157 | + return $arg; |
|
1158 | + case "keyword": |
|
1159 | + return $arg; |
|
1160 | + default: |
|
1161 | + return array("keyword", $this->compileValue($arg)); |
|
1162 | + } |
|
1163 | + } |
|
1164 | + |
|
1165 | + protected function lib__sprintf($args) |
|
1166 | + { |
|
1167 | + if ($args[0] != "list") { |
|
1168 | + return $args; |
|
1169 | + } |
|
1170 | + $values = $args[2]; |
|
1171 | + $string = array_shift($values); |
|
1172 | + $template = $this->compileValue($this->lib_e($string)); |
|
1173 | + |
|
1174 | + $i = 0; |
|
1175 | + $m = array(); |
|
1176 | + if (preg_match_all('/%[dsa]/', $template, $m)) { |
|
1177 | + foreach ($m[0] as $match) { |
|
1178 | + $val = isset($values[$i]) ? |
|
1179 | + $this->reduce($values[$i]) : array('keyword', ''); |
|
1180 | + |
|
1181 | + // lessjs compat, renders fully expanded color, not raw color |
|
1182 | + if ($color = $this->coerceColor($val)) { |
|
1183 | + $val = $color; |
|
1184 | + } |
|
1185 | + |
|
1186 | + $i++; |
|
1187 | + $rep = $this->compileValue($this->lib_e($val)); |
|
1188 | + $template = preg_replace( |
|
1189 | + '/'.self::preg_quote($match).'/', |
|
1190 | + $rep, |
|
1191 | + $template, |
|
1192 | + 1 |
|
1193 | + ); |
|
1194 | + } |
|
1195 | + } |
|
1196 | + |
|
1197 | + $d = $string[0] == "string" ? $string[1] : '"'; |
|
1198 | + return array("string", $d, array($template)); |
|
1199 | + } |
|
1200 | + |
|
1201 | + protected function lib_floor($arg) |
|
1202 | + { |
|
1203 | + $value = $this->assertNumber($arg); |
|
1204 | + return array("number", floor($value), $arg[2]); |
|
1205 | + } |
|
1206 | + |
|
1207 | + protected function lib_ceil($arg) |
|
1208 | + { |
|
1209 | + $value = $this->assertNumber($arg); |
|
1210 | + return array("number", ceil($value), $arg[2]); |
|
1211 | + } |
|
1212 | + |
|
1213 | + protected function lib_round($arg) |
|
1214 | + { |
|
1215 | + if ($arg[0] != "list") { |
|
1216 | + $value = $this->assertNumber($arg); |
|
1217 | + return array("number", round($value), $arg[2]); |
|
1218 | + } else { |
|
1219 | + $value = $this->assertNumber($arg[2][0]); |
|
1220 | + $precision = $this->assertNumber($arg[2][1]); |
|
1221 | + return array("number", round($value, $precision), $arg[2][0][2]); |
|
1222 | + } |
|
1223 | + } |
|
1224 | + |
|
1225 | + protected function lib_unit($arg) |
|
1226 | + { |
|
1227 | + if ($arg[0] == "list") { |
|
1228 | + list($number, $newUnit) = $arg[2]; |
|
1229 | + return array("number", $this->assertNumber($number), |
|
1230 | + $this->compileValue($this->lib_e($newUnit))); |
|
1231 | + } else { |
|
1232 | + return array("number", $this->assertNumber($arg), ""); |
|
1233 | + } |
|
1234 | + } |
|
1235 | + |
|
1236 | + /** |
|
1237 | + * Helper function to get arguments for color manipulation functions. |
|
1238 | + * takes a list that contains a color like thing and a percentage |
|
1239 | + */ |
|
1240 | + public function colorArgs($args) |
|
1241 | + { |
|
1242 | + if ($args[0] != 'list' || count($args[2]) < 2) { |
|
1243 | + return array(array('color', 0, 0, 0), 0); |
|
1244 | + } |
|
1245 | + list($color, $delta) = $args[2]; |
|
1246 | + $color = $this->assertColor($color); |
|
1247 | + $delta = (float) $delta[1]; |
|
1248 | + |
|
1249 | + return array($color, $delta); |
|
1250 | + } |
|
1251 | + |
|
1252 | + protected function lib_darken($args) |
|
1253 | + { |
|
1254 | + list($color, $delta) = $this->colorArgs($args); |
|
1255 | + |
|
1256 | + $hsl = $this->toHSL($color); |
|
1257 | + $hsl[3] = $this->clamp($hsl[3] - $delta, 100); |
|
1258 | + return $this->toRGB($hsl); |
|
1259 | + } |
|
1260 | + |
|
1261 | + protected function lib_lighten($args) |
|
1262 | + { |
|
1263 | + list($color, $delta) = $this->colorArgs($args); |
|
1264 | + |
|
1265 | + $hsl = $this->toHSL($color); |
|
1266 | + $hsl[3] = $this->clamp($hsl[3] + $delta, 100); |
|
1267 | + return $this->toRGB($hsl); |
|
1268 | + } |
|
1269 | + |
|
1270 | + protected function lib_saturate($args) |
|
1271 | + { |
|
1272 | + list($color, $delta) = $this->colorArgs($args); |
|
1273 | + |
|
1274 | + $hsl = $this->toHSL($color); |
|
1275 | + $hsl[2] = $this->clamp($hsl[2] + $delta, 100); |
|
1276 | + return $this->toRGB($hsl); |
|
1277 | + } |
|
1278 | + |
|
1279 | + protected function lib_desaturate($args) |
|
1280 | + { |
|
1281 | + list($color, $delta) = $this->colorArgs($args); |
|
1282 | + |
|
1283 | + $hsl = $this->toHSL($color); |
|
1284 | + $hsl[2] = $this->clamp($hsl[2] - $delta, 100); |
|
1285 | + return $this->toRGB($hsl); |
|
1286 | + } |
|
1287 | + |
|
1288 | + protected function lib_spin($args) |
|
1289 | + { |
|
1290 | + list($color, $delta) = $this->colorArgs($args); |
|
1291 | + |
|
1292 | + $hsl = $this->toHSL($color); |
|
1293 | + |
|
1294 | + $hsl[1] = $hsl[1] + $delta % 360; |
|
1295 | + if ($hsl[1] < 0) { |
|
1296 | + $hsl[1] += 360; |
|
1297 | + } |
|
1298 | + |
|
1299 | + return $this->toRGB($hsl); |
|
1300 | + } |
|
1301 | + |
|
1302 | + protected function lib_fadeout($args) |
|
1303 | + { |
|
1304 | + list($color, $delta) = $this->colorArgs($args); |
|
1305 | + $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) - $delta / 100); |
|
1306 | + return $color; |
|
1307 | + } |
|
1308 | + |
|
1309 | + protected function lib_fadein($args) |
|
1310 | + { |
|
1311 | + list($color, $delta) = $this->colorArgs($args); |
|
1312 | + $color[4] = $this->clamp((isset($color[4]) ? $color[4] : 1) + $delta / 100); |
|
1313 | + return $color; |
|
1314 | + } |
|
1315 | + |
|
1316 | + protected function lib_hue($color) |
|
1317 | + { |
|
1318 | + $hsl = $this->toHSL($this->assertColor($color)); |
|
1319 | + return round($hsl[1]); |
|
1320 | + } |
|
1321 | + |
|
1322 | + protected function lib_saturation($color) |
|
1323 | + { |
|
1324 | + $hsl = $this->toHSL($this->assertColor($color)); |
|
1325 | + return round($hsl[2]); |
|
1326 | + } |
|
1327 | + |
|
1328 | + protected function lib_lightness($color) |
|
1329 | + { |
|
1330 | + $hsl = $this->toHSL($this->assertColor($color)); |
|
1331 | + return round($hsl[3]); |
|
1332 | + } |
|
1333 | + |
|
1334 | + // get the alpha of a color |
|
1335 | + // defaults to 1 for non-colors or colors without an alpha |
|
1336 | + protected function lib_alpha($value) |
|
1337 | + { |
|
1338 | + if (!is_null($color = $this->coerceColor($value))) { |
|
1339 | + return isset($color[4]) ? $color[4] : 1; |
|
1340 | + } |
|
1341 | + return ''; |
|
1342 | + } |
|
1343 | + |
|
1344 | + // set the alpha of the color |
|
1345 | + protected function lib_fade($args) |
|
1346 | + { |
|
1347 | + list($color, $alpha) = $this->colorArgs($args); |
|
1348 | + $color[4] = $this->clamp($alpha / 100.0); |
|
1349 | + return $color; |
|
1350 | + } |
|
1351 | + |
|
1352 | + protected function lib_percentage($arg) |
|
1353 | + { |
|
1354 | + $num = $this->assertNumber($arg); |
|
1355 | + return array("number", $num * 100, "%"); |
|
1356 | + } |
|
1357 | + |
|
1358 | + /** |
|
1359 | + * Mix color with white in variable proportion. |
|
1360 | + * |
|
1361 | + * It is the same as calling `mix(#ffffff, @color, @weight)`. |
|
1362 | + * |
|
1363 | + * tint(@color, [@weight: 50%]); |
|
1364 | + * |
|
1365 | + * http://lesscss.org/functions/#color-operations-tint |
|
1366 | + * |
|
1367 | + * @return array Color |
|
1368 | + */ |
|
1369 | + protected function lib_tint($args) |
|
1370 | + { |
|
1371 | + $white = ['color', 255, 255, 255]; |
|
1372 | + if ($args[0] == 'color') { |
|
1373 | + return $this->lib_mix(['list', ',', [$white, $args]]); |
|
1374 | + } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
1375 | + return $this->lib_mix([$args[0], $args[1], [$white, $args[2][0], $args[2][1]]]); |
|
1376 | + } else { |
|
1377 | + $this->throwError("tint expects (color, weight)"); |
|
1378 | + } |
|
1379 | + return array(); |
|
1380 | + } |
|
1381 | + |
|
1382 | + /** |
|
1383 | + * Mix color with black in variable proportion. |
|
1384 | + * |
|
1385 | + * It is the same as calling `mix(#000000, @color, @weight)` |
|
1386 | + * |
|
1387 | + * shade(@color, [@weight: 50%]); |
|
1388 | + * |
|
1389 | + * http://lesscss.org/functions/#color-operations-shade |
|
1390 | + * |
|
1391 | + * @return array Color |
|
1392 | + */ |
|
1393 | + protected function lib_shade($args) |
|
1394 | + { |
|
1395 | + $black = ['color', 0, 0, 0]; |
|
1396 | + if ($args[0] == 'color') { |
|
1397 | + return $this->lib_mix(['list', ',', [$black, $args]]); |
|
1398 | + } elseif ($args[0] == "list" && count($args[2]) == 2) { |
|
1399 | + return $this->lib_mix([$args[0], $args[1], [$black, $args[2][0], $args[2][1]]]); |
|
1400 | + } else { |
|
1401 | + $this->throwError("shade expects (color, weight)"); |
|
1402 | + } |
|
1403 | + return array(); |
|
1404 | + } |
|
1405 | + |
|
1406 | + /** |
|
1407 | + * lib_mix |
|
1408 | + * mixes two colors by weight |
|
1409 | + * mix(@color1, @color2, [@weight: 50%]); |
|
1410 | + * http://sass-lang.com/docs/yardoc/Sass/Script/Functions.html#mix-instance_method |
|
1411 | + * |
|
1412 | + * @param array $args Args |
|
1413 | + * @return array |
|
1414 | + */ |
|
1415 | + protected function lib_mix($args) |
|
1416 | + { |
|
1417 | + if ($args[0] != "list" || count($args[2]) < 2) { |
|
1418 | + $this->throwError("mix expects (color1, color2, weight)"); |
|
1419 | + } |
|
1420 | + |
|
1421 | + list($first, $second) = $args[2]; |
|
1422 | + $first = $this->assertColor($first); |
|
1423 | + $second = $this->assertColor($second); |
|
1424 | + |
|
1425 | + $first_a = $this->lib_alpha($first); |
|
1426 | + $second_a = $this->lib_alpha($second); |
|
1427 | + |
|
1428 | + if (isset($args[2][2])) { |
|
1429 | + $weight = $args[2][2][1] / 100.0; |
|
1430 | + } else { |
|
1431 | + $weight = 0.5; |
|
1432 | + } |
|
1433 | + |
|
1434 | + $w = $weight * 2 - 1; |
|
1435 | + $a = $first_a - $second_a; |
|
1436 | + |
|
1437 | + $w1 = (($w * $a == -1 ? $w : ($w + $a) / (1 + $w * $a)) + 1) / 2.0; |
|
1438 | + $w2 = 1.0 - $w1; |
|
1439 | + |
|
1440 | + $new = array('color', |
|
1441 | + $w1 * $first[1] + $w2 * $second[1], |
|
1442 | + $w1 * $first[2] + $w2 * $second[2], |
|
1443 | + $w1 * $first[3] + $w2 * $second[3], |
|
1444 | + ); |
|
1445 | + |
|
1446 | + if ($first_a != 1.0 || $second_a != 1.0) { |
|
1447 | + $new[] = $first_a * $weight + $second_a * ($weight - 1); |
|
1448 | + } |
|
1449 | + |
|
1450 | + return $this->fixColor($new); |
|
1451 | + } |
|
1452 | + |
|
1453 | + /** |
|
1454 | + * lib_contrast |
|
1455 | + * |
|
1456 | + * @param array $args Args |
|
1457 | + * @return array |
|
1458 | + */ |
|
1459 | + protected function lib_contrast($args) |
|
1460 | + { |
|
1461 | + $darkColor = array('color', 0, 0, 0); |
|
1462 | + $lightColor = array('color', 255, 255, 255); |
|
1463 | + $threshold = 0.43; |
|
1464 | + |
|
1465 | + if ($args[0] == 'list') { |
|
1466 | + $inputColor = (isset($args[2][0])) ? $this->assertColor($args[2][0]) : $lightColor; |
|
1467 | + $darkColor = (isset($args[2][1])) ? $this->assertColor($args[2][1]) : $darkColor; |
|
1468 | + $lightColor = (isset($args[2][2])) ? $this->assertColor($args[2][2]) : $lightColor; |
|
1469 | + $threshold = (isset($args[2][3])) ? $this->assertNumber($args[2][3]) : $threshold; |
|
1470 | + } else { |
|
1471 | + $inputColor = $this->assertColor($args); |
|
1472 | + } |
|
1473 | + |
|
1474 | + $inputColor = $this->coerceColor($inputColor); |
|
1475 | + $darkColor = $this->coerceColor($darkColor); |
|
1476 | + $lightColor = $this->coerceColor($lightColor); |
|
1477 | + |
|
1478 | + //Figure out which is actually light and dark! |
|
1479 | + if ($this->toLuma($darkColor) > $this->toLuma($lightColor)) { |
|
1480 | + $t = $lightColor; |
|
1481 | + $lightColor = $darkColor; |
|
1482 | + $darkColor = $t; |
|
1483 | + } |
|
1484 | + |
|
1485 | + $inputColor_alpha = $this->lib_alpha($inputColor); |
|
1486 | + if (($this->toLuma($inputColor) * $inputColor_alpha) < $threshold) { |
|
1487 | + return $lightColor; |
|
1488 | + } |
|
1489 | + return $darkColor; |
|
1490 | + } |
|
1491 | + |
|
1492 | + private function toLuma($color) |
|
1493 | + { |
|
1494 | + list(, $r, $g, $b) = $this->coerceColor($color); |
|
1495 | + |
|
1496 | + $r = $r / 255; |
|
1497 | + $g = $g / 255; |
|
1498 | + $b = $b / 255; |
|
1499 | + |
|
1500 | + $r = ($r <= 0.03928) ? $r / 12.92 : pow((($r + 0.055) / 1.055), 2.4); |
|
1501 | + $g = ($g <= 0.03928) ? $g / 12.92 : pow((($g + 0.055) / 1.055), 2.4); |
|
1502 | + $b = ($b <= 0.03928) ? $b / 12.92 : pow((($b + 0.055) / 1.055), 2.4); |
|
1503 | + |
|
1504 | + return (0.2126 * $r) + (0.7152 * $g) + (0.0722 * $b); |
|
1505 | + } |
|
1506 | + |
|
1507 | + protected function lib_luma($color) |
|
1508 | + { |
|
1509 | + return array("number", round($this->toLuma($color) * 100, 8), "%"); |
|
1510 | + } |
|
1511 | + |
|
1512 | + |
|
1513 | + public function assertColor($value, $error = "expected color value") |
|
1514 | + { |
|
1515 | + $color = $this->coerceColor($value); |
|
1516 | + if (is_null($color)) { |
|
1517 | + $this->throwError($error); |
|
1518 | + } |
|
1519 | + return $color; |
|
1520 | + } |
|
1521 | + |
|
1522 | + public function assertNumber($value, $error = "expecting number") |
|
1523 | + { |
|
1524 | + if ($value[0] == "number") { |
|
1525 | + return $value[1]; |
|
1526 | + } |
|
1527 | + $this->throwError($error); |
|
1528 | + } |
|
1529 | + |
|
1530 | + public function assertArgs($value, $expectedArgs, $name = "") |
|
1531 | + { |
|
1532 | + if ($expectedArgs == 1) { |
|
1533 | + return $value; |
|
1534 | + } else { |
|
1535 | + if ($value[0] !== "list" || $value[1] != ",") { |
|
1536 | + $this->throwError("expecting list"); |
|
1537 | + } |
|
1538 | + $values = $value[2]; |
|
1539 | + $numValues = count($values); |
|
1540 | + if ($expectedArgs != $numValues) { |
|
1541 | + if ($name) { |
|
1542 | + $name = $name.": "; |
|
1543 | + } |
|
1544 | + |
|
1545 | + $this->throwError("{$name}expecting $expectedArgs arguments, got $numValues"); |
|
1546 | + } |
|
1547 | + |
|
1548 | + return $values; |
|
1549 | + } |
|
1550 | + } |
|
1551 | + |
|
1552 | + protected function toHSL($color) |
|
1553 | + { |
|
1554 | + if ($color[0] === 'hsl') { |
|
1555 | + return $color; |
|
1556 | + } |
|
1557 | + |
|
1558 | + $r = $color[1] / 255; |
|
1559 | + $g = $color[2] / 255; |
|
1560 | + $b = $color[3] / 255; |
|
1561 | + |
|
1562 | + $min = min($r, $g, $b); |
|
1563 | + $max = max($r, $g, $b); |
|
1564 | + |
|
1565 | + $L = ($min + $max) / 2; |
|
1566 | + if ($min == $max) { |
|
1567 | + $S = $H = 0; |
|
1568 | + } else { |
|
1569 | + if ($L < 0.5) { |
|
1570 | + $S = ($max - $min) / ($max + $min); |
|
1571 | + } else { |
|
1572 | + $S = ($max - $min) / (2.0 - $max - $min); |
|
1573 | + } |
|
1574 | + if ($r == $max) { |
|
1575 | + $H = ($g - $b) / ($max - $min); |
|
1576 | + } elseif ($g == $max) { |
|
1577 | + $H = 2.0 + ($b - $r) / ($max - $min); |
|
1578 | + } elseif ($b == $max) { |
|
1579 | + $H = 4.0 + ($r - $g) / ($max - $min); |
|
1580 | + } |
|
1581 | + } |
|
1582 | + |
|
1583 | + $out = array('hsl', |
|
1584 | + ($H < 0 ? $H + 6 : $H) * 60, |
|
1585 | + $S * 100, |
|
1586 | + $L * 100, |
|
1587 | + ); |
|
1588 | + |
|
1589 | + if (count($color) > 4) { |
|
1590 | + // copy alpha |
|
1591 | + $out[] = $color[4]; |
|
1592 | + } |
|
1593 | + return $out; |
|
1594 | + } |
|
1595 | + |
|
1596 | + protected function toRGB_helper($comp, $temp1, $temp2) |
|
1597 | + { |
|
1598 | + if ($comp < 0) { |
|
1599 | + $comp += 1.0; |
|
1600 | + } elseif ($comp > 1) { |
|
1601 | + $comp -= 1.0; |
|
1602 | + } |
|
1603 | + |
|
1604 | + if (6 * $comp < 1) { |
|
1605 | + return $temp1 + ($temp2 - $temp1) * 6 * $comp; |
|
1606 | + } |
|
1607 | + if (2 * $comp < 1) { |
|
1608 | + return $temp2; |
|
1609 | + } |
|
1610 | + if (3 * $comp < 2) { |
|
1611 | + return $temp1 + ($temp2 - $temp1) * ((2 / 3) - $comp) * 6; |
|
1612 | + } |
|
1613 | + |
|
1614 | + return $temp1; |
|
1615 | + } |
|
1616 | + |
|
1617 | + /** |
|
1618 | + * Converts a hsl array into a color value in rgb. |
|
1619 | + * Expects H to be in range of 0 to 360, S and L in 0 to 100 |
|
1620 | + */ |
|
1621 | + protected function toRGB($color) |
|
1622 | + { |
|
1623 | + if ($color[0] === 'color') { |
|
1624 | + return $color; |
|
1625 | + } |
|
1626 | + |
|
1627 | + $H = $color[1] / 360; |
|
1628 | + $S = $color[2] / 100; |
|
1629 | + $L = $color[3] / 100; |
|
1630 | + |
|
1631 | + if ($S == 0) { |
|
1632 | + $r = $g = $b = $L; |
|
1633 | + } else { |
|
1634 | + $temp2 = $L < 0.5 ? |
|
1635 | + $L * (1.0 + $S) : $L + $S - $L * $S; |
|
1636 | + |
|
1637 | + $temp1 = 2.0 * $L - $temp2; |
|
1638 | + |
|
1639 | + $r = $this->toRGB_helper($H + 1 / 3, $temp1, $temp2); |
|
1640 | + $g = $this->toRGB_helper($H, $temp1, $temp2); |
|
1641 | + $b = $this->toRGB_helper($H - 1 / 3, $temp1, $temp2); |
|
1642 | + } |
|
1643 | + |
|
1644 | + // $out = array('color', round($r*255), round($g*255), round($b*255)); |
|
1645 | + $out = array('color', $r * 255, $g * 255, $b * 255); |
|
1646 | + if (count($color) > 4) { |
|
1647 | + // copy alpha |
|
1648 | + $out[] = $color[4]; |
|
1649 | + } |
|
1650 | + return $out; |
|
1651 | + } |
|
1652 | + |
|
1653 | + protected function clamp($v, $max = 1, $min = 0) |
|
1654 | + { |
|
1655 | + return min($max, max($min, $v)); |
|
1656 | + } |
|
1657 | + |
|
1658 | + /** |
|
1659 | + * Convert the rgb, rgba, hsl color literals of function type |
|
1660 | + * as returned by the parser into values of color type. |
|
1661 | + */ |
|
1662 | + protected function funcToColor($func) |
|
1663 | + { |
|
1664 | + $fname = $func[1]; |
|
1665 | + if ($func[2][0] != 'list') { |
|
1666 | + // need a list of arguments |
|
1667 | + return false; |
|
1668 | + } |
|
1669 | + $rawComponents = $func[2][2]; |
|
1670 | + |
|
1671 | + if ($fname == 'hsl' || $fname == 'hsla') { |
|
1672 | + $hsl = array('hsl'); |
|
1673 | + $i = 0; |
|
1674 | + foreach ($rawComponents as $c) { |
|
1675 | + $val = $this->reduce($c); |
|
1676 | + $val = isset($val[1]) ? (float) $val[1] : 0; |
|
1677 | + |
|
1678 | + if ($i == 0) { |
|
1679 | + $clamp = 360; |
|
1680 | + } elseif ($i < 3) { |
|
1681 | + $clamp = 100; |
|
1682 | + } else { |
|
1683 | + $clamp = 1; |
|
1684 | + } |
|
1685 | + |
|
1686 | + $hsl[] = $this->clamp($val, $clamp); |
|
1687 | + $i++; |
|
1688 | + } |
|
1689 | + |
|
1690 | + while (count($hsl) < 4) { |
|
1691 | + $hsl[] = 0; |
|
1692 | + } |
|
1693 | + return $this->toRGB($hsl); |
|
1694 | + |
|
1695 | + } elseif ($fname == 'rgb' || $fname == 'rgba') { |
|
1696 | + $components = array(); |
|
1697 | + $i = 1; |
|
1698 | + foreach ($rawComponents as $c) { |
|
1699 | + $c = $this->reduce($c); |
|
1700 | + if ($i < 4) { |
|
1701 | + if ($c[0] == "number" && $c[2] == "%") { |
|
1702 | + $components[] = 255 * ($c[1] / 100); |
|
1703 | + } else { |
|
1704 | + $components[] = (float) $c[1]; |
|
1705 | + } |
|
1706 | + } elseif ($i == 4) { |
|
1707 | + if ($c[0] == "number" && $c[2] == "%") { |
|
1708 | + $components[] = 1.0 * ($c[1] / 100); |
|
1709 | + } else { |
|
1710 | + $components[] = (float) $c[1]; |
|
1711 | + } |
|
1712 | + } else { |
|
1713 | + break; |
|
1714 | + } |
|
1715 | + |
|
1716 | + $i++; |
|
1717 | + } |
|
1718 | + while (count($components) < 3) { |
|
1719 | + $components[] = 0; |
|
1720 | + } |
|
1721 | + array_unshift($components, 'color'); |
|
1722 | + return $this->fixColor($components); |
|
1723 | + } |
|
1724 | + |
|
1725 | + return false; |
|
1726 | + } |
|
1727 | + |
|
1728 | + protected function reduce($value, $forExpression = false) |
|
1729 | + { |
|
1730 | + switch ($value[0]) { |
|
1731 | + case "interpolate": |
|
1732 | + $reduced = $this->reduce($value[1]); |
|
1733 | + $var = $this->compileValue($reduced); |
|
1734 | + $res = $this->reduce(array("variable", $this->vPrefix.$var)); |
|
1735 | + |
|
1736 | + if ($res[0] == "raw_color") { |
|
1737 | + $res = $this->coerceColor($res); |
|
1738 | + } |
|
1739 | + |
|
1740 | + if (empty($value[2])) { |
|
1741 | + $res = $this->lib_e($res); |
|
1742 | + } |
|
1743 | + |
|
1744 | + return $res; |
|
1745 | + case "variable": |
|
1746 | + $key = $value[1]; |
|
1747 | + if (is_array($key)) { |
|
1748 | + $key = $this->reduce($key); |
|
1749 | + $key = $this->vPrefix.$this->compileValue($this->lib_e($key)); |
|
1750 | + } |
|
1751 | + |
|
1752 | + $seen = & $this->env->seenNames; |
|
1753 | + |
|
1754 | + if (!empty($seen[$key])) { |
|
1755 | + $this->throwError("infinite loop detected: $key"); |
|
1756 | + } |
|
1757 | + |
|
1758 | + $seen[$key] = true; |
|
1759 | + $out = $this->reduce($this->get($key)); |
|
1760 | + $seen[$key] = false; |
|
1761 | + return $out; |
|
1762 | + case "list": |
|
1763 | + foreach ($value[2] as &$item) { |
|
1764 | + $item = $this->reduce($item, $forExpression); |
|
1765 | + } |
|
1766 | + return $value; |
|
1767 | + case "expression": |
|
1768 | + return $this->evaluate($value); |
|
1769 | + case "string": |
|
1770 | + foreach ($value[2] as &$part) { |
|
1771 | + if (is_array($part)) { |
|
1772 | + $strip = $part[0] == "variable"; |
|
1773 | + $part = $this->reduce($part); |
|
1774 | + if ($strip) { |
|
1775 | + $part = $this->lib_e($part); |
|
1776 | + } |
|
1777 | + } |
|
1778 | + } |
|
1779 | + return $value; |
|
1780 | + case "escape": |
|
1781 | + list(, $inner) = $value; |
|
1782 | + return $this->lib_e($this->reduce($inner)); |
|
1783 | + case "function": |
|
1784 | + $color = $this->funcToColor($value); |
|
1785 | + if ($color) { |
|
1786 | + return $color; |
|
1787 | + } |
|
1788 | + |
|
1789 | + list(, $name, $args) = $value; |
|
1790 | + if ($name == "%") { |
|
1791 | + $name = "_sprintf"; |
|
1792 | + } |
|
1793 | + |
|
1794 | + $f = isset($this->libFunctions[$name]) ? |
|
1795 | + $this->libFunctions[$name] : array($this, 'lib_'.str_replace('-', '_', $name)); |
|
1796 | + |
|
1797 | + if (is_callable($f)) { |
|
1798 | + if ($args[0] == 'list') { |
|
1799 | + $args = self::compressList($args[2], $args[1]); |
|
1800 | + } |
|
1801 | + |
|
1802 | + $ret = call_user_func($f, $this->reduce($args, true), $this); |
|
1803 | + |
|
1804 | + if (is_null($ret)) { |
|
1805 | + return array("string", "", array( |
|
1806 | + $name, "(", $args, ")" |
|
1807 | + )); |
|
1808 | + } |
|
1809 | + |
|
1810 | + // convert to a typed value if the result is a php primitive |
|
1811 | + if (is_numeric($ret)) { |
|
1812 | + $ret = array('number', $ret, ""); |
|
1813 | + } elseif (!is_array($ret)) { |
|
1814 | + $ret = array('keyword', $ret); |
|
1815 | + } |
|
1816 | + |
|
1817 | + return $ret; |
|
1818 | + } |
|
1819 | + |
|
1820 | + // plain function, reduce args |
|
1821 | + $value[2] = $this->reduce($value[2]); |
|
1822 | + return $value; |
|
1823 | + case "unary": |
|
1824 | + list(, $op, $exp) = $value; |
|
1825 | + $exp = $this->reduce($exp); |
|
1826 | + |
|
1827 | + if ($exp[0] == "number") { |
|
1828 | + switch ($op) { |
|
1829 | + case "+": |
|
1830 | + return $exp; |
|
1831 | + case "-": |
|
1832 | + $exp[1] *= -1; |
|
1833 | + return $exp; |
|
1834 | + } |
|
1835 | + } |
|
1836 | + return array("string", "", array($op, $exp)); |
|
1837 | + } |
|
1838 | + |
|
1839 | + if ($forExpression) { |
|
1840 | + switch ($value[0]) { |
|
1841 | + case "keyword": |
|
1842 | + if ($color = $this->coerceColor($value)) { |
|
1843 | + return $color; |
|
1844 | + } |
|
1845 | + break; |
|
1846 | + case "raw_color": |
|
1847 | + return $this->coerceColor($value); |
|
1848 | + } |
|
1849 | + } |
|
1850 | + |
|
1851 | + return $value; |
|
1852 | + } |
|
1853 | + |
|
1854 | + |
|
1855 | + // coerce a value for use in color operation |
|
1856 | + protected function coerceColor($value) |
|
1857 | + { |
|
1858 | + switch ($value[0]) { |
|
1859 | + case 'color': |
|
1860 | + return $value; |
|
1861 | + case 'raw_color': |
|
1862 | + $c = array("color", 0, 0, 0); |
|
1863 | + $colorStr = substr($value[1], 1); |
|
1864 | + $num = hexdec($colorStr); |
|
1865 | + $width = strlen($colorStr) == 3 ? 16 : 256; |
|
1866 | + |
|
1867 | + for ($i = 3; $i > 0; $i--) { // 3 2 1 |
|
1868 | + $t = intval($num) % $width; |
|
1869 | + $num /= $width; |
|
1870 | + |
|
1871 | + $c[$i] = $t * (256 / $width) + $t * floor(16/$width); |
|
1872 | + } |
|
1873 | + |
|
1874 | + return $c; |
|
1875 | + case 'keyword': |
|
1876 | + $name = $value[1]; |
|
1877 | + if (isset(self::$cssColors[$name])) { |
|
1878 | + $rgba = explode(',', self::$cssColors[$name]); |
|
1879 | + |
|
1880 | + if (isset($rgba[3])) { |
|
1881 | + return array('color', $rgba[0], $rgba[1], $rgba[2], $rgba[3]); |
|
1882 | + } |
|
1883 | + return array('color', $rgba[0], $rgba[1], $rgba[2]); |
|
1884 | + } |
|
1885 | + return null; |
|
1886 | + } |
|
1887 | + return null; |
|
1888 | + } |
|
1889 | + |
|
1890 | + // make something string like into a string |
|
1891 | + protected function coerceString($value) |
|
1892 | + { |
|
1893 | + switch ($value[0]) { |
|
1894 | + case "string": |
|
1895 | + return $value; |
|
1896 | + case "keyword": |
|
1897 | + return array("string", "", array($value[1])); |
|
1898 | + } |
|
1899 | + return null; |
|
1900 | + } |
|
1901 | + |
|
1902 | + // turn list of length 1 into value type |
|
1903 | + protected function flattenList($value) |
|
1904 | + { |
|
1905 | + if ($value[0] == "list" && count($value[2]) == 1) { |
|
1906 | + return $this->flattenList($value[2][0]); |
|
1907 | + } |
|
1908 | + return $value; |
|
1909 | + } |
|
1910 | + |
|
1911 | + public function toBool($a) |
|
1912 | + { |
|
1913 | + return $a ? self::$TRUE : self::$FALSE; |
|
1914 | + } |
|
1915 | + |
|
1916 | + // evaluate an expression |
|
1917 | + protected function evaluate($exp) |
|
1918 | + { |
|
1919 | + list(, $op, $left, $right, $whiteBefore, $whiteAfter) = $exp; |
|
1920 | + |
|
1921 | + $left = $this->reduce($left, true); |
|
1922 | + $right = $this->reduce($right, true); |
|
1923 | + |
|
1924 | + if ($leftColor = $this->coerceColor($left)) { |
|
1925 | + $left = $leftColor; |
|
1926 | + } |
|
1927 | + |
|
1928 | + if ($rightColor = $this->coerceColor($right)) { |
|
1929 | + $right = $rightColor; |
|
1930 | + } |
|
1931 | + |
|
1932 | + $ltype = $left[0]; |
|
1933 | + $rtype = $right[0]; |
|
1934 | + |
|
1935 | + // operators that work on all types |
|
1936 | + if ($op == "and") { |
|
1937 | + return $this->toBool($left == self::$TRUE && $right == self::$TRUE); |
|
1938 | + } |
|
1939 | + |
|
1940 | + if ($op == "=") { |
|
1941 | + return $this->toBool($this->eq($left, $right)); |
|
1942 | + } |
|
1943 | + |
|
1944 | + if ($op == "+" && !is_null($str = $this->stringConcatenate($left, $right))) { |
|
1945 | + return $str; |
|
1946 | + } |
|
1947 | + |
|
1948 | + // type based operators |
|
1949 | + $fname = "op_{$ltype}_{$rtype}"; |
|
1950 | + if (is_callable(array($this, $fname))) { |
|
1951 | + $out = $this->$fname($op, $left, $right); |
|
1952 | + if (!is_null($out)) { |
|
1953 | + return $out; |
|
1954 | + } |
|
1955 | + } |
|
1956 | + |
|
1957 | + // make the expression look it did before being parsed |
|
1958 | + $paddedOp = $op; |
|
1959 | + if ($whiteBefore) { |
|
1960 | + $paddedOp = " ".$paddedOp; |
|
1961 | + } |
|
1962 | + if ($whiteAfter) { |
|
1963 | + $paddedOp .= " "; |
|
1964 | + } |
|
1965 | + |
|
1966 | + return array("string", "", array($left, $paddedOp, $right)); |
|
1967 | + } |
|
1968 | + |
|
1969 | + protected function stringConcatenate($left, $right) |
|
1970 | + { |
|
1971 | + if ($strLeft = $this->coerceString($left)) { |
|
1972 | + if ($right[0] == "string") { |
|
1973 | + $right[1] = ""; |
|
1974 | + } |
|
1975 | + $strLeft[2][] = $right; |
|
1976 | + return $strLeft; |
|
1977 | + } |
|
1978 | + |
|
1979 | + if ($strRight = $this->coerceString($right)) { |
|
1980 | + array_unshift($strRight[2], $left); |
|
1981 | + return $strRight; |
|
1982 | + } |
|
1983 | + return ''; |
|
1984 | + } |
|
1985 | + |
|
1986 | + |
|
1987 | + // make sure a color's components don't go out of bounds |
|
1988 | + protected function fixColor($c) |
|
1989 | + { |
|
1990 | + foreach (range(1, 3) as $i) { |
|
1991 | + if ($c[$i] < 0) { |
|
1992 | + $c[$i] = 0; |
|
1993 | + } |
|
1994 | + if ($c[$i] > 255) { |
|
1995 | + $c[$i] = 255; |
|
1996 | + } |
|
1997 | + } |
|
1998 | + |
|
1999 | + return $c; |
|
2000 | + } |
|
2001 | + |
|
2002 | + protected function op_number_color($op, $lft, $rgt) |
|
2003 | + { |
|
2004 | + if ($op == '+' || $op == '*') { |
|
2005 | + return $this->op_color_number($op, $rgt, $lft); |
|
2006 | + } |
|
2007 | + return array(); |
|
2008 | + } |
|
2009 | + |
|
2010 | + protected function op_color_number($op, $lft, $rgt) |
|
2011 | + { |
|
2012 | + if ($rgt[0] == '%') { |
|
2013 | + $rgt[1] /= 100; |
|
2014 | + } |
|
2015 | + |
|
2016 | + return $this->op_color_color( |
|
2017 | + $op, |
|
2018 | + $lft, |
|
2019 | + array_fill(1, count($lft) - 1, $rgt[1]) |
|
2020 | + ); |
|
2021 | + } |
|
2022 | + |
|
2023 | + protected function op_color_color($op, $left, $right) |
|
2024 | + { |
|
2025 | + $out = array('color'); |
|
2026 | + $max = count($left) > count($right) ? count($left) : count($right); |
|
2027 | + foreach (range(1, $max - 1) as $i) { |
|
2028 | + $lval = isset($left[$i]) ? $left[$i] : 0; |
|
2029 | + $rval = isset($right[$i]) ? $right[$i] : 0; |
|
2030 | + switch ($op) { |
|
2031 | + case '+': |
|
2032 | + $out[] = $lval + $rval; |
|
2033 | + break; |
|
2034 | + case '-': |
|
2035 | + $out[] = $lval - $rval; |
|
2036 | + break; |
|
2037 | + case '*': |
|
2038 | + $out[] = $lval * $rval; |
|
2039 | + break; |
|
2040 | + case '%': |
|
2041 | + $out[] = $lval % $rval; |
|
2042 | + break; |
|
2043 | + case '/': |
|
2044 | + if ($rval == 0) { |
|
2045 | + $this->throwError("evaluate error: can't divide by zero"); |
|
2046 | + } |
|
2047 | + $out[] = $lval / $rval; |
|
2048 | + break; |
|
2049 | + default: |
|
2050 | + $this->throwError('evaluate error: color op number failed on op '.$op); |
|
2051 | + } |
|
2052 | + } |
|
2053 | + return $this->fixColor($out); |
|
2054 | + } |
|
2055 | + |
|
2056 | + public function lib_red($color) |
|
2057 | + { |
|
2058 | + $color = $this->coerceColor($color); |
|
2059 | + if (is_null($color)) { |
|
2060 | + $this->throwError('color expected for red()'); |
|
2061 | + } |
|
2062 | + |
|
2063 | + return $color[1]; |
|
2064 | + } |
|
2065 | + |
|
2066 | + public function lib_green($color) |
|
2067 | + { |
|
2068 | + $color = $this->coerceColor($color); |
|
2069 | + if (is_null($color)) { |
|
2070 | + $this->throwError('color expected for green()'); |
|
2071 | + } |
|
2072 | + |
|
2073 | + return $color[2]; |
|
2074 | + } |
|
2075 | + |
|
2076 | + public function lib_blue($color) |
|
2077 | + { |
|
2078 | + $color = $this->coerceColor($color); |
|
2079 | + if (is_null($color)) { |
|
2080 | + $this->throwError('color expected for blue()'); |
|
2081 | + } |
|
2082 | + |
|
2083 | + return $color[3]; |
|
2084 | + } |
|
2085 | + |
|
2086 | + |
|
2087 | + // operator on two numbers |
|
2088 | + protected function op_number_number($op, $left, $right) |
|
2089 | + { |
|
2090 | + $unit = empty($left[2]) ? $right[2] : $left[2]; |
|
2091 | + |
|
2092 | + $value = 0; |
|
2093 | + switch ($op) { |
|
2094 | + case '+': |
|
2095 | + $value = $left[1] + $right[1]; |
|
2096 | + break; |
|
2097 | + case '*': |
|
2098 | + $value = $left[1] * $right[1]; |
|
2099 | + break; |
|
2100 | + case '-': |
|
2101 | + $value = $left[1] - $right[1]; |
|
2102 | + break; |
|
2103 | + case '%': |
|
2104 | + $value = $left[1] % $right[1]; |
|
2105 | + break; |
|
2106 | + case '/': |
|
2107 | + if ($right[1] == 0) { |
|
2108 | + $this->throwError('parse error: divide by zero'); |
|
2109 | + } |
|
2110 | + $value = $left[1] / $right[1]; |
|
2111 | + break; |
|
2112 | + case '<': |
|
2113 | + return $this->toBool($left[1] < $right[1]); |
|
2114 | + case '>': |
|
2115 | + return $this->toBool($left[1] > $right[1]); |
|
2116 | + case '>=': |
|
2117 | + return $this->toBool($left[1] >= $right[1]); |
|
2118 | + case '=<': |
|
2119 | + return $this->toBool($left[1] <= $right[1]); |
|
2120 | + default: |
|
2121 | + $this->throwError('parse error: unknown number operator: '.$op); |
|
2122 | + } |
|
2123 | + |
|
2124 | + return array("number", $value, $unit); |
|
2125 | + } |
|
2126 | + |
|
2127 | + |
|
2128 | + /* environment functions */ |
|
2129 | + |
|
2130 | + protected function makeOutputBlock($type, $selectors = null) |
|
2131 | + { |
|
2132 | + $b = new stdclass(); |
|
2133 | + $b->lines = array(); |
|
2134 | + $b->children = array(); |
|
2135 | + $b->selectors = $selectors; |
|
2136 | + $b->type = $type; |
|
2137 | + $b->parent = $this->scope; |
|
2138 | + return $b; |
|
2139 | + } |
|
2140 | + |
|
2141 | + // the state of execution |
|
2142 | + protected function pushEnv($block = null) |
|
2143 | + { |
|
2144 | + $e = new stdclass(); |
|
2145 | + $e->parent = $this->env; |
|
2146 | + $e->store = array(); |
|
2147 | + $e->block = $block; |
|
2148 | + |
|
2149 | + $this->env = $e; |
|
2150 | + return $e; |
|
2151 | + } |
|
2152 | + |
|
2153 | + // pop something off the stack |
|
2154 | + protected function popEnv() |
|
2155 | + { |
|
2156 | + $old = $this->env; |
|
2157 | + $this->env = $this->env->parent; |
|
2158 | + return $old; |
|
2159 | + } |
|
2160 | + |
|
2161 | + // set something in the current env |
|
2162 | + protected function set($name, $value) |
|
2163 | + { |
|
2164 | + $this->env->store[$name] = $value; |
|
2165 | + } |
|
2166 | + |
|
2167 | + |
|
2168 | + // get the highest occurrence entry for a name |
|
2169 | + protected function get($name) |
|
2170 | + { |
|
2171 | + $current = $this->env; |
|
2172 | + |
|
2173 | + $isArguments = $name == $this->vPrefix.'arguments'; |
|
2174 | + while ($current) { |
|
2175 | + if ($isArguments && isset($current->arguments)) { |
|
2176 | + return array('list', ' ', $current->arguments); |
|
2177 | + } |
|
2178 | + |
|
2179 | + if (isset($current->store[$name])) { |
|
2180 | + return $current->store[$name]; |
|
2181 | + } |
|
2182 | + |
|
2183 | + $current = isset($current->storeParent) ? |
|
2184 | + $current->storeParent : $current->parent; |
|
2185 | + } |
|
2186 | + |
|
2187 | + $this->throwError("variable $name is undefined"); |
|
2188 | + } |
|
2189 | + |
|
2190 | + // inject array of unparsed strings into environment as variables |
|
2191 | + protected function injectVariables($args) |
|
2192 | + { |
|
2193 | + $this->pushEnv(); |
|
2194 | + $parser = new lessc_parser($this, __METHOD__); |
|
2195 | + $value = null; |
|
2196 | + foreach ($args as $name => $strValue) { |
|
2197 | + if ($name[0] !== '@') { |
|
2198 | + $name = '@'.$name; |
|
2199 | + } |
|
2200 | + $parser->count = 0; |
|
2201 | + $parser->buffer = (string) $strValue; |
|
2202 | + if (!$parser->propertyValue($value)) { |
|
2203 | + throw new Exception("failed to parse passed in variable $name: $strValue"); |
|
2204 | + } |
|
2205 | + |
|
2206 | + $this->set($name, $value); |
|
2207 | + } |
|
2208 | + } |
|
2209 | + |
|
2210 | + /** |
|
2211 | + * Initialize any static state, can initialize parser for a file |
|
2212 | + * $opts isn't used yet |
|
2213 | + */ |
|
2214 | + public function __construct($fname = null) |
|
2215 | + { |
|
2216 | + if ($fname !== null) { |
|
2217 | + // used for deprecated parse method |
|
2218 | + $this->_parseFile = $fname; |
|
2219 | + } |
|
2220 | + } |
|
2221 | + |
|
2222 | + public function compile($string, $name = null) |
|
2223 | + { |
|
2224 | + $locale = setlocale(LC_NUMERIC, 0); |
|
2225 | + setlocale(LC_NUMERIC, "C"); |
|
2226 | + |
|
2227 | + $this->parser = $this->makeParser($name); |
|
2228 | + $root = $this->parser->parse($string); |
|
2229 | + |
|
2230 | + $this->env = null; |
|
2231 | + $this->scope = null; |
|
2232 | + |
|
2233 | + $this->formatter = $this->newFormatter(); |
|
2234 | + |
|
2235 | + if (!empty($this->registeredVars)) { |
|
2236 | + $this->injectVariables($this->registeredVars); |
|
2237 | + } |
|
2238 | + |
|
2239 | + $this->sourceParser = $this->parser; // used for error messages |
|
2240 | + $this->compileBlock($root); |
|
2241 | + |
|
2242 | + ob_start(); |
|
2243 | + $this->formatter->block($this->scope); |
|
2244 | + $out = ob_get_clean(); |
|
2245 | + setlocale(LC_NUMERIC, $locale); |
|
2246 | + return $out; |
|
2247 | + } |
|
2248 | + |
|
2249 | + public function compileFile($fname, $outFname = null) |
|
2250 | + { |
|
2251 | + if (!is_readable($fname)) { |
|
2252 | + throw new Exception('load error: failed to find '.$fname); |
|
2253 | + } |
|
2254 | + |
|
2255 | + $pi = pathinfo($fname); |
|
2256 | + |
|
2257 | + $oldImport = $this->importDir; |
|
2258 | + |
|
2259 | + $this->importDir = (array) $this->importDir; |
|
2260 | + $this->importDir[] = $pi['dirname'].'/'; |
|
2261 | + |
|
2262 | + $this->addParsedFile($fname); |
|
2263 | + |
|
2264 | + $out = $this->compile(file_get_contents($fname), $fname); |
|
2265 | + |
|
2266 | + $this->importDir = $oldImport; |
|
2267 | + |
|
2268 | + if ($outFname !== null) { |
|
2269 | + return file_put_contents($outFname, $out); |
|
2270 | + } |
|
2271 | + |
|
2272 | + return $out; |
|
2273 | + } |
|
2274 | + |
|
2275 | + // compile only if changed input has changed or output doesn't exist |
|
2276 | + public function checkedCompile($in, $out) |
|
2277 | + { |
|
2278 | + if (!is_file($out) || filemtime($in) > filemtime($out)) { |
|
2279 | + $this->compileFile($in, $out); |
|
2280 | + return true; |
|
2281 | + } |
|
2282 | + return false; |
|
2283 | + } |
|
2284 | + |
|
2285 | + /** |
|
2286 | + * Execute lessphp on a .less file or a lessphp cache structure |
|
2287 | + * |
|
2288 | + * The lessphp cache structure contains information about a specific |
|
2289 | + * less file having been parsed. It can be used as a hint for future |
|
2290 | + * calls to determine whether or not a rebuild is required. |
|
2291 | + * |
|
2292 | + * The cache structure contains two important keys that may be used |
|
2293 | + * externally: |
|
2294 | + * |
|
2295 | + * compiled: The final compiled CSS |
|
2296 | + * updated: The time (in seconds) the CSS was last compiled |
|
2297 | + * |
|
2298 | + * The cache structure is a plain-ol' PHP associative array and can |
|
2299 | + * be serialized and unserialized without a hitch. |
|
2300 | + * |
|
2301 | + * @param mixed $in Input |
|
2302 | + * @param bool $force Force rebuild? |
|
2303 | + * @return array|null lessphp cache structure |
|
2304 | + */ |
|
2305 | + public function cachedCompile($in, $force = false) |
|
2306 | + { |
|
2307 | + // assume no root |
|
2308 | + $root = null; |
|
2309 | + |
|
2310 | + if (is_string($in)) { |
|
2311 | + $root = $in; |
|
2312 | + } elseif (is_array($in) && isset($in['root'])) { |
|
2313 | + if ($force || !isset($in['files'])) { |
|
2314 | + // If we are forcing a recompile or if for some reason the |
|
2315 | + // structure does not contain any file information we should |
|
2316 | + // specify the root to trigger a rebuild. |
|
2317 | + $root = $in['root']; |
|
2318 | + } elseif (isset($in['files']) && is_array($in['files'])) { |
|
2319 | + foreach ($in['files'] as $fname => $ftime) { |
|
2320 | + if (!file_exists($fname) || filemtime($fname) > $ftime) { |
|
2321 | + // One of the files we knew about previously has changed |
|
2322 | + // so we should look at our incoming root again. |
|
2323 | + $root = $in['root']; |
|
2324 | + break; |
|
2325 | + } |
|
2326 | + } |
|
2327 | + } |
|
2328 | + } else { |
|
2329 | + // TODO: Throw an exception? We got neither a string nor something |
|
2330 | + // that looks like a compatible lessphp cache structure. |
|
2331 | + return null; |
|
2332 | + } |
|
2333 | + |
|
2334 | + if ($root !== null) { |
|
2335 | + // If we have a root value which means we should rebuild. |
|
2336 | + $out = array(); |
|
2337 | + $out['root'] = $root; |
|
2338 | + $out['compiled'] = $this->compileFile($root); |
|
2339 | + $out['files'] = $this->allParsedFiles(); |
|
2340 | + $out['updated'] = time(); |
|
2341 | + return $out; |
|
2342 | + } else { |
|
2343 | + // No changes, pass back the structure |
|
2344 | + // we were given initially. |
|
2345 | + return $in; |
|
2346 | + } |
|
2347 | + } |
|
2348 | + |
|
2349 | + // parse and compile buffer |
|
2350 | + // This is deprecated |
|
2351 | + public function parse($str = null, $initialVariables = null) |
|
2352 | + { |
|
2353 | + if (is_array($str)) { |
|
2354 | + $initialVariables = $str; |
|
2355 | + $str = null; |
|
2356 | + } |
|
2357 | + |
|
2358 | + $oldVars = $this->registeredVars; |
|
2359 | + if ($initialVariables !== null) { |
|
2360 | + $this->setVariables($initialVariables); |
|
2361 | + } |
|
2362 | + |
|
2363 | + if ($str == null) { |
|
2364 | + if (empty($this->_parseFile)) { |
|
2365 | + throw new exception("nothing to parse"); |
|
2366 | + } |
|
2367 | + |
|
2368 | + $out = $this->compileFile($this->_parseFile); |
|
2369 | + } else { |
|
2370 | + $out = $this->compile($str); |
|
2371 | + } |
|
2372 | + |
|
2373 | + $this->registeredVars = $oldVars; |
|
2374 | + return $out; |
|
2375 | + } |
|
2376 | + |
|
2377 | + protected function makeParser($name) |
|
2378 | + { |
|
2379 | + $parser = new lessc_parser($this, $name); |
|
2380 | + $parser->writeComments = $this->preserveComments; |
|
2381 | + |
|
2382 | + return $parser; |
|
2383 | + } |
|
2384 | + |
|
2385 | + public function setFormatter($name) |
|
2386 | + { |
|
2387 | + $this->formatterName = $name; |
|
2388 | + } |
|
2389 | + |
|
2390 | + protected function newFormatter() |
|
2391 | + { |
|
2392 | + $className = "lessc_formatter_lessjs"; |
|
2393 | + if (!empty($this->formatterName)) { |
|
2394 | + if (!is_string($this->formatterName)) { |
|
2395 | + return $this->formatterName; |
|
2396 | + } |
|
2397 | + $className = "lessc_formatter_$this->formatterName"; |
|
2398 | + } |
|
2399 | + |
|
2400 | + return new $className(); |
|
2401 | + } |
|
2402 | + |
|
2403 | + public function setPreserveComments($preserve) |
|
2404 | + { |
|
2405 | + $this->preserveComments = $preserve; |
|
2406 | + } |
|
2407 | + |
|
2408 | + public function registerFunction($name, $func) |
|
2409 | + { |
|
2410 | + $this->libFunctions[$name] = $func; |
|
2411 | + } |
|
2412 | + |
|
2413 | + public function unregisterFunction($name) |
|
2414 | + { |
|
2415 | + unset($this->libFunctions[$name]); |
|
2416 | + } |
|
2417 | + |
|
2418 | + public function setVariables($variables) |
|
2419 | + { |
|
2420 | + $this->registeredVars = array_merge($this->registeredVars, $variables); |
|
2421 | + } |
|
2422 | + |
|
2423 | + public function unsetVariable($name) |
|
2424 | + { |
|
2425 | + unset($this->registeredVars[$name]); |
|
2426 | + } |
|
2427 | + |
|
2428 | + public function setImportDir($dirs) |
|
2429 | + { |
|
2430 | + $this->importDir = (array) $dirs; |
|
2431 | + } |
|
2432 | + |
|
2433 | + public function addImportDir($dir) |
|
2434 | + { |
|
2435 | + $this->importDir = (array) $this->importDir; |
|
2436 | + $this->importDir[] = $dir; |
|
2437 | + } |
|
2438 | + |
|
2439 | + public function allParsedFiles() |
|
2440 | + { |
|
2441 | + return $this->allParsedFiles; |
|
2442 | + } |
|
2443 | + |
|
2444 | + public function addParsedFile($file) |
|
2445 | + { |
|
2446 | + $this->allParsedFiles[realpath($file)] = filemtime($file); |
|
2447 | + } |
|
2448 | + |
|
2449 | + /** |
|
2450 | + * Uses the current value of $this->count to show line and line number |
|
2451 | + */ |
|
2452 | + public function throwError($msg = null) |
|
2453 | + { |
|
2454 | + if ($this->sourceLoc >= 0) { |
|
2455 | + $this->sourceParser->throwError($msg, $this->sourceLoc); |
|
2456 | + } |
|
2457 | + throw new exception($msg); |
|
2458 | + } |
|
2459 | + |
|
2460 | + // compile file $in to file $out if $in is newer than $out |
|
2461 | + // returns true when it compiles, false otherwise |
|
2462 | + public static function ccompile($in, $out, $less = null) |
|
2463 | + { |
|
2464 | + if ($less === null) { |
|
2465 | + $less = new self(); |
|
2466 | + } |
|
2467 | + return $less->checkedCompile($in, $out); |
|
2468 | + } |
|
2469 | + |
|
2470 | + public static function cexecute($in, $force = false, $less = null) |
|
2471 | + { |
|
2472 | + if ($less === null) { |
|
2473 | + $less = new self(); |
|
2474 | + } |
|
2475 | + return $less->cachedCompile($in, $force); |
|
2476 | + } |
|
2477 | + |
|
2478 | + protected static $cssColors = array( |
|
2479 | + 'aliceblue' => '240,248,255', |
|
2480 | + 'antiquewhite' => '250,235,215', |
|
2481 | + 'aqua' => '0,255,255', |
|
2482 | + 'aquamarine' => '127,255,212', |
|
2483 | + 'azure' => '240,255,255', |
|
2484 | + 'beige' => '245,245,220', |
|
2485 | + 'bisque' => '255,228,196', |
|
2486 | + 'black' => '0,0,0', |
|
2487 | + 'blanchedalmond' => '255,235,205', |
|
2488 | + 'blue' => '0,0,255', |
|
2489 | + 'blueviolet' => '138,43,226', |
|
2490 | + 'brown' => '165,42,42', |
|
2491 | + 'burlywood' => '222,184,135', |
|
2492 | + 'cadetblue' => '95,158,160', |
|
2493 | + 'chartreuse' => '127,255,0', |
|
2494 | + 'chocolate' => '210,105,30', |
|
2495 | + 'coral' => '255,127,80', |
|
2496 | + 'cornflowerblue' => '100,149,237', |
|
2497 | + 'cornsilk' => '255,248,220', |
|
2498 | + 'crimson' => '220,20,60', |
|
2499 | + 'cyan' => '0,255,255', |
|
2500 | + 'darkblue' => '0,0,139', |
|
2501 | + 'darkcyan' => '0,139,139', |
|
2502 | + 'darkgoldenrod' => '184,134,11', |
|
2503 | + 'darkgray' => '169,169,169', |
|
2504 | + 'darkgreen' => '0,100,0', |
|
2505 | + 'darkgrey' => '169,169,169', |
|
2506 | + 'darkkhaki' => '189,183,107', |
|
2507 | + 'darkmagenta' => '139,0,139', |
|
2508 | + 'darkolivegreen' => '85,107,47', |
|
2509 | + 'darkorange' => '255,140,0', |
|
2510 | + 'darkorchid' => '153,50,204', |
|
2511 | + 'darkred' => '139,0,0', |
|
2512 | + 'darksalmon' => '233,150,122', |
|
2513 | + 'darkseagreen' => '143,188,143', |
|
2514 | + 'darkslateblue' => '72,61,139', |
|
2515 | + 'darkslategray' => '47,79,79', |
|
2516 | + 'darkslategrey' => '47,79,79', |
|
2517 | + 'darkturquoise' => '0,206,209', |
|
2518 | + 'darkviolet' => '148,0,211', |
|
2519 | + 'deeppink' => '255,20,147', |
|
2520 | + 'deepskyblue' => '0,191,255', |
|
2521 | + 'dimgray' => '105,105,105', |
|
2522 | + 'dimgrey' => '105,105,105', |
|
2523 | + 'dodgerblue' => '30,144,255', |
|
2524 | + 'firebrick' => '178,34,34', |
|
2525 | + 'floralwhite' => '255,250,240', |
|
2526 | + 'forestgreen' => '34,139,34', |
|
2527 | + 'fuchsia' => '255,0,255', |
|
2528 | + 'gainsboro' => '220,220,220', |
|
2529 | + 'ghostwhite' => '248,248,255', |
|
2530 | + 'gold' => '255,215,0', |
|
2531 | + 'goldenrod' => '218,165,32', |
|
2532 | + 'gray' => '128,128,128', |
|
2533 | + 'green' => '0,128,0', |
|
2534 | + 'greenyellow' => '173,255,47', |
|
2535 | + 'grey' => '128,128,128', |
|
2536 | + 'honeydew' => '240,255,240', |
|
2537 | + 'hotpink' => '255,105,180', |
|
2538 | + 'indianred' => '205,92,92', |
|
2539 | + 'indigo' => '75,0,130', |
|
2540 | + 'ivory' => '255,255,240', |
|
2541 | + 'khaki' => '240,230,140', |
|
2542 | + 'lavender' => '230,230,250', |
|
2543 | + 'lavenderblush' => '255,240,245', |
|
2544 | + 'lawngreen' => '124,252,0', |
|
2545 | + 'lemonchiffon' => '255,250,205', |
|
2546 | + 'lightblue' => '173,216,230', |
|
2547 | + 'lightcoral' => '240,128,128', |
|
2548 | + 'lightcyan' => '224,255,255', |
|
2549 | + 'lightgoldenrodyellow' => '250,250,210', |
|
2550 | + 'lightgray' => '211,211,211', |
|
2551 | + 'lightgreen' => '144,238,144', |
|
2552 | + 'lightgrey' => '211,211,211', |
|
2553 | + 'lightpink' => '255,182,193', |
|
2554 | + 'lightsalmon' => '255,160,122', |
|
2555 | + 'lightseagreen' => '32,178,170', |
|
2556 | + 'lightskyblue' => '135,206,250', |
|
2557 | + 'lightslategray' => '119,136,153', |
|
2558 | + 'lightslategrey' => '119,136,153', |
|
2559 | + 'lightsteelblue' => '176,196,222', |
|
2560 | + 'lightyellow' => '255,255,224', |
|
2561 | + 'lime' => '0,255,0', |
|
2562 | + 'limegreen' => '50,205,50', |
|
2563 | + 'linen' => '250,240,230', |
|
2564 | + 'magenta' => '255,0,255', |
|
2565 | + 'maroon' => '128,0,0', |
|
2566 | + 'mediumaquamarine' => '102,205,170', |
|
2567 | + 'mediumblue' => '0,0,205', |
|
2568 | + 'mediumorchid' => '186,85,211', |
|
2569 | + 'mediumpurple' => '147,112,219', |
|
2570 | + 'mediumseagreen' => '60,179,113', |
|
2571 | + 'mediumslateblue' => '123,104,238', |
|
2572 | + 'mediumspringgreen' => '0,250,154', |
|
2573 | + 'mediumturquoise' => '72,209,204', |
|
2574 | + 'mediumvioletred' => '199,21,133', |
|
2575 | + 'midnightblue' => '25,25,112', |
|
2576 | + 'mintcream' => '245,255,250', |
|
2577 | + 'mistyrose' => '255,228,225', |
|
2578 | + 'moccasin' => '255,228,181', |
|
2579 | + 'navajowhite' => '255,222,173', |
|
2580 | + 'navy' => '0,0,128', |
|
2581 | + 'oldlace' => '253,245,230', |
|
2582 | + 'olive' => '128,128,0', |
|
2583 | + 'olivedrab' => '107,142,35', |
|
2584 | + 'orange' => '255,165,0', |
|
2585 | + 'orangered' => '255,69,0', |
|
2586 | + 'orchid' => '218,112,214', |
|
2587 | + 'palegoldenrod' => '238,232,170', |
|
2588 | + 'palegreen' => '152,251,152', |
|
2589 | + 'paleturquoise' => '175,238,238', |
|
2590 | + 'palevioletred' => '219,112,147', |
|
2591 | + 'papayawhip' => '255,239,213', |
|
2592 | + 'peachpuff' => '255,218,185', |
|
2593 | + 'peru' => '205,133,63', |
|
2594 | + 'pink' => '255,192,203', |
|
2595 | + 'plum' => '221,160,221', |
|
2596 | + 'powderblue' => '176,224,230', |
|
2597 | + 'purple' => '128,0,128', |
|
2598 | + 'red' => '255,0,0', |
|
2599 | + 'rosybrown' => '188,143,143', |
|
2600 | + 'royalblue' => '65,105,225', |
|
2601 | + 'saddlebrown' => '139,69,19', |
|
2602 | + 'salmon' => '250,128,114', |
|
2603 | + 'sandybrown' => '244,164,96', |
|
2604 | + 'seagreen' => '46,139,87', |
|
2605 | + 'seashell' => '255,245,238', |
|
2606 | + 'sienna' => '160,82,45', |
|
2607 | + 'silver' => '192,192,192', |
|
2608 | + 'skyblue' => '135,206,235', |
|
2609 | + 'slateblue' => '106,90,205', |
|
2610 | + 'slategray' => '112,128,144', |
|
2611 | + 'slategrey' => '112,128,144', |
|
2612 | + 'snow' => '255,250,250', |
|
2613 | + 'springgreen' => '0,255,127', |
|
2614 | + 'steelblue' => '70,130,180', |
|
2615 | + 'tan' => '210,180,140', |
|
2616 | + 'teal' => '0,128,128', |
|
2617 | + 'thistle' => '216,191,216', |
|
2618 | + 'tomato' => '255,99,71', |
|
2619 | + 'transparent' => '0,0,0,0', |
|
2620 | + 'turquoise' => '64,224,208', |
|
2621 | + 'violet' => '238,130,238', |
|
2622 | + 'wheat' => '245,222,179', |
|
2623 | + 'white' => '255,255,255', |
|
2624 | + 'whitesmoke' => '245,245,245', |
|
2625 | + 'yellow' => '255,255,0', |
|
2626 | + 'yellowgreen' => '154,205,50' |
|
2627 | + ); |
|
2628 | 2628 | } |
@@ -62,8 +62,8 @@ |
||
62 | 62 | */ |
63 | 63 | class lessc_formatter_lessjs extends lessc_formatter_classic |
64 | 64 | { |
65 | - public $disableSingle = true; |
|
66 | - public $breakSelectors = true; |
|
67 | - public $assignSeparator = ": "; |
|
68 | - public $selectorSeparator = ","; |
|
65 | + public $disableSingle = true; |
|
66 | + public $breakSelectors = true; |
|
67 | + public $assignSeparator = ": "; |
|
68 | + public $selectorSeparator = ","; |
|
69 | 69 | } |
@@ -62,15 +62,15 @@ |
||
62 | 62 | */ |
63 | 63 | class lessc_formatter_compressed extends lessc_formatter_classic |
64 | 64 | { |
65 | - public $disableSingle = true; |
|
66 | - public $open = "{"; |
|
67 | - public $selectorSeparator = ","; |
|
68 | - public $assignSeparator = ":"; |
|
69 | - public $break = ""; |
|
70 | - public $compressColors = true; |
|
65 | + public $disableSingle = true; |
|
66 | + public $open = "{"; |
|
67 | + public $selectorSeparator = ","; |
|
68 | + public $assignSeparator = ":"; |
|
69 | + public $break = ""; |
|
70 | + public $compressColors = true; |
|
71 | 71 | |
72 | - public function indentStr($n = 0) |
|
73 | - { |
|
74 | - return ""; |
|
75 | - } |
|
72 | + public function indentStr($n = 0) |
|
73 | + { |
|
74 | + return ""; |
|
75 | + } |
|
76 | 76 | } |
@@ -607,7 +607,7 @@ discard block |
||
607 | 607 | } |
608 | 608 | } |
609 | 609 | |
610 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
610 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
611 | 611 | /** |
612 | 612 | * Set draft status |
613 | 613 | * |
@@ -617,7 +617,7 @@ discard block |
||
617 | 617 | */ |
618 | 618 | public function setDraft($user, $idwarehouse = -1) |
619 | 619 | { |
620 | - //phpcs:enable |
|
620 | + //phpcs:enable |
|
621 | 621 | global $conf, $langs; |
622 | 622 | |
623 | 623 | $error = 0; |
@@ -697,7 +697,7 @@ discard block |
||
697 | 697 | } |
698 | 698 | |
699 | 699 | |
700 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
700 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
701 | 701 | /** |
702 | 702 | * Tag the order as validated (opened) |
703 | 703 | * Function used when order is reopend after being closed. |
@@ -707,7 +707,7 @@ discard block |
||
707 | 707 | */ |
708 | 708 | public function set_reopen($user) |
709 | 709 | { |
710 | - // phpcs:enable |
|
710 | + // phpcs:enable |
|
711 | 711 | $error = 0; |
712 | 712 | |
713 | 713 | if ($this->statut != self::STATUS_CANCELED && $this->statut != self::STATUS_CLOSED) { |
@@ -1737,7 +1737,7 @@ discard block |
||
1737 | 1737 | } |
1738 | 1738 | |
1739 | 1739 | |
1740 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
1740 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
1741 | 1741 | /** |
1742 | 1742 | * Add line into array |
1743 | 1743 | * $this->client must be loaded |
@@ -1753,7 +1753,7 @@ discard block |
||
1753 | 1753 | */ |
1754 | 1754 | public function add_product($idproduct, $qty, $remise_percent = 0.0, $date_start = '', $date_end = '') |
1755 | 1755 | { |
1756 | - // phpcs:enable |
|
1756 | + // phpcs:enable |
|
1757 | 1757 | global $conf, $mysoc; |
1758 | 1758 | |
1759 | 1759 | if (!$qty) { |
@@ -1989,7 +1989,7 @@ discard block |
||
1989 | 1989 | } |
1990 | 1990 | |
1991 | 1991 | |
1992 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
1992 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
1993 | 1993 | /** |
1994 | 1994 | * Add line of fixed discount in the order in DB |
1995 | 1995 | * |
@@ -1998,7 +1998,7 @@ discard block |
||
1998 | 1998 | */ |
1999 | 1999 | public function insert_discount($idremise) |
2000 | 2000 | { |
2001 | - // phpcs:enable |
|
2001 | + // phpcs:enable |
|
2002 | 2002 | global $langs; |
2003 | 2003 | |
2004 | 2004 | include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php'; |
@@ -2058,7 +2058,7 @@ discard block |
||
2058 | 2058 | } |
2059 | 2059 | |
2060 | 2060 | |
2061 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2061 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2062 | 2062 | /** |
2063 | 2063 | * Load array lines |
2064 | 2064 | * |
@@ -2068,7 +2068,7 @@ discard block |
||
2068 | 2068 | */ |
2069 | 2069 | public function fetch_lines($only_product = 0, $loadalsotranslation = 0) |
2070 | 2070 | { |
2071 | - // phpcs:enable |
|
2071 | + // phpcs:enable |
|
2072 | 2072 | global $langs, $conf; |
2073 | 2073 | |
2074 | 2074 | $this->lines = array(); |
@@ -2335,7 +2335,7 @@ discard block |
||
2335 | 2335 | return 0; |
2336 | 2336 | } |
2337 | 2337 | |
2338 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2338 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2339 | 2339 | /** |
2340 | 2340 | * Return a array with the pending stock by product |
2341 | 2341 | * |
@@ -2426,7 +2426,7 @@ discard block |
||
2426 | 2426 | } |
2427 | 2427 | } |
2428 | 2428 | |
2429 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2429 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2430 | 2430 | /** |
2431 | 2431 | * Applique une remise relative |
2432 | 2432 | * |
@@ -2439,7 +2439,7 @@ discard block |
||
2439 | 2439 | */ |
2440 | 2440 | public function set_remise($user, $remise, $notrigger = 0) |
2441 | 2441 | { |
2442 | - // phpcs:enable |
|
2442 | + // phpcs:enable |
|
2443 | 2443 | dol_syslog(get_class($this) . "::set_remise is deprecated, use setDiscount instead", LOG_NOTICE); |
2444 | 2444 | // @phan-suppress-next-line PhanDeprecatedFunction |
2445 | 2445 | return $this->setDiscount($user, $remise, $notrigger); |
@@ -2507,7 +2507,7 @@ discard block |
||
2507 | 2507 | } |
2508 | 2508 | |
2509 | 2509 | |
2510 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2510 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2511 | 2511 | /** |
2512 | 2512 | * Set a fixed amount discount |
2513 | 2513 | * |
@@ -2574,7 +2574,7 @@ discard block |
||
2574 | 2574 | } |
2575 | 2575 | */ |
2576 | 2576 | |
2577 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2577 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2578 | 2578 | /** |
2579 | 2579 | * Set the order date |
2580 | 2580 | * |
@@ -2585,7 +2585,7 @@ discard block |
||
2585 | 2585 | */ |
2586 | 2586 | public function set_date($user, $date, $notrigger = 0) |
2587 | 2587 | { |
2588 | - // phpcs:enable |
|
2588 | + // phpcs:enable |
|
2589 | 2589 | if ($user->hasRight('commande', 'creer')) { |
2590 | 2590 | $error = 0; |
2591 | 2591 | |
@@ -2632,7 +2632,7 @@ discard block |
||
2632 | 2632 | } |
2633 | 2633 | } |
2634 | 2634 | |
2635 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2635 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2636 | 2636 | /** |
2637 | 2637 | * Set delivery date |
2638 | 2638 | * |
@@ -2644,7 +2644,7 @@ discard block |
||
2644 | 2644 | */ |
2645 | 2645 | public function set_date_livraison($user, $delivery_date, $notrigger = 0) |
2646 | 2646 | { |
2647 | - // phpcs:enable |
|
2647 | + // phpcs:enable |
|
2648 | 2648 | return $this->setDeliveryDate($user, $delivery_date, $notrigger); |
2649 | 2649 | } |
2650 | 2650 | |
@@ -2704,7 +2704,7 @@ discard block |
||
2704 | 2704 | } |
2705 | 2705 | } |
2706 | 2706 | |
2707 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2707 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2708 | 2708 | /** |
2709 | 2709 | * Return list of orders (eventuelly filtered on a user) into an array |
2710 | 2710 | * |
@@ -2720,7 +2720,7 @@ discard block |
||
2720 | 2720 | */ |
2721 | 2721 | public function liste_array($shortlist = 0, $draft = 0, $excluser = null, $socid = 0, $limit = 0, $offset = 0, $sortfield = 'c.date_commande', $sortorder = 'DESC') |
2722 | 2722 | { |
2723 | - // phpcs:enable |
|
2723 | + // phpcs:enable |
|
2724 | 2724 | global $user; |
2725 | 2725 | |
2726 | 2726 | $ga = array(); |
@@ -2840,7 +2840,7 @@ discard block |
||
2840 | 2840 | } |
2841 | 2841 | } |
2842 | 2842 | |
2843 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2843 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2844 | 2844 | /** |
2845 | 2845 | * Update order demand_reason |
2846 | 2846 | * |
@@ -2850,7 +2850,7 @@ discard block |
||
2850 | 2850 | */ |
2851 | 2851 | public function demand_reason($demand_reason_id, $notrigger = 0) |
2852 | 2852 | { |
2853 | - // phpcs:enable |
|
2853 | + // phpcs:enable |
|
2854 | 2854 | global $user; |
2855 | 2855 | |
2856 | 2856 | dol_syslog('Commande::demand_reason(' . $demand_reason_id . ')'); |
@@ -2904,7 +2904,7 @@ discard block |
||
2904 | 2904 | } |
2905 | 2905 | } |
2906 | 2906 | |
2907 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2907 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
2908 | 2908 | /** |
2909 | 2909 | * Set customer ref |
2910 | 2910 | * |
@@ -2915,7 +2915,7 @@ discard block |
||
2915 | 2915 | */ |
2916 | 2916 | public function set_ref_client($user, $ref_client, $notrigger = 0) |
2917 | 2917 | { |
2918 | - // phpcs:enable |
|
2918 | + // phpcs:enable |
|
2919 | 2919 | if ($user->hasRight('commande', 'creer')) { |
2920 | 2920 | $error = 0; |
2921 | 2921 | |
@@ -3546,7 +3546,7 @@ discard block |
||
3546 | 3546 | } |
3547 | 3547 | |
3548 | 3548 | |
3549 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
3549 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
3550 | 3550 | /** |
3551 | 3551 | * Load indicators for dashboard (this->nbtodo and this->nbtodolate) |
3552 | 3552 | * |
@@ -3556,7 +3556,7 @@ discard block |
||
3556 | 3556 | */ |
3557 | 3557 | public function load_board($user, $mode) |
3558 | 3558 | { |
3559 | - // phpcs:enable |
|
3559 | + // phpcs:enable |
|
3560 | 3560 | global $conf, $langs; |
3561 | 3561 | |
3562 | 3562 | $clause = " WHERE"; |
@@ -3665,7 +3665,7 @@ discard block |
||
3665 | 3665 | return $this->LibStatut($this->statut, $this->billed, $mode); |
3666 | 3666 | } |
3667 | 3667 | |
3668 | - // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
3668 | + // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
|
3669 | 3669 | /** |
3670 | 3670 | * Return label of status |
3671 | 3671 | * |
@@ -3677,7 +3677,7 @@ discard block |
||
3677 | 3677 | */ |
3678 | 3678 | public function LibStatut($status, $billed, $mode, $donotshowbilled = 0) |
3679 | 3679 | { |
3680 | - // phpcs:enable |
|
3680 | + // phpcs:enable |
|
3681 | 3681 | global $langs, $hookmanager; |
3682 | 3682 | |
3683 | 3683 | $billedtext = ''; |