SugarCRM GetAllRoles() to Retrieve Data for Custom Module/Page - php

I've been working with SugarCRM by creating a custom module and a custom PHP file to that module. I need to display all the Roles available to Users and have the data displayed on the custom PHP page.
I have looked in to getAllRoles() function and...
ACLRole::getAllRoles(boolean $returnAsArray=false);
Can any one help me get these functions to work properly?
(Please answer only if you know the answer and do not close the question with false reasons.)

You can easily get all Roles that aren't deleted, you will find the method in /modules/ACLRoles/ACLRole.php. It will return either an array of array representations of acl roles or an array of ACLRoles.
The query is:
'SELECT acl_roles.* FROM acl_roles WHERE acl_roles.deleted=0 ORDER BY name';
Try this code:
$roles = array();
$roles = ACLRole::getAllRoles(true);
print_r($roles);
sugar_die();
Hope that helps.

Related

Printing out the lesson list in a course on custom template (course page for visitors) learndash get_course_lessons()

Hi I have used the shortcode to print out the course content or the list of lessons in a course but it doesnt work
I have tried using the below method
get_course_lessons();
when I add get_course_lessons(); , it says that 'get_course_lessons' is undefined, and the get course lessons is a public class in the following file wp-content/plugins/sfwd-lms/includes/classes/class-ldlms-factory-post.php
Is the above path is the right way if so how can i get it working or is there a better way to do it?
Thanks
You should use learndash_get_course_lessons_list()
learndash_get_course_lessons_list( int|WP_Post|null $course = null, int|null $user_id = null, array $lessons_args = array() )
Pass the course ID to $course and it will return all the lessons from that specefic course.

Magento 2/Wordpress/Fishpig related posts

I am currently trying to retrieve a list of related posts on the post article page (post single). I have created a new function within /Block/Post/ListPost.php
public function getRelatedPosts()
{
$posts = $this->getPosts();
die($this->getCategoryId());
return $this->_postCollection;
}
However when I try and output getCategoryId, I get nothing. I'm also unsure how I apply a category filter to the post collection.
Can anyone advise here?
I'm not sure where you have got the getCategoryId method from but this is not part of the ListPost block class so will not work. You cannot just invent methods like this.
You should check the block class for what methods are available. An easy way to do that without even loading the file is to add the following PHP to the class:
echo sprintf('<pre>%s</pre>', print_r(get_class_methods($this)));
exit;
You don't specify in what way the posts should be related but I'm guessing you want to get posts from the same category. One option to do this would be to load the primary category of the post and then get a post collection based on this. If you look in the Post class file, you will see the getParentTerm($taxonomy) method.
if ($category = $post->getParentTerm('category')) {
$relatedPosts = $category->getPostCollection();
// Remove the current post from the collection
$relatedPosts->addFieldToFilter('ID', array('neq' => $post->getId()));
}
You should always look at the class file's you're working with. That is the beauty of open source. You can literally see what methods are available for each object and you can even see how they work.

How to change a Joomla User Group with php

