Jump to:

22997 Posts in 11853 Topics by 2828 members

General Questions

SilverStripe Forums » General Questions » I'm working on a licence key module

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 352 Views
  • Harley
    Avatar
    Community Member
    153 Posts

    I'm working on a licence key module Link to this post

    Hi there,

    I'm working on a simple licence key module which I think could be useful. However I am a little stuck on one thing.

    In the below example there is a hardcoded array '$numbers' which holds some example licence keys. What I am trying to do is populate this with a dataobject, simple right? Well the hard part seems to be retrieving that dataobject and putting it into an array. I'm a bit confused as there seems to be a fair bit out there about how you can do this but as yet none of those methods have worked.

    I'm wondering if anyone out there has any practical knowledge of converting dataobject results into arrays?

    LicenseKeyField.php

    <?php

    class LicenseKeyField extends TextField{

    function validate($validator){

    if(!empty ($this->value)){

    // get licence key field value
    $val = $this->value();

    $keys = array(
    'AJCAOkwO',
    'ANojxe4F',
    'ARmsWx37',
    'AfFBkUxU',
    'AsQ2r9Fb',
    'BDtvjcue',
    'BWwCTj5V',
    'BnZcCYJR'
    );

    // check the license key against a given pattern.
    $patterns_flattened = implode('|', $keys);

    if ( preg_match('/'. $patterns_flattened .'/', $val, $matches) ){
    // matched
    } else {
    $validator->validationError(
    $this->name,
    "Sorry, you have not entered a valid code",
    "validation",
    false
    );
    return false;
    }
    }
    }
    }

    Regards

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: I'm working on a licence key module Link to this post

    update, I now have this code which I'm almost certain produces the array I am looking for. However in the browser where the licence key field is outputted I have a blank page.

    <?php

    class LicenseKeyField extends TextField{

    function validate($validator){

    if(!empty ($this->value)){

    // get dataobject and convert to array for licencekeys
    public function getKeys(){
    $sqlQuery = new SQLQuery();
    $sqlQuery->setFrom('LicenceKeys');
    $sqlQuery->addWhere('ProductDownloadID = ' . $this->ID);
    $result = $sqlQuery->execute();
    // Iterate over results
    $keys = new ArrayList();
    foreach($result as $row) {
    $arrayData = new ArrayData(array(
    'LicenceKeys' => $row['LicenceKeys']
    )
    );
    $keys->add($arrayData);
    }
    return $keys;
    }

    // get licence key field value
    $val = $this->value();

    // check the license key against a given pattern.
    $patterns_flattened = implode('|', $keys);

    if ( preg_match('/'. $patterns_flattened .'/', $val, $matches) ){
    // matched
    } else {
    $validator->validationError(
    $this->name,
    "Sorry, you have not entered a valid code",
    "validation",
    false
    );
    return false;
    }
    }
    }
    }

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: I'm working on a licence key module Link to this post

    Assuming the DataObject your trying to get is called "LincenseKey", you could create an array from it like so.

    public function getKeys(){
    $licenseKey = LicenseKey::get()->where('ProductDownloadID= ' .$this->ID);
    $licenseKey = $licenseKey->toNestedArray();
    return $licenseKey
    }

    much easier yeah? Only two lines and you have an array.

    As for the blank page, do you have dev mode, and error reporting enabled? Very difficult to troubleshoot without some errors

  • Harley
    Avatar
    Community Member
    153 Posts

    Re: I'm working on a licence key module Link to this post

    Hi IOTI,

    Thank you for replying! Sorry I didn't reply sooner, I've only just returned to this project after it going off the boil for a while and missed your message.

    Your suggestion makes complete sense however the form is submitting without the correct validation check for the license key. My main problem is that I don't know how to debug LicenseKeyField.php as it extends the TextField class. Any ideas?

    Updated code:

    <?php

    class LicenseKeyField extends TextField{

    function validate($validator){

    if(!empty ($this->value)){

    // check the license key against a given pattern.

    $getlist = LicenseKeys::get('LicenseKeys')->where('ProductDownloadID= ' .$this->ID);
    $getlist = $getlist->toNestedArray();
    return $getlist;

    if (preg_match('/'. $getlist .'/', $this->value)){
    // matched
    } else {
    $validator->validationError(
    $this->name,
    "Sorry, you have not entered a valid code",
    "validation",
    false
    );
    return false;
    }
    }
    }
    }
    }

    Cheers

  • IOTI
    Avatar
    Community Member
    187 Posts

    Re: I'm working on a licence key module Link to this post

    You'll need to take out the "return $getlist;". Your If loop will never happen because of that.

    352 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.