PageRenderTime 51ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/views/post/import.php

https://code.google.com/
PHP | 337 lines | 276 code | 60 blank | 1 comment | 35 complexity | de9234db0fe05a91b1c23db161144765 MD5 | raw file
  1. <?php if (Request::$post) : ?>
  2. <?php if (!$errors) : ?>
  3. <?php echo print_preview($post, array('display' => 'large', 'disable_jpeg_direct_links' => true, 'hide_directlink' => true)) ?>
  4. <?php ;else: ?>
  5. <li style="width:160px;text-align:center">
  6. There was an error with<br />
  7. "<em><?php echo substr(Request::$params->post['filename'], 0, 50); strlen(Request::$params->post['filename']) > 50 && print '...' ?></em>":
  8. <h6><?php echo $errors ?></h6>
  9. <a class="directlink largeimg" href="#">
  10. <span class="directlink-info"><img class="directlink-icon directlink-icon-large" src="/images/ddl_large.gif" alt=""></span>
  11. <span class="directlink-res">Error</span>
  12. </a>
  13. </li>
  14. <?php endif ?>
  15. <div id="script<?php echo Request::$params->post['i'] ?>" style="display:none">
  16. Import.set_status(<?php echo Request::$params->post['i'] ?>, '<?php echo $status ?>');
  17. <?php if ($status != 'Posted') : ?>Import.error_count();<?php endif ?>
  18. <?php if (!$errors) : ?>Import.ids.push(<?php echo $post->id ?>);<?php endif ?>
  19. <?php if ($dupe) : ?>Import.dupes.push(<?php echo Request::$params->post['i'] ?>);<?php endif ?>
  20. </div>
  21. <?php return; endif ?>
  22. <h2>Import</h2>
  23. <p id="description">This will move files from the /public/data/import directory.<br />
  24. Data entered here will be applied to all posts.</p>
  25. <div style="margin-bottom:10px;">
  26. <span id="file-count">Files found: <?php echo count($files) ?></span> / Errors: <span id="error-count"><?php echo count($invalid_files) ?></span>
  27. <a href="#" id="details-toggle" onclick="$('details-container').toggle();return false;">(Show/hide details)</a>
  28. <div id="details-container" style="border:1px solid #ccc;padding:3px;width:910px;display:none">
  29. <table class="form" id="posts-details" style="display:none">
  30. <tbody>
  31. <tr>
  32. <th style="width:75px;"><label for="post_source">Source</label></th>
  33. <td id="detail-source"></td>
  34. </tr>
  35. <tr>
  36. <th><label for="post_pool" style="display:block">Pool</label></th>
  37. <td id="detail-pool"></td>
  38. </tr>
  39. <tr>
  40. <th><label for="post_tags">Tags</label></th>
  41. <td id="detail-tags"></td>
  42. </tr>
  43. <tr>
  44. <th><label for="post_rating_questionable">Rating</label></th>
  45. <td id="detail-rating"></td>
  46. </tr>
  47. </tbody>
  48. </table>
  49. <table id="errors-container" class="form" style="margin-bottom:0px">
  50. <tr class="import-thead">
  51. <th style="text-align:left;">File</th>
  52. <th style="width:100px;text-align:left;">Status</th>
  53. </tr>
  54. <?php if ($invalid_files):foreach (range(0, count($invalid_files) - 1) as $i): ?>
  55. <tr id="e<?php echo $invalid_files[$i] ?>" class="<?php echo cycle('even', 'odd') ?>">
  56. <td><?php echo substr($invalid_files[$i], 0, 105) ?></td>
  57. <td>Invalid filename</td>
  58. </tr>
  59. <?php endforeach;endif ?>
  60. <?php if ($files):foreach(range(0, count($files) - 1) as $i) : ?>
  61. <tr id="file<?php echo $i ?>" class="<?php echo cycle('even', 'odd') ?>">
  62. <td><?php echo substr($files[$i], 0, 100); strlen($files[$i]) > 100 && print '...' ?></td>
  63. <td>Waiting</td>
  64. </tr>
  65. <?php endforeach;endif ?>
  66. </table>
  67. <div id="delete-dupes" style="display:none">
  68. <a id="delete-dupes-link" href="#">Delete dupes</a>
  69. </div>
  70. </div>
  71. </div>
  72. <form action="" id="data-form">
  73. <div id="posts">
  74. <table class="form">
  75. <tfoot>
  76. <tr>
  77. <td></td>
  78. <td>
  79. <input accesskey="s" class="submit" name="start" style="margin: 0;" tabindex="7" type="submit" value="Start" />
  80. </td>
  81. </tr>
  82. </tfoot>
  83. <tbody>
  84. <tr>
  85. <th><label for="post_source">Source</label></th>
  86. <td>
  87. <input id="post_source" name="post[source]" size="50" tabindex="1" type="text" value="" />
  88. </td>
  89. </tr>
  90. <tr>
  91. <th><label for="post_pool" style="display:block">Pool</label></th>
  92. <td>
  93. <input id="post_pool" type="text" list="pool_list" size="50" tabindex="2" name="post[pool]" value="" />
  94. <?php echo $pool_list ?>
  95. </td>
  96. </tr>
  97. <tr>
  98. <th><label for="post_tags">Tags</label></th>
  99. <td>
  100. <textarea cols="60" id="post_tags" name="post[tags]" rows="2" tabindex="3"></textarea>
  101. </td>
  102. </tr>
  103. <tr>
  104. <th><label for="post_rating_questionable">Rating</label></th>
  105. <td>
  106. <input id="post_rating_explicit" name="post[rating]" type="radio" value="e" tabindex="4"<?php echo $chkbox_e ?>>
  107. <label for="post_rating_explicit">Explicit</label>
  108. <input id="post_rating_questionable" name="post[rating]" type="radio" value="q" tabindex="5"<?php echo $chkbox_q ?>>
  109. <label for="post_rating_questionable">Questionable</label>
  110. <input id="post_rating_safe" name="post[rating]" type="radio" value="s" tabindex="6"<?php echo $chkbox_s ?>>
  111. <label for="post_rating_safe">Safe</label>
  112. </td>
  113. </tr>
  114. </tbody>
  115. </table>
  116. <div id="related"><em>None</em></div>
  117. </div>
  118. </form>
  119. <div id="post-list">
  120. <ul id="post-list-posts"></ul>
  121. </div>
  122. <?php //render_partial('hover') ?>
  123. <script>RelatedTags.init(Cookie.get('my_tags'), '')</script>
  124. <script>
  125. files = [<?php if ($files) echo "'" . implode("', '", $files) . "'" ?>]
  126. url = '/post/import'
  127. Import = {
  128. importing : 0,
  129. busy : false,
  130. file_status : null,
  131. dupes : [],
  132. ids : [],
  133. pool : null,
  134. e_count : <?php echo count($invalid_files) ?>,
  135. start: function() {
  136. this.busy = true
  137. $('description').hide()
  138. $('posts-details').show()
  139. rating = this.get_rating()
  140. this.post_data = {
  141. 'post[tags]' : $('post_tags').value,
  142. 'post[source]': $('post_source').value,
  143. 'post[rating]': rating
  144. }
  145. $('detail-source').innerHTML = $('post_source').value || '<em>None</em>'
  146. $('detail-pool').innerHTML = $('post_pool').value || '<em>None</em>'
  147. $('detail-tags').innerHTML = $('post_tags').value || '<em>None</em>'
  148. $('detail-rating').innerHTML = this.get_rating(true)
  149. if ($('post_pool').value)
  150. this.pool = $('post_pool').value.toLowerCase().replace(/ /g, '_')
  151. // alert(this.pool)
  152. $('data-form').remove()
  153. this.send_request()
  154. },
  155. send_request: function(){
  156. i = this.importing
  157. post = this.post_data
  158. if (i == (files.length))
  159. return;
  160. $('file-count').innerHTML = 'Converting ' + (i+1) + '/' + files.length
  161. this.set_status(i, 'Importing')
  162. file = files[i]
  163. post['post[filename]'] = file
  164. post['post[i]'] = i
  165. new Ajax.Updater('post-list-posts', url, {
  166. parameters: post,
  167. method:'post',
  168. insertion: Insertion.Bottom,
  169. onSuccess: function(){
  170. Import.delayed(i)
  171. Import.send_request()
  172. }
  173. })
  174. Import.importing++
  175. },
  176. delayed: function(i){
  177. setTimeout(function(){
  178. scriptid = 'script'
  179. scriptid = scriptid + i
  180. eval($(scriptid).innerHTML)
  181. if (files.length - 1 == i) {
  182. Import.add_to_pool()
  183. }
  184. }, 100)
  185. },
  186. get_rating: function(proper) {
  187. if ($('post_rating_explicit').checked)
  188. return proper ? 'Explicit' : 'e';
  189. else if ($('post_rating_questionable').checked)
  190. return proper ? 'Questionable' : 'q';
  191. else
  192. return proper ? 'Safe' : 's';
  193. },
  194. set_status: function(i, status) {
  195. fileid = 'file'
  196. fileid = fileid + i
  197. children = $(fileid).childElements()
  198. children[1].innerHTML = status
  199. },
  200. error_count: function() {
  201. this.e_count++;
  202. $('error-count').innerHTML = this.e_count
  203. },
  204. add_to_pool: function(){
  205. if (!this.pool) {
  206. this.finished()
  207. return
  208. }
  209. params = {}
  210. for (i = 0; i < this.ids.length; i++) {
  211. params['post[' + i + '][tags]'] = 'pool:' + this.pool
  212. params['post[' + i + '][id]'] = this.ids[i]
  213. }
  214. notice('Adding files to pool...')
  215. new Ajax.Request('/post/update_batch',{
  216. parameters:params,
  217. onSuccess: function(){
  218. Import.finished()
  219. }
  220. })
  221. },
  222. finished: function() {
  223. this.busy = false
  224. $('file-count').innerHTML = 'All files processed'
  225. $('file-count').setStyle({fontWeight:'bold'})
  226. notice('All files processed')
  227. Cookie.remove('notice')
  228. this.dupes_setup()
  229. },
  230. dupes_setup: function(){
  231. if (!this.dupes.length)
  232. return;
  233. $('delete-dupes').show()
  234. },
  235. dupes_delete: function() {
  236. dupes = []
  237. for (i = 0; i < this.dupes.length; i++)
  238. dupes.push(files[this.dupes[i]])
  239. dupes = dupes.join('::')
  240. new Ajax.Request('/post/import.json', {
  241. parameters: {dupes:dupes},
  242. onSuccess: function(resp){
  243. resp = resp.responseJSON
  244. if (resp.success) {
  245. notice('Duped files deleted')
  246. $('delete-dupes-link').replace('<h6>Duped files deleted</h6>')
  247. } else {
  248. notice(resp.reason)
  249. $('delete-dupes-link').replace('<h6>There was an error deleting files</h6>')
  250. }
  251. },
  252. onFailure: function(r){alert(r.responseText)}
  253. })
  254. }
  255. }
  256. $('delete-dupes-link').observe('click', function(e){
  257. Event.stop(e)
  258. if (!confirm('Really delete dupe files?'))
  259. return;
  260. Import.dupes_delete()
  261. })
  262. $('data-form').observe('submit', function(e){
  263. Event.stop(e);
  264. if (!files.length) {
  265. notice('No files found');
  266. } else {
  267. Import.start()
  268. }
  269. });
  270. document.observe('click', function(e, el) {
  271. if (el = e.findElement('#post-list-posts li a')) {
  272. el.writeAttribute('target','_blank');
  273. } else if (el = e.findElement('a') && Import.busy) {
  274. e.stop()
  275. }
  276. });
  277. </script>