The inevitability of _something_

To borrow some words from Neil Gaiman…

What’s the name of the word for things not being the same always.  You know, I’m sure there is one, isn’t there?  There must be a word for it…  The thing that lets you know time is happening. Is there a word?

Change.

Things change.  It’s inevitable.  The more it stays the same, the less it changes (Spinal Tap).  The problem is, while it’s bound to happen sooner or later, not everyone is happy with it.

We’re not talking the big, impossible, philosophical imponderables here, though if you read enough Sandman you’re going to be fairly well equipped to deal with them.  We’re talking Science Fiction.

Over the course of my life, we’ve had six different Star Trek series, and if they’ve taught me one thing it’s that Star Trek can’t handle continuity no matter how many devoted fans you’ve got maintaining the wikis.  And if it’s taught me another thing it’s that sometimes changes take a long time to explain (why the original series Klingons looked the way they did, f’rinstance.  It’s a long haul, but it is there).  Sometimes, though, you change things just a bit too much in one go and you end up with the Star Trek: Discovery Klingons and that’s just “nope, I’m out”.  Strangely, I’ve just stopped watching rather than popping up on the Discovery Facebook group complaining about the Klingons after every episode has aired.

Another big change is coming to Doctor Who this year.  In case you’ve been living under a rock and haven’t heard the news, the latest actor to play the title role is Jodie Whittaker.

To say this announcement made some people unhappy would be fairly accurate.  I know people who have decided now to stop watching Dr Who completely.   In much the same way Stephen Fry did when he left Twitter or the way this last winter has been coming back and back and back.

Let’s ignore the fact that this is less science fiction and more story-driven space fantasy.  Let’s ignore the fact that the show has broken its own rules on more occasions than you can shake a big stick at for the sake of the story.  Okay, so a Timelord can only regenerate 12 times.  Fair enough.  Much has been made of The Master going on a quest for additional regeneration cycles.  Much was made of the big Story Arc Achievement Unlocked that gave Matt Smith the extra set of regenerations and brought us Peter Capaldi.  I mean, it was obviously going to happen, the BBC weren’t going to end Dr Who just to stick to this magic number.  Let’s ignore the fact that on at least one occasion the show has had details of regenerations before William Hartnell and this magic number should’ve been storied away around Sylvester McCoy.

The BBC like to telegraph things a long way in advance.  The writing was on the wall for a female Doctor when Michelle Gomez stepped into The Master’s shoes and delivered a delightful performance straight out of Green Wing.  And when a Timelord was shot on Gallifrey, regenerating smoothly from a bald, white guy into a black woman and no-one around her so much as raised an eyebrow.

Regenerations on Dr Who follow roughly the same procedure.

  1. Actor currently playing The Doctor announces they’re going to leave.  Much wailing and gnashing of teeth, how could they do this, who could ever play the role better, etc. etc. etc.
  2. BBC schedule an entire evening’s “entertainment” to revealing the new Doctor.  Cue much admiration of the current incumbent, interviews along the lines of “of course, when this actor did their audition we realised we’d got our new Doctor” without ever revealing anything, and cue me tuning in to Facebook about 10 seconds before the end of the show to find out who it is without having to watch this time filler.  Seriously, just tell us.  Stick it in Newsround that morning.
  3. Now we know who the new actor is, cue much wailing and gnashing of teeth, how could they do this, how is Tristan Farnon / Casanova / Malcolm Tucker going to fit? Why didn’t they pick Paterson Joseph / Idris Elba / Rufus Sewell / etc?  And, in the case of the 11th Doctor, cue a conversation that started, in a pub in Inverness with a friend I’d not seen for 3 years, not with “How are you?” or “What have you been up to?” but “Who the f*ck is Matt Smith?”  (By a curious coincidence, a year or so later, a friend of mine was returning from a University Dean’s dinner in Inverness, walking along the high street in full regalia, when a voice behind him commented on how cool his cape was.  Dean in question turned round to find Matt Smith, in Inverness having a tweed jacket made.)
  4. And after about 2 or 3 episodes of the new team, the new TARDIS console room, you’re wondering what all the fuss was about, of course <insert actor here> is The Doctor, how could it’ve been anyone else?
  5. And repeat.

Over the years, The Doctor has officially been played by 16 men.  Okay, so one of them was Peter Cushing for the Dalek movies, and that doesn’t entirely count, and one of them was an actor playing an actor playing The Doctor but who has now officially played that Doctor (wibbly wobbly timey wimey and so on).

