Klavyio woocommerce – Could not insert post

The last updates to WordPress core may issue theĀ  “Could not insert post to database” error while using the klavyio plugin for wordpress. The fix is pretty simple and it’s needed because of the length constraints of the password field:

 

add_filter('woocommerce_klaviyo_new_cart_data', function($args) {
     $args['post_password'] = uniqid( 'klvct_' );
     return $args;
});

Using jqueryUI’s blur and focus to hide on mouseout

As you probably already know, jquery UI triggers an focus and blur event for each element of the menu so it’s a bit tricky to hide the hole menu for example when the mouse hovers out. Still there’s a workaround:

                        $('.menu_dropdown').click(function() {
		    	    var self = this;
		    	    var test_timer = null;
	    		    $(this).next('ul').menu({ 
		    		    position: { my: "left top", at: "right-5 top+5" },
		    		    create: function( event, ui ) { $(this).position({my: "left top", at: "left bottom", of: $(self)}); },
		    		    select: function( event, ui) { $(this).menu('destroy'); $(this).css('display',''); $.removeData(this,'uiMenu'); },
		    		    blur: function(event, ui) {  test_timer = setInterval(function() { $(self).next('ul').menu('destroy'); $(self).next('ul').css('display',''); $.removeData($(self).next('ul'),'uiMenu'); clearInterval(test_timer); }, 500); },
		    		    focus: function(event, ui) { clearInterval(test_timer); }
		    		});
	    	   });

or you can use the hiding method presented here.

New jquery UI destroy menu not quite working

Well jquery 1.9 is here and so is the version 1.10 of the popular jquery UI. It has some nice additions like menus, tooltips, menu bars and so on. However the menu seems a little bit tricky to use when using an element to trigger it.

let’s have this example:

      some actions
      

ok now the style:

   .themenu { display: none }

and finally the javascript:

                    $('.menu-expander').click(function() {
		    	    var self = this;
	    		    $(this).next('ul').menu({ 
		    		    position: { my: "left top", at: "right-5 top+5" },
		    		    create: function( event, ui ) { $(this).position({my: "left top", at: "left bottom", of: $(self)}); },
		    		    select: function( event, ui) { $(this).menu('destroy'); $(this).css('display',''); $.removeData(this,'uiMenu'); },
		    		    blur: function() {}
		    		});
	    	   });

Calling the destroy will leave the menu object intact in the element’s data array under the name “uiMenu”. So destroying it takes the 3 steps shown in select method above.
You could also just hide it on select using this:

     $('.menu-expander').click(function() {
		    	    var self = this;
                            var mn = $(this).next('ul');
                            if(mn.data('uiMenu') != null) mn.show();
                            else 
	    		        $(this).next('ul').menu({ 
		    		    position: { my: "left top", at: "right-5 top+5" },
		    		    create: function( event, ui ) { $(this).position({my: "left top", at: "left bottom", of: $(self)}); },
		    		    select: function( event, ui) { $(this).hide(); },
		    		    blur: function() {}
		    	        });
	    	   });

PHP CLI has stopped working – php, windows 7

Sometimes you might encounter an unexpected “CLI has stopped working” while running php in cli mode on windows 7. One of the possible causes is the fact that you’re running without administrator privileges. For example, taking the following symfony statement “php symfony doctrine:build-schema” in a cmd.exe without administrator privileges, might end up with the aforementioned error. To avoid that, click start and write “cmd” in the bottom search box > cmd.exe will appear > right click it and click Run as administrator. The error should not show up anymore.

Toggle adapter status with applescript on Lion

Note: this script requires Satimage’s applescript additions(regex in particular) available here(they are downloadable together with smile).

//this script needs the scripting additions of Smile http://www.satimage.fr/software/en/smile/index.html
set servList to do shell script ("networksetup -listnetworkserviceorder")

set match to ""

try
set match to find text "^\\([0-9*]+\\)[^
]+" in servList with regexp and all occurrences
end try

set devList to {}
set lan to 1
repeat with dev in match
set devName to matchResult of dev
set devList to devList & {devName}
end repeat

set devBox to choose from list devList without multiple selections allowed
if devBox is false then
return
end if
set devAnswer to item 1 of devBox

set servName to ""
try
set servName to matchResult of (find text "^\\([0-9*]+\\)([^
]+)" in devAnswer using "\\1" with regexp)
end try

if (offset of "*" in devAnswer) > 0 then
do shell script ("networksetup -setnetworkserviceenabled " & servName & " on")
display alert devAnswer & " has been enabled!" as informational buttons {"Ok"} giving up after 5
else
do shell script ("networksetup -setnetworkserviceenabled " & servName & " off")
display alert devAnswer & " has been disabled!" as informational buttons {"Ok"} giving up after 5
end if

Dynamic method calling from applescript

Applescript comes handy sometimes when you need to do some automation job quick within the MAC OS. Though it lacks many of the features offered by the modern languages, you can sometimes simulate them doing some dirty tricks :). One of them is dynamic calling of the methods of a particular script. let’s take the case where you want for example to store some options for your script in a separate file though it might look like a fancy way of storing options. so it will look something like


