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

/baser/views/widget_areas/admin/form.php

https://github.com/hashing/basercms
PHP | 465 lines | 371 code | 44 blank | 50 comment | 23 complexity | 44760fba6c87df1b2cb771c091484807 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * [ADMIN] ウィジェットエリア編集
  5. *
  6. * PHP versions 5
  7. *
  8. * baserCMS : Based Website Development Project <http://basercms.net>
  9. * Copyright 2008 - 2011, baserCMS Users Community <http://sites.google.com/site/baserusers/>
  10. *
  11. * @copyright Copyright 2008 - 2011, baserCMS Users Community
  12. * @link http://basercms.net baserCMS Project
  13. * @package baser.views
  14. * @since baserCMS v 0.1.0
  15. * @version $Revision$
  16. * @modifiedby $LastChangedBy$
  17. * @lastmodified $Date$
  18. * @license http://basercms.net/license/index.html
  19. */
  20. ?>
  21. <div id="DelWidgetUrl" style="display:none"><?php $bcBaser->url(array('controller' => 'widget_areas', 'action' => 'del_widget', $bcForm->value('WidgetArea.id'))) ?></div>
  22. <div id="CurrentAction" style="display:none"><?php echo $this->action ?></div>
  23. <script type="text/javascript">
  24. $(window).load(function() {
  25. $("#WidgetAreaName").focus();
  26. });
  27. $(function() {
  28. var sortableOptions = {
  29. scroll: true,
  30. items: 'div.sortable',
  31. opacity: 0.80,
  32. zIndex: 55,
  33. containment: 'body',
  34. tolerance: 'intersect',
  35. distance: 5,
  36. cursor: 'move',
  37. placeholder: 'ui-widget-content placeholder',
  38. deactivate: function(event,ui){
  39. // 新しいウィジェットを追加しようとしてやめた場合
  40. // 再度追加する際に原因不明のエラーが連続で出現してしまうので、
  41. // 一旦リセットするようにした。
  42. $("#Target").sortable("destroy");
  43. $("#Target").sortable(sortableOptions
  44. ).droppable(
  45. {
  46. hoverClass: 'topDrop',
  47. accept: 'div.sortable',
  48. tolderance: 'intersect'
  49. });
  50. },
  51. update: function(event, ui){
  52. // jQueryUI 1.8.14 より、 ui.item.attr("id")で id が取得できない
  53. if($(ui.item.context).attr("id").match(/^Setting/i)){
  54. widgetAreaUpdateSortedIds();
  55. return;
  56. }
  57. var baseId = 0;
  58. $("#Target .setting").each(function () {
  59. var _baseId = parseInt($(this).attr('id').replace('Setting',''));
  60. if(_baseId > baseId){
  61. baseId = _baseId;
  62. }
  63. });
  64. baseId++;
  65. var id = $(ui.item.context).attr("id").replace('Widget','');
  66. var sourceId = id.replace('Widget','');
  67. var settingId = 'Setting' + (baseId);
  68. var tmpId = 'Tmp'+(baseId);
  69. /* ターゲットにテンプレートを追加 */
  70. ui.item.attr('id',tmpId);
  71. $("#"+tmpId).after($("#"+sourceId).clone().attr('id',settingId));
  72. $("#"+tmpId).remove();
  73. $("#"+settingId).addClass('setting');
  74. $("#"+settingId).removeClass('template');
  75. /* フィールドIDのリネーム */
  76. renameWidget(baseId);
  77. /* 値の設定 */
  78. var widgetname = $("#"+settingId+' .widget-name').html();
  79. $("#"+settingId+' .head').html($("#"+settingId+' .head').html()+$("#Target ."+widgetname).length);
  80. $("#WidgetId"+baseId).val(baseId);
  81. $("#WidgetName"+baseId).val($("#"+settingId+' .head').html());
  82. /* イベント登録 */
  83. registWidgetEvent(baseId);
  84. /* sortable をリフレッシュ */
  85. $("#Target").sortable("refresh");
  86. $("#"+settingId+" .content").slideDown('fast');
  87. /* ウィジェットを保存 */
  88. updateWidget(baseId);
  89. },
  90. activate: function(event, ui) {
  91. // ドラッグ時の幅を元の幅に合わせる
  92. $("#Source div:last").width(ui.item.width()-20);
  93. }
  94. };
  95. $("#Target").sortable(sortableOptions).droppable(
  96. {
  97. hoverClass: 'topDrop',
  98. accept: 'div.draggable',
  99. tolderance: 'intersect'
  100. });
  101. $("div.draggable").draggable(
  102. {
  103. scroll: true,
  104. helper: 'clone',
  105. opacity: 0.80,
  106. revert: 'invalid',
  107. cursor: 'move',
  108. connectToSortable: '#Target',
  109. containment: 'body'
  110. });
  111. $("#Target .sortable").each(function(k,v){
  112. registWidgetEvent($(this).attr('id').replace('Setting',''));
  113. });
  114. if($("#CurrentAction").html() == 'admin_edit') {
  115. $("#WidgetAreaUpdateTitleSubmit").click(function(){
  116. widgetAreaUpdateTitle();
  117. return false;
  118. });
  119. }
  120. });
  121. /**
  122. * ウィジェットごとにid/nameをリネームする
  123. */
  124. function renameWidget(baseId){
  125. var settingId = 'Setting'+baseId;
  126. $("#"+settingId+' .form').attr('id','WidgetUpdateWidgetForm'+baseId);
  127. $("#WidgetUpdateWidgetForm"+baseId).children().each(function(){
  128. if($(this).attr('id')){
  129. $(this).attr('id',$(this).attr('id')+baseId);
  130. }
  131. if($(this).attr('name')!=undefined){
  132. if($(this).attr('name').match(/data\[Widget\]/i)){
  133. $(this).attr('name',$(this).attr('name').replace('data[Widget]','data[Widget'+baseId+']'));
  134. }
  135. }
  136. });
  137. $("#"+settingId+" label[for=WidgetStatus]").attr('for','WidgetStatus'+baseId);
  138. }
  139. /**
  140. * ウィジェットイベントを登録
  141. */
  142. function registWidgetEvent(baseId){
  143. var settingId = 'Setting'+baseId;
  144. $("#WidgetUpdateWidgetSubmit"+baseId).click(function (){
  145. updateWidget(baseId);
  146. return false;
  147. });
  148. $("#"+settingId+" .action").click(function(){
  149. if($("#"+settingId+" .content").is(":hidden")){
  150. $("#"+settingId+" .content").slideDown('fast');
  151. }else{
  152. $("#"+settingId+" .content").slideUp('fast');
  153. }
  154. });
  155. $("#"+settingId+" .status").click(function(){
  156. if($("#"+settingId+" .status").attr('checked')){
  157. $("#"+settingId).addClass('enabled');
  158. }else{
  159. $("#"+settingId).removeClass('enabled');
  160. }
  161. });
  162. $("#"+settingId+" .del").click(function(){
  163. if(!confirm('設定内容も削除されますが、本当に削除してもいいですか?\n')){
  164. return;
  165. }
  166. delWidget(baseId);
  167. });
  168. }
  169. /**
  170. * ウィジェットを削除
  171. */
  172. function delWidget(id){
  173. $.ajax({
  174. url: $("#DelWidgetUrl").html()+'/'+id,
  175. type: 'GET',
  176. dataType: 'text',
  177. beforeSend: function() {
  178. $("#WidgetAreaUpdateSortLoader").show();
  179. $("#flashMessage").slideUp();
  180. },
  181. success: function(result){
  182. if(result != '1'){
  183. $("#flashMessage").html('ウィジェッの削除に失敗しました。');
  184. $("#flashMessage").slideDown();
  185. } else {
  186. $("#Setting"+id+"").slideUp(200, function(){
  187. $("#Setting"+id).remove();
  188. widgetAreaUpdateSortedIds();
  189. });
  190. }
  191. },
  192. error: function(){
  193. $("#flashMessage").html('ウィジェットの削除に失敗しました。');
  194. $("#flashMessage").slideDown();
  195. },
  196. complete: function(xhr, textStatus) {
  197. $("#WidgetAreaUpdateSortLoader").hide();
  198. }
  199. });
  200. }
  201. /**
  202. * 並び順を更新する
  203. */
  204. function widgetAreaUpdateSortedIds(){
  205. var ids = [];
  206. $("#Target .sortable").each(function(k,v){
  207. ids.push($(this).attr('id').replace('Setting',''));
  208. });
  209. $("#WidgetAreaSortedIds").val(ids.join(','));
  210. $.ajax({
  211. url: $("#WidgetAreaUpdateSortForm").attr('action'),
  212. type: 'POST',
  213. data: $("#WidgetAreaUpdateSortForm").serialize(),
  214. dataType: 'text',
  215. beforeSend: function() {
  216. $("#flashMessage").slideUp();
  217. $("#WidgetAreaUpdateSortLoader").show();
  218. },
  219. success: function(result){
  220. if(result != '1'){
  221. $("#flashMessage").html('ウィジェットエリアの並び替えの保存に失敗しました。');
  222. $("#flashMessage").slideDown();
  223. }
  224. },
  225. error: function(){
  226. $("#flashMessage").html('ウィジェットエリアの並び替えの保存に失敗しました。');
  227. $("#flashMessage").slideDown();
  228. },
  229. complete: function(xhr, textStatus) {
  230. $("#WidgetAreaUpdateSortLoader").hide();
  231. }
  232. });
  233. }
  234. /**
  235. * タイトルを更新する
  236. */
  237. function widgetAreaUpdateTitle(){
  238. $.ajax({
  239. url: $("#WidgetAreaUpdateTitleForm").attr('action'),
  240. type: 'POST',
  241. data: $("#WidgetAreaUpdateTitleForm").serialize(),
  242. dataType: 'text',
  243. beforeSend: function() {
  244. $("#WidgetAreaUpdateTitleSubmit").attr('disabled', 'disabled');
  245. $("#flashMessage").slideUp();
  246. $("#WidgetAreaUpdateTitleLoader").show();
  247. },
  248. success: function(result){
  249. if(result){
  250. $("#flashMessage").html('ウィジェットエリア名を保存しました。');
  251. }else{
  252. $("#flashMessage").html('ウィジェットエリア名の保存に失敗しました。');
  253. }
  254. $("#flashMessage").slideDown();
  255. },
  256. error: function(){
  257. $("#flashMessage").html('ウィジェットエリア名の保存に失敗しました。');
  258. $("#flashMessage").slideDown();
  259. },
  260. complete: function(xhr, textStatus) {
  261. $("#WidgetAreaUpdateTitleSubmit").removeAttr('disabled');
  262. $("#WidgetAreaUpdateTitleLoader").hide();
  263. }
  264. });
  265. }
  266. /**
  267. * ウィジェットを更新する
  268. */
  269. function updateWidget(id) {
  270. $.ajax({
  271. url: $("#WidgetUpdateWidgetForm"+id).attr('action'),
  272. type: 'POST',
  273. data: $("#WidgetUpdateWidgetForm"+id).serialize(),
  274. dataType: 'text',
  275. beforeSend: function() {
  276. $("#WidgetUpdateWidgetSubmit"+id).attr('disabled', 'disabled');
  277. $("#WidgetUpdateWidgetLoader"+id).show();
  278. $("#flashMessage").slideUp();
  279. },
  280. success: function(result){
  281. if(result != '1'){
  282. $("#flashMessage").html('ウィジェッの保存に失敗しました。');
  283. $("#flashMessage").slideDown();
  284. }else{
  285. $("#Setting"+id+' .head').html($("#Setting"+id+' .name').val());
  286. }
  287. },
  288. error: function(){
  289. $("#flashMessage").html('ウィジェットの保存に失敗しました。');
  290. $("#flashMessage").slideDown();
  291. },
  292. complete: function(xhr, textStatus) {
  293. $("#WidgetUpdateWidgetSubmit"+id).removeAttr('disabled');
  294. $("#WidgetUpdateWidgetLoader"+id).hide();
  295. widgetAreaUpdateSortedIds();
  296. }
  297. });
  298. }
  299. </script>
  300. <?php if($this->action == 'admin_add'): ?>
  301. <?php echo $bcForm->create('WidgetArea', array('url' => array('action' => 'add'))) ?>
  302. <?php elseif($this->action == 'admin_edit'): ?>
  303. <?php echo $bcForm->create('WidgetArea', array('action' => 'update_title', 'url' => array('action' => 'update_title', 'id' => false))) ?>
  304. <?php endif ?>
  305. <?php echo $bcForm->hidden('WidgetArea.id') ?>
  306. <?php echo $bcForm->label('WidgetArea.name', 'ウィジェットエリア名') ?>&nbsp;
  307. <?php echo $bcForm->input('WidgetArea.name', array('type' => 'text', 'size' => 40)) ?>&nbsp;
  308. <span class="submit"><?php echo $bcForm->end(array('label' => 'エリア名を保存する', 'div' => false, 'class' => 'button btn-red', 'id' => 'WidgetAreaUpdateTitleSubmit')) ?></span>
  309. <?php $bcBaser->img('ajax-loader-s.gif', array('style' => 'vertical-align:middle;display:none', 'id' => 'WidgetAreaUpdateTitleLoader')) ?>
  310. <?php echo $bcForm->error('WidgetArea.name') ?>
  311. <?php if(!empty($widgetInfos)): ?>
  312. <?php echo $bcForm->create('WidgetArea', array('action' => 'update_sort', 'url' => array('action' => 'update_sort', $bcForm->value('WidgetArea.id'), 'id' => false))) ?>
  313. <?php echo $bcForm->input('WidgetArea.sorted_ids', array('type' => 'hidden')) ?>
  314. <?php echo $bcForm->end() ?>
  315. <div id="WidgetSetting" class="clearfix" >
  316. <!-- 利用できるウィジェット -->
  317. <div id="SourceOuter">
  318. <div id="Source">
  319. <h2>利用できるウィジェット</h2>
  320. <?php foreach($widgetInfos as $widgetInfo) : ?>
  321. <h3><?php echo $widgetInfo['title'] ?></h3>
  322. <?php
  323. $widgets = array();
  324. foreach($widgetInfo['paths'] as $path){
  325. $Folder = new Folder($path);
  326. $files = $Folder->read(true,true,true);
  327. $widgets = array();
  328. foreach($files[1] as $file) {
  329. $widget = array('name'=>'','title'=>'','description'=>'','setting'=>'');
  330. ob_start();
  331. $key='Widget';
  332. // タイトルや説明文を取得する為、elementを使わず、includeする。
  333. // コントローラーでインクルードした場合、コントローラー内でヘルパ等が読み込まれていないのが原因で
  334. // エラーとなるのでここで読み込む
  335. include $file;
  336. $widget['name'] = basename($file, $this->ext);
  337. $widget['title'] = $title;
  338. $widget['description'] = $description;
  339. $widget['setting'] = ob_get_contents();
  340. $widgets[] = $widget;
  341. ob_end_clean();
  342. }
  343. }
  344. ?>
  345. <?php foreach($widgets as $widget): ?>
  346. <div class="ui-widget-content draggable widget" id="Widget<?php echo Inflector::camelize($widget['name']) ?>">
  347. <div class="head"><?php echo $widget['title'] ?></div>
  348. </div>
  349. <div class="description"><?php echo $widget['description'] ?></div>
  350. <div class="ui-widget-content sortable widget template <?php echo $widget['name'] ?>" id="<?php echo Inflector::camelize($widget['name']) ?>">
  351. <div class="clearfix">
  352. <div class="widget-name display-none"><?php echo $widget['name'] ?></div>
  353. <div class="del">削除</div>
  354. <div class="action">設定</div>
  355. <div class="head"><?php echo $widget['title'] ?></div>
  356. </div>
  357. <div class="content" style="text-align:right">
  358. <p class="widget-name"><small><?php echo $widget['title'] ?></small></p>
  359. <?php echo $bcForm->create('Widget', array('url' => array('controller' => 'widget_areas', 'action'=> 'update_widget', $bcForm->value('WidgetArea.id')), 'class' => 'form')) ?>
  360. <?php echo $bcForm->input('Widget.id', array('type' => 'hidden', 'class'=>'id')) ?>
  361. <?php echo $bcForm->input('Widget.type', array('type' => 'hidden', 'value' => $widget['title'])) ?>
  362. <?php echo $bcForm->input('Widget.element', array('type' => 'hidden', 'value' => $widget['name'])) ?>
  363. <?php echo $bcForm->input('Widget.plugin',array('type' => 'hidden', 'value' => $widgetInfo['plugin'])) ?>
  364. <?php echo $bcForm->input('Widget.sort', array('type' => 'hidden')) ?>
  365. <?php echo $bcForm->label('Widget.name','タイトル') ?>&nbsp;
  366. <?php echo $bcForm->input('Widget.name', array('type' => 'text', 'class' => 'name')) ?><br />
  367. <?php echo $widget['setting'] ?><br />
  368. <?php $bcBaser->img('ajax-loader-s.gif', array('style' => 'vertical-align:middle;display:none', 'id' => 'WidgetUpdateWidgetLoader', 'class' => 'loader')) ?>
  369. <?php echo $bcForm->input('Widget.use_title', array('type' => 'checkbox', 'label' => 'タイトルを表示', 'class' => 'use_title', 'checked' => 'checked')) ?>
  370. <?php echo $bcForm->input('Widget.status', array('type' => 'checkbox', 'label' => '利用する', 'class' => 'status')) ?>
  371. <?php echo $bcForm->end(array('label' => '保存', 'div' => false, 'id' => 'WidgetUpdateWidgetSubmit', 'class' => 'button')) ?>
  372. </div>
  373. </div>
  374. <?php endforeach ?>
  375. <?php endforeach ?>
  376. </div>
  377. </div>
  378. <!-- 利用中のウィジェット -->
  379. <div id="TargetOuter">
  380. <div id="Target">
  381. <h2>利用中のウィジェット <?php $bcBaser->img('ajax-loader-s.gif', array(
  382. 'style' => 'vertical-align:middle;display:none',
  383. 'id' => 'WidgetAreaUpdateSortLoader',
  384. 'class' => 'loader')) ?></h2>
  385. <?php if($bcForm->value('WidgetArea.widgets')): ?>
  386. <?php foreach($bcForm->value('WidgetArea.widgets') as $widget): ?>
  387. <?php $key = key($widget) ?>
  388. <?php $enabled = '' ?>
  389. <?php if($widget[$key]['status']): ?>
  390. <?php $enabled = ' enabled' ?>
  391. <?php endif ?>
  392. <div class="ui-widget-content sortable widget setting <?php echo $widget[$key]['element'] ?><?php echo $enabled ?>" id="Setting<?php echo $widget[$key]['id'] ?>">
  393. <div class="clearfix">
  394. <div class="widget-name display-none"><?php echo $widget[$key]['element'] ?></div>
  395. <div class="del">削除</div>
  396. <div class="action">設定</div>
  397. <div class="head"><?php echo $widget[$key]['name'] ?></div>
  398. </div>
  399. <div class="content" style="text-align:right">
  400. <p><small><?php echo $widget[$key]['type'] ?></small></p>
  401. <?php echo $bcForm->create('Widget', array('url' => array('controller' => 'widget_areas', 'action' => 'update_widget', $bcForm->value('WidgetArea.id'), 'id' => false), 'class' => 'form', 'id' => 'WidgetUpdateWidgetForm'.$widget[$key]['id'])) ?>
  402. <?php echo $bcForm->input($key.'.id', array('type' => 'hidden', 'class' => 'id')) ?>
  403. <?php echo $bcForm->input($key.'.type', array('type' => 'hidden')) ?>
  404. <?php echo $bcForm->input($key.'.element', array('type' => 'hidden')) ?>
  405. <?php echo $bcForm->input($key.'.plugin', array('type' => 'hidden')) ?>
  406. <?php echo $bcForm->input($key.'.sort', array('type' => 'hidden')) ?>
  407. <?php echo $bcForm->label($key.'name','タイトル') ?>&nbsp;
  408. <?php echo $bcForm->input($key.'.name', array('type' => 'text', 'class'=>'name')) ?><br />
  409. <?php $bcBaser->element('widgets/'.$widget[$key]['element'], array('key' => $key, 'plugin' => $widget[$key]['plugin'], 'mode' => 'edit')) ?><br />
  410. <?php $bcBaser->img('ajax-loader-s.gif', array('style' => 'vertical-align:middle;display:none', 'id' => 'WidgetUpdateWidgetLoader'.$widget[$key]['id'], 'class' => 'loader')) ?>
  411. <?php echo $bcForm->input($key.'.use_title', array('type' => 'checkbox', 'label' => 'タイトルを表示', 'class' => 'use_title')) ?>
  412. <?php echo $bcForm->input($key.'.status',array('type' => 'checkbox', 'label' => '利用する', 'class' => 'status')) ?>
  413. <?php echo $bcForm->end(array('label' => '保存', 'div' => false, 'id' => 'WidgetUpdateWidgetSubmit'.$widget[$key]['id'], 'class' => 'button')) ?>
  414. </div>
  415. </div>
  416. <?php endforeach ?>
  417. <?php endif ?>
  418. </div>
  419. </div>
  420. </div>
  421. <?php endif ?>