Jump to content

Live Text Replace or remove


Moster

Recommended Posts

Hello,

Is there any way to replace text or remove after page rendering by smarty?

in other words.. is replacing text possible with action Hooks?

I'm trying to modify some lines from an API response that are not necessarily to be displayed.

I've tried this:

https://www.php.net/str_replace

https://www.smarty.net/docsv2/en/language.modifier.replace.tpl

and

https://stackoverflow.com/questions/24753891/how-to-use-str-replace-in-smarty

It doesn't work in WHMCS. Although nothing impossible in technology but sometimes I think it's impossible.

Best.

 

Link to comment
Share on other sites

11 hours ago, Moster said:

Is there any way to replace text or remove after page rendering by smarty?

yes.

11 hours ago, Moster said:

I'm trying to modify some lines from an API response that are not necessarily to be displayed.

the API's basically just return JSON arrays - so a string replace wouldn't normally work on an array - you would have to treat it as an array and trim it accordingly - which you should be able to do in the hook (or whatever is calling the API) before returning the result to the template.... ideally, you should be able to get the result you want before returning it to the template and thus avoiding having to trim it in the template.

Link to comment
Share on other sites

Ok treating them as arrays, can you demonstrate one of the two methods you mentioned?

1- The hook for ClientAreaProductDetails

<?php
add_hook('ClientAreaProductDetails', 1, function($vars) {
    // Perform hook code here...?
});

Or ClientAreaProductDetailsOutput

<?php

add_hook('ClientAreaProductDetailsOutput', 1, function($service) {
    if (!is_null($service)) {
        $orderID = $service['service']->orderId;
        return 'OrderID: ' . $orderID;
    }
    return '';
});

2- The trim

?

I'm ok for any one of those.
An example would be helpful for any element of your choice.

 

Link to comment
Share on other sites

3 hours ago, Moster said:

An example would be helpful for any element of your choice.

an example of which API function you're using would be useful too - as i'm being asked how to trim an array I can't even see.

it almost certainly won't be a CAPD hook as that has no return, so it will be a CAPDO or ClientAreaPageProductDetails - but I don't know what you're trying to return in the hook.

Link to comment
Share on other sites

21 hours ago, Moster said:

It's application information coming from external API

that makes it even worse - at least if it were using WHMCS API, i'd have an idea what the resulting array might look like... 🙂

ultimately, you're probably going to foreach loop through the array and unset any values that you want to remove... to give specifics, i'd have to see the resulting array.. perhaps searching for posts where i've used unset to alter arrays in hooks might give you a rough idea

Link to comment
Share on other sites

<?php

# Replace arrays
# Written by Moster! 



function app_info($array) {
	$app_info = $array['get_app_info'];
	foreach($app_info as $array => $app_info) {
		if ($app_info['output'] == "My removed text")
			unset($app_info[$array]);
		
	}	
	return array("get_app_info" => $app_info);
	
add_hook("ClientAreaProductDetails", 1, "get_app_info");
?>

I have no idea what I'm doing at this point, lost totally

I'm going to unset my self today, pls don't laugh at my hook.🙂

Link to comment
Share on other sites

So, unset means remove

On 8/27/2019 at 5:20 PM, brian! said:

you're probably going to foreach loop through the array and unset any values that you want to remove

Yes but how?

 

On 8/27/2019 at 5:20 PM, brian! said:

i'd have to see the resulting array..

<div class="col-sm-12 app_content text-left">
				App Details<br>
<br>
To complete your App installation, follow these instructions.<br>
<br>
1. Log into the App admin panel using the following credentials:<br>
<br>
https://192.168.10.20/admin/<br>
User: name16342<br>
Pass: jq9u1E5muGXBnmm<br>
<br>
2. Point your domain name "A record" to: 192.168.10.20<br>
<br>
3. Edit your "App Address (URL)" and "Site Address (URL)" to match your domain name. This is located in: App Admin, Settings, General.<br>
<br>
Read more about this App on their Docs: <br>
<br>
https://www.app.com/help/amazing-app<br>

			</div>

And this is the .tmp cache:

{"app_info":"App Details\n\nTo complete your App installation, follow these instructions.\n\n1. Log into the App admin panel using the following credentials:\n\nhttps:\/\/192.168.10.20\/admin\/\nUser: name16342\nPass: jq9u1E5muGXBnmm\n\n2. Point your domain name \"A record\" to: 192.168.10.20\n\n3. Edit your \"App Address (URL)\" and \"Site Address (URL)\" to match your domain name. This is located in: App Admin, Settings, General.\n\nRead more about this app on APP Docs: \n\nhttps:\/\/www.app.com\/help\/amazing-app\n"}

I want to remove the last two paragraph, that's it.

 

On 8/27/2019 at 5:20 PM, brian! said:

perhaps searching for posts where i've used unset to alter arrays in hooks might give you a rough idea

I did, and with a good results but still these examples applies to different situation.

Link to comment
Share on other sites

5 hours ago, Moster said:

Yes but how?

that is the question - but after seeing the array, unset is not the right solution. 🙂

5 hours ago, Moster said:

I want to remove the last two paragraph, that's it.

well i'm going to assume that in your hook, you've json_decoded the API generated array and returned it to the template... so to save time created a json array and then decoding it, i'll miss out that step and just create a php array (which is what you'll likely have after the decoding)...

<?php

