Skip to content

Commit

Permalink
Merge pull request #495 from SinergiaTIC/develop
Browse files Browse the repository at this point in the history
SinergiaCRM 1.7.3 Release merge (#495)
  • Loading branch information
ManuSinergiaCRM authored Nov 28, 2024
2 parents b4d1206 + f269a80 commit 22a2d8b
Show file tree
Hide file tree
Showing 3,000 changed files with 269,445 additions and 655 deletions.
15 changes: 13 additions & 2 deletions SticInclude/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ public static function createMandate()
}

/**
* Set the proper decimal separator according to the user/system configuration
* Set value with appropriate separators according to user/system configuration
*
* @param Decimal $decimalValue
* @param Boolean $userSetting. Indicates whether to choose user or system configuration
Expand All @@ -494,13 +494,24 @@ public static function formatDecimalInConfigSettings($decimalValue, $userSetting
{
global $current_user, $sugar_config;

// Get the user preferences for the thousands and decimal separator and the number of decimal places
if ($userSetting) {
// User decimal separator
$user_dec_sep = (!empty($current_user->id) ? $current_user->getPreference('dec_sep') : null);
// User thousands separator
$user_grp_sep = (!empty($current_user->id) ? $current_user->getPreference('num_grp_sep') : null);
// User number of decimal places
$user_sig_digits = (!empty($current_user->id) ? $current_user->getPreference('default_currency_significant_digits') : null);
}

// Set the user preferences or the default preferences
$dec_sep = empty($user_dec_sep) ? $sugar_config['default_decimal_seperator'] : $user_dec_sep;
$grp_sep = empty($user_grp_sep) ? $sugar_config['default_number_grouping_seperator'] : $user_grp_sep;
$sig_digits = empty($user_sig_digits) ? $sugar_config['default_currency_significant_digits'] : $user_sig_digits;

return str_replace('.', $dec_sep, $decimalValue);
// Format the number
$value = number_format($decimalValue, $sig_digits, $dec_sep, $grp_sep);
return $value;
}

/**
Expand Down
519 changes: 519 additions & 0 deletions SticInclude/rector/SticRectorConfig.php

Large diffs are not rendered by default.

294 changes: 294 additions & 0 deletions SticInclude/rector/UpdateToPhp8RectorCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,294 @@
<?php
/**
* This file is part of SinergiaCRM.
* SinergiaCRM is a work developed by SinergiaTIC Association, based on SuiteCRM.
* Copyright (C) 2013 - 2023 SinergiaTIC Association
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by the
* Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License along with
* this program; if not, see http://www.gnu.org/licenses or write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*
* You can contact SinergiaTIC Association at email address [email protected].
*/

/**
* This script executes Rector during the update of SinergiaCRM instances,
* using the Rector configuration defined in SticRectorConfig.php on the root folder
*
* Without arguments, saves suggested changes to file
* With argument "adapt-code", modifies the code
*
*/

$cacheDirectory = __DIR__ . '/../../cache/rector_cached_files';
$output = new RectorCheckResultHelper(__DIR__."/../../rectorOutput.html", $cacheDirectory);

$rectorFile = __DIR__."/../vendor/rector-standalone/vendor/rector/rector/bin/rector.php";
$rectorConfigFile = __DIR__."/SticRectorConfig.php";

$output->addInfo($rectorFile, "Rector");
$output->addInfo($rectorConfigFile, "Rector config");

if (!file_exists($rectorFile)) {
$output->addFatalError("Rector is not installed. Can not run script");
$output->closeAndExit(1);
}

if (!file_exists($rectorConfigFile)) {
$output->addFatalError("Missing Rector config file. Can not run script");
$output->closeAndExit(1);
}

// Get command arguments
$arguments = $_SERVER['argv'];
$scriptName = array_shift($arguments);
$adaptCode = count($arguments) > 0 && $arguments[0] == "adapt-code";

$rectorCommand = "php {$rectorFile} process --no-progress-bar --config={$rectorConfigFile}";// --output-format json";
if (!$adaptCode) {
$rectorCommand .= " --dry-run";
}
$output->addInfo($rectorCommand, "Command");


$descriptorspec = [
1 => ['pipe', 'w'], // stdout
2 => ['pipe', 'w'], // stderr
];

// Open process
$process = proc_open($rectorCommand, $descriptorspec, $pipes);
if (is_resource($process)) {
// Read standard output
$stdout = stream_get_contents($pipes[1]);
$stdout = str_replace("\\n", "\n", $stdout);
$output->setResults(htmlspecialchars($stdout));
fclose($pipes[1]);

// Read error output
$stderr = stream_get_contents($pipes[2]);
$stderr = str_replace("\\n", "\n", $stderr);
$output->setErrorResults(htmlspecialchars($stderr));
fclose($pipes[2]);

// Close process
$returnCode = proc_close($process);
$output->closeAndExit($returnCode);
} else {
$output->addFatalError("Can not start Rector process");
$output->closeAndExit(1);
}

