/**
  * 
  * @param string $dir
  * @return array Zo2Profile
  */
 private function _getProfiles($dir)
 {
     $profiles = array();
     if (JFolder::exists($dir)) {
         $files = JFolder::files($dir);
         if ($files) {
             foreach ($files as $file) {
                 if (JFile::getExt($file) == 'json') {
                     $profile = new Zo2Profile();
                     $profile->load(JFile::stripExt($file));
                     if ($profile->isValid()) {
                         $profiles[$profile->name] = $profile;
                     }
                 }
             }
         }
     }
     return $profiles;
 }
 /**
  * Get instance of Zo2Profile by name
  * @staticvar array $profiles
  * @param type $profile
  * @return \Zo2Profile
  */
 public static function getProfile($profile = null)
 {
     static $profiles = array();
     $profileName = 'default';
     if ($profile === null) {
         if (JFactory::getApplication()->isSite()) {
             $profileName = Zo2Framework::getInstance()->template->params->get('profile', 'default');
             $profileName = JFactory::getApplication()->input->get('profile', $profileName);
         } else {
             $profileName = JFactory::getApplication()->input->get('profile');
             if (empty($profileName)) {
                 $profileName = Zo2Framework::getInstance()->template->params->get('profile', 'default');
             }
         }
     } else {
         if (is_string($profile)) {
             $profileName = $profile;
         } elseif ($profile instanceof Zo2Profile) {
             $profileName = $profile->name;
         }
     }
     if (!is_string($profileName) || empty($profileName)) {
         $profileName = 'default';
     }
     if (!isset($profiles[$profileName])) {
         $profile = new Zo2Profile();
         $profile->load($profileName);
         $profiles[$profileName] = $profile;
     }
     return $profiles[$profileName];
 }
 /**
  * 
  */
 private function _save()
 {
     $application = JFactory::getApplication();
     $jinput = JFactory::getApplication()->input;
     /* Zo2 data */
     $zo2 = $jinput->post->get('zo2', array(), 'array');
     // Joomla! form data
     $formData = $jinput->post->get('jform', array(), 'array');
     $formData['params']['layout'] = json_decode($formData['params']['layout']);
     $formData['params']['theme'] = json_decode($formData['params']['theme']);
     $formData['params']['menu_config'] = json_decode($formData['params']['menu_config']);
     /* Request profileName */
     $profileName = isset($zo2['newProfile']) ? $zo2['newProfile'] : $zo2['profiles'];
     if ($profileName != $zo2['profiles']) {
         JFactory::getApplication()->enqueueMessage('Added new profile: ' . $profileName, 'notice');
     }
     // Save Joomla! data
     $model = JModelLegacy::getInstance('Style', 'TemplatesModel');
     $model->save($formData);
     /**
      * Save profile
      */
     $profile = new Zo2Profile();
     $profile->loadArray($formData['params']);
     $profile->template = Zo2Framework::getInstance()->template->template;
     $profile->name = $profileName;
     if ($profile->save()) {
         /* Save Zo2 data */
         $zo2Data = $jinput->post->get('zo2', array(), 'array');
         $templateDir = JPATH_ROOT . '/templates/' . Zo2Framework::getInstance()->template->template;
         $customCssFile = $templateDir . '/assets/zo2/css/custom.css';
         $customCss = trim($zo2Data['custom_css']);
         JFile::write($customCssFile, $customCss);
         $customJsFile = $templateDir . '/assets/zo2/js/custom.js';
         $customJs = trim($zo2Data['custom_js']);
         JFile::write($customJsFile, $customJs);
         $application->enqueueMessage('Style successfully saved');
         if ($jinput->get('task') == 'style.apply') {
             $application->redirect(JRoute::_('index.php?option=com_templates&view=style&layout=edit&id=' . $jinput->get('id') . '&profile=' . $profileName, false));
         } else {
             $application->redirect(JRoute::_('index.php?option=com_templates&view=styles', false));
         }
     }
     JFactory::getApplication()->enqueueMessage('Style save error', 'error');
     $application->redirect(JRoute::_('index.php?option=com_templates&view=styles', false));
 }