PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_rsform/models/submissions.php

https://bitbucket.org/organicdevelopment/joomla-2.5
PHP | 395 lines | 305 code | 76 blank | 14 comment | 53 complexity | 97b2a2d26556419e33c1bd1167573d1c MD5 | raw file
Possible License(s): LGPL-3.0, GPL-2.0, MIT, BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /**
  3. * @version 1.4.0
  4. * @package RSform!Pro 1.4.0
  5. * @copyright (C) 2007-2011 www.rsjoomla.com
  6. * @license GPL, http://www.gnu.org/copyleft/gpl.html
  7. */
  8. defined('_JEXEC') or die('Restricted access');
  9. jimport('joomla.application.component.model');
  10. class RSFormModelSubmissions extends JModel
  11. {
  12. var $_form = null;
  13. var $_data = array();
  14. var $_total = 0;
  15. var $_query = '';
  16. var $_pagination = null;
  17. var $_db = null;
  18. var $formId = 1;
  19. var $params;
  20. var $replacements;
  21. function __construct()
  22. {
  23. parent::__construct();
  24. $mainframe =& JFactory::getApplication();
  25. $option = JRequest::getVar('option', 'com_rsform');
  26. $this->_db = JFactory::getDBO();
  27. $this->params = $mainframe->getParams('com_rsform');
  28. $this->formId = $this->params->get('formId');
  29. if (!$this->params->get('enable_submissions', 0))
  30. {
  31. JError::raiseWarning(500, JText::_('ALERTNOTAUTH'));
  32. $mainframe->redirect(JURI::root());
  33. return;
  34. }
  35. // Get pagination request variables
  36. $limit = JRequest::getVar('limit', $mainframe->getCfg('list_limit'), '', 'int');
  37. $limitstart = JRequest::getVar('limitstart', 0, '', 'int');
  38. // In case limit has been changed, adjust it
  39. $limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
  40. $this->setState($option.'.submissions.'.$this->formId.'.limit', $limit);
  41. $this->setState($option.'.submissions.'.$this->formId.'.limitstart', $limitstart);
  42. $this->_query = $this->_buildQuery();
  43. }
  44. function getForm()
  45. {
  46. if (empty($this->_form))
  47. {
  48. $this->_db->setQuery("SELECT * FROM #__rsform_forms WHERE FormId='".$this->formId."'");
  49. $this->_form = $this->_db->loadObject();
  50. $this->_form->MultipleSeparator = str_replace(array('\n', '\r', '\t'), array("\n", "\r", "\t"), $this->_form->MultipleSeparator);
  51. }
  52. return $this->_form;
  53. }
  54. function _buildQuery()
  55. {
  56. $query = "SELECT SQL_CALC_FOUND_ROWS DISTINCT(sv.SubmissionId), s.* FROM #__rsform_submissions s";
  57. $query .= " LEFT JOIN #__rsform_submission_values sv ON (s.SubmissionId=sv.SubmissionId)";
  58. $query .= " WHERE s.FormId='".$this->formId."'";
  59. $filter = $this->_db->getEscaped($this->getFilter());
  60. $confirmed = $this->params->get('show_confirmed', 0);
  61. if ($confirmed)
  62. $query .= " AND s.confirmed='1'";
  63. $lang = $this->params->get('lang', '');
  64. if ($lang)
  65. $query .= " AND s.Lang='".$this->_db->getEscaped($lang)."'";
  66. if ($filter != '')
  67. {
  68. $query .= " AND (sv.FieldValue LIKE '%".$filter."%'";
  69. $query .= " OR s.DateSubmitted LIKE '%".$filter."%'";
  70. $query .= " OR s.Username LIKE '%".$filter."%'";
  71. $query .= " OR s.UserIp LIKE '%".$filter."%')";
  72. }
  73. $userId = $this->params->def('userId', 0);
  74. if ($userId == 'login')
  75. {
  76. $user =& JFactory::getUser();
  77. if ($user->get('guest'))
  78. $query .= " AND 1>2";
  79. $query .= " AND s.UserId='".(int) $user->get('id')."'";
  80. }
  81. elseif ($userId == 0)
  82. {
  83. // Show all submissions
  84. }
  85. else
  86. {
  87. $userId = explode(',', $userId);
  88. JArrayHelper::toInteger($userId);
  89. $query .= " AND s.UserId IN (".implode(',', $userId).")";
  90. }
  91. $dir = $this->params->get('sort_submissions') ? 'ASC' : 'DESC';
  92. $query .= " ORDER BY s.DateSubmitted $dir";
  93. return $query;
  94. }
  95. function getPagination()
  96. {
  97. if (empty($this->_pagination))
  98. {
  99. jimport('joomla.html.pagination');
  100. $this->_pagination = new JPagination($this->getTotal(), $this->getState('com_rsform.submissions.'.$this->formId.'.limitstart'), $this->getState('com_rsform.submissions.'.$this->formId.'.limit'));
  101. }
  102. return $this->_pagination;
  103. }
  104. function getTotal()
  105. {
  106. return $this->_total;
  107. }
  108. function getSubmissions()
  109. {
  110. if (empty($this->_data))
  111. {
  112. $this->getComponents();
  113. $this->_db->setQuery("SET SQL_BIG_SELECTS=1");
  114. $this->_db->query();
  115. $submissionIds = array();
  116. $this->_db->setQuery($this->_query, $this->getState('com_rsform.submissions.'.$this->formId.'.limitstart'), $this->getState('com_rsform.submissions.'.$this->formId.'.limit'));
  117. $results = $this->_db->loadObjectList();
  118. $this->_db->setQuery("SELECT FOUND_ROWS()");
  119. $this->_total = $this->_db->loadResult();
  120. foreach ($results as $result)
  121. {
  122. $submissionIds[] = $result->SubmissionId;
  123. $this->_data[$result->SubmissionId]['FormId'] = $result->FormId;
  124. $this->_data[$result->SubmissionId]['DateSubmitted'] = RSFormProHelper::getDate($result->DateSubmitted);
  125. $this->_data[$result->SubmissionId]['UserIp'] = $result->UserIp;
  126. $this->_data[$result->SubmissionId]['Username'] = $result->Username;
  127. $this->_data[$result->SubmissionId]['UserId'] = $result->UserId;
  128. $this->_data[$result->SubmissionId]['Lang'] = $result->Lang;
  129. $this->_data[$result->SubmissionId]['confirmed'] = $result->confirmed ? JText::_('RSFP_YES') : JText::_('RSFP_NO');
  130. $this->_data[$result->SubmissionId]['SubmissionValues'] = array();
  131. }
  132. $form = $this->getForm();
  133. if (!empty($submissionIds))
  134. {
  135. $this->_db->setQuery("SELECT * FROM `#__rsform_submission_values` WHERE `SubmissionId` IN (".implode(',',$submissionIds).")");
  136. $results = $this->_db->loadObjectList();
  137. $config = JFactory::getConfig();
  138. $secret = $config->getValue('config.secret');
  139. foreach ($results as $result)
  140. {
  141. // Check if this is an upload field
  142. if (in_array($result->FieldName, $this->uploadFields) && !empty($result->FieldValue))
  143. {
  144. $result->FilePath = $result->FieldValue;
  145. $result->FieldValue = '<a href="'.JURI::root().'index.php?option=com_rsform&amp;task=submissions.view.file&amp;hash='.md5($result->SubmissionId.$secret.$result->FieldName).'">'.basename($result->FieldValue).'</a>';
  146. }
  147. // Check if this is a multiple field
  148. elseif (in_array($result->FieldName, $this->multipleFields))
  149. $result->FieldValue = str_replace("\n", $form->MultipleSeparator, $result->FieldValue);
  150. elseif ($form->TextareaNewLines && in_array($result->FieldName, $this->textareaFields))
  151. $result->FieldValue = nl2br($result->FieldValue);
  152. $this->_data[$result->SubmissionId]['SubmissionValues'][$result->FieldName] = array('Value' => $result->FieldValue, 'Id' => $result->SubmissionValueId);
  153. if (in_array($result->FieldName, $this->uploadFields) && !empty($result->FieldValue))
  154. {
  155. $filepath = $result->FilePath;
  156. $filepath = str_replace(JPATH_SITE.DS, JURI::root(), $filepath);
  157. $filepath = str_replace(array('\\', '\\/', '//\\'), '/', $filepath);
  158. $this->_data[$result->SubmissionId]['SubmissionValues'][$result->FieldName]['Path'] = $filepath;
  159. }
  160. }
  161. }
  162. unset($results);
  163. }
  164. return $this->_data;
  165. }
  166. function getReplacements($user_id)
  167. {
  168. $config = JFactory::getConfig();
  169. $user = JFactory::getUser((int) $user_id);
  170. $replace = array('{global:sitename}', '{global:siteurl}', '{global:userip}', '{global:userid}', '{global:username}', '{global:email}', '{/details}', '{/detailspdf}');
  171. $with = array($config->getValue('config.sitename'), JURI::root(), isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '', $user->get('id'), $user->get('username'), $user->get('email'), '</a>', '</a>');
  172. $this->replacements = array($replace, $with);
  173. return $this->replacements;
  174. }
  175. function getComponents()
  176. {
  177. $this->_db->setQuery("SELECT c.ComponentTypeId, p.ComponentId, p.PropertyName, p.PropertyValue FROM #__rsform_components c LEFT JOIN #__rsform_properties p ON (c.ComponentId=p.ComponentId) WHERE c.FormId='".$this->formId."' AND c.Published='1' AND p.PropertyName IN ('NAME', 'WYSIWYG')");
  178. $components = $this->_db->loadObjectList();
  179. $this->uploadFields = array();
  180. $this->multipleFields = array();
  181. $this->textareaFields = array();
  182. foreach ($components as $component)
  183. {
  184. // Upload fields
  185. if ($component->ComponentTypeId == 9)
  186. {
  187. $this->uploadFields[] = $component->PropertyValue;
  188. }
  189. // Multiple fields
  190. elseif (in_array($component->ComponentTypeId, array(3, 4)))
  191. {
  192. $this->multipleFields[] = $component->PropertyValue;
  193. }
  194. // Textarea fields
  195. elseif ($component->ComponentTypeId == 2)
  196. {
  197. if ($component->PropertyName == 'WYSIWYG' && $component->PropertyValue == 'NO')
  198. $this->textareaFields[] = $component->ComponentId;
  199. }
  200. }
  201. if (!empty($this->textareaFields))
  202. {
  203. $this->_db->setQuery("SELECT p.PropertyValue FROM #__rsform_components c LEFT JOIN #__rsform_properties p ON (c.ComponentId=p.ComponentId) WHERE c.ComponentId IN (".implode(',', $this->textareaFields).")");
  204. $this->textareaFields = $this->_db->loadResultArray();
  205. }
  206. }
  207. function getHeaders()
  208. {
  209. $query = "SELECT p.PropertyValue FROM #__rsform_components c";
  210. $query .= " LEFT JOIN #__rsform_properties p ON (c.ComponentId=p.ComponentId AND p.PropertyName='NAME')";
  211. $query .= " LEFT JOIN #__rsform_component_types ct ON (c.ComponentTypeId=ct.ComponentTypeId)";
  212. $query .= " WHERE c.FormId='".$this->formId."' AND c.Published='1'";
  213. $this->_db->setQuery($query);
  214. $headers = $this->_db->loadResultArray();
  215. return $headers;
  216. }
  217. function getTemplate()
  218. {
  219. $mainframe =& JFactory::getApplication();
  220. $Itemid = '';
  221. if ($Itemid = JRequest::getInt('Itemid'))
  222. $Itemid = '&Itemid='.$Itemid;
  223. $template_module = $this->params->def('template_module', '');
  224. $template_formdatarow = $this->params->def('template_formdatarow', '');
  225. $template_formdetail = $this->params->def('template_formdetail', '');
  226. $formdata = '';
  227. $has_suffix = $mainframe->getCfg('sef') && $mainframe->getCfg('sef_suffix');
  228. $layout = JRequest::getVar('layout', 'default');
  229. if ($layout == 'default')
  230. {
  231. $submissions = $this->getSubmissions();
  232. $headers = $this->getHeaders();
  233. $pagination = $this->getPagination();
  234. $i = 0;
  235. foreach ($submissions as $SubmissionId => $submission)
  236. {
  237. list($replace, $with) = $this->getReplacements($submission['UserId']);
  238. $pdf_link = JRoute::_('index.php?option=com_rsform&view=submissions&layout=view&cid='.$SubmissionId.'&format=pdf'.$Itemid);
  239. if ($has_suffix)
  240. {
  241. $pdf_link .= strpos($pdf_link, '?') === false ? '?' : '&';
  242. $pdf_link .= 'format=pdf';
  243. }
  244. $replace = array_merge($replace, array('{global:date_added}', '{global:submissionid}', '{global:submission_id}', '{global:counter}', '{details}', '{detailspdf}','{global:confirmed}'));
  245. $with = array_merge($with, array($submission['DateSubmitted'], $SubmissionId, $SubmissionId, $pagination->getRowOffset($i), '<a href="'.JRoute::_('index.php?option=com_rsform&view=submissions&layout=view&cid='.$SubmissionId.$Itemid).'">', '<a href="'.$pdf_link.'">',$submission['confirmed']));
  246. $replace[] = '{_STATUS:value}';
  247. $with[] = isset($submission['SubmissionValues']['_STATUS']) ? JText::_('RSFP_PAYPAL_STATUS_'.$submission['SubmissionValues']['_STATUS']['Value']) : '';
  248. foreach ($headers as $header)
  249. {
  250. if (!isset($submission['SubmissionValues'][$header]['Value']))
  251. $submission['SubmissionValues'][$header]['Value'] = '';
  252. $replace[] = '{'.$header.':value}';
  253. $with[] = $submission['SubmissionValues'][$header]['Value'];
  254. if (!empty($submission['SubmissionValues'][$header]['Path']))
  255. {
  256. $replace[] = '{'.$header.':path}';
  257. $with[] = $submission['SubmissionValues'][$header]['Path'];
  258. }
  259. }
  260. $formdata .= str_replace($replace, $with, $template_formdatarow);
  261. $i++;
  262. }
  263. $html = str_replace('{formdata}', $formdata, $template_module);
  264. }
  265. else
  266. {
  267. $cid = JRequest::getInt('cid');
  268. $user =& JFactory::getUser();
  269. $userId = $this->params->def('userId', 0);
  270. if ($userId != 'login' && $userId != 0)
  271. {
  272. $userId = explode(',', $userId);
  273. JArrayHelper::toInteger($userId);
  274. }
  275. $this->_db->setQuery("SELECT * FROM #__rsform_submissions WHERE SubmissionId='".$cid."'");
  276. $submission = $this->_db->loadObject();
  277. if (!$submission || ($submission->FormId != $this->params->get('formId')) || ($userId == 'login' && $submission->UserId != $user->get('id')) || (is_array($userId) && !in_array($user->get('id'), $userId)))
  278. {
  279. JError::raiseWarning(500, JText::_('ALERTNOTAUTH'));
  280. $mainframe->redirect(JURI::root());
  281. return;
  282. }
  283. if ($this->params->get('show_confirmed', 0) && !$submission->confirmed)
  284. {
  285. JError::raiseWarning(500, JText::_('ALERTNOTAUTH'));
  286. $mainframe->redirect(JURI::root());
  287. return;
  288. }
  289. $format = JRequest::getVar('format');
  290. $pdf_link = JRoute::_('index.php?option=com_rsform&view=submissions&layout=view&cid='.$cid.'&format=pdf'.$Itemid);
  291. if ($has_suffix)
  292. {
  293. $pdf_link .= strpos($pdf_link, '?') === false ? '?' : '&';
  294. $pdf_link .= 'format=pdf';
  295. }
  296. $confirmed = $submission->confirmed ? JText::_('RSFP_YES') : JText::_('RSFP_NO');
  297. list($replace, $with) = RSFormProHelper::getReplacements($cid, true);
  298. list($replace2, $with2) = $this->getReplacements($submission->UserId);
  299. $replace = array_merge($replace, $replace2, array('{global:date_added}', '{global:submissionid}', '{global:submission_id}', '{detailspdf}','{global:confirmed}'));
  300. $with = array_merge($with, $with2, array(RSFormProHelper::getDate($submission->DateSubmitted), $cid, $cid, '<a href="'.$pdf_link.'">',$confirmed));
  301. if ($format == 'pdf' && preg_match_all('#{detailspdf}(.*?){\/detailspdf}#is', $template_formdetail, $matches))
  302. foreach ($matches[0] as $fullmatch)
  303. $template_formdetail = str_replace($fullmatch, '', $template_formdetail);
  304. $html = str_replace($replace, $with, $template_formdetail);
  305. }
  306. return $html;
  307. }
  308. function getFilter()
  309. {
  310. $mainframe =& JFactory::getApplication();
  311. $formId = $this->params->get('formId',0);
  312. return $mainframe->getUserStateFromRequest('com_rsform.submissions.form'.$formId.'.filter', 'filter', '');
  313. }
  314. function getItemid()
  315. {
  316. $itemid = JRequest::getInt('itemid');
  317. return !empty($itemid) ? '&Itemid='.$itemid : '';
  318. }
  319. }