Custom component check-in/check-out best practices - joomla

I'm creating a component in Joomla! 1.7 and I'd like to take advantage of the framework's check-out/check-in features. Currently
How do I mark a component record as "checked out" when a user requests the edit task for that record?
How do I mark a record as "checked in" when the user attempts to store his or her edits?
How do I test the checked-in/checked-out status of a component's record at edit time?
Thanks!

Basicly you need two methods in your model, which you can call whenever you want to:
function checkin()
{
if ($this->_id)
{
$item= & $this->getTable();
if(! $item->checkin($this->_id)) {
$this->setError($this->_db->getErrorMsg());
return false;
}
}
return false;
}
function checkout($uid = null)
{
if ($this->_id)
{
// Make sure we have a user id to checkout the article with
if (is_null($uid)) {
$user =& JFactory::getUser();
$uid = $user->get('id');
}
// Lets get to it and checkout the thing...
$item= & $this->getTable();
if(!$item->checkout($uid, $this->_id)) {
$this->setError($this->_db->getErrorMsg());
return false;
}
return true;
}
return false;
}
To mark item as checked, first of all you have to have column called checked_out with default value 0, also you need checked_out_time to store time, when item was checked out.
Hope it helps.

Related

Prestashop 1.7.8 form fields validation

How to add custom class for example "error" to requiered fields when customer forget to fill them?
For example in registration form. When customer forget to fill multiple fields and click on Submit how to highlight all that missing fields?
Not a pretty solution, but it works
$('#login-form').submit(function() {
var $inputs = $('#login-form :input');
let hasError = false;
$inputs.each(function() {
if(!$(this).val() && $(this).prop('required')){
$(this).addClass('required-error-class');
hasError = true;
}
});
if(hasError){
return false;
}else {
return true;
};
});
A nicer solution is to use e.preventDefault and ajax

pass collection of records to policy

this is my tables structure :
i want to get all Request State Records where Request_status.Department_id equals with User.Department_id . and user only can see own request_state records i defined a policy to handle this job but this policy only can handle 1 request_state record . this policy can't handle array of request_state Model ! how can i use get() instead of first()
Policy :
public function view(User $user, RequestState $requestState)
{
return$user->department_id===$requestState->department_id&&$user->hasRole('department');
}
Controller :
public function show_request()
{
$RequestState=RequestState::where('department_id',auth()->user()->department_id)->first();
nullable($RequestState)->getOrSend(function (){
return Responder::requestDoesNotFound();
});
if(auth()->user()->can('view',$RequestState)){
return 'ok';
}
}
The policy is fine as-is. The changes should be in the controller.
Your controller method is called show_request(). The name implies it is showing a single request. But the controller logic shows it is showing all the requests for the user. You need to either validate each request individually, or assume that all the requests have the same department_id, since that is what your query pulls. Assuming you want to check all the RequestStates in the collection, you can them to a single TRUE/FALSE;
public function show_request()
{
$requestStates = RequestState::where('department_id', auth()->user()->department_id)->fget();
nullable($RequestState)->getOrSend(function (){
return Responder::requestDoesNotFound();
});
return $requestStates->reduce(function ($accumulator, $requestState) {
return $accumulator and auth()->user()->can('view', $requestState);
}, TRUE);
}
As I can see, you want lo allow the users to see the list of request they have access. You can create a new method in your controller method to validate all the requests.
It should be something like this:
public function show_requests()
{
$requestStates = RequestState::where('department_id',auth()->user()->department_id)->get();
if(sizeof($requestStates) > 0 ){
for($i = 0; $i < size($requestStates); $i++ ) {
if (!(auth()->user()->can('view', $requestStates[$i]))) {
//Unset the record the user can't see
unset($requestStates[$i]);
}
}
//Return all records the user can see
return $requestStates;
}
else {
return Responder::requestDoesNotFound();
}
}

Customer email is required error on Checkout when going through as a guest

