-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
207 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
70 changes: 70 additions & 0 deletions
70
src/migrations/m210810_120000_update_segment_field_columns.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace putyourlightson\campaign\migrations; | ||
|
||
use Craft; | ||
use craft\base\FieldInterface; | ||
use craft\db\Migration; | ||
use putyourlightson\campaign\elements\SegmentElement; | ||
use putyourlightson\campaign\helpers\SegmentHelper; | ||
|
||
class m210810_120000_update_segment_field_columns extends Migration | ||
{ | ||
// Public Methods | ||
// ========================================================================= | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeUp() | ||
{ | ||
$fields = Craft::$app->fields->getAllFields(); | ||
|
||
foreach ($fields as $field) { | ||
$this->_updateFieldColumn($field); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeDown(): bool | ||
{ | ||
echo self::class." cannot be reverted.\n"; | ||
|
||
return false; | ||
} | ||
|
||
private function _updateFieldColumn(FieldInterface $field) | ||
{ | ||
$newFieldColumn = SegmentHelper::fieldColumnFromField($field); | ||
$oldFieldColumn = 'field_'.$field->handle; | ||
|
||
if ($newFieldColumn && $newFieldColumn == $oldFieldColumn) { | ||
return; | ||
} | ||
|
||
$updated = false; | ||
|
||
$segments = SegmentElement::find() | ||
->status(null) | ||
->all(); | ||
|
||
foreach ($segments as $segment) { | ||
foreach ($segment->conditions as &$andCondition) { | ||
foreach ($andCondition as &$orCondition) { | ||
if ($orCondition[1] == $oldFieldColumn) { | ||
$orCondition[1] = $newFieldColumn; | ||
$updated = true; | ||
} | ||
} | ||
} | ||
|
||
if ($updated) { | ||
Craft::$app->elements->saveElement($segment); | ||
} | ||
} | ||
} | ||
} |
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
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