Silverstripe 3 doPublish in Controller - content-management-system

in an old silverstripe installation (2.4) i had a little counter function that looks basically like this:
class Page_Controller extends ContentController {
public function countUp(){
$this->Counter = $this->Counter+1;
$this->writeToStage('Stage');
$this->publish("Stage", "Live");
}
}
I want to move that over to SS 3.
Somehow it does not work anymore, the Counter Property is never updated in the Datebase.
Does someone know why?
Kind regards,
Florian

1 - That method should be on the Page class (model), not the controller. You can then call $this->data()->countUp(); from the Page_Controller class.
2 - You need the line $this->write(); before $this->writeToStage('Stage');

Related

Is there any way to route random string to dashboard in Codeigniter 3?

I have a question about Codeigniter: Is there any way to check function exists In the controller file and then route to the function path or else route it to a specific url?
I don't know what kind of Dashboard mean in your question.
I just answer how to check function exist or not in controller and route to that function.
In some case, you may need to add a route rule in config/routes.php.
$route['/programme/(.*)$'] = 'programme/programmes/$2';
Use method_exists() to check if function exist.
class Programme extends CI_Controller {
public function programmes($sub_part = NULL) {
if (!empty($sub_part) and method_exists($this, 'programmes_'. $sub_part)) {
return $this->{'programmes_'. $sub_part}(array_slice(func_get_args(), 1));
}
die('bar')
}
private function programmes_foo() {
die('foo')
}
}
When a visitor go to https://www.foobar.com/programme/foo (foo is something affect which function to call), your website should able to show foo. If the function is not exist, it show bar.
You can use
$route['product/(:any)'] = 'yourcontroller/your_function';
Source : https://codeigniter.com/userguide3/general/routing.html
i think you can change your route into this
$route[‘produk/(:num)/(:any)’] = ‘HomeControl/detail_produk/$1/$2’;
result from that route it's like this
http://www.localhost.com/produk/1/nama-produk-1
or maybe you can read the documentation CI 3 in this link or you can read my reference this link
i hope i can help you, thanks.

SugarCRM: how to use preDisplay function in ViewQuickcreate?

I'm trying to customize the quick create view to add a default value of a field in Sugar Community Edition 6.5.24
Similar code works fine for ViewEdit, but it seems never called in subpanels.
Current file is
custom/modules/Opportunities/views/view.quickcreate.php
Unfortunately the constructor is not invoked.
Any help very appreciated.
<?php
require_once('include/MVC/View/views/view.quickcreate.php');
class OpportunitiesViewQuickcreate extends ViewQuickcreate {
function OpportunitiesViewQuickcreate(){
parent::ViewQuickcreate();
}
function preDisplay() {
parent::preDisplay();
$_REQUEST['custom_field_c'] = "a value for this field";
}
}
After tens of trying, I've found solution.
The right way is to extend SubpanelQuickCreate in the file custom/modules/Opportunities/views/view.subpanelquickcreate
require_once('include/EditView/SubpanelQuickCreate.php');
class OpportunitiesSubpanelQuickcreate extends SubpanelQuickCreate {
function OpportunitiesSubpanelQuickcreate() {
$_REQUEST['custom_field_c'] = "a value for this field";
parent::SubpanelQuickCreate("Opportunities");
}
}
Going from memory, so I may be wrong, but try adding $this->useForSubpanel = true; in your constructor.

Objective-C - Access method from other controller