In Magento 1.7, whenever I go through the checkout process as guest at the end of the onepage checkout process I get the Magento error:
Customer email is required
Having used xDebug to profile the problem the code I have in my observer which is executed on the sales_order_place_after observer I have a function called afterOrderPlaced()
public function afterOrderPlaced($observer)
{
$organisation_id = Mage::getSingleton('customer/session')->getOrganisationId(); #$organisation_id = 25679;
$this->_order = $observer->getEvent()->getOrder();
$this->_order->setOrganisationId($organisation_id)->save();
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
// problem on the next line below #PROBLEM HERE#
$this->_customer->setOrganisationId($organisation_id)->save();
}
The issue is on the last line of the function - for some reason it doesn't seem to like the save() on the customer object. This goes into the core files in Mage\Core\Model\Resource\Transaction.php on line 161 within the save() - see below:
public function save()
{
$this->_startTransaction();
$error = false;
try {
foreach ($this->_objects as $object) {
$object->save();
}
} catch (Exception $e) {
$error = $e;
}
if ($error === false) {
try {
$this->_runCallbacks();
} catch (Exception $e) {
$error = $e; ## ERROR IS HAPPENING HERE?! ##
}
}
if ($error) {
$this->_rollbackTransaction();
throw $error;
} else {
$this->_commitTransaction();
}
return $this;
}
Can anyone indicate what my problem maybe within my observer and why it doesn't seem to like to save the custom organisation_id to the customer object?
For guest user why are you trying to save organisation id in customer profile, there will no customer profile to save because Magento doesn't create customer in customer entity for guest users. You need to add condition to your function above to resolve the problem
if(!$order->getCustomerIsGuest()){
// Customer stuff
$this->_customer_id = $this->_order->getCustomerId();
$this->_customer = $this->_order->getCustomer();
// problem on the next line below #PROBLEM HERE#
$this->_customer->setOrganisationId($organisation_id)->save();
}
Hope the above helps
Cheers
S

Joomla 3 "Submit an article" page - how to remove "access level"

I have been struggling with this problem for a week now, please can someone help me. On the "submit an article" page which is only available for "registered users", can you disable or remove the field that allows the registered user to select an "access level"? I want it to be automatically set to "public" because normal registered users don't know what that means so will get confused by it
thanks
Write a content plugin and implement the function onContentPrepareForm like
public function onContentPrepareForm($form, $data) {
if (! in_array($form->getName(), array('com_content.article'))) {
return true;
}
$form->removeField('access');
return true;
}
and onContentBeforeSave
public function onContentBeforeSave($context, $article, $isNew) {
if ($context != 'com_content.article' && $context != 'com_content.form') {
return;
}
$article->access = 1;
return true;
}
Perhaps are some of the context names incorrect as I wrote to code out of my memory :-)

Is there a way to send CakeResponse from blackhole callback

I am trying to make the ajax request more user friendly. It is a scenario that using a token which can be reuse in a period of time. However in time that a user has inactive for a certain time and tried to use the form, user will observe that the ajax not working but donno why. What I'm gonna do here is to display the message.
Using my testing code, doing return new CakeResponse seems to be return true in blackhole and therefore the $result is true although the User edit should not be triggered
public function beforeFilter() {
$this->Security->blackHoleCallback = 'blackhole';
}
public function blackhole($type) {
if ($this->request->is('ajax')) {
$this->log('blackhole here','debug');
return new CakeResponse(array('body'=> json_encode(array('data'=>'The request has been blackholed')),'status'=>500));
}else{
$this->log('blackhole there','debug');
return new CakeResponse(array('body'=> json_encode(array('data'=>'The request has been blackholed')),'status'=>500));
}
}
in the userapp
public function edit() {
$userId=$this->Auth->user('id');
if (empty($this->request->data)) {
$this->request->data = $this->User->read(null, $userId);
}
if ($this->request->is('ajax')) {
if($result = $this->{$this->modelClass}->edit($userId, $this->request->data)){
$this->Session->write('Auth', $this->User->read(null, $userId));
return new CakeResponse(array('body'=> json_encode(array('data'=>'saved')),'status'=>200));
}else{
return new CakeResponse(array('body'=> json_encode(array('data'=>'error')),'status'=>500));
}
}
}
Progress 1:
Unsolved, perhaps needed to use event handler or exception, though it will be somehow complicated . Still thinking while refining other plugin feature.

Resources