Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Service api changes #3000

Merged
merged 5 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

### Added
- Added a `leaves` element query param that limits the selected elements to just the leaves in the structure (elements without children).
- Added `craft\services\Categories::deleteGroup()`. ([#3000](https://github.com/craftcms/cms/pull/3000))
- Added `craft\services\Tags::deleteTagGroup()`. ([#3000](https://github.com/craftcms/cms/pull/3000))
- Added `craft\services\UserGroups::deleteGroup()`. ([#3000](https://github.com/craftcms/cms/pull/3000))

### Removed
- Removed `craft\services\Fields::updateFieldVersionAfterRequest()`.
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/AssetTransformsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public function actionDeleteTransform(): Response

$transformId = Craft::$app->getRequest()->getRequiredBodyParam('id');

Craft::$app->getAssetTransforms()->deleteTransform($transformId);
Craft::$app->getAssetTransforms()->deleteTransformById($transformId);

return $this->asJson(['success' => true]);
}
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/TagsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ public function actionDeleteTagGroup(): Response
$this->requireAcceptsJson();
$this->requireAdmin();

$sectionId = Craft::$app->getRequest()->getRequiredBodyParam('id');
$groupId = Craft::$app->getRequest()->getRequiredBodyParam('id');

Craft::$app->getTags()->deleteTagGroupById($sectionId);
Craft::$app->getTags()->deleteTagGroupById($groupId);

return $this->asJson(['success' => true]);
}
Expand Down
38 changes: 33 additions & 5 deletions src/services/AssetTransforms.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use yii\base\Application;
use yii\base\Component;
use yii\base\ErrorException;
use yii\base\InvalidArgumentException;

/**
* Asset Transforms service.
Expand Down Expand Up @@ -253,16 +254,43 @@ public function saveTransform(AssetTransform $transform, bool $runValidation = t
}

/**
* Deletes an asset transform by its id.
* Deletes an asset transform by its ID.
*
* @param int $transformId
* @return bool
* @param int $transformId The transform's ID
* @return bool Whether the transform was deleted.
* @throws \yii\db\Exception on DB error
*/
public function deleteTransform(int $transformId): bool
public function deleteTransformById(int $transformId): bool
{
$transform = $this->getTransformById($transformId);

if (!$transform) {
return false;
}

return $this->deleteTransform($transform);
}

/**
* Deletes an asset transform.
*
* Note that passing an ID to this function is now deprecated. Use [[deleteTransformById()]] instead.
*
* @param int|AssetTransform $transform The transform
* @return bool Whether the transform was deleted
* @throws \yii\db\Exception on DB error
*/
public function deleteTransform($transform): bool
{
// todo: remove this code in 3.0 & hardcode the $transform type
if (is_int($transform)) {
Craft::$app->getDeprecator()->log(self::class.'::deleteTransform(id)', self::class.'::deleteTransform() should only be called with a '.AssetTransform::class.' reference. Use '.self::class.'::deleteTransformById() to get a transform by its ID.');
return $this->deleteTransformById($transform);
}
if (!$transform instanceof AssetTransform) {
throw new InvalidArgumentException('$transform must be a '.AssetTransform::class.' object.');
}

// Fire a 'beforeDeleteAssetTransform' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_ASSET_TRANSFORM)) {
$this->trigger(self::EVENT_BEFORE_DELETE_ASSET_TRANSFORM, new AssetTransformEvent([
Expand All @@ -273,7 +301,7 @@ public function deleteTransform(int $transformId): bool
Craft::$app->getDb()->createCommand()
->delete(
'{{%assettransforms}}',
['id' => $transformId])
['id' => $transform->id])
->execute();

// Fire an 'afterDeleteAssetTransform' event
Expand Down
19 changes: 15 additions & 4 deletions src/services/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public function saveGroup(CategoryGroup $group, bool $runValidation = true): boo
/**
* Deletes a category group by its ID.
*
* @param int $groupId
* @param int $groupId The category group's ID
* @return bool Whether the category group was deleted successfully
* @throws \Throwable if reasons
*/
Expand All @@ -507,6 +507,17 @@ public function deleteGroupById(int $groupId): bool
return false;
}

return $this->deleteGroup($group);
}

/**
* Deletes a category group.
*
* @param CategoryGroup $group The category group
* @return bool Whether the category group was deleted successfully
*/
public function deleteGroup(CategoryGroup $group): bool
{
// Fire a 'beforeDeleteGroup' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_GROUP)) {
$this->trigger(self::EVENT_BEFORE_DELETE_GROUP, new CategoryGroupEvent([
Expand All @@ -520,7 +531,7 @@ public function deleteGroupById(int $groupId): bool
$fieldLayoutId = (new Query())
->select(['fieldLayoutId'])
->from(['{{%categorygroups}}'])
->where(['id' => $groupId])
->where(['id' => $group->id])
->scalar();

if ($fieldLayoutId) {
Expand All @@ -531,7 +542,7 @@ public function deleteGroupById(int $groupId): bool
$categories = Category::find()
->status(null)
->enabledForSite(false)
->groupId($groupId)
->groupId($group->id)
->all();

foreach ($categories as $category) {
Expand All @@ -541,7 +552,7 @@ public function deleteGroupById(int $groupId): bool
Craft::$app->getDb()->createCommand()
->delete(
'{{%categorygroups}}',
['id' => $groupId])
['id' => $group->id])
->execute();

$transaction->commit();
Expand Down
32 changes: 22 additions & 10 deletions src/services/Tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,26 +249,38 @@ public function saveTagGroup(TagGroup $tagGroup, bool $runValidation = true): bo
/**
* Deletes a tag group by its ID.
*
* @param int $tagGroupId
* @param int $groupId The tag group's ID
* @return bool Whether the tag group was deleted successfully
* @throws \Throwable if reasons
*/
public function deleteTagGroupById(int $tagGroupId): bool
public function deleteTagGroupById(int $groupId): bool
{
if (!$tagGroupId) {
if (!$groupId) {
return false;
}

$tagGroup = $this->getTagGroupById($tagGroupId);
$group = $this->getTagGroupById($groupId);

if (!$tagGroup) {
if (!$group) {
return false;
}

return $this->deleteTagGroup($group);
}

/**
* Deletes a tag group.
*
* @param TagGroup $group The tag group
* @return bool Whether the tag group was deleted successfully
* @throws \Throwable if reasons
*/
public function deleteTagGroup(TagGroup $group): bool
{
// Fire a 'beforeDeleteGroup' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_GROUP)) {
$this->trigger(self::EVENT_BEFORE_DELETE_GROUP, new TagGroupEvent([
'tagGroup' => $tagGroup
'tagGroup' => $group
]));
}

Expand All @@ -278,7 +290,7 @@ public function deleteTagGroupById(int $tagGroupId): bool
$fieldLayoutId = (new Query())
->select(['fieldLayoutId'])
->from(['{{%taggroups}}'])
->where(['id' => $tagGroupId])
->where(['id' => $group->id])
->scalar();

if ($fieldLayoutId) {
Expand All @@ -289,15 +301,15 @@ public function deleteTagGroupById(int $tagGroupId): bool
$tags = Tag::find()
->status(null)
->enabledForSite(false)
->groupId($tagGroupId)
->groupId($group->id)
->all();

foreach ($tags as $tag) {
Craft::$app->getElements()->deleteElement($tag);
}

Craft::$app->getDb()->createCommand()
->delete('{{%taggroups}}', ['id' => $tagGroupId])
->delete('{{%taggroups}}', ['id' => $group->id])
->execute();

$transaction->commit();
Expand All @@ -310,7 +322,7 @@ public function deleteTagGroupById(int $tagGroupId): bool
// Fire an 'afterSaveGroup' event
if ($this->hasEventHandlers(self::EVENT_AFTER_DELETE_GROUP)) {
$this->trigger(self::EVENT_AFTER_DELETE_GROUP, new TagGroupEvent([
'tagGroup' => $tagGroup
'tagGroup' => $group
]));
}

Expand Down
20 changes: 17 additions & 3 deletions src/services/UserGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ public function saveGroup(UserGroup $group, bool $runValidation = true): bool
/**
* Deletes a user group by its ID.
*
* @param int $groupId
* @return bool
* @param int $groupId The user group's ID
* @return bool Whether the user group was deleted successfully
* @throws WrongEditionException if this is called from Craft Solo edition
*/
public function deleteGroupById(int $groupId): bool
Expand All @@ -232,6 +232,20 @@ public function deleteGroupById(int $groupId): bool
return false;
}

return $this->deleteGroup($group);
}

/**
* Deletes a user group.
*
* @param UserGroup $group The user group
* @return bool Whether the user group was deleted successfully
* @throws WrongEditionException if this is called from Craft Solo edition
*/
public function deleteGroup(UserGroup $group): bool
{
Craft::$app->requireEdition(Craft::Pro);

// Fire a 'beforeDeleteUserGroup' event
if ($this->hasEventHandlers(self::EVENT_BEFORE_DELETE_USER_GROUP)) {
$this->trigger(self::EVENT_BEFORE_DELETE_USER_GROUP, new UserGroupEvent([
Expand All @@ -240,7 +254,7 @@ public function deleteGroupById(int $groupId): bool
}

Craft::$app->getDb()->createCommand()
->delete('{{%usergroups}}', ['id' => $groupId])
->delete('{{%usergroups}}', ['id' => $group->id])
->execute();

// Fire an 'afterDeleteUserGroup' event
Expand Down