i have a little question about getting access to a method in another controller, nu i am trying this.
So for example i have the controller A and B. In the controller A i have programmed a method, now i want to get access this through controller B.
What i have done in class A in the header file:
+(void)goBack;
and in the implementation file:
+(void)goBack {
NSLog(#"go back");
}
in the controller B i do this to get access to the method in controller A:
+(void)goPreviousArticle:(id)sender {
ViewProductInformation_ViewController *theInstance = [[ViewProductInformation_ViewController alloc] init];
[theInstance goBack];
}
However when i execute the program, then it does not work, the program just shuts down, when i do command click on the function goBack in controller B i get referred to the method in controller A.
Does anybody have an idea what the problem could be?
thanks in advance,
snowy
It's quite easy ... you just mixed the class and instance-method declaration: The "+" sign indicates that the method is a class method. In your case it should be a "-" so
-(void)goBack; // a instance method declaration!
Hope this helps.
Class vs instance method declaration ... see also What is the difference between class and instance methods?
You are declaring goBack as a CLASS method (with the preceding "+"). Change the + to a -.
Since goBack is a static method of Class A, you don't need an instance of A to call it's method, you can just call it like so:
[ClassA goBack];
You don'y need to declare static functions you can writ like this:
-(void)goBack {
NSLog(#"go back");
}
In the class A and same in the class B:
-(void)goPreviousArticle:(id)sender {
ViewProductInformation_ViewController *theInstance = [[ViewProductInformation_ViewController alloc] init];
[theInstance goBack];
}
Then use them. I think in that case application will not crashed.

How to call custom filters in Zend?

I want to use htmlpurifier on my website, but can't figure out how to load my filter in the view. I've added my filter the way described in the first answer here.
I want to be able to call it from my view with something like $this->filter($content) Any suggestions how I do that?
It is a two step process:
Write an actual Zend_Filter implementation of HTMLPurifier (done, answer in the question you mentioned)
Write a view helper
It will look like this:
class My_View_Helper_Purify extends Zend_View_Helper_Abstract
{
public function purify($value)
{
$filter = new My_Filter_HtmlPurifier();
return $filter->filter($value);
}
}
Don't forget to add your custom view helper path:
$view->addHelperPath(
APPLICATION_PATH . '/../library/My/View/Helper',
'My_View_Helper_'
);
And later in any of your view scripts:
<?= $this->purify($text) ?>

How to run same lines in all controllers init() function?

I need same 2 lines in all my controllers, each controller have its own init logic, but these two lines are common for all of them.
public function init()
{
$fm =$this->_helper->getHelper('FlashMessenger');
$this->view->messages = $fm->getMessages();
}
How can I avoid repeat code ?
Update:
Ok, the FlashMessenger was only an example, let's say I need write a log line in every action except for 'someAction' # 'someController'. So the new common lines should be.
$this->logger = new Zend_Log();
$writer = new Zend_Log_Writer_Stream(APPLICATION_PATH.'/../logs/log.txt');
$this->logger->addWriter($writer);
$this->logger->log('Some Message',Zend_Log::DEBUG);
The question is, where should I place these lines in order to avoid repeat them in all init() of each controller.
These lines should be placed at bootstrap?. If so: How can skip log lines for 'someAction'.
Or should I implement a 'BaseController' and make all my controller extend from it. If so: How can I Autoload it? (Fatal error: Class 'BaseController' not found) .
Just subclass the controller:
class Application_ControllerAction extends Zend_Controller_Action {
public function init()
{
$fm =$this->_helper->getHelper('FlashMessenger');
$this->view->messages = $fm->getMessages();
}
}
class IndexController extends Application_ControllerAction {
}
You may also achieve the same writing Controller Plugin.
Edit:
Front controller plugins are executed on each request, just like the Controllers and have the same hook methods:
routeStartup(): prior to routing the request
routeShutdown(): after routing the request
dispatchLoopStartup(): prior to entering the dispatch loop
preDispatch(): prior to dispatching an individual action
postDispatch(): after dispatching an individual action
dispatchLoopShutdown(): after completing the dispatch loop
I addition, you may check controller params to execute the code only on selected requests:
if ('admin' == $this->getRequest()->getModuleName()
&& 'update' == $this->getRequest()->getActionName() ) …
You can access your flash messages through (you dont need to send anything from your controller to your view, it's all automated)
$fm = new Zend_Controller_Action_Helper_FlashMessenger();
Zend_Debug::dump($fm->getMessages());
in you view, i would also recommand that you encapsulate this code in a view helper like it is shown on this site http://grummfy.be/blog/191
In your bootstrap:
protected function _initMyActionHelpers() {
$fm = new My_Controller_Action_Helper_FlashMessenger();
Zend_Controller_Action_HelperBroker::addHelper($fm);;
}
How can I avoid repeat code ?
Write your own custom controller, implement that in init method of that controller, and then extend all controllers in your app from your custom controller.
But approach with separate view helper as #Jeff mentioned (look at link) is often taken as a better solution.
In your controller do only:
$this->_helper->flashMessanger('My message');
and view helper will do the rest.
It's not the reason for creating new custom controller. Just add this line to all you init() methods.
$this->view->messages = $this->_helper->getHelper('FlashMessenger')->getMessages();