final class RectorCheckResultHelper {
private $startDate;
private $fileName;
private $cacheDirectory;
private $fatalErrors = [];
private $infos = [];
private $results = [];
private $errorResults = [];
private $isAllOk = false;
private $numFilesWithChanges = -1;

public function __construct($fileName, $cacheDirectory)
{
$this->startDate = date('Y-m-d H:i:s');

$this->cacheDirectory = $cacheDirectory;
$this->cleanCache();

$this->fileName = $fileName;
if (file_exists($this->fileName)) {
unlink($this->fileName);
}
}

private function cleanCache() {
$this->deleteDirectoryTree($this->cacheDirectory);
}

private function deleteDirectoryTree($dir) {
if (!is_dir($dir)) {
return false;
}

$items = scandir($dir);
foreach ($items as $item) {
if ($item === '.' || $item === '..') {
continue;
}

$path = $dir . '/' . $item;
if (is_dir($path)) {
// Remove sub-Directories
$this->deleteDirectoryTree($path);
} else {
// Remove file
unlink($path);
}
}

// Remove empty directory
return rmdir($dir);
}

private function getLabelText($text, $label = "", $newLine = false)
{
if(!empty($label)) {
$text = "<strong>{$label}</strong>: {$text}";
}
return $text. ($newLine ? "\n" : "");
}
private function getTitle($text, $newLine = false) {
return "<h3>{$text}</h3>" . ($newLine ? "\n" : "");
}

public function addInfo($text, $label = "")
{
$this->infos[] = $this->getLabelText($text, $label);
}

public function addFatalError($text)
{
$this->fatalErrors[] = $text;
}

public function setResults($text)
{
$textArray = explode("\n", $text);
$fileChanges = [];
$endingFile = false;
$fileWithChanges = false;
$isInDiff = false;
if (count($textArray) > 0) {
$this->numFilesWithChanges = 0;
}
for ($i = 0; $i < count($textArray); $i++) {

if ($textArray[$i] == "=====================") {
$fileChanges = [];
$endingFile = false;
}
if (empty($textArray[$i]) ||
strpos($textArray[$i], "files with changes") !== false ||
$textArray[$i] == "=====================") {
continue;
}
if ($textArray[$i] == " ---------- begin diff ----------") {
$fileChanges[count($fileChanges)-1] = "<hr /> <strong>" . $fileChanges[count($fileChanges)-1] . "</strong>";
$fileChanges[] = $textArray[$i];
$fileWithChanges = false;
$isInDiff = true;
}
else if ($textArray[$i] == "@@ @@") {
if (end($this->results) != " ---------- begin diff ----------") {
$fileChanges[] = "</pre></details>";
}
$fileChanges[] = "<details open><summary>" . $textArray[$i] . "</summary><pre style='background:lightyellow; display: inline-block;'>";
}
else if ($textArray[$i] == " ----------- end diff -----------") {
$fileChanges[] = "</pre></details>" . $textArray[$i];
$fileChanges[] = "";
$endingFile = true;
$isInDiff = false;
}
else if ($isInDiff && strpos($textArray[$i], "-") === 0) {
$fileChanges[] = "<div style='color:red; background:lightpink; display:inline;'>" . $textArray[$i] . "</div>";
}
else if ($isInDiff && strpos($textArray[$i], "+") === 0) {
$fileChanges[] = "<div style='color:green; background:lightgreen; display:inline;'>" . $textArray[$i] . "</div>";
}
else {
if ($endingFile) {
// Only show files with applied rules (real changes)
if ($textArray[$i] == "Applied rules:" ||
strpos($textArray[$i], " * ") === 0) {
$fileChanges[] = "<strong>" . $textArray[$i] . "</strong>";
$fileWithChanges = true;
} else {
if ($fileWithChanges) {
$this->results = [...$this->results, ...$fileChanges];
$this->results[] = "";
$this->results[] = "";
$this->numFilesWithChanges++;
}
$endingFile = false;
$fileChanges = [];
$fileChanges[] = $textArray[$i];
}
} else {
$fileChanges[] = $textArray[$i];
}
}
}
$this->isAllOk = (strpos($text, "[OK] Rector is done!") !== false);
}

public function setErrorResults($text)
{
$this->errorResults = explode("\n", $text);
}

private function getIsAllOk($resultCode) {
$this->isAllOk = ($this->isAllOk && $resultCode == 0) ||
($this->numFilesWithChanges == 0 && ($resultCode == 2 || $resultCode == 0) && count($this->fatalErrors) == 0);
return $this->isAllOk;
}

public function closeAndExit($resultCode)
{
$this->cleanCache();

if (!$this->getIsAllOk($resultCode)) {
$content = "Result: {$resultCode} <br />\n";

$this->addInfo($resultCode, "Result");

// Last Execution
$content .= $this->getTitle("Execution", true);
$content .= $this->getLabelText($this->startDate . "<br />", "Start", true);
$content .= $this->getLabelText(date('Y-m-d H:i:s') . "<br />", "End", true);

// Fatal Errors
if (count($this->fatalErrors) > 0) {
$content .= $this->getTitle("Error", true);
$content .= implode("<br />\n", $this->fatalErrors);
$content .= "<br />\n";
}

// Informations
if (count($this->infos) > 0) {
$content .= $this->getTitle("Information", true);
$content .= implode("<br />\n", $this->infos);
$content .= "<br />\n";
}

// Results
if (count($this->results) > 0) {
$content .= $this->getTitle("Result", true);
$content .= implode("<br />\n", $this->results);
$content .= "<br />\n";
}

// Error Results
if (count($this->results) > 0) {
$content .= $this->getTitle("Rector Errors", true);
$content .= implode("<br />\n", $this->errorResults);
$content .= "<br />\n";
}

file_put_contents($this->fileName, $content);
}
exit($resultCode);
}
}
5 changes: 5 additions & 0 deletions SticInclude/vendor/rector-standalone/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require-dev": {
"rector/rector": "^1.2"
}
}
Loading

0 comments on commit 22a2d8b

Please sign in to comment.