Now I’ve not entirely enjoyed the last few series of Dr Who.  Not because of the actor playing The Doctor, but because the writing has been poor.  I miss the good old days of a story being strung out over 4 or 6 episodes, not wrapped up in 1.  Recently, the pacing of the episodes have been off, single-episode stories have felt rushed, two-parters have dragged along.  So for me, at least, the biggest change is that Stephen Moffat is stepping down from Dr Who.

Like Mr M, the new head honcho has written episodes of Dr Who in the past.  Unfortunately, they’re no “Blink”.  Instead he’s given us “Dinosaurs on a Spaceship”.  He has written some unusual episodes, though, playing more with the pacing and the episode format.  He’s also written a large portion of Torchwood.  So this change is the one I’m really excited about.  This new Doctor will survive on the strength of the stories she’s given.

Magento Free Shipping -> Click and Collect Observer

Recently encountered an issue with Magento and a basic implementation of a “Click and Collect” feature.

We’d set up a system using the “Free Shipping” option within Magento to act as a “Click and Collect” option, modifying the page template for the appropriate part of the one-page checkout to include the information about the click and collect service – where it would be available from, what the opening hours are, that sort of thing.  Now there are plugins that handle store collection but they all tend to focus on retailers with multiple stores, and we’ve just got the one.

When you go to print out the order, postage cost is £0.00 and it clearly says “click and collect” but the shipping address is still the one the customer entered during checkout and that was going to cause problems with the system we’ve got for picking, packing, and shipping orders.

So….  Enter the Observer.  Observers are cool.  They’re Magento’s equivalent of Drupal’s hooks, allowing you to watch what’s going on and dive in when something specific happens.

As things go in Magento, this is a relatively straightforward hack, only took me a couple of days to work out.

Step 1.  Create a module to do the work.  SSH onto your server and…1

cd <magento install dir>/app/code/local
mkdir Satcol
cd Satcol
mkdir Clickandcollect
cd Clickandcollect
mkdir etc
mkdir Model

‘course, you can do this via FTP as well.

You don’t have to call the folders “Satcol” and “Clickandcollect”, it just made sense for me to do that.  Remember what you did call them, though, you’ll need it later.

Inside the “etc” folder, create config.xml

<?xml version="1.0"?>
<config>
 <modules>
  <Satcol_Clickandcollect>
   <version>0.0.1</version>
  </Satcol_Clickandcollect>
 </modules>
 <global>
  <models>
   <satcolclickandcollect>
    <class>Clickandcollect_Model</class>
   </satcolclickandcollect>
  </models>
  <events>
   <checkout_controller_onepage_save_shipping_method>
    <observers>
     <sales_order_place_after>
      <type>singleton</type>
      <class>Satcol_Clickandcollect_Model_Observer</class>
      <method>satcoltest</method>
     </sales_order_place_after>
    </observers>
   </checkout_controller_onepage_save_shipping_method>
  </events>
 </global>
</config>

And if you’ve called your directory anything other than “Satcol” and “Clickandcollect” you need to change the lines where those words are found.

Under “events”, we’re specifying that we’re looking for the exact moment when Magento saves the shipping method – checkout_controller_onepage_save_shipping_method – catchy name, I’m sure you’ll agree, but it does exactly what it says on the tin.  If you’re not using the onepage checkout, then you’ll need to determine what the event you’re observing is called.

The “method” line is important for the next file as it’s the name of the php function you’re going to call in Observer.php

cd back to Model and create Observer.php

<?php
  class Satcol_Clickandcollect_Model_Observer {
    public function satcoltest($observer) {
      $quote = Mage::getSingleton('checkout/session')->getQuote();
      $shippingAddress = $quote->getShippingAddress();
      $shippingMethod = $shippingAddress->getShippingMethod();
      if($shippingMethod=='freeshipping_freeshipping'){
        $address1 = $shippingAddress->getStreet(1);
        $shippingAddress->setStreet(array('Collect From Your Shop','66-78 Denington Road'))
          ->setCity('Your City')
          ->setRegion('Your County')
          ->setPostcode('Your postcode')
          ->save();
        Mage::log('Click and Collect order placed');
      }
    }
  }

Obviously change the setStreet, setCity, setRegion, and setPostcode values as appropriate.  And the Mage::log is there for debug purposes, it can be removed – or left if you want to check the system log periodically and see how many orders have used this method.

And if you’ve used a different shipping method for your Click and Collect solution,  you’ll need to change the if statement that’s looking at shipping method.

Once these files are in place, you need to turn the module on.  In Drupal, this would be a simple tick box.  In WordPress, you’d enable the plugin in the lovely friendly GUI.  In Magento we’ve got to go to another location on the server and add in another xml file.

SSH to your server, and…