--script_options.scpt
on AppleOptions()
  return some options
end AppleOptions

on GrapesOptions()
  return some options
end GrapesOptions

....

on Options()
  --this should be a default option 
  return some sort of options
end Options

Now if the calling of these options depends on some context that changes every time you run your script, you can still call your methods without knowing beforehand what’s going to be called, like this:

--main.scpt
set theFruit to some_value_that_came_from_current_context
set selScpt to path_to_the_options_script_file
set selScpt to load script selScpt
set scptOpt to Options() of selScpt
try
set _scptOpt to run script ("on run (inArgs)" & return & "set theCaller to item 1 of inArgs" & return & "tell (item 1 of inArgs) to " & theFruit & "Options()" & return & "end run") with parameters {selScpt}
--optionally check for empty list or string returned in the try block
end try
if (_scptOpt is not "" and _scptOpt is not {}) then
--display dialog "ok"
set scptOpt to _scptOpt
end if

Calling the above script with theFruit set as “Apple” (no case checking in the above script though) will call your imported script’s AppleOptions() or if not found will get the default Options method.

Free barcode generator for mac using postscriptbarcode

What’s this?
Simply a barcode generator made up from a few open-source tools. It works and it’s written with Applescript(but it’s easy portable to other platforms and/or languages because of the great library behind … PostScript Barcode written by Terry Burton).

How it works?
Well it simply uses the barcode.eps procedures for the dirty work of drawing the barcodes, and exports only the needed bits to an .eps file named after the barcode. Then ghostscript( the script uses the one provided by macports … you can however use the the gs library provided by mac replacing the /opt/local/bin/gs in the below script with the output of the which gs command executed in terminal ) helps us get the bounding box of the new created barcode and sed will make the needed replacements for us inside the .eps file.

What is needed?
Basic knowledge of a few unix tools and some applescript. Also the documentation of PostScriptBarcode must be read in order to find out the options that can be applied to a particular barcode.

Does it really work?
Well i used with success a modified version of the below script for a long time, and the results were at least as good as the ones from a commercial software for my particular case.

Some pointers …
My version of postscript barcode is slightly changed in order to fix some problems like missing fonts. The first part of my barcode.eps looks like this:

%!PS-Adobe-3.0 EPSF-3.0
%%Creator: spherix v0.6.1
%%BoundingBox%%
%%DocumentPaperSizes: a4
%%DocumentFonts: OCRB
%%DocumentNeededFonts: OCRB
%%DocumentProcessColors: Black
%%EndComments

% Barcode Writer in Pure PostScript - Version 2011-05-10

this script might need refining (make sure you change the defined paths to the ones of your system):

property the_save_dir : "/Volumes/Data/data/EANs"
--this is the path to the barcode.eps file
property the_posix_dir : "/Volumes/Data/Barcode App/"

on DoRun(symbology, theBarcodesFile, indexFieldIndex, separatorChar, theFont, theOptions)
	--display dialog separatorChar
	set symbology to do shell script ("echo '" & symbology & "' | sed 's/[-\\s]//g'")
	set oDelim to AppleScript's text item delimiters

	do shell script ("sed -e s/\\r\\n/\\r/g '" & theBarcodesFile & "'")
	set theContents to my readfile(theBarcodesFile)
	set psLine to "10 10 moveto (%s) (%s) /%s /uk.co.terryburton.bwipp findresource exec"
	set master to "barcode.eps"

	repeat with i from 1 to (count of (paragraphs of theContents))
		repeat 1 times
			set options to {}
			if (paragraph i of theContents is "") then
				exit repeat
			end if
			set AppleScript's text item delimiters to {separatorChar}
			set options to options & {text item indexFieldIndex of paragraph i of theContents}
			--display dialog "Options:" & options

			if theFont is not "" then
				do shell script ("sed \"s/Helvetica/" & theFont & "/g\" '" & the_posix_dir & "/" & "barcode.ps' > '" & the_posix_dir & "/barcode_" & theFont & ".ps'")
				set master to "barcode_" & theFont & ".ps"
			end if

			set _options to {}
			repeat with opt in items of theOptions
				if item 2 of opt is not false then
					set _opt to item 1 of opt
					set _opt2 to item 2 of opt

					if (_opt2 is not "" and _opt2 is not true and _opt2 is not false) then
						--display dialog "not null"
						set _opt to _opt & "=" & _opt2
					end if
					set _options to _options & _opt
				end if
			end repeat

			set AppleScript's text item delimiters to {" "}
			--display dialog theOptions as string
			set options to options & {("'" & _options as string) & "'"}
			set options to options & {symbology}

			set psLineComp to do shell script ("printf '" & psLine & "' " & (options as string))
			set AppleScript's text item delimiters to {separatorChar}

			--display dialog psLineComp
			if (text item indexFieldIndex of paragraph i of theContents is "") then
				exit repeat
			end if
			my makeEPS(master, the_save_dir & "/" & text item indexFieldIndex of paragraph i of theContents, psLineComp, symbology)
		end repeat
	end repeat
	set AppleScript's text item delimiters to oDelim
