1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OroCRM\Bundle\TaskBundle\Migrations\Schema\v1_10; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\ConnectionException; |
6
|
|
|
|
7
|
|
|
use Psr\Log\LoggerInterface; |
8
|
|
|
|
9
|
|
|
use Oro\Bundle\MigrationBundle\Migration\ParametrizedMigrationQuery; |
10
|
|
|
|
11
|
|
|
class UpdateReminderEmailTemplates extends ParametrizedMigrationQuery |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* {@inheritdoc} |
15
|
|
|
*/ |
16
|
|
|
public function getDescription() |
17
|
|
|
{ |
18
|
|
|
return 'Update date format in reminder email templates using recipient organization localization settings'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function execute(LoggerInterface $logger) |
25
|
|
|
{ |
26
|
|
|
$dateFilterPattern = "|date('F j, Y, g:i A')"; |
27
|
|
|
$calendarRangePattern[] = 'calendar_date_range(entity.start, entity.end, entity.allDay, 1)'; |
|
|
|
|
28
|
|
|
$calendarRangePattern[] = "calendar_date_range(entity.start, entity.end, entity.allDay, 'F j, Y', 1)"; |
29
|
|
|
$dateFilterReplacement = "|oro_format_datetime_organization({'organization': entity.organization})"; |
30
|
|
|
$calendarRangeReplacement = 'calendar_date_range_organization(entity.start,' . |
31
|
|
|
' entity.end, entity.allDay, 1, null, null, null, entity.organization)'; |
32
|
|
|
$this->updateReminderTemplates( |
33
|
|
|
$logger, |
34
|
|
|
'task_reminder', |
35
|
|
|
$dateFilterPattern, |
36
|
|
|
$dateFilterReplacement, |
37
|
|
|
$calendarRangePattern, |
38
|
|
|
$calendarRangeReplacement |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param LoggerInterface $logger |
44
|
|
|
* @param string $templateName |
45
|
|
|
* @param string|array $dateFilterPattern |
46
|
|
|
* @param string $dateFilterReplacement |
47
|
|
|
* @param string|array $calendarRangePattern |
48
|
|
|
* @param string $calendarRangeReplacement |
49
|
|
|
* |
50
|
|
|
* @throws ConnectionException |
51
|
|
|
* @throws \Exception |
52
|
|
|
*/ |
53
|
|
|
protected function updateReminderTemplates( |
54
|
|
|
LoggerInterface $logger, |
55
|
|
|
$templateName, |
56
|
|
|
$dateFilterPattern, |
57
|
|
|
$dateFilterReplacement, |
58
|
|
|
$calendarRangePattern, |
59
|
|
|
$calendarRangeReplacement |
60
|
|
|
) { |
61
|
|
|
$sql = 'SELECT * FROM oro_email_template WHERE name = :name ORDER BY id'; |
62
|
|
|
$parameters = ['name' => $templateName]; |
63
|
|
|
$types = ['name' => 'string']; |
64
|
|
|
|
65
|
|
|
$this->logQuery($logger, $sql, $parameters, $types); |
66
|
|
|
$templates = $this->connection->fetchAll($sql, $parameters, $types); |
67
|
|
|
|
68
|
|
|
try { |
69
|
|
|
$this->connection->beginTransaction(); |
70
|
|
|
foreach ($templates as $template) { |
71
|
|
|
$subject = str_replace($dateFilterPattern, $dateFilterReplacement, $template['subject']); |
72
|
|
|
$content = str_replace($dateFilterPattern, $dateFilterReplacement, $template['content']); |
73
|
|
|
$content = str_replace($calendarRangePattern, $calendarRangeReplacement, $content); |
74
|
|
|
$this->connection->update( |
75
|
|
|
'oro_email_template', |
76
|
|
|
['subject' => $subject, 'content' => $content], |
77
|
|
|
['id' => $template['id']] |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
$this->connection->commit(); |
81
|
|
|
} catch (\Exception $e) { |
82
|
|
|
$this->connection->rollBack(); |
83
|
|
|
throw $e; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.