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

/Quản lý website tuyển dụng việc làm PHP/webcaoca/public_html/vlv/administrator/components/com_sh404sef/models/aliases.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 388 lines | 192 code | 87 blank | 109 comment | 19 complexity | a8af7e5385ef8d00447d293fc78ed590 MD5 | raw file
  1. <?php
  2. /**
  3. * SEF module for Joomla!
  4. *
  5. * @author $Author: shumisha $
  6. * @copyright Yannick Gaultier - 2007-2010
  7. * @package sh404SEF-15
  8. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
  9. * @version $Id: aliases.php 1687 2010-11-28 19:15:19Z silianacom-svn $
  10. */
  11. // Security check to ensure this file is being included by a parent file.
  12. if (!defined('_JEXEC')) die('Direct Access to this location is not allowed.');
  13. class Sh404sefModelAliases extends Sh404sefClassBaselistModel {
  14. protected $_context = 'sh404sef.aliases';
  15. protected $_defaultTable = 'aliases';
  16. /**
  17. * Save a list of aliases as entered by user in backend to the database
  18. *
  19. * @param string $aliasList data from an html textarea field
  20. * @param string $nonSefUrl the non sef url to which aliases are attached
  21. *
  22. * @return boolean true on success
  23. */
  24. public function saveFromInput( $aliasList, $nonSefUrl) {
  25. // split aliases from raw input data into an array
  26. $aliasList = explode("\n", $aliasList);
  27. // delete them all. We should do a transaction, but not worth it
  28. $query = 'DELETE from #__sh404sef_aliases where newurl = '. $this->_db->Quote( $nonSefUrl);
  29. $this->_db->setQuery($query);
  30. $this->_db->query();
  31. // Write new aliases.
  32. if (!empty( $aliasList[0])) {
  33. $baseQuery = 'INSERT INTO #__sh404sef_aliases (newurl, alias, type) VALUES __shValue__;';
  34. $endOfLine = array("\r\n", "\n", "\r");
  35. foreach($aliasList as $alias) {
  36. // remove end of line chars
  37. $alias = str_replace($endOfLine, '', $alias);
  38. // if something left, try insert it into DB
  39. if (!empty($alias)) {
  40. // first check value is not invalid
  41. // either the alias already exists
  42. // or same SEF url already exists
  43. $query = 'select count(id) from #__redirection where '
  44. . $this->_db->nameQuote( 'oldurl') . '='
  45. . $this->_db->Quote( $alias)
  46. . ' and ' . $this->_db->nameQuote( 'newurl') . '<>' . $this->_db->Quote( ''); // allow adding aliases on 404
  47. $this->_db->setQuery( $query);
  48. $count = $this->_db->loadResult();
  49. if (empty( $count)) {
  50. $query = 'select count(id) from #__sh404sef_aliases where '
  51. . $this->_db->nameQuote( 'alias') . '='
  52. . $this->_db->Quote( $alias);
  53. $this->_db->setQuery( $query);
  54. $count = $this->_db->loadResult();
  55. }
  56. // if ok, insert into db
  57. if (empty($count)) {
  58. $value = '('
  59. . $this->_db->Quote( $nonSefUrl) . ', '
  60. . $this->_db->Quote( $alias)
  61. . ', '
  62. . $this->_db->Quote( Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_ALIAS)
  63. . ')';
  64. $query = str_replace('__shValue__', $value, $baseQuery);
  65. $this->_db->setQuery( $query);
  66. $this->_db->query();
  67. // check errors
  68. $error = $this->_db->getErrorNum();
  69. if (!empty( $error)) {
  70. $this->setError( 'Internal database error # ' . $query);
  71. }
  72. } else {
  73. // alias already exists either as an alias or a SEF url
  74. $this->setError( JText16::sprintf( 'COM_SH404SEF_ALIAS_ALREADY_EXISTS', $alias));
  75. }
  76. }
  77. }
  78. }
  79. // return true if no error
  80. $error = $this->getError();
  81. return empty( $error);
  82. }
  83. /**
  84. * Read data from model and turns it into
  85. * a string suitable for display in a text area field
  86. *
  87. * @param array $options key/value pairs to restrict data selection
  88. */
  89. public function getDisplayableList( $options) {
  90. // get raw data
  91. $rawList = $this->getList( $options, $returnZeroElement = false, $forcedLimitstart = null, $forcedLimit = 5000);
  92. // make a simple string suitable for editing in a text area input field
  93. $displayableList = '';
  94. if (!empty( $rawList)) {
  95. foreach ($rawList as $alias) {
  96. $displayableList .= shUrlSafeDisplay( $alias->alias) . "\n";
  97. }
  98. }
  99. return $displayableList;
  100. }
  101. /**
  102. * Purge urls from database (and cache)
  103. * either all automatic, or according to current
  104. * sef url list page select options as stored in
  105. * in session
  106. * @param unknown_type $type
  107. */
  108. public function purge( $type = 'auto') {
  109. // make sure we use latest user state
  110. $this->_updateContextData();
  111. // call the appropriate sub-method to get the db query
  112. $methodName = '_getPurgeQuery' . ucfirst($type);
  113. if (is_callable( array( $this, $methodName))) {
  114. $deleteQuery = $this->$methodName();
  115. } else {
  116. $this->setError( 'Invalid method call _purge' . $type);
  117. return;
  118. }
  119. // then run the query
  120. $this->_db->setQuery( $deleteQuery);
  121. $this->_db->query();
  122. // reset limit and limitstart variables, to avoid
  123. // issue when displaying again results
  124. $this->_setState( 'limitstart', 0);
  125. $this->_setState( 'limit', 0);
  126. // set error
  127. $error = $this->_db->getErrorNum();
  128. if (!empty($error)) {
  129. $this->setError( 'Internal database error # ' . $error);
  130. }
  131. }
  132. public function getAliasesCount( $which = 'auto') {
  133. switch (strtolower( $which)) {
  134. // we want to read all automatic urls (include duplicates)
  135. case 'auto':
  136. $query = 'select count(*) from ' . $this->_db->nameQuote( $this->_getTableName())
  137. . ' where ' . $this->_db->nameQuote( 'type') . '=' . $this->_db->Quote( Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_ALIAS);
  138. $this->_db->setQuery( $query);
  139. $numberOfUrls = $this->_db->loadResult();
  140. break;
  141. // we want to read urls as per current selection input fields
  142. // ie : component, language, custom, ...
  143. case 'selected':
  144. $numberOfUrls = $this->getTotal();
  145. break;
  146. default:
  147. $numberOfUrls = 0;
  148. break;
  149. }
  150. return intval( $numberOfUrls);
  151. }
  152. /**
  153. * Finds the sef url record to which an
  154. * alias record, identified by its id,
  155. * elongs to
  156. *
  157. * @param integer $aliasId
  158. */
  159. public function getUrlByAliasId( $aliasId) {
  160. $aliasId = empty( $aliasId) ? 0 : intval( $aliasId);
  161. $query = 'select r.* from ' . $this->_db->nameQuote( '#__redirection') . ' as r'
  162. . ' left join ' . $this->_db->nameQuote( '#__sh404sef_aliases') . ' as a'
  163. . ' on a.' . $this->_db->nameQuote( 'newurl') . ' = r.' . $this->_db->nameQuote( 'newurl')
  164. . ' where a.' . $this->_db->nameQuote( 'id') . ' = ' . $this->_db->Quote( $aliasId)
  165. . ' order by ' . $this->_db->nameQuote( 'rank');
  166. $this->_db->setQuery( $query);
  167. $url = $this->_db->loadObject();
  168. // set error
  169. $error = $this->_db->getErrorNum();
  170. if (!empty($error)) {
  171. $this->setError( 'Internal database error # ' . $error);
  172. }
  173. return $url;
  174. }
  175. /**
  176. * Gets alist of current filters and sort options which have
  177. * been applied when building up the data
  178. * @override
  179. * @return object the list ov values as object properties
  180. */
  181. public function getDisplayOptions() {
  182. $options = parent::getDisplayOptions();
  183. // get additional options vs base class
  184. // component used in url
  185. $options->filter_component = $this->_getState( 'filter_component');
  186. // show all/only one language
  187. $options->filter_language = $this->_getState( 'filter_language');
  188. // return cached instance
  189. return $options;
  190. }
  191. protected function _buildListSelect( $options) {
  192. // array to hold select clause parts
  193. $select = array();
  194. // get options
  195. $select[] = ' select a.*, r.oldurl';
  196. // add from clause
  197. $select[] = 'from ' . $this->_getTableName() . ' as a';
  198. // aggregate clauses
  199. $select = ( count( $select ) ? implode( ' ', $select ) : '' );
  200. return $select;
  201. }
  202. protected function _buildListWhere( $options) {
  203. // array to hold where clause parts
  204. $where = array();
  205. // get set of filters applied to the current view
  206. $filters = $this->getDisplayOptions();
  207. // only aliases, no pageid
  208. $where[] = 'a.type = ' . $this->_db->Quote( Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_ALIAS);
  209. // are we reading aliases for one specific url ?
  210. $newurl = $this->_getOption( 'newurl', $options);
  211. if (!empty( $newurl)) {
  212. $where[] = 'a.newurl = ' . $this->_db->Quote( $newurl);
  213. } else {
  214. // we read them all, except possibly the home page aliases
  215. $includeHomeData = $this->_getOption( 'includeHomeData', $options);
  216. if (empty( $includeHomeData)) {
  217. $where[] = 'a.newurl != ' . $this->_db->Quote( sh404SEF_HOMEPAGE_CODE);
  218. }
  219. }
  220. // add search all urls term if any
  221. if ( !empty($filters->search_all) ) { // V 1.2.4.q added search URL feature
  222. jimport( 'joomla.utilities.string');
  223. $searchTerm = $this->_cleanForQuery( JString::strtolower($filters->search_all));
  224. $where[] = " (LOWER(a.alias) LIKE '%" . $searchTerm . "%' OR "
  225. . "LOWER(r.newurl) LIKE '%" . $searchTerm . "%')";
  226. }
  227. // components check
  228. if (!empty( $filters->filter_component)) {
  229. $where[] = "LOWER(a.newurl) LIKE '%option=" . $this->_cleanForQuery( $filters->filter_component ) . "%'";
  230. }
  231. // language check
  232. if (!empty( $filters->filter_language)) {
  233. $where[] = "LOWER(a.newurl) LIKE '%lang=" . $this->_cleanForQuery( $filters->filter_language ) . "%'";
  234. }
  235. // aggregate clauses
  236. $where = ( count( $where ) ? ' WHERE '. implode( ' AND ', $where ) : '' );
  237. return $where;
  238. }
  239. protected function _buildListJoin( $options) {
  240. // array to hold join clause parts
  241. $join = array();
  242. // read also the sef url
  243. $join[] = 'left join ' . $this->_db->nameQuote( '#__redirection') . ' as r';
  244. $join[] = 'on r.' . $this->_db->nameQuote( 'newurl') . ' = a.' . $this->_db->nameQuote( 'newurl');
  245. // aggregate clauses
  246. $join = ( count( $join ) ? ' ' . implode( ' ', $join ) : '' );
  247. return $join;
  248. }
  249. protected function _buildListOrderBy( $options) {
  250. // get set of filters applied to the current view
  251. $filters = $this->getDisplayOptions();
  252. // build query fragment
  253. $orderBy = ' order by ' . $filters->filter_order;
  254. $orderBy .= ' ' . $filters->filter_order_Dir;
  255. return $orderBy;
  256. }
  257. protected function _getTableName() {
  258. return '#__sh404sef_aliases';
  259. }
  260. /**
  261. * Provides context data definition, to be used by context handler
  262. * Should be overriden by descendant
  263. */
  264. protected function _getContextDataDef() {
  265. $contextData = parent::_getContextDataDef();
  266. // define context data to be retrieved. Cannot be done at class level,
  267. // as some default values are dynamic
  268. $addedContextData = array(
  269. // redefined default sort order
  270. array( 'name' => 'filter_order', 'html_name' => 'filter_order', 'default' => 'alias', 'type' => 'string')
  271. // component used in url
  272. , array( 'name' => 'filter_component', 'html_name' => 'filter_component', 'default' => '', 'type' => 'string')
  273. // show all/only one language
  274. , array( 'name' => 'filter_language', 'html_name' => 'filter_language', 'default' => '', 'type' => 'string')
  275. );
  276. return array_merge( $contextData, $addedContextData);
  277. }
  278. /**
  279. * Delete all automatically generated url records
  280. * from database and cache
  281. */
  282. private function _getPurgeQueryAuto() {
  283. // delete from database
  284. $query = 'delete from ' . $this->_db->nameQuote( $this->_getTableName())
  285. . ' where type = ' . $this->_db->Quote( Sh404sefHelperGeneral::COM_SH404SEF_URLTYPE_ALIAS);
  286. return $query;
  287. }
  288. private function _getPurgeQuerySelected() {
  289. // a 2 steps process : first collect those urls id we need
  290. // in accordance with select drop-down lists
  291. // then combine it with a delete query
  292. $options = null;
  293. $query = $this->_buildListQuery($options);
  294. // collect only the ids
  295. $queryIds = 'select t.id from (' . $query . ') as t';
  296. // start delete query
  297. $deleteQuery = 'delete from ' . $this->_db->nameQuote( $this->_getTableName())
  298. . ' where id = any (' . $queryIds . ')';
  299. return $deleteQuery;
  300. }
  301. }