How to process and change output before it is sent back to the browser? - cakephp

I want to implement a mechanism in CakePHP that is like ShortCodes in Wordpress. I want to save my pages (in the DB) with tokens, e.g.
[form id=12]
and then, after the view got rendered and before it is sent back to the browser, I want to search the rendered view for these tokens, and replace them with something else.
I assume I'll have to use beforeFilter, afterFilter or beforeRender, but I can't find any documentation (including in CakePHP's own documentation!) about how these overriden functions can be used to change the output.
Can anyone help?

Related

Using Matomo API to get top 10 visited pages starting with a certain URL

For a weblog I am trying to get the top 10 popular posts from for example the last month. I figured I'd get the data out of Matomo, as that's already tracking visits and has an API. I've never used this API before though, so I've been reading the documentation and trying out some things. I am able to get data from the API using the Actions.getPageUrls method. However, when I try to filter using segment=^http://example.org/post I still get data from other URL's. It looks like it filters on session and gives back all data from the sessions that have at least 1 page that conforms to the filter.
The full URL I'm using is: http://example.org/matomo/index.php?&module=API&token_auth=12345&method=Actions.getPageUrls&format=json&idSite=1&period=month&date=today&expanded=1&segment=pageUrl%3D%5Ehttp%253A%252F%252Fexample.org%252Fpost. I've also tried with less and no URL encoding for the segment, but that doesn't seem to make a difference. If I use a URL that doesn't exist I get an empty array returned.
Am I doing something wrong? Is there a different way to only get the top pages with a URL starting with http://example.org/post? Or do I have to sift through the data myself to only get the pages I want?
I am using Matomo version 3.13.5.
I figured it out. There is no need to use segment. This can be achieved using the flat, filter_column and filter_pattern parameters.
Setting flat=1 will make it so all pages are returned in a single array, instead of hierarchically.
With filter_column and filter_pattern I can filter the results.
The URL I use now is: http://example.org/matomo/index.php?&module=API&token_auth=12345&method=Actions.getPageUrls&format=json&idSite=1&period=month&date=today&flat=1&filter_column=label&filter_pattern=%5E%2Fpost%2F. This does exactly what I want.
The unencoded pattern is ^/post/, so this will filter out any page that does not start with /post/.

How to call Data Editor from Code, C1 CMS?

Under definition tree, I can attach event to open Data Editor for certain element.
However, I have no idea how to trigger the workflow to open the Editor for a given id.
I have a custom action URL which point to a custom ASPX (List of businesses about 50,000) in the console.
Now I want to trigger call the default data editor from the custom ASPX. Is that possible.
Yes you can. There is a few steps you need to take, but then you can basically mimic all console behaviors from your own aspx-page.
First of all you need to include the necessary C1 Console javascript frameworks on your page. Do that by calling ScriptLoader.Render("sub") and write the result on your page. You would also need to include this javascript on your page https://github.com/burningice2866/CompositeC1Contrib/blob/master/Teasers/Package/content/Composite/InstalledPackages/CompositeC1Contrib.Teasers/teaserConsoleFunctions.js - its some wrapper methods which are able to construct the javascript objects needed to call C1's webservice methods.
When that is setup you can trigger opening a Edit Workflow by calling the executeAction javascript method via a link containing the necessary ActionToken and EntityToken like this https://github.com/burningice2866/CompositeC1Contrib/blob/master/Teasers/Web/UI/TeaserHtmlHelper.cs#L65.
Its important to understand that everything in the C1 Console in based on these tokens. You don't edit something based on its id but always based on the EntityToken the something is represented by.
And what you want to do, whether its edit something or delete it, that thing you want to do is represented by an ActionToken. So to be able to edit something, you need an EntityToken for that something and a ActionToken for the Data Editor.

Nested CakePHP Routing

I'm working on a CMS and would like to configure cake to automatically find the proper page to use based upon the URL. I don't want to have to manually set this and want it to automatically work if I create new pages (based upon slugs).
Let's say I have the following "page layout":
News
-Local
-World
-article1
Products
-Product1
-details
-photos
etc
So: mysite.com/local/world/article1 would load "Article1". If I added a page under article1 titled "photos", then: mysite.com/local/world/article1/photos would work. Also, mysite.com/products/product1/details would work, etc. Any page I add to the site should be available (it should use a parent/child method). Any tips on making this happen with CakePHP?

DotNetNuke Switching Between Multple Edit Modules

I have a custom module in DNN 7 that has a data structure where items belong to categories (called "sections", not DNN taxonomy, just a simple list of section names). The module edit screens work so that on the view control you may click on an edit link on each category, which loads the category edit screen (passing the category id). This works great, and when you save I use Globals.NavigateURL() to return to the view screen. This all works as intended.
On each category edit screen I also have a list of the items within that category, each with an edit link. Clicking the edit link opens the item edit screen, passing the correct item id, and allowing me to edit that item. This all works great, until you save. The save works properly, but when I want to send the user back to the edit screen for the category it doesn't work. When I use:
Response.Redirect(EditUrl("SectionId", sectionid.ToString(), "EditSections"), true);
...nothing happens. It simply doesn't redirect anywhere. This is exactly the same URL I'm using to get to the category edit page in the first place:
EditUrl("SectionId", Eval("SectionId").ToString(), "EditSections")
And then I use a similar URL to get to the item edit page:
EditUrl("ItemId", Eval("ItemId").ToString(), "EditItems")
I don't understand why using the same URL to navigate to the same page I already navigated to would simply not do anything. For now I am sending them all the way back to the view, but it's painful if you need to add several items to the same category to have to navigate back into the category and add another item, only to be sent back to the view.
Anyone see anything like this before?
Have you tried to use the overload of NavigateUrl instead of EditUrl?
Globals.NavigateURL(TabId, "EditSections", "mid", ModuleId.ToString(), "SectionId", Eval("SectionId").ToString())
I haven't seen that myself but I would have to assume that somehow the context is being lost with EditURL and you're not getting sent to the proper location due to that.
I would suggest you try one of two things (or both).
Debug the URL that EditURL is returning and see if you can find the
difference.
Use NavigateURL for all your links and pass in MID=## for your
moduleid as a querystring parameter, to ensure that the proper
values are being passed.
UPDATE: If you're trying to have multiple edit views, and move between them, you might look at using a "loader" instead of having separate module definitions for the edit controls. Basically have a single Edit.ascx file defined, and it loads other ASCX files within it, injecting into a Panel. The View control on this module http://dnnsimplearticle.codeplex.com/ does that, but I haven't tried it with an edit control before.

Grab a variable from the URL and then pass it to a controller CakePHP 2.0

I'm trying to build a sort of Wordpress-esque CMS system to a project I'm building. I want the user to be able to create pages on the fly, and for them to appear in certain areas of the website.
I have made something similar in Symfony2, where the controller grabs a specific variable from the URL (as definded in the route.yml file, usually $id etc) and I then use the variable in the controller to display whatever content it relates to in the database.
However, I'm not used to CakePHP 2.0, and struggling to find what I need. I know it's possible, but I don't know the best way to achieve it. Especially as CakePHP uses a different routes file than Symfony.
How would I grab a variable from the URL and pass it for use inside a controller?
By variable do you mean GET query string parameters, like in /foo?key=value? You can access them in the controller through the request object: $this->request->query['key'].
If you are looking for something more integrated you can use CakePHP's default routes or make your own.
The default routes work with URLs like /controller/action/param1/param2 and pass the parameters to the action by position. For instance /posts/view/521 maps to a call to view(521) in PostsController, and /posts/byMonth/2012/02 maps to a call to byMonth("2012","02").
You can also use named parameters and the URLs look like /controller/action/key1:value1/key2:value2. In controller actions you would read them with $this->params['named']['key1'].
With custom routes you can make your URLs anything you want. You're not forced to the /controller/action pattern; you can make /archives/2012-02 map to PostsController::byMonth(2012,2), or have /512-post-title map to PostsController::view(512).
Typically you would start out with the default routes and add custom routes when you decide you need them. You can read all about the default and custom routes in http://book.cakephp.org/2.0/en/development/routing.html
Short answer: it depends.
If you're looking to pass parameters to a function, you don't need to mess with routes at all; every non-named URL path segment is processed in order and handed to the action as method parameters (so /controller/action/1234 passes "1234" as the first parameter to the action method in the controller controller class).
Named parameters allow you to pass parameters anywhere in the URL string, and to make them optional. The form is just key:value and they're accessed via $this->params['named'] in the controller.
The last option is prefix routing. The best place to get to speed on that is naturally the CakePHP Cookbook, but the general gist is that in a route definition, you can name a path component in the URL by prefixing it with a colon, identically to how the default routes show :controller, :plugin and :action. You can define any name you like, even optionally applying regex pattern requirements and then access it through $this->params['variablename'] in the controller.

Resources