Skip to content

Instantly share code, notes, and snippets.

@nklatt
nklatt / dimension-input.html
Last active February 6, 2024 16:22
HTML/JS regex pattern to match one of: whole number, float, fraction or whole number <space> fraction
<input pattern="/^(?:(\d+\/[1-9]\d*)|(\d*(?:(\s\d+\/[1-9]\d*)|\.\d+)?))$/">
<!--
(?: => non-capturing group, EITHER
(\d+\/[1-9]\d*) => any number of digits followed by a slash then any number of digits not beginning with 0 (fraction)
| => OR
(\d*(?: => zero or more digits followed by non-capturing group, EITHER
(
\s => a space
\d+ => any number of digits
\/ => a slash
@nklatt
nklatt / 0 Laravel Backpack CRUD entry list filtering based on related rows
Last active June 16, 2023 21:50
Laravel Backpack CRUD entry list filtering based on related rows
Site allows members (people) to register for programs. People belong to one or
more Accounts. Programs are divided up into Seasons.
When viewing the entity list page for Accounts, we want to be able to filter the
list based on the seasons the people within each account are registered for.
E.g., we often want to show only Accounts which have People registered for
Programs within the current Season.
Pertinent database tables and columns:
accounts: id
@nklatt
nklatt / MySQL copy rows with tweaks
Created August 29, 2022 21:52
PHP code that generates a MySQL query to copy rows from a table to the same table but with some column values changed.
$table = 'some_table';
$columns = implode(',', array(
'title',
'description',
'etc...',
));
$sql = '
INSERT INTO '.$some_table.' (product_id, '.$columns.')
SELECT "'.$destId.'", '.$columns.'
FROM '.$some_table.' WHERE product_id = "'.$origId->id.'"
@nklatt
nklatt / html_responses.php
Created April 6, 2022 19:21
Basic post handler as a HTTP response code example.
<?php
$status = 500; // if we don't change this then the problem is this code
$result = array();
if (empty($_FILES['file']['tmp_name'])) {
$status = 400;
$result['message'] = 'No file found.';
} else if (empty($_FILES['file']['name'])) {
$status = 400;
$result['message'] = 'No file name found.';
@nklatt
nklatt / 0 README
Last active July 7, 2021 18:34
Magento 2 CLI product attribute value checker
I needed a way to check the value of a product attribute so whipped up this
little command line interface tool to do it. It accepts a SKU and a product
attribute code and outputs the value of that attribute for that product.
@nklatt
nklatt / iterate_paged_product_collection.php
Last active January 18, 2021 22:43
How to iterate through pages of products in Magento 2 using a Product CollectionFactory.
<?php
namespace Company\Module\Console\Command;
// How to iterate through pages of products in Magento 2 (specifically tested with version 2.3.5-p2) using a
// Product CollectionFactory. This specific example is for a console command that maintains that products that
// meet a certain set of criteria are in the Clearance category. It also happens to show how to include stock
// quantity information with the products.
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
@nklatt
nklatt / 0 intro.txt
Last active July 1, 2023 15:13
Import Google Maps polygons into MySQL
We have a client that ships from multiple warehouses using zones to determine
pricing. They maintain the zones in a Goole Maps document. We are creating a
front end that clients can enter their address and we'll show them what their
shipping options are. We will have the client export their zones as a KML
file and we'll import it into a MySQL database and use ST_Contains to find
the shipping zone(s) customers are in. (They can only be in one zone for any
given warehouse but may be within range of multiple warehouses.)
One thing to point out is there is confusion about the order of latitude and
longitude. Mathematically, it is naturally ordered "longitude, latitude" but
@nklatt
nklatt / concrete5_adjust_file_permissions.php
Last active September 10, 2022 08:10
For Concrete5 v8, how to programmatically set file permissions so that they are only accessible by a single user plus a single group of users and are stored outside of the webroot.
use Concrete\Core\Entity\File\File;
use Concrete\Core\File\Set\Set as FileSet;
use Concrete\Core\File\StorageLocation\StorageLocationFactory as FileStorageLocationFactory;
use Concrete\Core\Permission\Access\Entity\GroupEntity as GroupPermissionAccessEntity;
use Concrete\Core\Permission\Access\Entity\UserEntity as UserPermissionAccessEntity;
use Concrete\Core\Support\Facade\Application;
use Concrete\Core\User\Group\Group as UserGroup;
use PermissionKey;
use UserInfo;
@nklatt
nklatt / create_attributes.php
Last active May 14, 2020 07:45
Programmatically add new product attribute with options to a Magento 2 store and add to Default attribute group
<?php
defined('STDIN') or die(_("Access Denied. CLI Only"));
// execute from the command line: php <path to magento root>/cli/create_attributes.php
require __DIR__.'/../app/bootstrap.php'; // assuming this file is in /cli under the root magento dir
$bootstrap = \Magento\Framework\App\Bootstrap::create(BP, $_SERVER);
$installer = $bootstrap->getObjectManager()->create('Magento\Catalog\Setup\CategorySetup');
$objectManager = $bootstrap->getObjectManager();
@nklatt
nklatt / add_column.php
Created May 12, 2016 17:11
Ensure a column exists in a MySQL table using concrete5, though that is secondary
<?php defined('C5_EXECUTE') or die(_('Access Denied.'));
function addColumnToTable($table, $column, $type = 'tinyint(1) not null default 0') {
$db = Loader::db();
$qResult = $db->Execute('show columns from `'.$table.'` like "'.$column.'"');
$columnAlreadyExists = false;
foreach ($qResult as $row) {
if (is_array($row) && !empty($row['Field']) && $row['Field'] === $column) {
$columnAlreadyExists = true;
}
}