Jump to content

Hanna Code


ryan

Recommended Posts

After a whiIe of not using the module I recently ran into an issue when I reinstalled the hanna code module. I tried uninstalling the module and reinstalling it, but then I run into the issue of:

SQLSTATE[HY000]: General error: 1813 Tablespace '`revamp`.`hanna_code`' exists.

I checked the database with adminer and searched for "hanna_code", but I am not seeing any tablespace in the database. Is there a way of clearing this out so I can do a fresh install?

Also, I was going to use this module to allow people to define the form fields they would like to use in a setup I am currently developing (one per line). For example, they could type in [[form-firstname]] and the output would be:

<input type="text" name="firstname" id="firstname" required>
<label for="firstname" class="is-required">First Name</label>

In my infinite wisdom, I thought I could explode by the line and wrap each input into a column/row. Obviously this doesnt work as the output is actually two lines (and some of my hanna codes would output up to 6 lines). Is there a good way to go about adding in rows/columns to the actual inputs?

Link to comment
Share on other sites

  • 1 month later...

Is it possible to pass session vars immediately in Hanna Code?

Let’s say, I include two PHP files file_1.php and file_2.php via Hanna Code input in a textarea. When I set $session->foo = "bar" in file_1.php and try to get the value in file_2.php it is not there before page reload. Obviously it has to do with the rendering/formatting order. Is there a more or less dirty trick to intrude the session vars into Hanna Code in the first run?

Link to comment
Share on other sites

Hi,

I know this module have not changed since many years, but could I suggest to add a description for each Hanna code? I come back to my setup page and have some codes that I didn't update since years and I don't know why/where they used through my website. I would like to have written a small description. Yes, you could tell me I should have written comments in the code itself, and it's what I would do starting from now... 🙄 But something on the setup page will be faster to read.
Thanks!

  • Like 3
Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...
13 minutes ago, JFn said:

Although the issue is closed, I commented on it, since it's the same error but not the same resolution or offending module installed.

The old version of @Robin S's HannaCodeDialog module also called that method: https://github.com/Toutouwai/HannaCodeDialog/blob/7ea31c31f151fbe3ea1982c83ac94dfa209e763e/HannaCodeDialog.module#L175

Update to the latest version of that and you should be fine.

Link to comment
Share on other sites

After a second look, apparently the offending HannaCodeHelper module was installed, although I don't recall installing it, was it part of the previous version by default?

Anyway, disabling it, no more errors, problem solved.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

I have trouble with latest update v0.3.0 – my hanna code stops working.

In v0.2.0 I could use 
#1: wire("pages")->get($someid) – in v0.3.0 it throws error "Call to undefined function wire()".
It's easily fixed by calling $pages->get($someid) instead - but I wonder why it's now the opposite as it was in v0.2.0 where I couldn't use $pages?

#2: Inside the hanna code I call custom functions defined in _func.php (included @ready.php – I once moved the include from site/_init.php to ready.php to be able to use the shared functions in hanna code). That worked with v0.2.0, now in v0.3.0 I receive the same error as above "Call to undefined function myFunction()"s

I guess I've got something basically wrong here… I have no clue how to get my custom functions work inside hanna code. Anybody can shed a light … ?

ProcessWire: 3.0.165
PHP: 7.4.11

Link to comment
Share on other sites

On 4/29/2021 at 3:40 PM, adrian said:

Try calling the function like:

\ProcessWire\myFunction()

…ah… thank you adrian! I rember seeing that before. The whole change from v0.2.0 to v0.3.0 is kind of nebulous for me… I should read about namespacing I guess.
And it works! I namespaced all my hanna codes now, changed all wire('…') to $…-> and prefixed my custom function calls as you mentioned. 

Link to comment
Share on other sites

  • 4 months later...
On 4/29/2021 at 9:34 AM, Ivan Gretsky said:

Try adding ProcessWire namespace in you Hanna codes.

 

On 4/29/2021 at 2:40 PM, adrian said:

Try calling the function like:

\ProcessWire\myFunction()

I am confused by this. Surely it should not be necessary to both add the namespace in the Hanna code AND call the function \ProcessWire\myFunction()?

So I looked at the TextFormatterHannaCode.module - getPHP() method and found this:


		$php = '<' . '?php';
		$openPHP = $php;
		$openPHPNS = "$php namespace ProcessWire;";
		$firstLine = 'if(!defined("PROCESSWIRE")) die("no direct access");';

		if(strpos($code, 'namespace') && preg_match('/(namespace\s+ProcessWire(?:;|\s*;))/', $code, $matches)) {
			$openPHP = $openPHPNS;
			$code = str_replace($matches[1], '', $code);
		}

		if(strpos($code, $openPHP) !== 0 && strpos($code, $php) !== 0) {
			// prepend open PHP tag to code if not already present
			$code = "$openPHP\n$firstLine\n$code"; 
		} else {
			// otherwise insert our $firstLine security check
			$code = str_replace($openPHP, "$openPHP\n$firstLine\n", $code); 
		}

It seems that the method is removing "namespace ProcessWire;" from the Hanna code. Looking at the cached files shows this to be the case. In other words, adding  "namespace ProcessWire;" to the Hanna code is pointless as \ProcessWire\myfunction() is still going to be needed.

I really don't know why the above code has been added or what it is trying to achieve?

  • Like 1
Link to comment
Share on other sites

On 9/6/2021 at 9:12 AM, MarkE said:

I really don't know why the above code has been added or what it is trying to achieve?

The more I look at this, the more it seems to me to be a bug. The current method has the effect of not only removing the namespace, but also failing to add:

if(!defined("PROCESSWIRE")) die("no direct access");

to the Hanna code.

In my case, I have dozens of Hanna codes all just calling one hannaCode() function, so that I can maintain the codes in my IDE rather than the PW UI (much less time-consuming!). The upgrade seems to require me to edit every single Hanna code, which surely ought not to be the intention. I have therefore hacked my TextFormatterHannaCode.module to include the following in getPHP():
 


		$php = '<' . '?php';
		$openPHP = $php;
		$openPHPNS = "$php namespace ProcessWire;";
		$firstLine = 'if(!defined("PROCESSWIRE")) die("no direct access");';
		
		if(strpos($code, 'namespace') && preg_match('/(namespace\s+ProcessWire(?:;|\s*;))/', $code, $matches)) {
			$openPHP = $openPHPNS;
			$code = str_replace($matches[1], '', $code);
		}

		if(strpos($code, $openPHP) !== 0 && strpos($code, $php) !== 0) {
			// prepend open PHP tag to code if not already present
			$code = "$openPHPNS\n$firstLine\n$code"; 
		} else {
			// otherwise insert our $firstLine security check
			$code = str_replace($php, "$openPHPNS\n$firstLine\n", $code);
		}

This automatically adds the namespace and the 'die' to all Hanna codes and works (so far) for me. Perhaps others ( @adrian, @ryan?) might suggest a better fix or tell me that I've got it all wrong 😉 

EDIT: I have raised an issue on GitHub for this

Link to comment
Share on other sites

  • 4 months later...

The namespace issue still seems to be unresolved. I think that 

is also related to this. I would be grateful for any comments on the GitHub issue I raised and whether I should just raise a PR to get @ryan’s attention? 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...