end DoRun

on readfile(unixPath)
	set foo to (open for access (POSIX file unixPath))
	set txt to (read foo for (get eof foo))
	close access foo
	return txt
end readfile

on writeFile(unixPath, content)
	set foo to open for access (POSIX file unixPath) with write permission
	set eof of foo to 0
	write content to foo
	close access foo
end writeFile

on getRequirements(targetFile, symbology)
	set requirements to "sed -n -e \"/% --BEGIN ENCODER " & symbology & "--/,/% --END ENCODER " & symbology & "--/p\" " & targetFile & " | sed -n -e \"s/^dup \\/\\([^\\s]\\)/\\1/p\""
	set reqlines to do shell script (requirements)
	set oDelim to AppleScript's text item delimiters
	set reqList to {}
	set renderers to {"renlinear", "renmatrix", "renmaximatrix"}

	repeat with i from 1 to (length of paragraphs of reqlines)
		set AppleScript's text item delimiters to {" "}
		set _req to text item 1 of paragraph i of reqlines
		if _req is in renderers then
			set reqList to reqList & {"-e \"/% --BEGIN RENDERER " & _req & "--/,/% --END RENDERER " & _req & "--/p\"" & " "}
		else
			set reqList to reqList & {"-e \"/% --BEGIN ENCODER " & _req & "--/,/% --END ENCODER " & _req & "--/p\"" & " "}
		end if
	end repeat

	set command to "sed -n -e \"/%\\!PS-Adobe-2.0/,/% --END PREAMBLE--/p\" "
	if (length of reqList > 0) then

		repeat with j in reqList
			set command to command & j & " "
		end repeat
	end if

	set command to command & "-e \"/% --BEGIN ENCODER ean13--/,/% --END ENCODER ean13--/p\" " & targetFile

	set fileContents to do shell script (command)

	return fileContents
end getRequirements

on makeEPS(master, unixPath, content, symbology)
	set EPInstr to my getRequirements("/Volumes/Data/Barcode App/" & master, symbology)
	set EPInstr to EPInstr & content
	my writeFile(unixPath, EPInstr)
	set bbox to "%%BoundingBox: 0 0 97 76"
	try
		set bbox to do shell script ("/opt/local/bin/gs -dNOPAUSE -dBATCH -q -sDEVICE=bbox " & unixPath & " 2>&1")
	end try
	do shell script ("sed -i \"\" 's/%%BoundingBox%%/" & bbox & "/g' " & unixPath)

	set theFile to POSIX file unixPath as string
	tell application "Finder" to set file type of alias theFile to "EPSF"
end makeEPS

Calling the script to generate barcodes from a particular file:

my DoRun("the symbology: eg ean13 - must match postscript's barcode intern definitions", "csv data file", "index of the barcode field in csv file", "csv file separator", "font to use with barcode", "options as a list where every option is defined as a list eg: {postscript barcode option, value of the option - in case there is no value put true in the second value(false will ignore option)} => example of an option list {{\"includetext\", true}, {\"height\", cheight / 25.4}, {\"textsize", 11}}\") of myBarcodeGenerator

Embedding multiple forms with symfony … hardcore :)

Well this should be one of the last posts about branch 1.x of symfony. The 2nd version should come to alpha soon so there is no real need to go further with it. Anyway … some time ago I desperately needed a quick and reliable way to embed multiple symfony forms on one page and submit them at once. I generally take the time to look into the source files, but this time i was in a hurry. So this is the “hacky” way i got around:

function executeMoo(sfWebRequest $request) {
$this->forms = array();

$it = 0;

foreach($some_model as $s) {

$f = new MooForm($s);

$f->getWidgetSchema()->setNameFormat('foo[' . $form->getName() . $it . '][%s]');

$this->form->embedForm($it, $form);
$this->forms[] = $this->form;
$it++;

}
}

The presentation part should be as simple as:

<form ...>
<table>
<tr>
<td><table>
<?php
foreach($forms as $form) :
   echo $form;
endforeach;
?>
</table></td>
</tr>
</table>
</form>
</pre>

Ok now for the validation part:

public function executeSubmit_Foo(sfWebRequest $request) {
         $this->forward404Unless($request->isMethod(sfRequest::POST));

   	$base_forms = $request->getParameter('foo');
    
	foreach($base_forms as $base_form) {
		$f = new MooForm();
		$f->bind($base_form,null);
		if($f->isValid()) {
			$f->save();			
		}
	}
}

What’s the catch?
Well the catch is in this line: $f->getWidgetSchema()->setNameFormat(‘foo[‘ . $form->getName() . $it . ‘][%s]’);
What it really does? it generates the form fields like foo[‘forms_name1′] … so the only thing you have to do is grabbing the foo var from request and looping through to get the embedded forms.
Sure there are better ways, but this worked nice with a minimum ammount of time spent.
Hope it helps.