In a Joomla 2.5 site I want to assign a registered user to another group after they complete a form. The user would be logged in when they complete the form which will be created in Fabrik so that I can add a PHP script to run on save. My php skills are rudimentary. What would the lines in php look like that adds the current user to a group?
You can use JUserHelper::setUserGroups to set the group(s) of a user.
To retrieve the current user you can use JFactory::getUser.
When you have the user, you can (I haven't confirmed this) probably get the id right away. Some non-tested code which will hopefully get you on the way:
$user = JFactory::getUser();
$userId = $user->id;
JUserHelper::setUserGroups($userId, 3);
Or in short:
JUserHelper::setUserGroups(JFactory::getUser()->id, 3); // 3 Is the group number
You can find the group ID's by going into the table-prefix_usergroups table and retrieve the primary key. I haven't found a way to retrieve a list of groups without going into the database, but this may help.
A screenshot of my (non-edited) database with usergroups:
Here I used a different approach as JUserHelper::setUserGroups method always failed with my Joomla 3.3.1 installation.
$user = JFactory::getUser();//get current user
$g = array('2'=>2,'3'=>3);//2 for registered user, 3 for author
$user->groups = $g;
$user->save();
I'm using Joomla 3.7.5, and this method worked for me:
JUserHelper::setUserGroups($joomlaId, array(10));
Where, $joomlaId is the id of the user I want to change. This sets the user to only be a member of group 10, and saves the changes to the user DB immediately.
You may also want to look at the addUserToGroup method (since Joomla Platform 11.1)
JUserHelper::addUserToGroup (JFactory::getUser()->id, 3); // 3 Is the group number
So for example if your user was previously in the 'registered' group (id=2), he/she will also be assigned to group 3 after this.
Similarly there is also the removeUserFromGroup method which does the opposite.
As per the documentation, it may be useful to note that the setUserGroups method supports an array for the groups, whereas the other 2 methods I mention here support an integer.
setUserGroups(integer $userId, array $groups) : boolean
addUserToGroup(integer $userId, integer $groupId) : boolean
removeUserFromGroup(integer $userId, integer $groupId) : boolean
https://api.joomla.org/cms-3/classes/JUserHelper.html
I couldn't find any useful way to do this in joomla 3, so here's a quickie for people who just need this to work:
$db = JFactory::getDbo();
$db->setQuery("replace into xo6uf_user_usergroup_map values ($user_id, $group_id)");
$db->query();

Multi select filter in layered navigation

I have a custom multi select attribute which I'd like to take part in filtering of products. The attribute is set as used in Layered Navigation however doesn't appear in the list of available filters. Could be due to custom model implementation?
Anyone have some tips where to check why it doesn't appear? Attribute is set for several products
Magento version used is EE 1.11
Thanks
For those who will struggle with this in the future: the problem is in Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source file on line 191. By default multi select attribute values are being pulled from eav_attribute_option and if your custom attribute uses custom source model the attribute will not be indexed.
I don't know as of yet if it's intended but I couldn't find a better solution than overriding that model in local pull and adding required values in $options array.
Hope this helps someone, someday
What is the backend_type. i.e. are the values stored in the catalog_product_entity_varchar or catalog_product_entity_text table?
The backend_type has to match the checks in Mage_Catalog_Model_Resource_Eav_Attribute::isIndexable(), so text wouldn't work without rewriting the attribute model.
Is the is_filterable and/or is_filterable_in_search attribute property set?
The Mage_Catalog_Model_Product_Indexer_Eav::_registerCatalogAttributeSaveEvent() checks for those when updating the index for the layered navigation.
Are the methods getFlatColums(), getFlatIndexes() and getFlatUpdateSelect() implemented in the custom source model?
This actually is only required for building and updating the flat catalog product tables (so the used_in_product_listing or is_filterable property needs to be set in order for Magento to pick up the attribute).
Check the class Mage_Eav_Model_Entity_Attribute_Source_Table as a reference on what these there methods are supposed to return.
NOTE: I'm adding this in a new answer to use the code format.
How it was said, the problem is with multiselect attributes using a custom source model.
Solution:
Rewrite the class
Mage_Catalog_Model_Resource_Product_Indexer_Eav_Source
Override the method:
_prepareMultiselectIndex
add this code after the $options array is filled with the default code (check line 200 in original file)
foreach($attrIds as $attId){
if( ! isset($options[$attId])){
$options[$attId] = $this->_getOptionsFromSourceModel($attId);
}
}
add this method too:
protected function _getOptionsFromSourceModel($attId)
{
$options = array();
/** #var Mage_Eav_Model_Entity_Attribute_Abstract $attribute */
$attribute = Mage::getResourceSingleton('catalog/product')->getAttribute($attId);
/** #var Mage_Eav_Model_Entity_Attribute_Source_Abstract $source */
$source = $attribute->getSource();
$sourceOptions = $source->getAllOptions();
if($sourceOptions){
foreach($sourceOptions as $sourceOption){
if(isset($sourceOption['value'])){
$options[$sourceOption['value']] = true;
}
}
}
return $options;
}
I couldn't find a less intrusive way to fix this.

Getting list of products by category in Magento using SOAP-based API

I need to get all products belonging to a specific category in Magento using the web services API. I tried this method:
$product_filter = array(
'category_ids' => array('eq' => '41')
);
$product_templates = $magento_client -> call($magento_session, 'product.list');
But it returns an error. I can only assume it's because category_ids is an array, so it won't really ever equal one specific value.
I did some research and found another method called category.assignedProducts and tried:
$product_templates =
$magento_client ->
call($magento_session, 'catalog_category.assignedProducts', array('41'));
But this returned an 'Access Denied' error. I went and looked at my sandbox of Magneto and saw that 'Assigned Products' has 3 options: 'Remove', 'Update', 'Assign', and I know that the admin for the system I'm linking to has set my access to 'Read-Only'. So I'm guessing that we'd have to check off 'assign' in that list, which would give me more access than they want to give.
I could retrieve all of the data and do the filtering on my end, but I wanted to check if anyone knew of a better way.
Thanks.
assignedProducts sounds like what you need but you shouldn't need to be passing along an array but an integer value and the store ID or code.
See the arguments required: http://www.magentocommerce.com/wiki/doc/webservices-api/api/catalog_category#catalog_category.assignedproducts
I think I found the answer on http://www.magentocommerce.com/boards/viewthread/207099/ which basically says it can't be done from the product any more. We must now look at the problem from the category point of view.
category_ids no longer works in 1.4, they changed the table
structures around so that the categories are not available on the
product. Use the code below to get the products from the category and
then do a catalog_product.list call. To make it faster you can also
create a custom api solution to combine these in Magento and perform
just one call instead of two.
$proxy = new SoapClient($soapUrl.’api/soap/?wsdl’); $sessionId =
$proxy->login($apiUser, $apiPass);
$productList = $proxy->call($sessionId,
‘catalog_category.assignedProducts’, array(’4’));
$proxy->endSession($sessionId); print_r($productList);

Categories