Skip to content

Commit

Permalink
Added Support for stuff and closed pixelandtonic#17 issue
Browse files Browse the repository at this point in the history
Also added support for the isLicensed hidden field type - essentially what it will do is allow devs to find all users licensed to a particular product where the license is enabled. Also added support for save and add another in case that wasnt already in the pipeline.
  • Loading branch information
gtettelaar committed Mar 16, 2018
1 parent b9e34c4 commit ba540b2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,10 @@ private function _prepareVariableArray(&$variables)

$variables['continueEditingUrl'] = "digitalproducts/products/".$variables['productType']->handle."/{id}".
(craft()->isLocalized() && !empty($variables['localeId']) && craft()->getLanguage() != $variables['localeId'] ? '/'.$variables['localeId'] : '');

// Save and add another url
$variables['saveAddAnotherUrl'] = "digitalproducts/products/".$variables['productType']->handle."/new";

}

/**
Expand Down
36 changes: 36 additions & 0 deletions digitalproducts/fieldtypes/DigitalProducts_IsLicensedFieldType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/**
* Created by PhpStorm.
* User: Giel Tettelaar PC
* Date: 3/14/2018
* Time: 3:56 PM
*/

namespace Craft;


class DigitalProducts_IsLicensedFieldType extends BaseFieldType{

public function getName()
{
return Craft::t('Is licensed');
}
public function getInputHtml($name, $value)
{
return false;
}

public function defineContentAttribute()
{
return false;
}

public function modifyElementsQuery(DbCommand $query, $value)
{
if ($value !== null)
craft()->digitalProducts_licenses->modifyQuery($query, $value);

return $query;
}

}
39 changes: 39 additions & 0 deletions digitalproducts/services/DigitalProducts_LicensesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,45 @@ public function generateLicenseKey()
return $licenseKey;
}

/**
* Modify Query based on the hidden isLicensed field type.
*
* @param DbCommand $query
* @param array $params
*/
public function modifyQuery (DbCommand $query, $params = array())
{
if (array_key_exists('productId', $params)) {
$this->isLicensedQuery($query, $params);
}
}

/**
* Takes the given param - being a product id - and builds an array of userIds that this product is
* licensed to. We then andWhere() the query and pass in this array of userId's
* @param DbCommand $query
* @param array $params
*/
public function isLicensedQuery(DbCommand $query, $params){
// Cache the product id
$desiredProduct = $params['productId'];

// Find all licenses that are enabled - with a product id of the entered param.
$criteria = craft()->elements->getCriteria('DigitalProducts_License');
$criteria->enabled = true;
$criteria->productId = $desiredProduct;
$licenses = $criteria->find();
$userIds = [];

// TODO: More effecient way to do this? Looping through all licenses feels a bit ancient...
foreach($licenses as $license){
$userIds[] = $license->userId;
}

// Up up and away
$query->andWhere(array('in', 'elements.id', $userIds));
}

/**
* Delete a License.
*
Expand Down
4 changes: 4 additions & 0 deletions digitalproducts/templates/products/_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
{{ "Save and continue editing"|t }}
{{ forms.optionShortcutLabel('S') }}
</a></li>
<li><a class="formsubmit" data-redirect="{{ saveAddAnotherUrl }}">
{{ "Save and add another"|t }}
{{ forms.optionShortcutLabel('S') }}
</a></li>
</ul>
</div>
</div>
Expand Down

0 comments on commit ba540b2

Please sign in to comment.