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

2 hours ago, Ivan Gretsky said:

I guess it is namespace related. Try adding ProcessWire namespace in you Hanna codes.

Ah, I forgot to mention it, I tried already – adding <?php Namespace Processwire; doesn't change anything…

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...
  • 1 year later...

@ryan - this still seems to be open. I assume the intention is not to add the namespace if it isn't in the hanna PHP code, otherwise it should be added and so should the 'die'.
I think, in that case, the fix is simple - replace the line
`$code = str_replace($openPHP, "$openPHP\n$firstLine\n", $code);`
by
`$code = str_replace($php, "$openPHP\n$firstLine\n", $code);`

The existing line does not work because, if namespace was in the code it has been removed, but $opnPHP includes it, so the str_replace does not operate.
I have created a PR for this

-----------------

If you do not have namespace declarations in your hanna code php and need them, then the following executed in Tracy console seems to do the trick:

$h = $modules->get('TextformatterHannaCode');
$codes = $h->hannaCodes();
$codesAll = $codes->getAll();
foreach($codesAll as $code) {
    $codeString = $code->code;
    if(!strpos($codeString, 'namespace')) {
        $codeString = str_replace('?php', '?php namespace ProcessWire;', $codeString);
    }
    $code->code = $codeString;
    $codes->save($code);
}

 

Link to comment
Share on other sites

  • 8 months later...

Are people still actively using Hanna Code? I'm trying to do an install of it on a site for the first time in a while. Hitting a problem getting the module to install.

The text formatter will install, but not the Hanna Code module meaning I don't get the editor (even with the config added or site in debug 😞).

 

image.thumb.png.d94b8bf7bc7b75f5029485d87b79fe76.png

Link to comment
Share on other sites

2 hours ago, Denis Schultz said:

yes. Without problems.

On which Processwire version are you?

It was on latest stable 3.0.229. Updating to latest dev 3.0.231 seem to resolve it.

For clarity I'm doing to work updating a fairly old site - so I can't 100% say it isn't something in the legacy code or a Handover from the older PW version I updated from.

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...