function moster_array_hook($vars){

	$array = array('app_info' => '*your string*');
			
	foreach($array as $key => $result) {
		$keyword = "Read";
		if ($key == "app_info" && strpos($result, $keyword) !== false) {
			$array2[$key] = strstr($result, $keyword, true) . "</div>";
		}
	}
	return array("brian1" => $array,"brian2" => $array2);
}
add_hook("ClientAreaPageHome", 1, "moster_array_hook");

so ignore the $array line... what that foreach is doing is looping through the array, and if it finds the keyword "Read", it removes everything after that.. and because that removes the final </div> too, we add that back in at the end.

four points on this...

  1. I assume that you don't know what those last 2 paragraphs are going to be for sure, e.g they may change in the future if this text is being pulled via API - hence why I used strpos.
  2. if you do know what those last 2 paragraphs will be, then you could just do a string replace as you previously tried...
    	foreach($array as $key => $result) {
    		$keyword = "<br>
    <br>
    Read more about this App on their Docs: <br>
    <br>
    https://www.app.com/help/amazing-app<br>";
    		if ($key == "app_info") {
    			$array2[$key] = str_replace($keyword,"",$result);
    		}
    	}
    	return array("brian1" => $array,"brian2" => $array2);

    obviously, if this text is likely to change, then replacing strings is not ideal.

  3. in my hook, i've created 2 arrays - $brian1 is the original array; $brian2 is the modified array - in your case, you won't need to do this and can just return the modified array.. so where i've referenced $array2, change it to $array (or whatever your array is called)..

    	foreach($array as $key => $result) {
    		$keyword = "Read";
    		if ($key == "app_info" && strpos($result, $keyword) !== false) {
    			$array[$key] = strstr($result, $keyword, true) . "</div>";
    		}
    	}
    	return array("brian1" => $array);

     

  4. while i'm here, you also asked if you could do this in the template using Smarty... below is a screenshot of 3 arrays - first in the unmodified $array; second is the mofidied $array2; and the third is the unmodified $array, but has Smarty code applied to it in the template... e.g the line of Smarty code below is the equivalent of the PHP code inside the hook IF statement.

    {$brian1.app_info|strstr:"Read":true|cat:'</div>'}

    jVphzt6.png

    <div class="col-sm-4">{$brian1.app_info}</div><div class="col-sm-4">{$brian2.app_info}</div><div class="col-sm-4">{$brian1.app_info|strstr:"Read":true|cat:'</div>'}</div>

    possibly you're using a foreach loop in the template to display the array, but I saved time by calling the array directly - hopefully, this will help you with your array. 🙂

Edited by brian!
Link to comment
Share on other sites

13 hours ago, Moster said:

I've tried all the above, and nothing changes.

did you spot that my demo hook was using ClientAreaPageHome, which only works on the client homepage.

if this is to be used in product details, then it would be another hook... which hook are you using to send this array to the template ?? and I assume the array is being sent to the template and outputted in full within it ???

as an absolute last resort, if you can find the template that is outputting the variable, then you *will* be able to modify it in the template with Smarty.

13 hours ago, Moster said:

the text is made of rock, not able to change or remove a dot.  🙁

oh it will change.. it just needs a little persuasion. 🥊

Link to comment
Share on other sites

7 minutes ago, brian! said:

did you spot that my demo hook was using ClientAreaPageHome, which only works on the client homepage.

Yes, I noticed that, I tried everyone:

ClientAreaProductDetails
ClientAreaProductDetailsOutput
ClientAreaHomePage
ClientAreaPage
ClientAreaFooterOutput
ClientAreaHeadOutput
10 minutes ago, brian! said:

then it would be another hook... which hook are you using to send this array to the template ?

I'm not sure, I found this on the template index.tpl file (Module Dir) Could be useful for other method.

{if isset($appInfo)}
	<div class="row">
		<div class="col-sm-6 col-sm-offset-3">
			<h3 class="so_title">
				{$_LANG.main.index.app_info}
			</h3>
		</div>
		<div class="col-sm-12 app_content text-left">
			{$appInfo|nl2br}
		</div>
	</div>
{/if}

What if I use these hooks inside the module folder instead of  {WHMCS}/includes/hooks/ ?

 

Link to comment
Share on other sites

2 minutes ago, Moster said:

Yes, I noticed that, I tried everyone:

except the one I suggested - ClientAreaPageProductDetails  ? 🙂

but product details can be a tricky page for hooks as some content is generated after the above hook runs.

4 minutes ago, Moster said:

I'm not sure, I found this on the template index.tpl file (Module Dir) Could be useful for other method.

you could try...

{$appInfo|nl2br|strstr:"Read":true|cat:'</div>'}
6 minutes ago, Moster said:

What if I use these hooks inside the module folder instead of  {WHMCS}/includes/hooks/ ?

I don't think it would make any difference unless you find the correct one to use - but I suspect the Smarty code above would work.

Link to comment
Share on other sites

2 hours ago, Moster said:

We (you) did it without hooks .. Amazing!

hey it was a team effort - I couldn't have done it without your array. 🙂

20 minutes ago, Moster said:

I found another way, to redirect to the proper help page.

cool... but just for completeness (in case someone reads this in the future), if you ever needed to replace in text, using Smarty, it would be...

{$appInfo|replace:'App Details':'<b><u>App Details</u></b>'|replace:'the following credentials':'a crowbar'|replace:'https://www.app.com/help/amazing-app':'<a href="https://docs.whmcs.com">https://docs.whmcs.com</a>'}

with the above code, i've added bold & underline to the heading (use could use a css class for this too); i've replace the text on how to login to the admin panel(!) and i've changed their URL link and hyperlinked it to another URL. 😲

3khyxE2.png

Edited by brian!
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated