<?php
/**
* @package OrdaSoft CCK
* @copyright OrdaSoft.
* @author Andrey Kvasnevskiy(akbet@mail.ru),Roman Akoev (akoevroman@gmail.com)
* @link http://ordasoft.com/cck-content-construction-kit-for-joomla.html
* @description OrdaSoft Joomla CCK - Content Construction Kit
*/

defined('_JEXEC') or die;



class AdminViewInstance{

  static function showInstances($option, & $rows_item, & $clist,
            & $publist, & $search, & $pageNav, & $sort_arr, $show_fields,$entity_list, & $approvedlist,& $userslist, $lang_list){
      global $doc, $user, $app, $session, $db;
      $db = new os_cckDb();
    //  var_dump(cck_lang_text('COM_OS_CCK_SHOW_SEARCH'));
      $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />" . cck_lang_text('COM_OS_CCK_SHOW') . "</div>";
      $app = JFactory::getApplication();
      $app->JComponentTitle = $html;
      $onclick = "Joomla.checkAll(this);";
      
      $html = os_cckInstanceManagerView::getViewIM($rows_item, $clist, $publist, $search, $pageNav, 
        $sort_arr, $show_fields,$entity_list, $approvedlist, $userslist, $lang_list);
      echo $html;
      
      if( checkOsCckVersionProFree() == "pro" ) {
          $avaibleUpdate = false;
          $xml = @simplexml_load_file(JPATH_BASE . "/components/com_os_cck/os_cck.xml");
          if($xml){
              $version = (string)$xml->version;
              $creationDate = (string)$xml->creationDate;
              unset($xml);

              //check update
              $url="http://ordasoft.com/xml_update/os_cck.xml";
              $ch = curl_init();
              curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
              curl_setopt($ch, CURLOPT_URL, $url);
              curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,0); 
              curl_setopt($ch, CURLOPT_TIMEOUT, 1);

              $data = curl_exec($ch);

              $data = curl_exec($ch);
              $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

              if( !curl_errno($ch) && $code == 200 ){

                  $xml = simplexml_load_string($data);
                  $updateArticleUrl = '#';
                  $versionArr = explode(".", $version);
                  if($xml && isset($xml->version)){
                      $ordasoft_version = (string)$xml->version;
                      $ordasoft_versionArr = explode(".", $ordasoft_version);
                      $ordasoftCreationDate = (string)$xml->creationDate;
                      $updateArticleUrl = (string)$xml->updateArticleUrl;
                      unset($xml);
                      foreach ($versionArr as $k => $ordasoftSubV) {
                          if(isset($ordasoft_versionArr[$k])){
                              if((int)$ordasoft_versionArr[$k] < (int)$ordasoftSubV){
                                  break;
                              }
                              if((int)$ordasoft_versionArr[$k] > (int)$ordasoftSubV){
                                  $avaibleUpdate = true;
                                  break;
                              }
                          }
                      }
                  }

              }
              curl_close($ch);

          }

          $update = 'unavaible';
          if( $avaibleUpdate ||  checkOsCckActivationNeed()  ) $update = 'avaible';
          if($update == 'avaible'){
            $about_version_icon = JURI::root() . 'components/com_os_cck/images/yes-update.png';
          }else{
            $about_version_icon = JURI::root() . 'components/com_os_cck/images/no-update.png';
          }
          $button_class = 'update-'.$update;
//          $table->create_button('', 'about_version', '', 'fa-bell icon-'. $button_class, 'right-button cck-about', 'false', 'false');
        }
        
        if (version_compare(JVERSION, "4.0.0", "ge")) {
            $bs_delimiter = '-bs';
        } else {
            $bs_delimiter = '';
        }
?>

      <!-- about form  start-->
      <div id="ordasoft-notification">
          <p></p>
      </div>
      <div id="about-modal" class="ordasoft-dashboard-apps-dialog  ordasoft-dashboard-about modal hide fade">
          <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-body">
          <div class="about-image">
              <img src="<?php echo JURI::root().'administrator/components/com_os_cck/images/os_cck_logo.png'?>" alt="OrdaSoft CCK">
          </div>
        </div>
        <div class="modal-footer">
              <span class="span3">
                  <div class="about-col-1"><?php echo cck_lang_text("COM_OS_CCK_DOC_VERSION"); ?></div><span id="os_cck-version" class="about-col-2"><?php echo $version; ?></span>
              </span>
              <span class="span3">
                  <div class="about-col-1"><?php echo cck_lang_text("COM_OS_CCK_DOC_RELEASE_DATE"); ?></div><span id="booklibrary-version" class="about-col-2"><?php echo $creationDate?></span>
              </span>
          <?php if(!checkOsCckActivationNeed() &&  $avaibleUpdate ){?>
              <span class="span3">
                      <div class="about-col-1 new-version"><?php echo cck_lang_text("COM_OS_CCK_NEW_VERSION_AVALIABLE"); ?>: <span class="version-osbooklibrary"><?php echo $ordasoft_version; ?></span></div><a class="update-link dashboard-link-action" href="#" >UPDATE</a>
              </span>
          <?php }else if( $avaibleUpdate  ) {?>
              <span class="span3">
                      <div class="about-col-1 new-version"><?php echo cck_lang_text("COM_OS_CCK_NEW_VERSION_AVALIABLE"); ?>:<span class="version-osbooklibrary"><?php echo $ordasoft_version; ?></span></div>
              </span>
          <?php }?>
              <span class="span3 active-deactive">

      <?php
              $need_activation = checkOsCckActivationNeed();

      ?>
      <link rel="stylesheet" type="text/css" href="<?php echo JUri::root(); ?>components/com_os_cck/assets/css/os-about.css">

      <script type="text/javascript">
      var JUri = '<?php echo JUri::root(); ?>';
      </script>
      <?php includeJavaScriptCCK("components/com_os_cck/assets/js/os-about.js"); ?>
      <!--<script src="<?php echo JUri::root(); ?>components/com_os_cck/assets/js/os-about.js"></script>-->

              <div class="forms-deactivate-license"
                  <?php echo !($need_activation) ? '' : 'style="display:none;"'; ?>>
                  <i class="zmdi zmdi-shield-check"></i>
                  <span><?php echo cck_lang_text("COM_OS_CCK_YOUR_LICENSE_ACTIVE"); ?></span>
                  <a class="deactivate-link dashboard-link-action" href="#"><?php echo cck_lang_text("COM_OS_CCK_DEACTIVATE"); ?></a>
              </div>
              <div class="forms-activate-license"
                  <?php echo ($need_activation) ? '' : 'style="display:none;"'; ?>>
                  <i class="zmdi zmdi-shield-check"></i>
                  <span><?php echo cck_lang_text("COM_OS_CCK_YOUR_LICENSE_NEED_ACTIVE"); ?></span>
                  <a class="activate-link dashboard-link-action" href="#"><?php echo cck_lang_text("COM_OS_CCK_ACTIVATE") ?></a>
              </div>


              </span>

              <span class="span12 submit-close">
                  <button class="btn" data<?php echo $bs_delimiter; ?>-dismiss="modal" aria-hidden="true">Close</button>
              </span>
            
        </div>
      </div>
          </div>
      </div>
      <div id="deactivate-dialog" class="ordasoft-modal-sm modal hide" style="display:none">
          <div class="modal-dialog">
              <div class="modal-content">
                  <div class="modal-body">
                      <h3><?php echo cck_lang_text("COM_OS_CCK_LICENSE_DEACTIVATION"); ?></h3>
                      <p class="modal-text can-delete"><?php echo cck_lang_text("COM_OS_CCK_ARE_YOU_SURE_DEACTIVATE"); ?></p>
                  </div>
                  <div class="modal-footer">
                      <a href="#" class="ordasoft-btn" data<?php echo $bs_delimiter; ?>-dismiss="modal" id="deactivate-close">
                          <?php echo cck_lang_text("COM_OS_CCK_CANCEL"); ?>
                      </a>
                      <a href="#" class="ordasoft-btn-primary red-btn" id="apply-deactivate">
                          <?php echo cck_lang_text("COM_OS_CCK_APPLY"); ?>
                      </a>
                  </div>
              </div>
          </div>
      </div>
      <div id="login-modal" class="ordasoft-modal-sm modal hide" aria-hidden="true" style="display: none;">
          <div class="modal-dialog">
              <div class="modal-content">
                  <img class="img-preloader notification-in" src="<?php echo JURI::base(); ?>/components/com_os_cck/images/reload.svg" ></img>
                  <div class="modal-body">
              
                  </div>
              </div>
          </div>
      </div>

      <script>
        document.addEventListener('DOMContentLoaded', function(){
          jQuerOs('.cck-help-button').on('click', function(){
            window.open('https://ordasoft.com/cck-content-construction-kit-for-joomla.html#joomla-website-builder-documentation', 'Help', "width=700,height=500,toolbar=no")
            
          })
          jQuerOs('.cck-about').on('click', function(){
            if(typeof bootstrap == "undefined"){
                jQuery("#about-modal").modal("show");
            }else{
                var myModal = new bootstrap.Modal(document.getElementById("about-modal"), {});
                myModal.show()
            }
          })
        })
      </script>

  <?php
  }

  static function showInstancesModal($option, & $rows_item, & $clist,
                                & $publist, & $search, & $pageNav, & $sort_arr, $show_fields)
  {
      global $doc, $user, $app, $session, $templateDir,$db;
      $db = new os_cckDb();
      
      ?>
      <form action="index.php?option=com_os_cck" method="post" name="manageInstanceModal" id="manageInstanceModal">
      <h4 class="modal-title" id="attached-layout-modal-Label"><?php echo cck_lang_text("COM_OS_CCK_MODAL_SELECT_INSTANCE_BUTTON")?></h4>
        <table cellpadding="4" cellspacing="0" border="0" width="100%" class="manageInstanceModalTable">
          <tr>
            <?php
            foreach($show_fields as $value){
              foreach($value as $field){
                ?>
                <th align="center" class="title" width="15%"
                  nowrap="nowrap"><?php echo $field->field_name;?></th>
                <?php
              }
            }
            if(is_object($rows_item[0]) && property_exists($rows_item[0], 'title') && empty($show_fields)){ ?>
                <th align="center" class="title" width="15%"
                  nowrap="nowrap">Title</th>
            <?php }
            ?>
            <th align="center" class="title" width="5%"
                nowrap="nowrap"><?php echo cck_lang_text('COM_OS_CCK_LABEL_ENTITY');?></th>
            <th align="center" class="title" width="2%" nowrap="nowrap">
                <?php echo cck_lang_text('COM_OS_CCK_LABEL_INSTANCE_ID');?></th>
          </tr>
          <?php
          if(isset($sort_arr['sorting_direction'])){
            $sorting_direction = $sort_arr['sorting_direction'];
          }else{
              $sorting_direction = '';
          }
            $session->set('sorting_direction', $sorting_direction);
            // var_dump($rows_item); exit;
            for ($i = 0, $n = count($rows_item); $i < $n; $i++) {
              $row = & $rows_item[$i];
              ?>
              <tr onclick="if(window.parent)window.parent.selectInstance(<?php echo $row->eiid?>)" class="modalRow<?php echo $i % 2; ?>">
    <!-- **************************************************************** -->
                <?php
                  foreach($show_fields as $key => $value){
                    foreach($value as $field){
                      // $html = '';
                      if($row->fk_eid != $key){
                        echo'<td></td>';
                        continue;
                      }
                      if($field->field_type == 'categoryfield'){
                        echo "<td align='center'>$row->category</td>";
                        continue;
                      }
                      ?>
                      <td align="center">
                        <?php
                        $entityInstance = new os_cckEntityInstance($db->_db);
                        $entityInstance->load($row->eiid);
                        $value = $entityInstance->getFieldValue($field);
                        ?>
                        <div style="margin-right:15px";>
                            <span class="col_box" style="display:block;
                            <?php echo ($field->field_type=='imagefield'
                                        && isset($field->options['width'])
                                        && isset($field->options['height']))? 'width:'.$field->options['width'].'px; height:'.$field->options['height'].'px;':'';?>">
                                <?php
                                    // ob_start();
                                      require getSiteShowFiledViewPath('com_os_cck', $field->field_type);
                                      // $html .= ob_get_contents();
                                    // ob_end_clean();
                                    // echo $html;
                                ?>
                            </span>
                        </div>
                      </td>
                      <?php
                    }
                  } 
                  if(property_exists($row, 'title') && empty($show_fields)){ ?>
                      <td align="center">
                        
                        <div style="margin-right:15px";>
                            <span class="col_box" style="display:block;">
                            <?php echo $row->title; ?>
                            </span>
                        </div>
                      </td>
                  <?php } ?>
                  
                <td align="center"><?php echo $row->entity; ?></td>
                <td align="center"><?php echo $row->eiid; ?></td>
            </tr>
          <?php
          }//end for
          ?>
        </table>
        <input type="hidden" name="option" value="<?php echo $option; ?>"/>
        <input type="hidden" name="task" value="blablabla"/>
        <input type="hidden" name="boxchecked" value="0"/>
      </form>

  <?php
  }

  static function showInstanceModalPlg($option, & $rows_item, & $clist,
                                & $publist, & $search, & $pageNav, & $sort_arr, $show_fields, $lid)
  {
      global $doc, $user, $app, $session, $templateDir,$db;
      $db = new os_cckDb();
      ?>
      <form action="index.php?option=com_os_cck" method="post" name="manageInstanceModal" id="manageInstanceModal">
      <h4 class="modal-title" id="attached-layout-modal-Label"><?php echo cck_lang_text("COM_OS_CCK_ATTACHED_LAYOUT_MODAL_TITLE")?></h4>
        <table cellpadding="4" cellspacing="0" border="0" width="100%" class="manageInstanceModalTable">
          <tr>
            <?php
            foreach($show_fields as $value){
              foreach($value as $field){
                ?>
                <th align="center" class="title" width="15%"
                  nowrap="nowrap"><?php echo $field->field_name;?></th>
                <?php
              }
            }
            ?>
            <th align="center" class="title" width="5%"
                nowrap="nowrap"><?php echo cck_lang_text('COM_OS_CCK_LABEL_ENTITY');?></th>
            <th align="center" class="title" width="2%" nowrap="nowrap">
                <?php echo cck_lang_text('COM_OS_CCK_LABEL_INSTANCE_ID');?></th>
          </tr>
          <?php
//            $session->set('sorting_direction', $sort_arr['sorting_direction']);
            for ($i = 0, $n = count($rows_item); $i < $n; $i++) {
              $row = & $rows_item[$i];
              ?>
              <tr class="modalRow<?php echo $i % 2; ?>"
              onclick="window.location.href = 'index.php?option=com_os_cck&task=select_data_for_editor_button&tmpl=component&lid=<?php echo $lid?>&eiid='+<?php echo $row->eiid?>">
    <!-- **************************************************************** -->
                <?php
                  foreach($show_fields as $key => $value){
                    foreach($value as $field){
                      // $html = '';
                      if($row->fk_eid != $key){
                        echo'<td></td>';
                        continue;
                      }
                      if($field->field_type == 'categoryfield'){
                        echo "<td align='center'>$row->category</td>";
                        continue;
                      }
                      ?>
                      <td align="center">
                        <?php
                        $entityInstance = new os_cckEntityInstance($db->_db);
                        $entityInstance->load($row->eiid);
                        $value = $entityInstance->getFieldValue($field);
                        ?>
                        <div style="margin-right:15px";>
                            <span class="col_box" style="display:block;
                            <?php echo ($field->field_type=='imagefield'
                                        && isset($field->options['width'])
                                        && isset($field->options['height']))? 'width:'.$field->options['width'].'px; height:'.$field->options['height'].'px;':'';?>">
                                <?php
                                    // ob_start();
                                      require getSiteShowFiledViewPath('com_os_cck', $field->field_type);
                                      // $html .= ob_get_contents();
                                    // ob_end_clean();
                                    // echo $html;
                                ?>
                            </span>
                        </div>
                      </td>
                      <?php
                    }
                  } ?>
                <td align="center"><?php echo $row->entity; ?></td>
                <td align="center"><?php echo $row->eiid; ?></td>
            </tr>
          <?php
          }//end for
          ?>
        </table>
        <input type="hidden" name="option" value="<?php echo $option; ?>"/>
        <input type="hidden" name="task" value="show_instance"/>
        <input type="hidden" name="boxchecked" value="0"/>
      </form>

  <?php
  }

  static function editInstance($option, $entityInstance, $layout, $layout_params){
    global $os_cck_configuration,$user, $app, $cck_entity_configuration, $db;
    $db = new os_cckDb();
    $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />" . cck_lang_text('COM_OS_CCK_SHOW') . "</div>";
    $app = JFactory::getApplication();
    $app->JComponentTitle = $html;
    $type = 'editInstance';
    require JPATH_ADMINISTRATOR . '/components/com_os_cck/elements/getUserModal.php';
    require getLayoutPathCCK::getAdminLayoutViewPath('com_os_cck',$type);
  }
  
  static function cloneInstance($eiid, $html_form){
      global $doc;
      $doc->addStyleSheet("https://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-glyphicons.css");
      $html = "<div class='os_cck_caption' ><img src='./components/com_os_cck/images/os_cck_logo.png' alt ='Config' />" . cck_lang_text('COM_OS_CCK_SHOW') . "</div>";
      $app = JFactory::getApplication();
      $app->JComponentTitle = $html;
      if($html_form){
      ?>
    <form action="index.php?option=com_os_cck" method="post" name="cloneInstance" id="cloneInstance">
        <div class="clone-inst-title">Clone Instance</div>
        <?php echo $html_form; ?>
        
        <input type="hidden" name="eiid" value="<?php echo $eiid; ?>">
        <input type="hidden" name="option" value="com_os_cck">
        <input type="hidden" name="task" value="clone_instance">
    </form>
      <?php } ?>
    <script>
     
        Joomla.submitbutton = function(pressbutton) {
            if(pressbutton == 'cancel'){
                window.history.back();
            }
            
        }
    </script>
  <?php }

}
