-
Notifications
You must be signed in to change notification settings - Fork 0
/
natsort_product_groups_by_name.php
94 lines (72 loc) · 2.1 KB
/
natsort_product_groups_by_name.php
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
<?php
/**
* WHMCS tool used to naturally resort by name all product groups
* @author Shaun Reitan
* @see https://github.com/ShaunR/whmcs-tools
*/
use WHMCS\Database\Capsule;
use WHMCS\Product\Group;
// Opts
$opts = getopt("p:d:h", [ 'path:', 'dryrun:', 'help::'], $optind);
// Default
$path = rtrim(__DIR__, '/');
foreach ($opts as $opt => $value) {
switch ($opt) {
// WHMCS Path
case 'p':
case 'path':
$path = rtrim($value, '/');
break;
// Dry Run
case 'd':
case 'dryrun':
$dryrun = true;
break;
// Help Me!
case 'h':
case 'help':
echo $argv[0] . " <options>\n\n";
echo "OPTIONS\n";
echo " --path Path to WHMCS installation (default: " . __DIR__ . " )\n";
echo " --dryrun Only show what changes would be made (optional)\n";
echo " --help Your looking at it!\n";
exit(1);
default:
echo $opt . " is an unknown option, use --help for available options\n";
exit(1);
}
}
// Path exist?
if (!is_dir($path)) {
echo $path . " does not exist or is not accessible\n";
exit(1);
}
// WHMCS init.php exist in path?
if (!is_file($path . "/init.php")) {
echo "init.php was not found in " . $path . " are you sure thats where your installed WHMCS?\n";
exit(1);
}
// Init WHMCS
try {
require_once $path . "/init.php";
} catch (\Exception $e) {
echo "WHMCS init failed, " . $e->getMessage() . "\n";
exit(1);
}
// Groups
$groups = Group::all()->sortBy('name', SORT_NATURAL | SORT_FLAG_CASE, false);
// Start at 0
$displayOrder = 0;
// Loop through groups
foreach ($groups as $group) {
// Increment display order counter
$displayOrder++;
// Only commit change if dryrun option was not passed.
if (!isset($dryrun)) {
echo "DRYRUN: ";
// Upate display order
$group->displayOrder = $displayOrder;
$group->save();
}
echo "Group: " . $group->name . " display order is now " . $displayOrder . "\n";
}