cd <magento install dir>/app/etc/modules
vi Satcol_Clickandcollect.xml

(Or whatever you called your directories back at the start).

<?xml version="1.0"?>
<config>
 <modules>
   <Satcol_Clickandcollect>
     <codePool>local</codePool>
     <active>true</active>
   </Satcol_Clickandcollect>
 </modules>
</config>

cd back to magento’s /var/cache and clear the cache (or flush it from the admin console) and you’re golden.  Now whenever someone places an order using the Free shipping method you’ve assigned to “Click and Collect” the delivery address will be automagically updated to the address you specify.

And if you know of a simpler, easier, better, way of doing this – please let me know.  Just as long as it isn’t “buy this plugin for only $79”.

Synology and Drush 8.1.16 – Finally Working

Yep, it’s Tech Monday on a Wednesday again…

Okay, so I’m lumbered with these Fisher Price “My First Server” things.  They run Linux (Yay!) but a strangely butchered and hobbled version (Boo!) and as I need them to run Drupal, I need to get some things working properly.  And, of course, they don’t have APT or RPM on there, that would be too easy.  And by “things”, I mean Drush – essential for command line maintenance of all your Drupal sites.

Apologies for the technical nature of this post, I didn’t have time to paint it or build it to scale.

First thing needed is php7.  Synology has a package for php7, so that’s not a big problem.  It’s currently stuck at 7.0 but I expect we’ll see 7.1 and 2 soon (for a certain definition of “soon”, it took them long enough to get MariaDB 10 on there).  Install that package and you’re good to go forward.  Once the package is installed, nip into the configuration website and enable ALL THE EXTENSIONS EVER.

Drush itself.  First off, I had to create my own home directory as somewhere to put things that stay vaguely safe between DSM updates.  Weirdly, my Synology boxes didn’t create home directories for users (you know, the way normal operating systems do), so there were chunks of things always throwing errors.   Do this for your username…

sudo mkdir /volume1/homes/<username>

Sorted.  Golden.  Etc.  You’ll probably need your root password to do this, then you’ll need to chown it to your user before you can use it properly…

cd into that directory, head over to http://docs.drush.org/en/8.x/install/ and grab the latest 8 release from GitHub.  At time of writing, it’s 8.1.16

Okay.  This is where it gets technical.  More technical, sort of.

Sure, you could just run

php70 ~/drush.phar pm-update

But (a) it’s tedious to type that in all the time and (b) it’ll fail because pm-update calls php pm-updatestatus and that’ll fail because php is not the same as php70 and doesn’t have all the extensions installed.

So we need to perform some surgery in /bin.

Step 1.  Get /bin/php to point to the *right* php.

sudo su
<password>
cd /bin
mv php php56
which php70
(assuming /usr/local/bin/php70, if not then substitute appropriately)
ln -s /usr/local/bin/php70 php
exit
php --version

Now when you run php –version you’ll see a lovely, shiny, 7.0 there instead of the dull, old, tedious 5.6.

Step 2, get Drush in place.

sudo su
cd /bin
cp /volume1/homes/<username>/drush.phar drush
chmod a+x drush
exit

Now you can run drush from anywhere on the server, without having to specify a version of php or a location for the drush.phar file.  Ain’t that a treat!

And if you’re being clever, you’ll see that you could have both drush 8 and 9 installed at the same time – just have different filenames in /bin – you could have “Doctor” running drush 8 and “Master” running 9.  Jeeves and Wooster.  Armstrong and Miller.  Bonnie and Clyde.  Any pairing you like, really.  Or you could be boring and have “drush8” and “drush9”.  Up to you, really.

Word of warning 1 – If you do this to /bin/php it will alter your default command line php for everything.  Shouldn’t be a problem, in fact it will probably make life easier.  But worth bearing in mind if you suddenly start getting weirdness with scripts you’re triggering through cron jobs (and don’t get me started on Synology’s implementation of cron, that makes no sense).

Word of warning 2 – There will come a day when, totally unexpectedly, you’ll type “drush” and your Synology box will reply with “What the f*ck is drush?” Or the Linux equivalent, usually a bit more polite – imagine Jeeves stepping smartly yet unobtrusively forward, coughing politely, whispering “I’m terribly sorry, sir, but I can’t find that command”, then stepping back into the butler’s pantry.  This means your Synology box has updated the Disk Station Manager software and reset ALL of your configuration tweaks.  Go back into php.ini and reset that max_upload_size from 32M again.  Fix those timeout values.  And redo steps 1 and 2, above, assuming you’re lucky enough to still have a home directory with drush.phar in it.

If this has helped you, good.  And I hope you find a way to get to a real server sooner rather than later!