Skip to content

Instantly share code, notes, and snippets.

@rabin-io
Last active March 16, 2020 13:03
Show Gist options
  • Save rabin-io/a019fc6e456297e1aeb5061360dbd1a5 to your computer and use it in GitHub Desktop.
Save rabin-io/a019fc6e456297e1aeb5061360dbd1a5 to your computer and use it in GitHub Desktop.
<?php
$dbh = new PDO(
'mysql:host=127.0.0.1:3066;dbname=cacti',
'cacti',
'password',
);
$paletteName = "50 Steps of Flame";
$gradient = array("FF0000","FF0300","FF0600","FF0900","FF0C00","FF1000","FF1300",
"FF1600","FF1900","FF1C00","FF2000","FF2300","FF2600","FF2900",
"FF2D00","FF3000","FF3300","FF3600","FF3900","FF3D00","FF4000",
"FF4300","FF4600","FF4900","FF4D00","FF5000","FF5300","FF5600",
"FF5A00","FF5C00","FF5E00","FF6100","FF6300","FF6500","FF6800",
"FF6A00","FF6C00","FF6F00","FF7100","FF7400","FF7600","FF7800",
"FF7B00","FF7D00","FF7F00","FF8200","FF8400","FF8700","FF8900",
"FF8B00","FF8E00","FF9000","FF9200","FF9500","FF9700","FF9A00",
"FF9B00","FF9D00","FF9F00","FFA100","FFA300","FFA500","FFA700",
"FFA900","FFAB00","FFAD00","FFAF00","FFB100","FFB300","FFB400",
"FFB600","FFB800","FFBA00","FFBC00","FFBE00","FFC000","FFC200",
"FFC400","FFC600","FFC800","FFCA00","FFCC00","FFCE00","FFCE00",
"FFCF00","FFD000","FFD101","FFD201","FFD301","FFD402","FFD502",
"FFD602","FFD702","FFD803","FFD903","FFDA03","FFDB04","FFDC04",
"FFDD04","FFDE05","FFDF05","FFE005","FFE105","FFE206","FFE306",
"FFE406","FFE507","FFE607","FFE707","FFE808");
try {
$stmt = $dbh->prepare("SELECT hex,id FROM colors");
$stmt->execute();
// fetch all colors into an array.
$rows = $stmt->fetchAll(); //print_r($rows); die();
// and create a hash map from color to color_id,
// which we can then use to reference HEX->id
foreach ($rows as $key => $value) {
$colors['#'.$value['hex']] = $value['id'];
}
// print_r($colors); die();
// Generate the commands which inset the new colors to the DB
// and skip the one we all ready have.
$SQL = '';
foreach ($gradient as $key => $value) {
if (isset($colors['#' . $value])) {
continue;
}
$SQL .= "INSERT INTO colors (hex) VALUES ('$value');" . PHP_EOL;
}
// this is a quick work around, that if we inset new color
// we need to read the table all over again any way
// so instead we stop here, and run again the script
if (!empty($SQL)) {
$dbh->beginTransaction();
$dbh->query($SQL);
$dbh->commit();
echo "re-run the script again to create the color template"
exit();
}
/*-------------------------------------------------------------------*/
/*
* Here we create the template color
* and link the colors to the template
*/
$dbh->beginTransaction();
$newColorPaletName = "INSERT INTO plugin_aggregate_color_templates (name) VALUES ('${paletteName}');";
$dbh->exec($newColorPaletName);
$lastColorPaletId = $dbh->lastInsertId();
$items = '';
$counter = 0;
foreach ($gradient as $key => $value) {
$colorId = $colors['#' . $value];
$counter++;
$items .= "INSERT INTO plugin_aggregate_color_template_items (color_template_id,color_id,sequence) VALUES (${lastColorPaletId},${colorId},$counter);" . PHP_EOL;
}
$dbh->query($items);
$dbh->commit();
} catch (PDOException $e ) {
var_dump($e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment