PageRenderTime 33ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/com_flexicontent_v2.x/site/classes/flexicontent.fields.php

http://flexicontent.googlecode.com/
PHP | 3300 lines | 2156 code | 526 blank | 618 comment | 526 complexity | 2069c6a2b4cbc71ed7cea742ffd2753f MD5 | raw file
Possible License(s): MIT, GPL-2.0, Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * @version 1.5 stable $Id: flexicontent.fields.php 1884 2014-04-13 10:38:52Z ggppdk $
  4. * @package Joomla
  5. * @subpackage FLEXIcontent
  6. * @copyright (C) 2009 Emmanuel Danan - www.vistamedia.fr
  7. * @license GNU/GPL v2
  8. *
  9. * FLEXIcontent is a derivative work of the excellent QuickFAQ component
  10. * @copyright (C) 2008 Christoph Lukes
  11. * see www.schlu.net for more information
  12. *
  13. * FLEXIcontent is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. defined( '_JEXEC' ) or die( 'Restricted access' );
  19. // Include com_content helper files, these are needed by some content plugins
  20. require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'route.php');
  21. require_once (JPATH_SITE.DS.'components'.DS.'com_content'.DS.'helpers'.DS.'query.php');
  22. //include constants file
  23. require_once (JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'defineconstants.php');
  24. class FlexicontentFields
  25. {
  26. /**
  27. * Function to render the field display variables for the given items
  28. *
  29. * @param int $item_id
  30. * @return string : the HTML of the item view, also the CSS / JS file would have been loaded
  31. * @since 1.5
  32. */
  33. static function renderFields( $item_per_field=true, $item_ids=array(), $field_names=array(), $view=FLEXI_ITEMVIEW, $methods=array(), $cfparams=array() )
  34. {
  35. require_once (JPATH_ADMINISTRATOR.DS.'components/com_flexicontent/defineconstants.php');
  36. JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_flexicontent'.DS.'tables');
  37. //require_once("components/com_flexicontent/classes/flexicontent.fields.php");
  38. require_once("components/com_flexicontent/classes/flexicontent.helper.php");
  39. // ***************************
  40. // Check if no data were given
  41. // ***************************
  42. if ( empty($item_ids) || empty($field_names) ) return false;
  43. // Get item data, needed for rendering fields
  44. $db = JFactory::getDBO();
  45. $item_ids = array_unique(array_map('intval', $item_ids));
  46. $item_ids_list = implode("," , $item_ids) ;
  47. $query = 'SELECT i.id, i.*, ie.*, '
  48. . ' CASE WHEN CHAR_LENGTH(i.alias) THEN CONCAT_WS(\':\', i.id, i.alias) ELSE i.id END as slug,'
  49. . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END as categoryslug'
  50. . ' FROM #__content AS i'
  51. . ' LEFT JOIN #__flexicontent_items_ext AS ie ON ie.item_id = i.id'
  52. . ' LEFT JOIN #__categories AS c ON c.id = i.catid'
  53. . ' WHERE i.id IN ('. $item_ids_list .')'
  54. . ' GROUP BY i.id';
  55. $db->setQuery($query);
  56. $items = $db->loadObjectList();
  57. if ($db->getErrorNum()) JFactory::getApplication()->enqueueMessage(__FUNCTION__.'(): SQL QUERY ERROR:<br/>'.nl2br($db->getErrorMsg()),'error');
  58. if (!$items) return false;
  59. foreach ($items as $i => $item) $_item_id_map[$item->id] = & $items[$i];
  60. // **************
  61. // Get Field info
  62. // **************
  63. /*if ( $using_ids )
  64. {
  65. $field_ids = array_unique(array_map('intval', $_field_ids));
  66. $field_ids_list = implode("," , $field_ids) ;
  67. $field_where = ' WHERE f.id IN ('. $field_ids_list .')';
  68. }
  69. else {
  70. foreach ($field_names as $i => $field_name) {
  71. $field_names[$i] = preg_replace("/[\"'\\\]/u", "", $field_name);
  72. }
  73. $field_names_list = "'". implode("','" , $field_names) ."'";
  74. $field_where = 'f.name IN ('. $field_names_list .')';
  75. }
  76. $query = 'SELECT f.*'
  77. . ' FROM #__flexicontent_fields AS f'
  78. . ' WHERE 1 '.$field_where
  79. ;
  80. $db->setQuery($query);
  81. $fields = $db->loadObjectList('id');
  82. if (!$fields) return false;*/
  83. // *********************************
  84. // Render Display Variable of Fields
  85. // *********************************
  86. // Get Field values at once to minimized performance impact, null 'params' mean only retrieve values
  87. /*if ($item_per_field && count($items)>1)
  88. // we have at least 2 item and item is per field, this will retrieve all values with single SQL query
  89. FlexicontentFields::getFields($items, $view, $params = null, $aid = false);*/
  90. $return = array();
  91. foreach ($field_names as $i => $field_name)
  92. {
  93. $method = isset( $methods[$i] ) ? $methods[$i] : 'display';
  94. if ( $item_per_field )
  95. {
  96. if ( !isset( $_item_id_map[ $item_ids[$i] ] ) ) { echo "not found item: ".$item_ids[$i] ." <br/>"; continue;}
  97. // Render Display variable of Field for respective item
  98. $_item = & $_item_id_map[$item_ids[$i]];
  99. FlexicontentFields::getFieldDisplay($_item, $field_name, $values=null, $method, $view);
  100. // Add to return array
  101. $return[$_item->id][$field_name] = $_item->fields[$field_name]->$method;
  102. }
  103. else
  104. {
  105. // Render Display variable of Field for all items
  106. FlexicontentFields::getFieldDisplay($items, $field_name, $values=null, $method, $view);
  107. // Add to return array
  108. foreach ($items as $item) {
  109. $return[$item->id][$field_name] = $item->fields[$field_name]->display;
  110. }
  111. }
  112. }
  113. return $return;
  114. }
  115. /**
  116. * Method to bind fields to an items object
  117. *
  118. * @access private
  119. * @return object
  120. * @since 1.5
  121. */
  122. static function &getFields(&$_items, $view = FLEXI_ITEMVIEW, $params = null, $aid = false, $use_tmpl = true)
  123. {
  124. static $apply_cache = null;
  125. static $expired_cleaned = false;
  126. if (!$_items) return $_items;
  127. if (!is_array($_items)) $items = array( & $_items ); else $items = & $_items ;
  128. $user = JFactory::getUser();
  129. $mainframe = JFactory::getApplication();
  130. $cparams = $mainframe->getParams('com_flexicontent');
  131. $print_logging_info = $cparams->get('print_logging_info');
  132. if ( $print_logging_info ) {
  133. global $fc_run_times;
  134. $start_microtime = microtime(true);
  135. }
  136. // Calculate access if it was not providden
  137. if (FLEXI_J16GE) {
  138. $aid = is_array($aid) ? $aid : $user->getAuthorisedViewLevels();
  139. } else {
  140. $aid = $aid!==false ? (int) $aid : (int) $user->get('aid');
  141. }
  142. // Apply cache to public (unlogged) users only
  143. /*if ($apply_cache === null) {
  144. if (FLEXI_J16GE) {
  145. $apply_cache = max($aid) <= 1; // ACCESS LEVEL : PUBLIC 1 , REGISTERED 2
  146. } else {
  147. //$apply_cache = FLEXI_ACCESS ? ($user->gmid == '0' || $user->gmid == '0,1') : ($user->gid <= 18); // This is for registered too
  148. $apply_cache = $aid <= 0; // ACCESS LEVEL : PUBLIC 0 , REGISTERED 1
  149. }
  150. $apply_cache = $apply_cache && FLEXI_CACHE;
  151. }
  152. if ($apply_cache) {
  153. $itemcache = JFactory::getCache('com_flexicontent_items'); // Get Joomla Cache of '...items' Caching Group
  154. $itemcache->setCaching(1); // Force cache ON
  155. $itemcache->setLifeTime(FLEXI_CACHE_TIME); // Set expiration to default e.g. one hour
  156. $filtercache = JFactory::getCache('com_flexicontent_filters'); // Get Joomla Cache of '...filters' Caching Group
  157. $filtercache->setCaching(1); // Force cache ON
  158. $filtercache->setLifeTime(FLEXI_CACHE_TIME); // Set expiration to default e.g. one hour
  159. // Auto-clean expired item & filters cache, only done here once
  160. if (FLEXI_GC && !$expired_cleaned) {
  161. $itemcache->gc();
  162. $filtercache->gc();
  163. $expired_cleaned = true;
  164. }
  165. }*/
  166. // @TODO : move to the constructor
  167. // This is optimized regarding the use of SINGLE QUERY to retrieve the core item data
  168. $vars['tags'] = FlexicontentFields::_getTags($items);
  169. $vars['cats'] = FlexicontentFields::_getCategories($items);
  170. $vars['favourites'] = FlexicontentFields::_getFavourites($items);
  171. $vars['favoured'] = FlexicontentFields::_getFavoured($items);
  172. $vars['authors'] = FlexicontentFields::_getAuthors($items);
  173. $vars['modifiers'] = FlexicontentFields::_getModifiers($items);
  174. $vars['typenames'] = FlexicontentFields::_getTypenames($items);
  175. $vars['votes'] = FlexicontentFields::_getVotes($items);
  176. $vars['custom'] = FlexicontentFields::_getCustomValues($items);
  177. FlexicontentFields::getItemFields($items, $vars, $view, $aid);
  178. if ( $print_logging_info ) @$fc_run_times['field_values_params'] += round(1000000 * 10 * (microtime(true) - $start_microtime)) / 10;
  179. if ($params) // NULL/empty parameters mean only retrieve field values
  180. {
  181. // CHECK if 'always_create_fields_display' enabled and create the display for all item's fields
  182. // *** This should be normally set to ZERO (never), to avoid a serious performance penalty !!!
  183. foreach ($items as $i => $item)
  184. {
  185. $always_create_fields_display = $cparams->get('always_create_fields_display',0);
  186. $flexiview = JRequest::getVar('view', false);
  187. // 0: never, 1: always, 2: only in item view
  188. if ($always_create_fields_display==1 || ($always_create_fields_display==2 && $flexiview==FLEXI_ITEMVIEW) ) {
  189. if ($items[$i]->fields)
  190. {
  191. foreach ($items[$i]->fields as $field)
  192. {
  193. $values = isset($items[$i]->fieldvalues[$field->id]) ? $items[$i]->fieldvalues[$field->id] : array();
  194. $field = FlexicontentFields::renderField($items[$i], $field, $values, $method='display', $view);
  195. }
  196. }
  197. }
  198. }
  199. // Render field positions
  200. $items = FlexicontentFields::renderPositions($items, $view, $params, $use_tmpl);
  201. }
  202. return $items;
  203. }
  204. /**
  205. * Method to fetch the fields from an item object
  206. *
  207. * @access private
  208. * @return object
  209. * @since 1.5
  210. */
  211. static function & getItemFields(&$items, &$vars, $view=FLEXI_ITEMVIEW, $aid=false)
  212. {
  213. if ( empty($items) ) return;
  214. static $type_fields = array();
  215. $mainframe = JFactory::getApplication();
  216. $dispatcher = JDispatcher::getInstance();
  217. $db = JFactory::getDBO();
  218. $user = JFactory::getUser();
  219. jimport('joomla.html.parameter');
  220. foreach ($items as $i => $item)
  221. {
  222. if (!FLEXI_J16GE && $item->sectionid != FLEXI_SECTION) continue;
  223. $item_id = $item->id;
  224. $cats = isset($vars['cats'][$item_id]) ? $vars['cats'][$item_id] : array();
  225. $tags = isset($vars['tags'][$item_id]) ? $vars['tags'][$item_id] : array();
  226. $favourites= isset($vars['favourites'][$item_id])? $vars['favourites'][$item_id]->favs : 0;
  227. $favoured = isset($vars['favoured'][$item_id]) ? $vars['favoured'][$item_id]->fav : 0;
  228. $author = isset($vars['authors'][$item_id]) ? $vars['authors'][$item_id] : '';
  229. $modifier = isset($vars['modifiers'][$item_id]) ? $vars['modifiers'][$item_id] : '';
  230. $typename = isset($vars['typenames'][$item_id]) ? $vars['typenames'][$item_id] : '';
  231. $vote = isset($vars['votes'][$item_id]) ? $vars['votes'][$item_id] : '';
  232. $custom = isset($vars['custom'][$item_id]) ? $vars['custom'][$item_id] : array();
  233. // ONCE per Content Item Type
  234. if ( !isset($type_fields[$item->type_id]) )
  235. {
  236. if (FLEXI_J16GE) {
  237. $aid_arr = is_array($aid) ? $aid : $user->getAuthorisedViewLevels();
  238. $aid_list = implode(",", $aid);
  239. $andaccess = ' AND fi.access IN (0,'.$aid_list.')' ;
  240. $joinaccess = '';
  241. } else {
  242. $aid = $aid!==false ? (int) $aid : (int) $user->get('aid');
  243. $andaccess = FLEXI_ACCESS ? ' AND (gi.aro IN ( '.$user->gmid.' ) OR fi.access <= '. $aid . ')' : ' AND fi.access <= '.$aid ;
  244. $joinaccess = FLEXI_ACCESS ? ' LEFT JOIN #__flexiaccess_acl AS gi ON fi.id = gi.axo AND gi.aco = "read" AND gi.axosection = "field"' : '' ;
  245. }
  246. $query = 'SELECT fi.*'
  247. . ' FROM #__flexicontent_fields AS fi'
  248. . ' LEFT JOIN #__flexicontent_fields_type_relations AS ftrel ON ftrel.field_id = fi.id AND ftrel.type_id = '.$item->type_id
  249. . $joinaccess
  250. . ' WHERE fi.published = 1'
  251. . $andaccess
  252. . ' GROUP BY fi.id'
  253. . ' ORDER BY ftrel.ordering, fi.ordering, fi.name'
  254. ;
  255. $db->setQuery($query);
  256. $type_fields[$item->type_id] = $db->loadObjectList('name');
  257. }
  258. $item->fields = array();
  259. if ($type_fields[$item->type_id]) foreach($type_fields[$item->type_id] as $field_name => $field_data)
  260. $item->fields[$field_name] = clone($field_data);
  261. $item->fields = $item->fields ? $item->fields : array();
  262. if (!isset($item->parameters)) $item->parameters = FLEXI_J16GE ? new JRegistry($item->attribs) : new JParameter($item->attribs);
  263. $item->params = $item->parameters;
  264. $item->text = $item->introtext . chr(13).chr(13) . $item->fulltext;
  265. $item->tags = $tags;
  266. $item->cats = $cats;
  267. $item->favs = $favourites;
  268. $item->fav = $favoured;
  269. $item->creator = @$author->alias ? $author->alias : (@$author->name ? $author->name : '') ;
  270. $item->author = & $item->creator; // An alias ... of creator
  271. $item->modifier = @$modifier->name ? $modifier->name : $item->creator; // If never modified, set modifier to be the creator
  272. $item->modified = ($item->modified != $db->getNulldate()) ? $item->modified : $item->created; // If never modified, set modification date to be the creation date
  273. $item->cmail = @$author->email ? $author->email : '' ;
  274. $item->cuname = @$author->username ? $author->username : '' ;
  275. $item->mmail = @$modifier->email ? $modifier->email : $item->cmail;
  276. $item->muname = @$modifier->muname ? $modifier->muname : $item->cuname;
  277. $item->typename = @$typename->name ? $typename->name : JText::_('Article');
  278. $item->vote = @$vote ? $vote : '';
  279. // some aliases to much CORE field names
  280. $item->categories = & $item->cats;
  281. $item->favourites = & $item->favs;
  282. $item->document_type = & $item->typename;
  283. $item->voting = & $item->vote;
  284. // custom field values
  285. $item->fieldvalues = $custom;
  286. /*if ($item->fields) {
  287. // IMPORTANT the items model and possibly other will set item PROPERTY version_id to indicate loading an item version,
  288. // It is not the responisibility of this CODE to try to detect previewing of an item version, it is better left to the model
  289. $item->fieldvalues = FlexicontentFields::_getFieldsvalues($item->id, $item->fields, !empty($item->version_id) ? $item->version_id : 0);
  290. }*/
  291. }
  292. return $items;
  293. }
  294. /**
  295. * Method to render (display method) a field on demand and return the display
  296. *
  297. * @access public
  298. * @return object
  299. * @since 1.5.5
  300. */
  301. static function &getFieldDisplay(&$item_arr, $fieldname, $single_item_vals=null, $method='display', $view = FLEXI_ITEMVIEW)
  302. {
  303. // 1. Convert to array of items if not an array already
  304. if ( empty($item_arr) )
  305. return __FUNCTION__."(): empty item data given";
  306. else if ( !is_array($item_arr) )
  307. $items = array( & $item_arr );
  308. else
  309. $items = & $item_arr;
  310. // 2. Make sure that fields have been created for all given items
  311. $_items = array();
  312. foreach ($items as $i => $item) if (!isset($item->fields)) $_items[] = & $items[$i];
  313. if ( count($_items) ) FlexicontentFields::getFields($_items, $view);
  314. // 3. Check and create HTML display for the given field name
  315. $_return = array();
  316. foreach ($items as $item)
  317. {
  318. // Check if we have already created the display and skip current item
  319. if ( isset($item->onDemandFields[$fieldname]->{$method}) ) continue;
  320. // Find the field inside item
  321. foreach ($item->fields as $field) {
  322. if ( !empty($field->name) && $field->name==$fieldname ) break;
  323. }
  324. // Check for not found field, and skip it, this is either due to no access or wrong name ...
  325. $item->onDemandFields[$fieldname] = new stdClass();
  326. if ( empty($field->name) || $field->name!=$fieldname) {
  327. $item->onDemandFields[$fieldname]->label = '';
  328. $item->onDemandFields[$fieldname]->noaccess = true;
  329. $item->onDemandFields[$fieldname]->errormsg = 'field not assigned to this type of item or current user has no access';
  330. $item->onDemandFields[$fieldname]->{$method} = '';
  331. continue;
  332. }
  333. // Get field's values if they were custom values were not given
  334. if ( $single_item_vals!==null && count($items) == 1 ) {
  335. // $values is used only if rendering a single item
  336. $values = $single_item_vals;
  337. } else {
  338. $values = isset($item->fieldvalues[$field->id]) ? $item->fieldvalues[$field->id] : array();
  339. }
  340. // Set other field data like label and field itself !!!
  341. $item->onDemandFields[$fieldname]->label = $field->label;
  342. $item->onDemandFields[$fieldname]->noaccess = false;
  343. $item->onDemandFields[$fieldname]->field = & $field;
  344. // Render the (display) method of the field
  345. if (!isset($field->{$method})) $field = FlexicontentFields::renderField($item, $field, $values, $method, $view);
  346. if (!isset($field->{$method})) $field->{$method} = '';
  347. $item->onDemandFields[$fieldname]->{$method} = & $field->{$method};
  348. $_method_html[$item->id] = & $field->{$method};
  349. }
  350. // Return field(s) HTML (in case of multiple items this will be an array indexable by item ids
  351. if ( !is_array($item_arr) ) {
  352. $_method_html = @ $_method_html[$item_arr->id]; // Suppress field name not found ...
  353. }
  354. return $_method_html;
  355. }
  356. /**
  357. * Method to render a field
  358. *
  359. * @access public
  360. * @return object
  361. * @since 1.5
  362. */
  363. static function renderField(&$_items, &$_field, &$values, $method='display', $view=FLEXI_ITEMVIEW)
  364. {
  365. static $_trigger_plgs_ft = array();
  366. $flexiview = JRequest::getVar('view');
  367. // If $method (e.g. display method) is already created, then return the $field without recreating the $method
  368. if ( is_object($_field) && isset($_field->{$method}) ) return $_field;
  369. // Handle multi-item call
  370. if (!is_array($_items)) $items = array( & $_items ); else $items = & $_items ;
  371. // **********************************************************************************************
  372. // Create field parameters in an optimized way, and also apply Type Customization for CORE fields
  373. // **********************************************************************************************
  374. foreach($items as $item) {
  375. $field = is_object($_field) ? $_field : $item->fields[$_field];
  376. $field->item_id = (int)$item->id;
  377. $field->value = $values; // NOTE: currently ignored and overwritten by all CORE fields
  378. FlexicontentFields::loadFieldConfig($field, $item);
  379. }
  380. // ***************************************************************************************************
  381. // Create field HTML by calling the appropriate DISPLAY-CREATING field plugin method.
  382. // NOTE 1: We will not pass the 'values' method parameter to the display-creating field method,
  383. // instead we have set it above as the 'value' field property
  384. // NOTE 2: For CUSTOM fields the 'values' method parameter is prefered over the 'value' field property
  385. // For CORE field, both the above ('values' method parameter and 'value' field property) are
  386. // ignored and instead the other method parameters are used, along with the ITEM properties
  387. // ****************************************************************************************************
  388. // Log content plugin and other performance information
  389. $cparams = JComponentHelper::getParams('com_flexicontent');
  390. $print_logging_info = $cparams->get('print_logging_info');
  391. if ($print_logging_info) global $fc_run_times;
  392. if ($print_logging_info) $start_microtime = microtime(true);
  393. if ($field->iscore == 1) // CORE field
  394. {
  395. //$results = $dispatcher->trigger('onDisplayCoreFieldValue', array( &$field, $item, &$item->parameters, $item->tags, $item->cats, $item->favs, $item->fav, $item->vote ));
  396. //FLEXIUtilities::call_FC_Field_Func('core', 'onDisplayCoreFieldValue', array( &$_field, & $items, &$item->parameters, $item->tags, $item->cats, $item->favs, $item->fav, $item->vote, null, $method ) );
  397. $items_params = null;
  398. FLEXIUtilities::call_FC_Field_Func('core', 'onDisplayCoreFieldValue', array( &$_field, & $items, & $items_params, false, false, false, false, false, null, $method ) );
  399. }
  400. else // NON CORE field
  401. {
  402. //$results = $dispatcher->trigger('onDisplayFieldValue', array( &$field, $item ));
  403. FLEXIUtilities::call_FC_Field_Func($field->field_type, 'onDisplayFieldValue', array(&$field, $_items, null, $method) );
  404. }
  405. if ($print_logging_info) {
  406. $field_render_time = round(1000000 * 10 * (microtime(true) - $start_microtime)) / 10;
  407. if ( isset($fc_run_times['render_subfields'][$item->id."_".$field->id]) ) {
  408. $field_render_time = $field_render_time - $fc_run_times['render_subfields'][$item->id."_".$field->id];
  409. @$fc_run_times['render_subfields'][$field->field_type] += $fc_run_times['render_subfields'][$item->id."_".$field->id];
  410. unset($fc_run_times['render_subfields'][$item->id."_".$field->id]);
  411. }
  412. @$fc_run_times['render_field'][$field->field_type] += $field_render_time;
  413. }
  414. // *****************************************
  415. // Trigger content plugins on the field text
  416. // *****************************************
  417. if ( !isset($_trigger_plgs_ft[$field->name]) ) {
  418. $_t = $field->parameters->get('trigger_onprepare_content', 0);
  419. if ($flexiview=='category') $_t = $_t && $field->parameters->get('trigger_plgs_incatview', 1);
  420. $_trigger_plgs_ft[$field->name] = $_t;
  421. }
  422. if ( $_trigger_plgs_ft[$field->name] ) {
  423. if ($print_logging_info) $start_microtime = microtime(true);
  424. FlexicontentFields::triggerContentPlugins($field, $item, $method, $view);
  425. if ( $print_logging_info ) @$fc_run_times['content_plg'] += round(1000000 * 10 * (microtime(true) - $start_microtime)) / 10;
  426. }
  427. return $field;
  428. }
  429. /**
  430. * Method to selectively trigger content plugins for the text of the specified field
  431. *
  432. * @access public
  433. * @return object
  434. * @since 1.5
  435. */
  436. static function triggerContentPlugins(&$field, &$item, $method, $view=FLEXI_ITEMVIEW)
  437. {
  438. $debug = false;
  439. static $_plgs_loaded = array();
  440. static $_fields_plgs = array();
  441. static $_initialize = false;
  442. static $_view, $_option, $limitstart;
  443. static $dispatcher, $fcdispatcher;
  444. //$flexiparams = JComponentHelper::getParams('com_flexicontent');
  445. //$print_logging_info = $flexiparams->get('print_logging_info');
  446. // Log content plugin and other performance information
  447. //if ($print_logging_info) global $fc_run_times;
  448. if (!$_initialize) {
  449. // some request and other variables
  450. $_view = JRequest::getVar('view');
  451. $_option = JRequest::getVar('option');
  452. $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
  453. $_initialize = true;
  454. // ***********************************************************************
  455. // We use a custom Dispatcher to allow selective Content Plugin triggering
  456. // ***********************************************************************
  457. require_once (JPATH_SITE.DS.'components'.DS.'com_flexicontent'.DS.'helpers'.DS.'dispatcher.php');
  458. $dispatcher = JDispatcher::getInstance();
  459. $fcdispatcher = FCDispatcher::getInstance_FC($debug);
  460. }
  461. if ($debug) echo "<br><br>Executing plugins for <b>".$field->name."</b>:<br>";
  462. if ( !@$_fields_plgs[$field->name] )
  463. {
  464. // Make sure the necessary plugin are already loaded, but do not try to load them again since this will harm performance
  465. if (!$field->parameters->get('plugins'))
  466. {
  467. $_plgs = null;
  468. if (!@$_plgs_loaded['__ALL__']) {
  469. JPluginHelper::importPlugin('content', $plugin = null, $autocreate = true, $dispatcher);
  470. $_plgs_loaded['__ALL__'] = 1;
  471. }
  472. }
  473. else
  474. {
  475. $_plgs = $field->parameters->get('plugins');
  476. $_plgs = $_plgs ? $_plgs : array();
  477. $_plgs = is_array($_plgs) ? $_plgs : explode('|', $_plgs); // compatibility because old versions did not JSON encode the parameters
  478. if (!@$_plgs_loaded['__ALL__']) foreach ($_plgs as $_plg) if (!@$_plgs_loaded[$_plg]) {
  479. JPluginHelper::importPlugin('content', $_plg, $autocreate = true, $dispatcher);
  480. $_plgs_loaded[$_plg] = 1;
  481. }
  482. }
  483. $_fields_plgs[$field->name] = $_plgs;
  484. }
  485. $plg_arr = $_fields_plgs[$field->name];
  486. // Suppress some plugins from triggering for compatibility reasons, e.g.
  487. // (a) jcomments, jom_comment_bot plugins, because we will get comments HTML manually inside the template files
  488. $suppress_arr = array('jcomments', 'jom_comment_bot');
  489. FLEXIUtilities::suppressPlugins($suppress_arr, 'suppress' );
  490. // Initialize field for plugin triggering
  491. $field->text = isset($field->{$method}) ? $field->{$method} : '';
  492. $field->title = $item->title;
  493. $field->slug = $item->slug;
  494. $field->sectionid = !FLEXI_J16GE ? $item->sectionid : false;
  495. $field->catid = $item->catid;
  496. $field->catslug = @$item->categoryslug;
  497. $field->fieldid = $field->id;
  498. $field->id = $item->id;
  499. $field->state = $item->state;
  500. $field->type_id = $item->type_id;
  501. // CASE: FLEXIcontent item view:
  502. // Set triggering 'context' to 'com_content.article', (and also set the 'view' request variable)
  503. if ($view == FLEXI_ITEMVIEW) {
  504. JRequest::setVar('view', 'article');
  505. $context = 'com_content.article';
  506. }
  507. // ALL OTHER CASES: (FLEXIcontent category, FLEXIcontent module, etc),
  508. // Set triggering 'context' to 'com_content.article', (and also set the 'view' request variable)
  509. else {
  510. JRequest::setVar('view', 'category');
  511. $context = 'com_content.category';
  512. }
  513. // Set the 'option' to 'com_content' but set a flag 'isflexicontent' to indicate triggering from inside FLEXIcontent ... code
  514. JRequest::setVar('option', 'com_content');
  515. JRequest::setVar("isflexicontent", "yes");
  516. // Trigger content plugins on field's HTML display, as if they were a "joomla article"
  517. if (FLEXI_J16GE) $results = $fcdispatcher->trigger('onContentPrepare', array ($context, &$field, &$item->parameters, $limitstart), $plg_arr);
  518. else $results = $fcdispatcher->trigger('onPrepareContent', array (&$field, &$item->parameters, $limitstart), false, $plg_arr);
  519. // Restore 'view' and 'option' request variables
  520. JRequest::setVar('view', $_view);
  521. JRequest::setVar('option', $_option);
  522. $field->id = $field->fieldid;
  523. $field->{$method} = $field->text;
  524. // Restore suppressed plugins
  525. FLEXIUtilities::suppressPlugins( $suppress_arr,'restore' );
  526. }
  527. /**
  528. * Method to get the fields in their positions
  529. *
  530. * @access private
  531. * @return object
  532. * @since 1.5
  533. */
  534. static function &renderPositions(&$items, $view = FLEXI_ITEMVIEW, $params = null, $use_tmpl = true)
  535. {
  536. if (!$items) return;
  537. if (!$params) return $items;
  538. if ($view == 'category') $layout = 'clayout';
  539. if ($view == FLEXI_ITEMVIEW) $layout = 'ilayout';
  540. // field's source code, can use this JRequest variable, to detect who rendered the fields (e.g. they can detect rendering from 'module')
  541. JRequest::setVar("flexi_callview", $view);
  542. if ( $use_tmpl && ($view == 'category' || $view == FLEXI_ITEMVIEW) ) {
  543. $fbypos = flexicontent_tmpl::getFieldsByPositions($params->get($layout, 'default'), $view);
  544. } else { // $view == 'module', or other
  545. // Create a fake template position, for fields defined via parameters
  546. $fbypos[0] = new stdClass();
  547. $fbypos[0]->fields = explode(',', $params->get('fields'));
  548. $fbypos[0]->methods = explode(',', $params->get('methods'));
  549. $fbypos[0]->position = $view;
  550. }
  551. $always_create_fields_display = $params->get('always_create_fields_display',0);
  552. foreach ($items as $item)
  553. {
  554. if ($always_create_fields_display != 3) { // value 3 means never create for any view (blog template incompatible)
  555. // 'description' item field is implicitly used by category layout of some templates (blog), render it
  556. $custom_values = false;
  557. if ($view == 'category') {
  558. if (isset($item->fields['text'])) {
  559. $field = $item->fields['text'];
  560. $field = FlexicontentFields::renderField($item, $field, $custom_values, $method='display', $view);
  561. }
  562. }
  563. // 'core' item fields are IMPLICITLY used by some item layout of some templates (blog), render them
  564. else if ($view == FLEXI_ITEMVIEW) {
  565. foreach ($item->fields as $field) {
  566. if ($field->iscore) {
  567. $field = FlexicontentFields::renderField($item, $field, $custom_values, $method='display', $view);
  568. }
  569. }
  570. }
  571. }
  572. }
  573. // *** RENDER fields on DEMAND, (if present in template positions)
  574. foreach ($fbypos as $pos) {
  575. // RENDER fields if they are present in a template position (or in a dummy template position ... e.g. when called by module)
  576. foreach ($pos->fields as $c => $f) {
  577. // Render field (if already rendered above, the function will return result immediately)
  578. $method = (isset($pos->methods[$c]) && $pos->methods[$c]) ? $pos->methods[$c] : 'display';
  579. // Check that field with given name: $f exists, (this will handle deleted fields, that still exist in a template position)
  580. $item = reset($items);
  581. if (!isset($item->fields[$f])) continue;
  582. $field = $item->fields[$f];
  583. if ($field->iscore) {
  584. $values = null;
  585. FlexicontentFields::renderField($items, $f, $values, $method, $view);
  586. }
  587. else foreach ($items as $item) {
  588. $field = $item->fields[$f];
  589. // Set field values, currently, this exists for CUSTOM fields only, OR versioned CORE/CUSTOM fields too ...
  590. $values = isset($item->fieldvalues[$field->id]) ? $item->fieldvalues[$field->id] : array();
  591. $field = FlexicontentFields::renderField($item, $field, $values, $method, $view);
  592. }
  593. foreach ($items as $item) {
  594. $field = $item->fields[$f];
  595. // Set template position field data
  596. if (isset($field->display) && strlen($field->display))
  597. {
  598. if (!isset($item->positions[$pos->position]))
  599. $item->positions[$pos->position] = new stdClass();
  600. $item->positions[$pos->position]->{$f} = new stdClass();
  601. $item->positions[$pos->position]->{$f}->id = $field->id;
  602. $item->positions[$pos->position]->{$f}->id = $field->id;
  603. $item->positions[$pos->position]->{$f}->name = $field->name;
  604. $item->positions[$pos->position]->{$f}->label = $field->parameters->get('display_label') ? $field->label : '';
  605. $item->positions[$pos->position]->{$f}->display = $field->display;
  606. }
  607. }
  608. }
  609. }
  610. return $items;
  611. }
  612. /**
  613. * Method to get the values of the fields for an item
  614. *
  615. * @access private
  616. * @return object
  617. * @since 1.5
  618. */
  619. static function _getFieldsvalues($item, $fields, $version=0)
  620. {
  621. $db = JFactory::getDBO();
  622. $query = 'SELECT field_id, value'
  623. .( $version ? ' FROM #__flexicontent_items_versions':' FROM #__flexicontent_fields_item_relations')
  624. .' WHERE item_id = ' . (int)$item
  625. .( $version ? ' AND version=' . (int)$version:'')
  626. .' AND value > "" '
  627. .' ORDER BY field_id, valueorder'
  628. ;
  629. $db->setQuery($query);
  630. $values = $db->loadObjectList();
  631. $fieldvalues = array();
  632. foreach ($fields as $f) {
  633. foreach ($values as $v) {
  634. if ((int)$f->id == (int)$v->field_id) {
  635. $fieldvalues[$f->id][] = $v->value;
  636. }
  637. }
  638. }
  639. return $fieldvalues;
  640. }
  641. /**
  642. * Method to get the values of the fields for multiple items at once
  643. *
  644. * @access private
  645. * @return object
  646. * @since 1.5
  647. */
  648. static function _getCustomValues($items)
  649. {
  650. $versioned_item = count($items)==1 && !empty($items[0]->version_id) && !empty($items[0]->created_by);
  651. $version = $versioned_item ? $items[0]->version_id : 0;
  652. $item_ids = array();
  653. foreach ($items as $item) $item_ids[] = $item->id;
  654. $db = JFactory::getDBO();
  655. $query = 'SELECT field_id, value, item_id'
  656. .( $version ? ' FROM #__flexicontent_items_versions':' FROM #__flexicontent_fields_item_relations')
  657. .' WHERE item_id IN (' . implode(',', $item_ids) .')'
  658. .( $version ? ' AND version=' . (int)$version:'')
  659. .' AND value > "" '
  660. .' ORDER BY item_id, field_id, valueorder' // first 2 parts are not needed ...
  661. ;
  662. $db->setQuery($query);
  663. $values = $db->loadObjectList();
  664. $fieldvalues = array();
  665. foreach ($values as $v) {
  666. $fieldvalues[$v->item_id][$v->field_id][] = $v->value;
  667. }
  668. return $fieldvalues;
  669. }
  670. /**
  671. * Method to get the tags
  672. *
  673. * @access private
  674. * @return object
  675. * @since 1.5
  676. */
  677. static function _getTags($items)
  678. {
  679. // This is fix for versioned field of creator in items view when previewing
  680. $versioned_item = count($items)==1 && !empty($items[0]->version_id) && !empty($items[0]->tags);
  681. $db = JFactory::getDBO();
  682. if ($versioned_item) {
  683. if (!count($items[0]->tags)) return array();
  684. $tids = $items[0]->tags;
  685. $query = 'SELECT DISTINCT t.id, t.name, ' . $items[0]->id .' as itemid, '
  686. . ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(\':\', t.id, t.alias) ELSE t.id END as slug'
  687. . ' FROM #__flexicontent_tags AS t'
  688. . " WHERE t.id IN ('" . implode("','", $tids) . "')"
  689. . ' AND t.published = 1'
  690. . ' ORDER BY t.name'
  691. ;
  692. } else {
  693. $cids = array();
  694. foreach ($items as $item) { array_push($cids, $item->id); }
  695. $query = 'SELECT DISTINCT t.id, t.name, i.itemid,'
  696. . ' CASE WHEN CHAR_LENGTH(t.alias) THEN CONCAT_WS(\':\', t.id, t.alias) ELSE t.id END as slug'
  697. . ' FROM #__flexicontent_tags AS t'
  698. . ' JOIN #__flexicontent_tags_item_relations AS i ON i.tid = t.id'
  699. . " WHERE i.itemid IN ('" . implode("','", $cids) . "')"
  700. . ' AND t.published = 1'
  701. . ' ORDER BY t.name'
  702. ;
  703. }
  704. $db->setQuery( $query );
  705. $tags = $db->loadObjectList();
  706. // improve performance by doing a single pass of tags to aggregate them per item
  707. $taglists = array();
  708. foreach ($tags as $tag) {
  709. $taglists[$tag->itemid][] = $tag;
  710. }
  711. return $taglists;
  712. }
  713. /**
  714. * Method to get the categories
  715. *
  716. * @access private
  717. * @return object
  718. * @since 1.5
  719. */
  720. static function _getCategories($items)
  721. {
  722. // This is fix for versioned field of creator in items view when previewing
  723. $versioned_item = count($items)==1 && !empty($items[0]->version_id) && !empty($items[0]->categories);
  724. $db = JFactory::getDBO();
  725. if ($versioned_item) {
  726. $catids = $items[0]->categories;
  727. $query = 'SELECT DISTINCT c.id, c.title, ' . $items[0]->id .' as itemid, '
  728. . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END as slug'
  729. . ' FROM #__categories AS c'
  730. . " WHERE c.id IN ('" . implode("','", $catids) . "')"
  731. ;
  732. } else {
  733. $cids = array();
  734. foreach ($items as $item) { array_push($cids, $item->id); }
  735. $query = 'SELECT DISTINCT c.id, c.title, rel.itemid,'
  736. . ' CASE WHEN CHAR_LENGTH(c.alias) THEN CONCAT_WS(\':\', c.id, c.alias) ELSE c.id END as slug'
  737. . ' FROM #__categories AS c'
  738. . ' JOIN #__flexicontent_cats_item_relations AS rel ON rel.catid = c.id'
  739. . " WHERE rel.itemid IN ('" . implode("','", $cids) . "')"
  740. ;
  741. }
  742. $db->setQuery( $query );
  743. $cats = $db->loadObjectList();
  744. // improve performance by doing a single pass of cats to aggregate them per item
  745. $catlists = array();
  746. foreach ($cats as $cat) {
  747. $catlists[$cat->itemid][] = $cat;
  748. }
  749. return $catlists;
  750. }
  751. /**
  752. * Method to get the nr of favourites
  753. *
  754. * @access private
  755. * @return object
  756. * @since 1.5
  757. */
  758. static function _getFavourites($items)
  759. {
  760. $db = JFactory::getDBO();
  761. $cids = array();
  762. foreach ($items as $item) { array_push($cids, $item->id); }
  763. $query = 'SELECT itemid, COUNT(id) AS favs FROM #__flexicontent_favourites'
  764. . " WHERE itemid IN ('" . implode("','", $cids) . "')"
  765. . ' GROUP BY itemid'
  766. ;
  767. $db->setQuery($query);
  768. $favs = $db->loadObjectList('itemid');
  769. return $favs;
  770. }
  771. /**
  772. * Method to get the favourites of an user
  773. *
  774. * @access private
  775. * @return object
  776. * @since 1.5
  777. */
  778. static function _getFavoured($items)
  779. {
  780. $db = JFactory::getDBO();
  781. $cids = array();
  782. foreach ($items as $item) { array_push($cids, $item->id); }
  783. $user = JFactory::getUser();
  784. $query = 'SELECT itemid, COUNT(id) AS fav FROM #__flexicontent_favourites'
  785. . " WHERE itemid IN ('" . implode("','", $cids) . "')"
  786. . " AND userid = '" . ((int)$user->id) ."'"
  787. . ' GROUP BY itemid'
  788. ;
  789. $db->setQuery($query);
  790. $fav = $db->loadObjectList('itemid');
  791. return $fav;
  792. }
  793. /**
  794. * Method to get the modifiers of the items
  795. *
  796. * @access private
  797. * @return object
  798. * @since 1.5
  799. */
  800. static function _getModifiers($items)
  801. {
  802. // This is fix for versioned field of modifier in items view when previewing
  803. $versioned_item = count($items)==1 && !empty($items[0]->version_id) && !empty($items[0]->modified_by);
  804. $db = JFactory::getDBO();
  805. $cids = array();
  806. foreach ($items as $item) { array_push($cids, $item->id); }
  807. $query = 'SELECT i.id, u.name, u.username, u.email FROM #__content AS i'
  808. . ' LEFT JOIN #__users AS u ON ' . ( $versioned_item ? 'u.id = '.$items[0]->modified_by : 'u.id = i.modified_by' )
  809. . " WHERE i.id IN ('" . implode("','", $cids) . "')"
  810. ;
  811. $db->setQuery($query);
  812. $modifiers = $db->loadObjectList('id');
  813. return $modifiers;
  814. }
  815. /**
  816. * Method to get the authors of the items
  817. *
  818. * @access private
  819. * @return object
  820. * @since 1.5
  821. */
  822. static function _getAuthors($items)
  823. {
  824. // This is fix for versioned field of creator in items view when previewing
  825. $versioned_item = count($items)==1 && !empty($items[0]->version_id) && !empty($items[0]->created_by);
  826. $db = JFactory::getDBO();
  827. $cids = array();
  828. foreach ($items as $item) { array_push($cids, $item->id); }
  829. $query = 'SELECT i.id, u.name, i.created_by_alias as alias, u.username, u.email FROM #__content AS i'
  830. . ' LEFT JOIN #__users AS u ON ' . ( $versioned_item ? 'u.id = '.$items[0]->created_by : 'u.id = i.created_by' )
  831. . " WHERE i.id IN ('" . implode("','", $cids) . "')"
  832. ;
  833. $db->setQuery($query);
  834. $authors = $db->loadObjectList('id');
  835. return $authors;
  836. }
  837. /**
  838. * Method to get the types names of the items
  839. *
  840. * @access private
  841. * @return object
  842. * @since 1.5
  843. */
  844. static function _getTypenames($items)
  845. {
  846. $db = JFactory::getDBO();
  847. $type_ids = array();
  848. foreach ($items as $item) { $type_ids[$item->type_id]=1; }
  849. $type_ids = array_keys($type_ids);
  850. $query = 'SELECT id, name FROM #__flexicontent_types'
  851. . " WHERE id IN ('" . implode("','", $type_ids) . "')"
  852. ;
  853. $db->setQuery($query);
  854. $types = $db->loadObjectList('id');
  855. $typenames = array();
  856. foreach ($items as $item) {
  857. $typenames[$item->id] = new stdClass();
  858. $typenames[$item->id]->name = $types[$item->type_id]->name;
  859. }
  860. return $typenames;
  861. }
  862. /**
  863. * Method to get the votes of the items
  864. *
  865. * @access private
  866. * @return object
  867. * @since 1.5
  868. */
  869. static function _getVotes($items)
  870. {
  871. $db = JFactory::getDBO();
  872. $cids = array();
  873. foreach ($items as $item) { array_push($cids, $item->id); }
  874. $query = 'SELECT * FROM #__content_rating'
  875. . " WHERE content_id IN ('" . implode("','", $cids) . "')"
  876. ;
  877. $db->setQuery($query);
  878. $votes = $db->loadObjectList('content_id');
  879. $query = 'SELECT *, field_id as extra_id FROM #__flexicontent_items_extravote'
  880. . " WHERE content_id IN ('" . implode("','", $cids) . "')"
  881. ;
  882. $db->setQuery($query);
  883. $extra_votes= $db->loadObjectList();
  884. // Assign each item 's extra votes to the item's votes as member variable "extra"
  885. foreach ($extra_votes as $extra_vote ) {
  886. $votes[$extra_vote->content_id]->extra[$extra_vote->extra_id] = $extra_vote;
  887. }
  888. return $votes;
  889. }
  890. // ***********************************************************
  891. // Methods for creating field configuration in an OPTMIZED way
  892. // ***********************************************************
  893. // Method to create field parameters in an optimized way, and also apply Type Customization for CORE fields
  894. static function loadFieldConfig(&$field, &$item, $name='', $field_type='', $label='', $desc='', $iscore=1) {
  895. $db = JFactory::getDBO();
  896. static $tparams = array();
  897. static $tinfo = array();
  898. static $fdata = array();
  899. static $no_typeparams = null;
  900. if ($no_typeparams) $no_typeparams = FLEXI_J16GE ? new JRegistry() : new JParameter("");
  901. static $is_form=null;
  902. if ($is_form===null) $is_form = JRequest::getVar('task')=='edit' && JRequest::getVar('option')=='com_flexicontent';
  903. // Create basic field data if no field given
  904. if (!empty($name)) {
  905. $field->iscore = $iscore; $field->name = $name; $field->field_type = $field_type; $field->label = $label; $field->description = $desc; $field->attribs = '';
  906. }
  907. // Get Content Type parameters if not already retrieved
  908. $type_id = @$item->type_id;
  909. if ($type_id && ( !isset($tinfo[$type_id]) || !isset($tparams[$type_id]) ) )
  910. {
  911. $tinfo[$type_id] = $tparams[$type_id] = null;
  912. FlexicontentFields::_getTypeToCoreFieldParams ($type_id, $tinfo[$type_id], $tparams[$type_id]);
  913. }
  914. // Set Content Type parameters otherwise set empty defaults (e.g. new item form with not typeid set)
  915. $type_data_exist = $type_id && $tinfo[$type_id] && $tparams[$type_id] ;
  916. $typename = $type_data_exist ? $tinfo[$type_id]['typename'] : '';
  917. $typealias = $type_data_exist ? $tinfo[$type_id]['typealias'] : '';
  918. $tindex = $type_data_exist ? $typename.'_'.$typealias : 'no_type';
  919. if ($type_data_exist) $typeparams = & $tparams[$type_id]; else $typeparams = & $no_typeparams;
  920. // Create the (CREATED ONCE per field) SHARED object that will contain: (a) label, (b) description, (c) all (merged) field parameters
  921. // Create parameters once per custom field OR once per pair of: Core FIELD type - Item CONTENT type
  922. if ( !isset($fdata[$tindex][$field->name]) ) {
  923. if ( !$field->iscore || !$type_id ) {
  924. // CUSTOM field or CORE field with no type
  925. $fdata[$tindex][$field->name] = new stdClass();
  926. $fdata[$tindex][$field->name]->parameters = FLEXI_J16GE ? new JRegistry($field->attribs) : new JParameter($field->attribs);
  927. } else {
  928. $pn_prefix = $field->field_type!='maintext' ? $field->name : $field->field_type;
  929. // Initialize an empty object, and create parameters object of the field
  930. $fdata[$tindex][$field->name] = new stdClass();
  931. $fdata[$tindex][$field->name]->parameters = FLEXI_J16GE ? new JRegistry($field->attribs) : new JParameter($field->attribs);
  932. // SET a type specific label, description for the current CORE field (according to current language)
  933. $field_label_type = $tparams[$type_id]->get($pn_prefix.'_label', '');
  934. $field_desc_type = $tparams[$type_id]->get($pn_prefix.($is_form ? '_desc' : '_viewdesc'), '');
  935. FlexicontentFields::_getLangSpecificValue ($type_id, $field_label_type, 'label', $fdata[$tindex][$field->name]);
  936. FlexicontentFields::_getLangSpecificValue ($type_id, $field_desc_type, 'description', $fdata[$tindex][$field->name]);
  937. // Override field parameters with Type specific Parameters
  938. if ( isset($tinfo[$type_id]['params'][$pn_prefix]) ) {
  939. foreach ($tinfo[$type_id]['params'][$pn_prefix] as $param_name => $param_value) {
  940. $fdata[$tindex][$field->name]->parameters->set( $param_name, $param_value) ;
  941. }
  942. }
  943. // SPECIAL CASE: check if it exists a FAKE (custom) field that customizes CORE field per Content Type
  944. $query = "SELECT attribs, published FROM #__flexicontent_fields WHERE name=".$db->Quote($field->name."_".$typealias);
  945. $db->setQuery($query); //echo $query;
  946. $data = $db->loadObject(); //print_r($data);
  947. if (@$data->published) JFactory::getApplication()->enqueueMessage(__FUNCTION__."(): Please unpublish plugin with name: ".$field->name."_".$typealias." it is used for customizing a core field",'error');
  948. // Finally merge custom field parameters with the type specific parameters ones
  949. if ($data) {
  950. $ts_params = FLEXI_J16GE ? new JRegistry($data->attribs) : new JParameter($data->attribs);
  951. $fdata[$tindex][$field->name]->parameters->merge($ts_params);
  952. }
  953. }
  954. }
  955. // Set custom label, description or maintain default
  956. $field->label = isset($fdata[$tindex][$field->name]->label) ? $fdata[$tindex][$field->name]->label : $field->label;
  957. $field->description = isset($fdata[$tindex][$field->name]->description) ? $fdata[$tindex][$field->name]->description : $field->description;
  958. $field->label = JText::_($field->label);
  959. // Finally set field's parameters, but to clone ... or not to clone, better clone to allow customizations for individual item fields ...
  960. $field->parameters = clone($fdata[$tindex][$field->name]->parameters);
  961. return $field;
  962. }
  963. // Method to override PARAMETER VALUES with their Type Specific values
  964. static function _getTypeToCoreFieldParams ($type_id, & $tinfo, & $tparams) {
  965. static $core_field_names = null;
  966. if ( $core_field_names == null ) {
  967. $query = "SELECT field_type FROM #__flexicontent_fields WHERE iscore=1";
  968. //echo $query;
  969. $db = JFactory::getDBO();
  970. $db->setQuery($query);
  971. $core_field_names = FLEXI_J16GE ? $db->loadColumn() : $db->loadResultArray();
  972. $core_field_names[] = 'maintext';
  973. $core_field_names = array_flip($core_field_names);
  974. unset($core_field_names['text']);
  975. }
  976. $query = 'SELECT t.attribs, t.name, t.alias FROM #__flexicontent_types AS t WHERE t.id = ' . $type_id;
  977. $db = JFactory::getDBO();
  978. $db->setQuery($query);
  979. $typedata = $db->loadObject();
  980. if ( $typedata ) {
  981. $tinfo['typename'] = $typedata->name;
  982. $tinfo['typealias'] = $typedata->alias;
  983. $tparams = FLEXI_J16GE ? new JRegistry($typedata->attribs) : new JParameter($typedata->attribs);
  984. $_tparams = $tparams->toArray();
  985. $tinfo['params'] = array();
  986. // Extra voting parameters of --voting-- field parameters are overriden contiditionally
  987. if ( $tparams->get('voting_override_extra_votes', 0) ) {
  988. $tinfo['params']['voting']['voting_extra_votes'] = $tparams->get('voting_extra_votes', '');
  989. $main_label = $tparams->get('voting_main_label', '') ? $tparams->get('voting_main_label', '') : JText::_('FLEXI_OVERALL'); // Set default label in case of empty
  990. $tinfo['params']['voting']['main_label'] = $main_label;
  991. }
  992. foreach ($_tparams as $param_name => $param_value) {
  993. $res = preg_split('/_/', $param_name, 2);
  994. if ( count($res) < 2 ) continue;
  995. $o_field_type = $res[0]; $o_param_name = $res[1];
  996. if ( !isset($core_field_names[$o_field_type]) ) continue;
  997. //echo "$o_field_type _ $o_param_name = $param_value <br>\n";
  998. $skipparam = false;
  999. if ( strlen($param_value) ) {
  1000. if ($o_field_type=='voting') {
  1001. $skipparam = in_array($o_param_name, array('override_extra_votes','voting_extra_votes','voting_main_label'));
  1002. } else if ( in_array($o_param_name, array('label','desc','viewdesc')) ) {
  1003. $skipparam = true;
  1004. }
  1005. if (!$skipparam) {
  1006. $tinfo['params'][$o_field_type][$o_param_name] = $param_value;
  1007. //echo "$o_field_type _ $o_param_name = $param_value <br>\n";
  1008. }
  1009. }
  1010. }
  1011. //echo "<pre>"; print_r($tinfo['params']); echo "</pre>";
  1012. }
  1013. }
  1014. // Method get a language specific value from given Content Type (or other) Data
  1015. static function _getLangSpecificValue ($type_id, $type_prop_value, $prop_name, & $fdata)
  1016. {
  1017. //--. Get a 2 character language tag
  1018. static $lang = null;
  1019. $cntLang = substr(JFactory::getLanguage()->getTag(), 0,2); // Current Content language (Can be natively switched in J2.5)
  1020. $urlLang = JRequest::getWord('lang', '' ); // Language from URL (Can be switched via Joomfish in J1.5)
  1021. $lang = (FLEXI_J16GE || empty($urlLang)) ? $cntLang : $urlLang;
  1022. // --. SET a type specific label for the current field
  1023. // a. Try field label to get for current language
  1024. $result = preg_match("/(\[$lang\])=([^[]+)/i", $type_prop_value, $matches);
  1025. if ($result) {
  1026. $fdata->{$prop_name} = $matches[2];
  1027. } else if ($type_prop_value) {
  1028. // b. Try to get default for all languages
  1029. $result = preg_match("/(\[default\])=([^[]+)/i", $type_prop_value, $matches);
  1030. if ($result) {
  1031. $fdata->{$prop_name} = $matches[2];
  1032. } else {
  1033. // c. Check that no languages specific string are defined
  1034. $result = preg_match("/(\[??\])=([^[]+)/i", $type_prop_value, $matches);
  1035. if (!$result) {
  1036. $fdata->{$prop_name} = $type_prop_value;
  1037. }
  1038. }
  1039. } else {
  1040. // d. Maintain field 's default label
  1041. }
  1042. }
  1043. // **************************************************************************************
  1044. // Methods (a) for INDEXED FIELDs, and (b) for replacement field values / item properties
  1045. // **************************************************************************************
  1046. // Common method to get the allowed element values (field values with index,label,... properties) for fields that use indexed values
  1047. static function indexedField_getElements(&$field, $item, $extra_props=array(), &$item_pros=true, $create_filter=false)
  1048. {
  1049. static $_elements_cache = null;
  1050. if ( isset($_elements_cache[$field->id]) ) return $_elements_cache[$field->id];
  1051. $canCache = true;
  1052. $sql_mode = $field->parameters->get( 'sql_mode', 0 ) ; // For fields that use this parameter
  1053. $field_elements = $field->parameters->get( 'field_elements', '' ) ;
  1054. if ($create_filter) {
  1055. $filter_customize_options = $field->parameters->get('filter_customize_options', 0);
  1056. $filter_custom_options = $field->parameters->get('filter_custom_options', '');
  1057. if ( $filter_customize_options && $filter_custom_options) {
  1058. // Custom query for value retrieval
  1059. $sql_mode = $filter_customize_options==1;
  1060. $field_elements = $filter_custom_options;
  1061. } else if ( !$field_elements ) {
  1062. $field_elements = "SELECT value, value as text FROM #__flexicontent_fields_item_relations as fir WHERE field_id='{field_id}' AND value != '' GROUP BY value";
  1063. }
  1064. // Set parameters may be used later
  1065. $field->parameters->set('sql_mode', $sql_mode);
  1066. $field->parameters->set('field_elements', $field_elements);
  1067. }
  1068. // TODO: examine this in combination with canCache
  1069. //$field_elements = FlexicontentFields::replaceFieldValue( $field, $item, $field_elements, 'field_elements' );
  1070. if ($sql_mode) { // SQL mode, parameter field_elements contains an SQL query
  1071. $db = JFactory::getDBO

Large files files are truncated, but you can click here to view the full file