This repository was archived by the owner on Jun 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFieldGeneratorHooks.module
133 lines (123 loc) · 3.88 KB
/
FieldGeneratorHooks.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
class FieldGeneratorHooks extends WireData implements Module
{
public static function getModuleInfo()
{
return array(
'title' => 'Field Generator Hooks',
'version' => 99,
'summary' => 'Generate random strings for any field - Hooks',
'href' => 'https://github.com/plauclair/FieldGenerator',
'singular' => true,
'autoload' => true,
);
}
/**
* Initialize the module
*/
public function init()
{
$this->pages->addHookAfter('setupNew', $this, 'generatefield');
}
/**
* Helper functions
*/
private function crypto_rand_secure($min, $max)
{
$range = $max - $min;
if ($range < 0) return $min; // not so random...
$log = log($range, 2);
$bytes = (int) ($log / 8) + 1; // length in bytes
$bits = (int) $log + 1; // length in bits
$filter = (int) (1 << $bits) - 1; // set all lower bits to 1
do {
$rnd = hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
$rnd = $rnd & $filter; // discard irrelevant bits
} while ($rnd >= $range);
return $min + $rnd;
}
private function getToken($length,$dict,$pre)
{
$token = $pre;
for ($i=0;$i<$length;$i++) {
$token .= $dict[$this->crypto_rand_secure(0,strlen($dict))];
}
return $token;
}
/**
* Main generator function
*/
public function generatefield($event)
{
$page = $event->arguments[0];
// Grab the new page's parent template
$parentId = $page->parent()->id;
$pageTemplate = $page->template;
// check if it matches against some rule
foreach (wire('pages')->find("template=fieldgenerator, check_access=0") as $rule)
{
// initiate or reset things
$ParentMatch = false;
$TemplateMatch = false;
$ParentIsSet = false;
$TemplateIsSet = false;
// does parent ID match
if ($parentId == $rule->fieldgenerator_parentid) {
$ParentMatch = true;
}
// does template match
if ($pageTemplate == $rule->fieldgenerator_template) {
$TemplateMatch = true;
}
// is rule parent id set
if (strlen($rule->fieldgenerator_parentid) > 0) {
$ParentIsSet = true;
}
// is rule template set
if (strlen($rule->fieldgenerator_template) > 0) {
$TemplateIsSet = true;
}
// if match, run rule, please forgive ugliness of code
if (($ParentMatch and $ParentIsSet and $TemplateMatch and $TemplateIsSet) or
($ParentMatch and $ParentIsSet and !$TemplateMatch and !$TemplateIsSet) or
(!$ParentMatch and !$ParentIsSet and $TemplateMatch and $TemplateIsSet))
{
// there's a match, so let's use the field specified in $rule
$field = $rule->fieldgenerator_field;
// generate a unique value, let's start by initiating
$uniqueValue = false;
// loop until it's unique
while ($uniqueValue == false) {
// actual string generation
$value = $this->getToken($rule->fieldgenerator_length, $rule->fieldgenerator_dictionary,'');
// verify it's not used already
$check = $this->pages->find("$field=$value");
if(count($check) == 0) {
$uniqueValue = true;
}
}
// set field value
// let's check first if the field supports language (and handle the 'name' field exception)
if (method_exists($page->$field, "setLanguageValue") or $field == "name") {
foreach ($this->languages as $language) {
// name field needs a special way of doing things
if ($field == "name") {
// if this is the default language (not necessarily LanguageSupportPageNames)
if ($language->name == "default") $page->$field = $value;
// or set language page name
if ($this->modules->isInstalled("LanguageSupportPageNames") and $language->name != "default") $page->set($field.$language->id, $value);
} else {
//else just set the value
$page->$field->setLanguageValue($language, $value);
}
}
} else {
// if it's not a language field
$page->$field = $value;
}
$this->message("Field '{$field}' was generated");
// and we're done
}
}
}
}