PageRenderTime 91ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/app/models/Artist.php

https://gitlab.com/Kille250/myimouto
PHP | 282 lines | 229 code | 44 blank | 9 comment | 22 complexity | afbf454b0115da3ef140b673e8a40c49 MD5 | raw file
  1. <?php
  2. // ActiveRecord::load_model(array('ArtistUrl', 'WikiPage'));
  3. class Artist extends Rails\ActiveRecord\Base
  4. {
  5. protected $alias_names = [];
  6. protected $member_names = [];
  7. public $updater_ip_addr = [];
  8. protected $notes;
  9. protected $urls;
  10. public function __toString()
  11. {
  12. return $this->name;
  13. }
  14. protected function associations()
  15. {
  16. return [
  17. 'has_many' => [
  18. 'artist_urls' => ['class_name' => 'ArtistUrl']
  19. ],
  20. 'belongs_to' => [
  21. 'updater' => ['class_name' => 'User', 'foreign_key' => "updater_id"]
  22. ]
  23. ];
  24. }
  25. protected function callbacks()
  26. {
  27. return [
  28. 'after_save' => ['commit_urls', 'commit_notes', 'commit_aliases', 'commit_members'],
  29. 'before_validation' => ['normalize']
  30. ];
  31. }
  32. protected function validations()
  33. {
  34. return [
  35. 'name' => ['uniqueness' => true]
  36. ];
  37. }
  38. /* UrlMethods { */
  39. static public function find_all_by_url($url)
  40. {
  41. $url = ArtistUrl::normalize($url);
  42. $artists = new Rails\ActiveRecord\Collection();
  43. while ($artists->blank() && strlen($url) > 10) {
  44. $u = str_replace('*', '%', $url) . '%';
  45. $artists->merge(Artist::where("artists.alias_id IS NULL AND artists_urls.normalized_url LIKE ?", $u)->joins("JOIN artists_urls ON artists_urls.artist_id = artists.id")->order("artists.name")->take());
  46. # Remove duplicates based on name
  47. $artists->unique('name');
  48. $url = dirname($url);
  49. }
  50. return $artists->slice(0, 20);
  51. }
  52. protected function commit_urls()
  53. {
  54. if ($this->urls) {
  55. $this->artist_urls->each('destroy');
  56. foreach (array_unique(array_filter(preg_split('/\v/', $this->urls))) as $url)
  57. ArtistUrl::create(array('url' => $url, 'artist_id' => $this->id));
  58. }
  59. }
  60. public function urls()
  61. {
  62. $urls = array();
  63. foreach($this->artist_urls as $x)
  64. $urls[] = $x->url;
  65. return implode("\n", $urls);
  66. }
  67. public function setUrls($url)
  68. {
  69. $this->urls = $url;
  70. }
  71. /* Note Methods */
  72. protected function wiki_page()
  73. {
  74. return WikiPage::find_page($this->name);
  75. }
  76. public function notes_locked()
  77. {
  78. if ($this->wiki_page()) {
  79. return !empty($this->wiki_page()->is_locked);
  80. }
  81. }
  82. public function notes()
  83. {
  84. if ($this->wiki_page())
  85. return $this->wiki_page()->body;
  86. else
  87. return '';
  88. }
  89. public function setNotes($notes)
  90. {
  91. $this->notes = $notes;
  92. }
  93. protected function commit_notes()
  94. {
  95. if ($this->notes) {
  96. if (!$this->wiki_page())
  97. WikiPage::create(array('title' => $this->name, 'body' => $this->notes, 'ip_addr' => $this->updater_ip_addr, 'user_id' => $this->updater_id));
  98. elseif ($this->wiki_page()->is_locked)
  99. $this->errors()->add('notes', "are locked");
  100. else
  101. $this->wiki_page()->updateAttributes(array('body' => $this->notes, 'ip_addr' => $this->updater_ip_addr, 'user_id' => $this->updater_id));
  102. }
  103. }
  104. /* Alias Methods */
  105. protected function commit_aliases()
  106. {
  107. self::connection()->executeSql("UPDATE artists SET alias_id = NULL WHERE alias_id = ".$this->id);
  108. if ($this->alias_names) {
  109. foreach ($this->alias_names as $name) {
  110. $a = Artist::where(['name' => $name])->firstOrCreate()->updateAttributes(array('alias_id' => $this->id, 'updater_id' => $this->updater_id));
  111. }
  112. }
  113. }
  114. public function alias_names()
  115. {
  116. return implode(', ', $this->aliases()->getAttributes('name'));
  117. }
  118. public function aliases()
  119. {
  120. if ($this->isNewRecord())
  121. return new Rails\ActiveRecord\Collection();
  122. else
  123. return Artist::where("alias_id = ".$this->id)->order("name")->take();
  124. }
  125. public function alias_name()
  126. {
  127. $name = '';
  128. if ($this->alias_id) {
  129. try {
  130. $name = Artist::find($this->alias_id)->name;
  131. } catch(Rails\ActiveRecord\Exception\RecordNotFoundException $e) {
  132. }
  133. }
  134. return $name;
  135. }
  136. /* Group Methods */
  137. protected function commit_members()
  138. {
  139. self::connection()->executeSql("UPDATE artists SET group_id = NULL WHERE group_id = ".$this->id);
  140. if ($this->member_names) {
  141. foreach ($this->member_names as $name) {
  142. $a = Artist::where(['name' => $name])->firstOrCreate();
  143. $a->updateAttributes(array('group_id' => $this->id, 'updater_id' => $this->updater_id));
  144. }
  145. }
  146. }
  147. public function group_name()
  148. {
  149. if ($this->group_id) {
  150. $artist = Artist::where('id = ' . $this->group_id)->first();
  151. return $artist ? $artist->name : '';
  152. }
  153. }
  154. public function members()
  155. {
  156. if ($this->isNewRecord())
  157. return new Rails\ActiveRecord\Collection();
  158. else
  159. return Artist::where("group_id = ".$this->id)->order("name")->take();
  160. }
  161. public function member_names()
  162. {
  163. return implode(', ', $this->members()->getAttributes('name'));
  164. }
  165. public function setAliasNames($names)
  166. {
  167. if ($names = array_filter(explode(',', trim($names)))) {
  168. foreach ($names as $name)
  169. $this->alias_names[] = trim($name);
  170. }
  171. }
  172. public function setAliasName($name)
  173. {
  174. if ($name) {
  175. if ($artist = Artist::where(['name' => $name])->firstOrCreate())
  176. $this->alias_id = $artist->id;
  177. else
  178. $this->alias_id = null;
  179. } else
  180. $this->alias_id = null;
  181. }
  182. public function setMemberNames($names)
  183. {
  184. if ($names = array_filter(explode(',', trim($names)))) {
  185. foreach ($names as $name)
  186. $this->member_names[] = trim($name);
  187. }
  188. }
  189. public function setGroupName($name)
  190. {
  191. if (!$name)
  192. $this->group_id = null;
  193. else
  194. $this->group_id = Artist::where(['name' => $name])->firstOrCreate()->id;
  195. }
  196. /* Api Methods */
  197. public function api_attributes()
  198. {
  199. return [
  200. 'id' => $this->id,
  201. 'name' => $this->name,
  202. 'alias_id' => $this->alias_id,
  203. 'group_id' => $this->group_id,
  204. 'urls' => $this->artist_urls->getAttributes('url')
  205. ];
  206. }
  207. public function toXml(array $options = array())
  208. {
  209. $options['root'] = "artist";
  210. $options['attributes'] = $this->api_attributes();
  211. return parent::toXml($options);
  212. }
  213. public function asJson(array $args = array())
  214. {
  215. return $this->api_attributes();
  216. }
  217. public function normalize()
  218. {
  219. $this->name = str_replace(' ', '_', trim(strtolower($this->name)));
  220. }
  221. static public function generate_sql($name)
  222. {
  223. if (strpos($name, 'http') === 0) {
  224. // $conds = array('artists.id IN (?)', self::where(['url' => $name])->pluck('id'));
  225. // $sql = self::where('true');
  226. if (!$ids = self::find_all_by_url($name)->getAttributes('id'))
  227. $ids = [0];
  228. $sql = self::where('artists.id IN (?)', $ids);
  229. } else {
  230. $sql = self::where(
  231. "(artists.name LIKE ? OR a2.name LIKE ?)",
  232. $name . "%",
  233. $name . "%")
  234. ->joins('LEFT JOIN artists a2 ON artists.alias_id = a2.id');
  235. }
  236. return $sql;
  237. }
  238. }