PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Ionize/application/models/media_model.php

http://you.googlecode.com/
PHP | 248 lines | 112 code | 51 blank | 85 comment | 13 complexity | 3d43737e8f1b3292e0e5054a0e3e0087 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause, LGPL-2.1
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Ionize
  4. *
  5. * @package Ionize
  6. * @author Ionize Dev Team
  7. * @license http://ionizecms.com/doc-license
  8. * @link http://ionizecms.com
  9. * @since Version 0.90
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * Ionize Media Model
  14. *
  15. * @package Ionize
  16. * @subpackage Models
  17. * @category Media management
  18. * @author Ionize Dev Team
  19. *
  20. */
  21. class Media_model extends Base_model
  22. {
  23. /**
  24. * Constructor
  25. *
  26. * @access public
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. $this->table = 'media';
  32. $this->pk_name = 'id_media';
  33. $this->lang_table = 'media_lang';
  34. }
  35. // ------------------------------------------------------------------------
  36. /**
  37. * Get media list for one defined parent
  38. *
  39. * @param string Media type. Can be 'picture', 'music', 'video', 'file'
  40. * @param string parent. Example : 'article', 'page'
  41. * @param string Parent ID
  42. *
  43. */
  44. public function get_list($parent, $id_parent, $type=NULL)
  45. {
  46. $data = array();
  47. // $this->db->where('id_'.$parent, $id_parent);
  48. $this->db->order_by('ordering', 'ASC');
  49. $this->db->select($this->table.'.*', false);
  50. $this->db->where($parent.'_'.$this->table.'.id_'.$parent, $id_parent);
  51. if ( ! is_null($type))
  52. $this->db->where($this->table.'.type', $type);
  53. $this->db->join($parent.'_'.$this->table, $this->table.'.id_'.$this->table.'='.$parent.'_'.$this->table.'.id_'.$this->table);
  54. $this->db->select($parent.'_'.$this->table.'.ordering');
  55. $query = $this->db->get($this->table);
  56. if($query->num_rows() > 0)
  57. $data = $query->result_array();
  58. return $data;
  59. }
  60. // ------------------------------------------------------------------------
  61. /**
  62. * Inserts / Update a media into the media table.
  63. * Updates the media if the media complete path already exists
  64. *
  65. * @param string Medium type. Can be 'picture', 'music', 'video', 'file'
  66. * @param string Complete path to the medium, including file name.
  67. * @return boolean true if succeed, false if errors
  68. *
  69. */
  70. function insert_media($type, $path)
  71. {
  72. if ($path) {
  73. // If no '/' in the path...
  74. if(strpos($path, '/') === false)
  75. {
  76. $file_name = $path;
  77. $base_path = '';
  78. }
  79. else
  80. {
  81. $file_name = substr( strrchr($path, '/') ,1 );
  82. $base_path = str_replace($file_name, '', $path);
  83. }
  84. $data['type'] = $type;
  85. $data['path'] = $path;
  86. $data['file_name'] = $file_name;
  87. $data['base_path'] = $base_path;
  88. // Update if exists
  89. $query = $this->get_where(array('path'=>$path));
  90. if( $query->num_rows() > 0)
  91. {
  92. $medium = $query->row_array();
  93. $this->db->where('path', $path);
  94. $this->db->update($this->table, $data);
  95. $id = $medium['id_media'];
  96. }
  97. // Insert
  98. else
  99. {
  100. $this->db->insert($this->table, $data);
  101. $id = $this->db->insert_id();
  102. }
  103. return $id;
  104. }
  105. return false;
  106. }
  107. // ------------------------------------------------------------------------
  108. /**
  109. * Attach one medium to a parent
  110. *
  111. * @param string Media type. Can be 'picture', 'video', etc.
  112. * @param string parent. Example : 'article', 'page'
  113. * @param string Parent ID
  114. * @param string Medium ID
  115. * @return boolean true if success, false if error
  116. *
  117. */
  118. public function attach_media($type, $parent, $id_parent, $id_media)
  119. {
  120. // Get the media ordering value, regarding to the type
  121. if ($this->db->field_exists('ordering', $parent.'_media'))
  122. {
  123. $this->db->select_max('ordering');
  124. $this->db->join('media', 'media.id_media = '.$parent.'_media.id_media');
  125. $this->db->where('id_'.$parent, $id_parent);
  126. $this->db->where('media.type', $type);
  127. $query = $this->db->get($parent.'_media');
  128. if ($query->num_rows() > 0)
  129. {
  130. $row = $query->row();
  131. $ordering = $row->ordering;
  132. }
  133. else
  134. {
  135. $ordering = 0;
  136. }
  137. $this->db->set('ordering', $ordering += 1);
  138. }
  139. $this->db->where('id_media', $id_media);
  140. $this->db->where('id_'.$parent, $id_parent);
  141. $query = $this->db->get($parent.'_media');
  142. if ($query->num_rows() == 0) {
  143. $this->db->set('id_media', $id_media);
  144. $this->db->set('id_'.$parent, $id_parent);
  145. $this->db->insert($parent.'_media');
  146. return true;
  147. }
  148. return false;
  149. }
  150. // ------------------------------------------------------------------------
  151. /**
  152. * Detach all media from a parent depending on the type
  153. * If no type, all media attached to this parent will be deleted
  154. *
  155. * @param string parent type. Ex 'page', 'article'
  156. * @param string parent ID
  157. * @param string media type. Optional.
  158. *
  159. */
  160. function detach_media_by_type($parent, $id_parent, $type = false)
  161. {
  162. /* INNER JOIN on delete is not possible with CI Active Record.
  163. * So this request needs to be handly written
  164. */
  165. $sql = ' DELETE first from ' . $parent . '_media AS first';
  166. if ($type)
  167. {
  168. $sql .= ' INNER JOIN ' . $this->table . ' AS second WHERE first.id_media = second.id_media ';
  169. $sql .= ' AND second.type = \'' . $type . '\'';
  170. $sql .= ' AND first.id_'.$parent.' = '.$id_parent;
  171. }
  172. else
  173. {
  174. $sql .= ' WHERE first.id_' . $parent . ' = ' . $id_parent;
  175. }
  176. $this->db->query($sql);
  177. return (int) $this->db->affected_rows();
  178. }
  179. // ------------------------------------------------------------------------
  180. /**
  181. * Saves one media data
  182. *
  183. * @param array standard data array
  184. * @param array lang data array
  185. *
  186. * @return string Inserted / Updated media ID
  187. */
  188. function save($data, $lang_data)
  189. {
  190. // Dates
  191. $data['date'] = ($data['date']) ? getMysqlDatetime($data['date']) : '0000-00-00';
  192. // Media saving
  193. return parent::save($data, $lang_data);
  194. }
  195. }
  196. /* End of file media_model.php */
  197. /* Location: ./application/models/media_model.php */