Last active
October 12, 2024 08:49
-
-
Save byStrange/7efe7bfc5970f6ffe97511bffdc60ca9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace app\components; | |
use Exception; | |
use Yii; | |
use yii\base\Component; | |
use yii\filters\AccessControl; | |
use yii\helpers\Html; | |
use yii\helpers\Url; | |
class Utils extends Component | |
{ | |
const languages_list = ['uz' => 'uz-UZ', 'en' => 'en-US', 'ru' => 'ru-RU']; | |
const localEmailDir = '@app/mail'; | |
public static function preSelectOptions($existingRelation) | |
{ | |
if (empty($existingRelation)) { | |
return []; | |
} | |
$options = []; | |
$existingIds = array_column($existingRelation, "id"); | |
foreach ($existingIds as $id) { | |
$options[$id] = [ | |
"selected" => in_array($id, $existingIds), | |
]; | |
} | |
return $options; | |
} | |
public static function uploadImage( | |
$image, | |
$outputFolder = "uploads", | |
$generateImageId = null | |
) { | |
if (!$image || !$image->tempName) { | |
return false; | |
} | |
if (!is_dir($outputFolder)) { | |
if (!mkdir($outputFolder, 0755, true)) { | |
throw new Exception( | |
"Failed to create upload directory: $outputFolder" | |
); | |
} | |
} | |
$filename = $generateImageId | |
? $generateImageId($image) . "." . $image->extension | |
: $image->baseName . uniqid() . "." . $image->extension; | |
$filePath = $outputFolder . "/" . $filename; | |
if (!$image->saveAs($filePath)) { | |
throw new Exception("Failed to save uploaded image to $filePath"); | |
} | |
return $filePath; | |
} | |
public static function printAsError($text, $die = true) | |
{ | |
echo '<pre>'; | |
var_dump($text); | |
echo '</pre>'; | |
if ($die) die; | |
} | |
public static function daysInSeconds(int|float $days) | |
{ | |
return $days * 24 * 60 * 60; | |
} | |
public static function sendEmail($to, $subject, $content) | |
{ | |
$timestamp = date('Y-m-d_H-i-s'); | |
$filename = Yii::getAlias(self::localEmailDir) . "/{$timestamp}_{$to}.eml"; | |
$headers = implode("\r\n", [ | |
"To: {$to}", | |
"Subject: {$subject}", | |
"X-Mailer: PHP/" . PHP_VERSION, | |
"MIME-Version: 1.0", | |
"Content-Type: text/html; charset=UTF-8" | |
]); | |
$fullContent = $headers . "\r\n\r\n" . $content; | |
if (!is_dir(dirname($filename))) { | |
mkdir(dirname($filename), 0777, true); | |
} | |
return file_put_contents($filename, $fullContent) !== false; | |
} | |
# send email for real | |
public static function sendEmailFr($to, $subject, $content) | |
{ | |
try { | |
$result = Yii::$app->mailer->compose() | |
->setFrom('[email protected]') | |
->setTo($to) | |
->setSubject($subject) | |
->setHtmlBody($content) | |
->send(); | |
if (!$result) { | |
Yii::error("Failed to send email to {$to}", 'email'); | |
} | |
return $result; | |
} catch (\Exception $e) { | |
Yii::error("Error sending email to {$to}: " . $e->getMessage(), 'email'); | |
return false; | |
} | |
} | |
public static function crudActionsDisableOnly($actions, $extraActions = []) | |
{ | |
$crud_actions = ['create', 'view', 'update', 'delete', 'index']; | |
$crud_actions = array_merge($crud_actions, $extraActions); | |
$remaining = array_diff($crud_actions, $actions); | |
return [ | |
"class" => AccessControl::class, | |
"rules" => [ | |
[ | |
"allow" => false, | |
"actions" => $actions | |
], | |
[ | |
"allow" => true, | |
"actions" => $remaining, | |
] | |
] | |
]; | |
} | |
public static function find($array, $callback) | |
{ | |
if (!$array || !count($array)) return null; | |
foreach ($array as $item) { | |
if ($callback($item)) { | |
return $item; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment