commit b85ed4803622fa7c049a09aec98a2fdf5c9cf8d1
Author: Werner Hahn <wh@futureworldsearch.net>
Date:   Mon Apr 11 19:11:02 2016 +0200

    Shopmodul: ShopPart Bilder und Kategorien
    
    Bilder werden als Tabelle angezeigt
    
    Conflicts:
            templates/webpages/ic/tabs/_shop.html
    
    Shopparts: Bilder werden angezeigt TODO Bilder löschen
    Shopkategorien werden aus der Api/Shop geholt und angezeigt zum markieren
    
    Conflicts:
            SL/Controller/ShopPart.pm
            SL/ShopConnector/Shopware.pm
            js/kivi.shop_part.js

diff --git a/SL/Controller/FileUploader.pm b/SL/Controller/FileUploader.pm
index 68f69e99a..35e563762 100644
--- a/SL/Controller/FileUploader.pm
+++ b/SL/Controller/FileUploader.pm
@@ -29,6 +29,9 @@ sub action_upload_form{
 
 sub action_show_files {
   my ($self) = @_;
+$main::lxdebug->dump(0, 'WH: Show_Files',\$::form);
+  my $images = SL::DB::Manager::File->get_all( query => [ trans_id => $::form->{id}, modul => $::form->{modul} ] );
+  $main::lxdebug->dump(0, 'WH: ',\$images);
 
 
 }
diff --git a/SL/Controller/ShopPart.pm b/SL/Controller/ShopPart.pm
index 60c570079..eb85d80b7 100644
--- a/SL/Controller/ShopPart.pm
+++ b/SL/Controller/ShopPart.pm
@@ -8,6 +8,7 @@ use parent qw(SL::Controller::Base);
 use Data::Dumper;
 use SL::Locale::String qw(t8);
 use SL::DB::ShopPart;
+use SL::DB::Default;
 use SL::Helper::Flash;
 
 use Rose::Object::MakeMethods::Generic
@@ -53,7 +54,44 @@ sub action_update_shop {
 
 };
 
+sub action_show_files {
+  my ($self) = @_;
+$main::lxdebug->dump(0, 'WH: Show_Files',\$::form);
+  require SL::DB::File;
+  my $images = SL::DB::Manager::File->get_all_sorted( where => [ trans_id => $::form->{id}, modul => $::form->{modul}, file_content_type => { like => 'image/%' } ], sort_by => 'position' );
+  $main::lxdebug->dump(0, 'WH: ',\$images);
+  #my $html = $self->render('shop_part/_list_images', { output => 0 }, IMAGES => $images);
+  $self->render('shop_part/_list_images', { header => 0 }, IMAGES => $images);
+  #$main::lxdebug->dump(0, 'WH: ',\$html);
+
+  #$self->js->html('#shop_images', $html);
+  #$self->js->render;
+
+  # $self->render('customer_vendor_turnover/count_open_items_by_year', { layout => 0 });
+
+
+}
+
+sub action_get_categories {
+  my ($self) = @_;
+
+  my $shop_part = SL::DB::Manager::ShopPart->find_by(id => $::form->{shop_part_id});
+  die unless $shop_part;
+  require SL::Shop;
+  my $shop = SL::Shop->new( config => $shop_part->shop );
+  my $categories = $shop->connector->get_categories;
+  $main::lxdebug->dump(0, 'WH: GET',\$categories);
 
+  $self->js
+    ->run(
+      'kivi.shop_part.shop_part_dialog',
+      t8('Shopcategories'),
+      $self->render('shop_part/categories', { output => 0 }, CATEGORIES => $categories ) #, shop_part => $self->shop_part)
+    )
+    ->reinit_widgets;
+
+  $self->js->render;
+}
 
 # old:
 # sub action_edit {
@@ -107,9 +145,31 @@ sub render_shop_part_edit_dialog {
   $self->js->render;
 }
 
-sub action_save_categories {
+sub render_shop_categories_edit_dialog {
   my ($self) = @_;
 
+  # when self->shop_part is called in template, it will be an existing shop_part with id,
+  # or a new shop_part with only part_id and shop_id set
+  $self->js
+    ->run(
+      'kivi.shop_part.shop_part_dialog',
+      t8('Shopcategories'),
+      $self->render('shop_part/categories', { output => 0 }) #, shop_part => $self->shop_part)
+    )
+    ->reinit_widgets;
+
+  $self->js->render;
+}
+sub action_reorder {
+  my ($self) = @_;
+$main::lxdebug->message(0, "WH:REORDER ");
+  require SL::DB::File;
+  SL::DB::File->reorder_list(@{ $::form->{image_id} || [] });
+  $main::lxdebug->message(0, "WH:REORDER II ");
+
+  $self->render(\'', { type => 'json' });
+}
+
 #
 # internal stuff
 #
diff --git a/SL/DB/ShopPart.pm b/SL/DB/ShopPart.pm
index 084b003c6..ee82732c8 100644
--- a/SL/DB/ShopPart.pm
+++ b/SL/DB/ShopPart.pm
@@ -4,11 +4,12 @@
 package SL::DB::ShopPart;
 #package SL::DB::ShopPart;
 
-use SL::DB::Helper::AttrHTML;
 use strict;
 
 use SL::DB::MetaSetup::ShopPart;
 use SL::DB::Manager::ShopPart;
+use SL::DB::Helper::AttrHTML;
+use SL::DB::Helper::ActsAsList;
 
 __PACKAGE__->meta->initialize;
 __PACKAGE__->attr_html('shop_description');
diff --git a/SL/ShopConnector/Base.pm b/SL/ShopConnector/Base.pm
index 5b1cca421..e6259abd3 100644
--- a/SL/ShopConnector/Base.pm
+++ b/SL/ShopConnector/Base.pm
@@ -13,6 +13,7 @@ sub get_new_orders { die 'get_order needs to be implemented' }
 
 sub update_part { die 'update_part needs to be implemented' }
 
+sub get_categories { die 'get_order needs to be implemented' }
 1;
 
 __END__
diff --git a/SL/ShopConnector/Shopware.pm b/SL/ShopConnector/Shopware.pm
index 3d12e9e50..732f4e0e4 100644
--- a/SL/ShopConnector/Shopware.pm
+++ b/SL/ShopConnector/Shopware.pm
@@ -150,6 +150,35 @@ sub get_new_orders {
   # return $import;
 };
 
+sub get_categories {
+  my ($self) = @_;
+
+  my $url = $self->url;
+
+  my $data = $self->connector->get("http://$url/api/categories");
+  my $data_json = $data->content;
+  my $import = SL::JSON::decode_json($data_json);
+  my @daten = @{$import->{data}};
+  my %categories = map { ($_->{id} => $_) } @daten;
+
+  for(@daten) {
+    my $parent = $categories{$_->{parentId}};
+    $parent->{children} ||= [];
+    push @{$parent->{children}},$_;
+  }
+
+  return \@daten;
+}
+
+sub get_article {
+}
+
+sub get_articles {
+}
+
+sub set_article {
+}
+
 sub init_url {
   my ($self) = @_;
   # TODO: validate url and port
diff --git a/js/kivi.shop_part.js b/js/kivi.shop_part.js
index 2367550b1..3b775a923 100644
--- a/js/kivi.shop_part.js
+++ b/js/kivi.shop_part.js
@@ -2,7 +2,7 @@
 namespace('kivi.shop_part', function(ns) {
   var $dialog;
 
-  // this is called by sub render, with a certain prerendered html (edit.html)
+  // this is called by sub render, with a certain prerendered html (edit.html,categories.html)
   ns.shop_part_dialog = function(title, html) {
     var id            = 'jqueryui_popup_dialog';
     var dialog_params = {
diff --git a/templates/webpages/ic/tabs/_shop.html b/templates/webpages/ic/tabs/_shop.html
index bb4b16a07..ee623b75f 100644
--- a/templates/webpages/ic/tabs/_shop.html
+++ b/templates/webpages/ic/tabs/_shop.html
@@ -102,6 +102,7 @@
    <td>[% L.html_tag('span', shop_part.last_update.to_kivitendo('precision' => 'minute'), id => 'shop_part_last_update_' _ shop_part.id ) %]</td>
    <td>[% L.button_tag("kivi.shop_part.edit_shop_part(" _ shop_part.id _ ")", LxERP.t8("Edit"))  %]</td>
    <td>[% L.button_tag("kivi.shop_part.update_shop_part(" _ shop_part.id _ ")", LxERP.t8("Update"))  %]</td>
+   <td>[% L.button_tag("kivi.shop_part.get_all_categories(" _ shop_part.id _ ")", LxERP.t8("Shopcategories"))  %]</td>
   </tr>
   [%- END %]
   [%- FOREACH shop = SHOPS_NOT_ASSIGNED %]
@@ -137,7 +138,17 @@
    </p>
   </div>
   [%- END %]
-  <div id="shop_images">
+  <script type="text/javascript">
+    function show_images() {
+      var url = 'controller.pl?action=ShopPart/show_files&modul=shop_part&id=[% shop_part.part_id %]';
+      $('#shop_images').load(url);
+    }
+
+$(window).load(function() {
+        show_images();
+});
+  </script>
+  <div id="shop_images" border=1 >
   </div>
 </div>
 
diff --git a/templates/webpages/shop_part/_list_images.html b/templates/webpages/shop_part/_list_images.html
new file mode 100644
index 000000000..532e3680f
--- /dev/null
+++ b/templates/webpages/shop_part/_list_images.html
@@ -0,0 +1,52 @@
+[%- USE HTML %][%- USE L -%][%- USE P -%][%- USE LxERP -%]
+[%- USE T8 %][% USE Base64 %]
+[%- USE Dumper %]
+<table width="100%" id="images_list">
+  <thead>
+    <tr class="listheading">
+      <th width="10px"><img src="image/updown.png" alt="[%- LxERP.t8('reorder item') %]" class="dragdrop"></th>
+      <th width="70px"></th>
+      <th>[% 'Title' | $T8 %]</th>
+      <th>[% 'Description' | $T8 %]</th>
+      <th>[% 'Filename' | $T8 %]</th>
+      <th>[% 'Orig. Size w/h' | $T8 %]</th>
+    </tr>
+  </thead>
+  <tbody>
+   [%-  FOREACH img = IMAGES %]
+    <tr class="listrow" id="image_id_[%  img.id %]">
+      <td><img src="image/updown.png" alt="[%- LxERP.t8('reorder item') %]" class="dragdrop"></td>
+      <td width="70px"><img src="data:[%  img.thumbnail_img_content_type %];base64,[%  img.thumbnail_img_content.encode_base64 %]" alt="[%  img.title %]"></td>
+      <td>[%  HTML.escape(img.title) %]</td>
+      <td>[%  HTML.escape(img.description) %]</td>
+      <td>[%  HTML.escape(img.filename) %]</td>
+      <td>[%  HTML.escape(img.thumbnail_img_width) _  ' x ' _ HTML.escape(img.thumbnail_img_height) %]</td>
+    </tr>
+   [%  END %]
+  </tbody>
+</table>
+
+[% L.sortable_element('#images_list tbody', url=SELF.url_for(action='reorder'), with='image_id') %]
+<p>
+[% L.button_tag("add_file(this.form.id.value,'shop_part')", 'Fileupload') %]
+</p>
+
+
+
+
+<script type='text/javascript'>
+<!--
+function add_file(id,modul) {
+  //var id = this.$('#part').val();
+  kivi.popup_dialog({
+        url :           'controller.pl?action=FileUploader/ajax_add_file',
+        data:           'id=' + id + '&modul=' + modul,
+        dialog:         { title: kivi.t8('File upload') }
+  } );
+   // var url = 'controller.pl?action=ShopPart/show_files?modul=shop_part&id=' + $('#id').val();
+   // $('#images_list').load(url);
+    //$('#jqueryui_popup_dialog').dialog('close');
+  return true;
+}
+-->
+</script>
diff --git a/templates/webpages/shop_part/edit.html b/templates/webpages/shop_part/edit.html
index fbac62179..0929bc77b 100644
--- a/templates/webpages/shop_part/edit.html
+++ b/templates/webpages/shop_part/edit.html
@@ -8,60 +8,63 @@
 [% LxERP.t8("Shop") %]: [% HTML.escape(SELF.shop_part.shop.description) %]
 
 <form action="controller.pl" method="post">
+  <div>
+    [% IF SELF.shop_part.id %]
+    [%- L.hidden_tag("shop_part.id", SELF.shop_part.id) %]
+    [%- L.hidden_tag("shop_part.shop_id", SELF.shop_part.shop_id) %]
+    [% ELSE %]
+    [%- L.hidden_tag("shop_part.shop_id", FORM.shop_id) %]
+    [%- L.hidden_tag("shop_part.part_id", FORM.part_id) %]
+    [% END %]
 
-[% IF SELF.shop_part.id %]
-[%- L.hidden_tag("shop_part.id", SELF.shop_part.id) %]
-[% ELSE %]
-[%- L.hidden_tag("shop_part.shop_id", FORM.shop_id) %]
-[%- L.hidden_tag("shop_part.part_id", FORM.part_id) %]
-[% END %]
+    <table>
+    <tr>
+     <td>[% LxERP.t8("Description") %]</td>
+     <td>[% L.textarea_tag('shop_part.shop_description_as_restricted_html', SELF.shop_part.shop_description_as_restricted_html, style='width:100%', rows=20, class="texteditor") %]</td>
+    </tr>
+    <tr>
+     <td>[% LxERP.t8("Active") %]</td>
+     <td>[% L.yes_no_tag("shop_part.active", SELF.shop_part.active) %]</td>
+    </tr>
+    <tr>
+     <td>[% LxERP.t8("Sort order") %]</td>
+     <td>[% L.input_tag("shop_part.sortorder", SELF.shop_part.sortorder, size=2) %]</td>
+    </tr>
+    <tr>
+     <td>[% LxERP.t8("Date") %]</td>
+     <td>[% L.date_tag("shop_part.show_date", SELF.shop_part.show_date) %]</td>
+    </tr>
+    <tr>
+     <td>[% LxERP.t8("Front page") %]</td>
+     <td>[% L.yes_no_tag('shop_part.front_page', SELF.shop_part.front_page) %]</td>
+    </tr>
+    <tr>
+     <td>[% LxERP.t8("Meta Tags") %]</td>
+     <td>[% L.input_tag("shop_part.meta_tags", SELF.shop_part.meta_tags, size=2) %]</td>
+    </tr>
+    <tr>
+     <td>[% LxERP.t8("Shop Category") %]</td>
+     <td>[% L.input_tag("shop_part.shop_category", SELF.shop_part.category, size=2) %]</td>
+    </tr>
+    </table>
+    [% # L.dump(SELF.shop_part) %]
 
- <table>
-  <tr>
-   <td>[% LxERP.t8("Description") %]</td>
-   <td>[% L.textarea_tag('shop_part.shop_description_as_restricted_html', SELF.shop_part.shop_description_as_restricted_html, style='width:100%', rows=20, class="texteditor") %]</td>
-     <td>[% L.textarea_tag('letter.body_as_restricted_html', letter.body_as_restricted_html, style='width:100%', rows=20, class="texteditor") %]</td>
-   <!--   <td>[% L.textarea_tag("shop_part.shop_description", SELF.shop_part.shop_description) %]</td> -->
-  </tr>
-  <tr>
-   <td>[% LxERP.t8("Active") %]</td>
-   <td>[% L.yes_no_tag("shop_part.active", SELF.shop_part.active) %]</td>
-  </tr>
-  <tr>
-   <td>[% LxERP.t8("Sort order") %]</td>
-   <td>[% L.input_tag("shop_part.sortorder", SELF.shop_part.sortorder, size=2) %]</td>
-  </tr>
-  <tr>
-   <td>[% LxERP.t8("Date") %]</td>
-   <td>[% L.date_tag("shop_part.show_date", SELF.shop_part.show_date) %]</td>
-  </tr>
-  <tr>
-   <td>[% LxERP.t8("Front page") %]</td>
-   <td>[% L.yes_no_tag('shop_part.front_page', SELF.shop_part.front_page) %]</td>
-  </tr>
-  <tr>
-   <td>[% LxERP.t8("Meta Tags") %]</td>
-   <td>[% L.input_tag("shop_part.meta_tags", SELF.shop_part.meta_tags, size=2) %]</td>
-  </tr>
-  <tr>
-   <td>[% LxERP.t8("Shop Category") %]</td>
-   <td>[% L.input_tag("shop_part.shop_category", SELF.shop_part.category, size=2) %]</td>
-  </tr>
-</table>
-[% # L.dump(SELF.shop_part) %]
+    [% IF SELF.shop_part.id %]
+    [% L.button_tag("kivi.shop_part.save_shop_part(" _ SELF.shop_part.id _ ")", LxERP.t8("Save"))  %]</td>
+    [% ELSE %]
+    [% L.button_tag("kivi.shop_part.add_shop_part(" _ FORM.part_id _", " _ FORM.shop_id _")", LxERP.t8("Save"))  %]</td>
+    [% END %]
+    [% # L.button_tag("kivi.shop_part.update_partnumber()", LxERP.t8("Update Partnumber"))  %]</td>
 
-[% IF SELF.shop_part.id %]
-[% L.button_tag("kivi.shop_part.save_shop_part(" _ SELF.shop_part.id _ ")", LxERP.t8("Save"))  %]</td>
-[% ELSE %]
-[% L.button_tag("kivi.shop_part.add_shop_part(" _ FORM.part_id _", " _ FORM.shop_id _")", LxERP.t8("Save"))  %]</td>
-[% END %]
-[% # L.button_tag("kivi.shop_part.update_partnumber()", LxERP.t8("Update Partnumber"))  %]</td>
+    [% # L.hidden_tag("action", "ShopPart/dispatch") %]
+    [% # L.submit_tag('action_update', LxERP.t8('Save')) %]
 
-[% # L.hidden_tag("action", "ShopPart/dispatch") %]
-[% # L.submit_tag('action_update', LxERP.t8('Save')) %]
 
-</div>
+  </div>
 </form>
+
+
+
 [%- IF SELF.shop_part.part.image && INSTANCE_CONF.get_parts_show_image %]
          <a href="[% SELF.shop_part.part.image | html %]" target="_blank"><img style="[% INSTANCE_CONF.get_parts_image_css %]" src="[% SELF.shop_part.part.image | html %]"/></a>
 [%- END %]
