Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

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

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

I'm working on a licence key module


Go to End


5 Posts   1181 Views

Avatar
Harley

Community Member, 165 Posts

19 June 2013 at 1:55am

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

Avatar
Harley

Community Member, 165 Posts

19 June 2013 at 3:16am

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;
         }
      }
   }
}

Avatar
copernican

Community Member, 189 Posts

26 June 2013 at 12:37am

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 :)

Avatar
Harley

Community Member, 165 Posts

29 July 2013 at 7:12am

Edited: 29/07/2013 7:13am

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

Avatar
copernican

Community Member, 189 Posts

30 July 2013 at 12:11am

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