Example #1
0
 /**
  * Set the tag template
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function setTemplate(AbstractController $controller, Application $application)
 {
     if ($application->isRegistered('phire-templates') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView() && $controller->view()->isStream()) {
         if (null !== $controller->view()->tag_title) {
             $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Tag ' . $controller->view()->tag_title]);
             if (!isset($template->id)) {
                 $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Tag']);
             }
         } else {
             $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Tag']);
         }
         if (isset($template->id)) {
             if (isset($template->id)) {
                 $device = \Phire\Templates\Event\Template::getDevice($controller->request()->getQuery('mobile'));
                 if (null !== $device && $template->device != $device) {
                     $childTemplate = \Phire\Templates\Table\Templates::findBy(['parent_id' => $template->id, 'device' => $device]);
                     if (isset($childTemplate->id)) {
                         $tmpl = $childTemplate->template;
                     } else {
                         $tmpl = $template->template;
                     }
                 } else {
                     $tmpl = $template->template;
                 }
                 $controller->view()->setTemplate(\Phire\Templates\Event\Template::parse($tmpl));
             }
         }
     } else {
         if ($application->isRegistered('phire-themes') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView() && $controller->view()->isFile()) {
             $theme = \Phire\Themes\Table\Themes::findBy(['active' => 1]);
             if (isset($theme->id)) {
                 $template = null;
                 $themePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder . '/';
                 if (null !== $controller->view()->tag_slug) {
                     $tagSlug = 'tag-' . str_replace('/', '-', $controller->view()->tag_slug);
                     if (file_exists($themePath . $tagSlug . '.phtml') || file_exists($themePath . $tagSlug . '.php')) {
                         $template = file_exists($themePath . $tagSlug . '.phtml') ? $tagSlug . '.phtml' : $tagSlug . '.php';
                     } else {
                         if (file_exists($themePath . 'tag.phtml') || file_exists($themePath . 'tag.php')) {
                             $template = file_exists($themePath . 'tag.phtml') ? 'tag.phtml' : 'tag.php';
                         }
                     }
                 } else {
                     if (file_exists($themePath . 'tag.phtml') || file_exists($themePath . 'tag.php')) {
                         $template = file_exists($themePath . 'tag.phtml') ? 'tag.phtml' : 'tag.php';
                     }
                 }
                 if (null !== $template) {
                     $device = \Phire\Themes\Event\Theme::getDevice($controller->request()->getQuery('mobile'));
                     if (null !== $device && file_exists($themePath . $device . '/' . $template)) {
                         $template = $device . '/' . $template;
                     }
                     $controller->view()->setTemplate($themePath . $template);
                 }
             }
         }
     }
 }
Example #2
0
 /**
  * Parse form object
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function parseForms(AbstractController $controller, Application $application)
 {
     if ($controller->hasView() && ($application->isRegistered('phire-content') && $controller instanceof \Phire\Content\Controller\IndexController || $application->isRegistered('phire-categories') && $controller instanceof \Phire\Categories\Controller\IndexController)) {
         $body = $controller->response()->getBody();
         if (strpos($body, '[{form_') !== false) {
             // Parse any form placeholders
             $formIds = [];
             $forms = [];
             preg_match_all('/\\[\\{form.*\\}\\]/', $body, $forms);
             if (isset($forms[0]) && isset($forms[0][0])) {
                 foreach ($forms[0] as $form) {
                     $id = substr($form, strpos($form, 'form_') + 5);
                     $formIds[] = str_replace('}]', '', $id);
                 }
             }
             if (count($formIds) > 0) {
                 foreach ($formIds as $id) {
                     try {
                         $form = new \Phire\Forms\Form\Form($id);
                         if ($form->isSubmitted()) {
                             $values = $form->getMethod() == 'post' ? $_POST : $_GET;
                             if ($form->isFiltered()) {
                                 $form->addFilter('strip_tags');
                             }
                             $form->setFieldValues($values);
                             if ($form->isValid()) {
                                 $form->process();
                                 $body = str_replace('[{form_' . $id . '}]', $form->getMessage(), $body);
                             } else {
                                 $body = str_replace('[{form_' . $id . '}]', (string) $form, $body);
                             }
                         } else {
                             $body = str_replace('[{form_' . $id . '}]', (string) $form, $body);
                         }
                     } catch (\Exception $e) {
                         $body = str_replace('[{form_' . $id . '}]', '', $body);
                     }
                 }
             }
             $controller->response()->setBody($body);
         }
     }
 }
Example #3
0
 /**
  * Bootstrap the module
  *
  * @param  Application $application
  * @return void
  */
 public static function bootstrap(Application $application)
 {
     if ($application->isRegistered('phire-content') && $application->isRegistered('phire-fields')) {
         $fields = \Phire\Fields\Table\Fields::findBy(['models' => "%Phire\\\\Content\\\\Model\\\\Content%"]);
         $names = [];
         foreach ($fields->rows() as $field) {
             $names[] = $field->name;
         }
         if (!in_array('seo_title', $names)) {
             $field = new \Phire\Fields\Table\Fields(['group_id' => null, 'storage' => 'eav', 'type' => 'text', 'name' => 'seo_title', 'label' => 'SEO Title', 'values' => null, 'default_values' => null, 'attributes' => 'size="80" style="width: 99.5%;"', 'validators' => 'a:0:{}', 'encrypt' => 0, 'order' => -3, 'required' => 0, 'prepend' => 0, 'dynamic' => 0, 'editor' => null, 'models' => 'a:1:{i:0;a:3:{s:5:"model";s:27:"Phire\\Content\\Model\\Content";s:10:"type_field";N;s:10:"type_value";N;}}']);
             $field->save();
         }
         if (!in_array('description', $names)) {
             $field = new \Phire\Fields\Table\Fields(['group_id' => null, 'storage' => 'eav', 'type' => 'text', 'name' => 'description', 'label' => 'Description', 'values' => null, 'default_values' => null, 'attributes' => 'size="80" style="width: 99.5%;"', 'validators' => 'a:0:{}', 'encrypt' => 0, 'order' => -2, 'required' => 0, 'prepend' => 0, 'dynamic' => 0, 'editor' => null, 'models' => 'a:1:{i:0;a:3:{s:5:"model";s:27:"Phire\\Content\\Model\\Content";s:10:"type_field";N;s:10:"type_value";N;}}']);
             $field->save();
         }
         if (!in_array('keywords', $names)) {
             $field = new \Phire\Fields\Table\Fields(['group_id' => null, 'storage' => 'eav', 'type' => 'text', 'name' => 'keywords', 'label' => 'Keywords', 'values' => null, 'default_values' => null, 'attributes' => 'size="80" style="width: 99.5%;"', 'validators' => 'a:0:{}', 'encrypt' => 0, 'order' => -1, 'required' => 0, 'prepend' => 0, 'dynamic' => 0, 'editor' => null, 'models' => 'a:1:{i:0;a:3:{s:5:"model";s:27:"Phire\\Content\\Model\\Content";s:10:"type_field";N;s:10:"type_value";N;}}']);
             $field->save();
         }
     }
 }
Example #4
0
 /**
  * Set the search template
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function setTemplate(AbstractController $controller, Application $application)
 {
     if ($application->isRegistered('phire-templates') && $controller instanceof \Phire\Search\Controller\IndexController && $controller->hasView()) {
         $template = \Phire\Templates\Table\Templates::findBy(['name' => 'Search']);
         if (isset($template->id)) {
             if (isset($template->id)) {
                 $device = \Phire\Templates\Event\Template::getDevice($controller->request()->getQuery('mobile'));
                 if (null !== $device && $template->device != $device) {
                     $childTemplate = \Phire\Templates\Table\Templates::findBy(['parent_id' => $template->id, 'device' => $device]);
                     if (isset($childTemplate->id)) {
                         $tmpl = $childTemplate->template;
                     } else {
                         $tmpl = $template->template;
                     }
                 } else {
                     $tmpl = $template->template;
                 }
                 $controller->view()->setTemplate(\Phire\Templates\Event\Template::parse($tmpl));
             }
         }
     } else {
         if ($application->isRegistered('phire-themes') && $controller instanceof \Phire\Search\Controller\IndexController && $controller->hasView()) {
             $theme = \Phire\Themes\Table\Themes::findBy(['active' => 1]);
             if (isset($theme->id)) {
                 $themePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder . '/';
                 if (file_exists($themePath . 'search.phtml') || file_exists($themePath . 'search.php')) {
                     $template = file_exists($themePath . 'search.phtml') ? 'search.phtml' : 'search.php';
                     $device = \Phire\Themes\Event\Theme::getDevice($controller->request()->getQuery('mobile'));
                     if (null !== $device && file_exists($themePath . $device . '/' . $template)) {
                         $template = $device . '/' . $template;
                     }
                     $controller->view()->setTemplate($themePath . $template);
                 }
             }
         }
     }
 }
Example #5
0
 /**
  * Init the entity model and parse any entity placeholders
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function init(AbstractController $controller, Application $application)
 {
     if ($application->isRegistered('phire-templates') && $controller->hasView() && $controller->view()->isStream()) {
         $ents = [];
         preg_match_all('/\\[\\{entity_.*\\}\\]/', $controller->view()->getTemplate()->getTemplate(), $ents);
         if (isset($ents[0]) && isset($ents[0][0])) {
             foreach ($ents[0] as $ent) {
                 $id = str_replace('}]', '', substr($ent, strpos($ent, '_') + 1));
                 $controller->view()->{'entity_' . $id} = (new Model\Entity())->getByType($id);
             }
         }
     }
     if ($controller->hasView()) {
         $controller->view()->phire->entity = new Model\Entity();
     }
 }
Example #6
0
 /**
  * Init category nav and categories
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function init(AbstractController $controller, Application $application)
 {
     if (!$_POST && $controller->hasView()) {
         $category = new Model\Category();
         $category->show_total = $application->module('phire-categories')['show_total'];
         $category->filters = $application->module('phire-categories')['filters'];
         $category->datetime_formats = $application->module('phire-categories')['datetime_formats'];
         $controller->view()->category_nav = $category->getNav($application->module('phire-categories')['nav_config']);
         if ($application->isRegistered('phire-templates') && $controller->view()->isStream() && (strpos($controller->view()->getTemplate()->getTemplate(), '[{category_') !== false || strpos($controller->view()->getTemplate()->getTemplate(), '[{categories_') !== false)) {
             $catIds = self::parseCategoryIds($controller->view()->getTemplate()->getTemplate());
             $catParentIds = self::parseParentCategoryIds($controller->view()->getTemplate()->getTemplate());
             if (count($catIds) > 0) {
                 foreach ($catIds as $key => $value) {
                     $category->getById($value['id']);
                     $categoryName = 'category_' . $value['id'];
                     if (isset($value['limit']) && $value['limit'] > 0 && $category->hasPages($value['limit'])) {
                         $limit = $value['limit'];
                         $categoryName .= '_' . $limit;
                         $pages = null;
                     } else {
                         if ($category->pagination > 0 && $category->hasPages($category->pagination)) {
                             $limit = $category->pagination;
                             $pages = new \Pop\Paginator\Paginator($category->getCount(), $limit);
                             $pages->useInput(true);
                         } else {
                             $limit = null;
                             $pages = null;
                         }
                     }
                     if (null !== $pages) {
                         $controller->view()->pages = $pages;
                     }
                     $controller->view()->{$categoryName} = $category->getItems($limit, $controller->request()->getQuery('page'));
                 }
             }
             if (count($catParentIds) > 0) {
                 foreach ($catParentIds as $key => $value) {
                     if (isset($value['limit']) && $value['limit'] > 0) {
                         $limit = $value['limit'];
                         $categoryName = 'categories_' . $value['id'] . '_' . $limit;
                     } else {
                         $limit = null;
                         $categoryName = 'categories_' . $value['id'];
                     }
                     $controller->view()->{$categoryName} = $category->getCategoryChildren($value['id'], $limit);
                 }
             }
         } else {
             if (($controller instanceof \Phire\Content\Controller\IndexController || $controller instanceof \Phire\Categories\Controller\IndexController) && $controller->view()->isFile()) {
                 $controller->view()->phire->category = $category;
             }
         }
         if (($controller instanceof \Phire\Content\Controller\ContentController || $controller instanceof \Phire\Media\Controller\IndexController) && $application->router()->getRouteMatch()->getAction()) {
             $categories = [];
             $cats = Table\Categories::findAll();
             foreach ($cats->rows() as $cat) {
                 $categories[$cat->id] = $cat->title;
             }
             $controller->view()->categories = $categories;
         }
     }
 }
Example #7
0
 /**
  * Set the navigation objects
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function getNavigation(AbstractController $controller, Application $application)
 {
     if ($application->isRegistered('phire-categories') && $controller instanceof \Phire\Categories\Controller\IndexController || $application->isRegistered('phire-content') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView()) {
         $navigation = Table\Navigation::findAll();
         foreach ($navigation->rows() as $nav) {
             $tree = (new Model\Navigation())->getTree($nav->id);
             $slug = Slug::filter($nav->title);
             $name = str_replace('-', '_', $slug);
             $topId = empty($nav->top_id) ? $slug : $nav->top_id;
             $config = [];
             if (!empty($nav->on_class)) {
                 $config['on'] = $nav->on_class;
             }
             if (!empty($nav->off_class)) {
                 $config['off'] = $nav->off_class;
             }
             $config['top'] = ['id' => $topId];
             if (!empty($nav->top_node)) {
                 $config['top']['node'] = $nav->top_node;
             }
             if (!empty($nav->top_class)) {
                 $config['top']['class'] = $nav->top_class;
             }
             if (!empty($nav->top_attributes)) {
                 $attribs = explode('" ', $nav->top_attributes);
                 $attribAry = [];
                 foreach ($attribs as $att) {
                     $val = explode('="', $att);
                     $attribAry[trim($val[0])] = trim($val[1]);
                 }
                 $config['top']['attributes'] = $attribAry;
             }
             if (!empty($nav->parent_node)) {
                 if (!isset($config['parent'])) {
                     $config['parent'] = [];
                 }
                 $config['parent']['node'] = $nav->parent_node;
             }
             if (!empty($nav->parent_id)) {
                 if (!isset($config['parent'])) {
                     $config['parent'] = [];
                 }
                 $config['parent']['id'] = $nav->parent_id;
             }
             if (!empty($nav->parent_class)) {
                 if (!isset($config['parent'])) {
                     $config['parent'] = [];
                 }
                 $config['parent']['class'] = $nav->parent_class;
             }
             if (!empty($nav->parent_attributes)) {
                 if (!isset($config['parent'])) {
                     $config['parent'] = [];
                 }
                 $attribs = explode('" ', $nav->parent_attributes);
                 $attribAry = [];
                 foreach ($attribs as $att) {
                     $val = explode('="', $att);
                     $attribAry[trim($val[0])] = trim($val[1]);
                 }
                 $config['parent']['attributes'] = $attribAry;
             }
             if (!empty($nav->child_node)) {
                 if (!isset($config['child'])) {
                     $config['child'] = [];
                 }
                 $config['child']['node'] = $nav->child_node;
             }
             if (!empty($nav->child_id)) {
                 if (!isset($config['child'])) {
                     $config['child'] = [];
                 }
                 $config['child']['id'] = $nav->child_id;
             }
             if (!empty($nav->child_class)) {
                 if (!isset($config['child'])) {
                     $config['child'] = [];
                 }
                 $config['child']['class'] = $nav->child_class;
             }
             if (!empty($nav->child_attributes)) {
                 if (!isset($config['child'])) {
                     $config['child'] = [];
                 }
                 $attribs = explode('" ', $nav->child_attributes);
                 $attribAry = [];
                 foreach ($attribs as $att) {
                     $val = explode('="', $att);
                     $attribAry[trim($val[0])] = trim($val[1]);
                 }
                 $config['child']['attributes'] = $attribAry;
             }
             if (!empty($nav->indent)) {
                 $config['indent'] = str_repeat(' ', (int) $nav->indent);
             }
             if ($application->isRegistered('phire-content')) {
                 $sess = $application->services()->get('session');
                 $roleId = isset($sess->user) && isset($sess->user->role_id) ? $sess->user->role_id : null;
                 self::checkTreeStatus($tree, $roleId);
             }
             $navObject = new Nav($tree, $config);
             $controller->view()->set($name, $navObject);
         }
     }
 }
Example #8
0
 /**
  * Save dynamic field values to the EAV table
  *
  * @param  Application        $application
  * @param  Table\Fields       $field
  * @param  mixed              $value
  * @param  string             $model
  * @param  int                $modelId
  * @param  string             $uploadFolder
  * @param  string             $mediaLibrary
  * @return void
  */
 public static function save(Application $application, $field, $value, $model, $modelId, $uploadFolder = null, $mediaLibrary = null)
 {
     $dynamicFieldIds = [];
     $fieldId = $field->id;
     $key = 'field_' . $fieldId;
     if ($field->dynamic) {
         $dynamicFieldIds[] = $field->id;
     }
     $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]);
     if ($field->type == 'file' && isset($_FILES[$key]) && !empty($_FILES[$key]['tmp_name']) && !empty($_FILES[$key]['name'])) {
         if (isset($fv->field_id)) {
             $oldFile = json_decode($fv->value);
             if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile)) {
                 unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $oldFile);
             }
         }
         if (null !== $mediaLibrary && $application->isRegistered('phire-media')) {
             $library = new \Phire\Media\Model\MediaLibrary();
             $library->getByFolder($mediaLibrary);
             if (isset($library->id)) {
                 $settings = $library->getSettings();
                 $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
                 if ($mediaUpload->test($_FILES[$key])) {
                     $media = new \Phire\Media\Model\Media();
                     $media->save($_FILES[$key], ['library_id' => $library->id]);
                     $value = $media->file;
                     copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file);
                 }
             }
         } else {
             $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['disallowed_types'], $application->module('phire-fields')->config()['allowed_types']);
             $value = $upload->upload($_FILES[$key]);
         }
     }
     if (!empty($value) && $value != ' ') {
         if ($field->encrypt && !is_array($value)) {
             $value = (new Mcrypt())->create($value);
         }
     }
     if (isset($fv->field_id)) {
         $oldValue = json_decode($fv->value, true);
         if (!empty($value) && $value != ' ') {
             if (strpos($field->type, '-history') !== false) {
                 if ($value != $oldValue) {
                     $ts = null !== $fv->timestamp ? $fv->timestamp : time() - 180;
                     if (null !== $fv->history) {
                         $history = json_decode($fv->history, true);
                         $history[$ts] = $oldValue;
                         if (count($history) > $application->module('phire-fields')->config()['history']) {
                             $history = array_slice($history, 1, $application->module('phire-fields')->config()['history'], true);
                         }
                         $fv->history = json_encode($history);
                     } else {
                         $fv->history = json_encode([$ts => $oldValue]);
                     }
                 }
             }
             if ($field->dynamic && is_array($oldValue) && isset($oldValue[0])) {
                 $oldValue[0] = $value;
                 $newValue = json_encode($oldValue);
             } else {
                 $newValue = json_encode($value);
             }
             $fv->value = $newValue;
             $fv->timestamp = time();
             $fv->save();
         } else {
             if (!$field->dynamic && $field->type != 'file') {
                 $fv->delete();
             } else {
                 if ($field->dynamic && $field->type != 'file' && is_array($oldValue) && isset($oldValue[0])) {
                     $oldValue[0] = '';
                     $newValue = json_encode($oldValue);
                     $fv->value = $newValue;
                     $fv->timestamp = time();
                     $fv->save();
                 }
             }
         }
     } else {
         if (!empty($value) && $value != ' ') {
             $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => $field->dynamic ? json_encode([$value]) : json_encode($value), 'timestamp' => time()]);
             $fv->save();
         }
     }
     foreach ($dynamicFieldIds as $fieldId) {
         $i = 1;
         $offset = 0;
         $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]);
         $checkValue = json_decode($fv->value, true);
         if (is_array($checkValue) && isset($checkValue[0]) && is_array($checkValue[0])) {
             foreach ($checkValue as $k => $v) {
                 $fieldToCheck = $k > 0 ? 'field_' . $fieldId . '_' . $k : 'field_' . $fieldId;
                 if (!isset($_POST[$fieldToCheck])) {
                     unset($checkValue[$k]);
                 }
             }
             $checkValue = array_values($checkValue);
             $fv->value = json_encode($checkValue);
             $fv->timestamp = time();
             $fv->save();
         }
         while (isset($_POST['field_' . $fieldId . '_' . $i])) {
             if (!empty($_POST['field_' . $fieldId . '_' . $i]) && $_POST['field_' . $fieldId . '_' . $i] != ' ') {
                 $postValue = $_POST['field_' . $fieldId . '_' . $i];
                 if (isset($fv->field_id)) {
                     $value = json_decode($fv->value, true);
                     if (isset($value[$i - $offset])) {
                         $value[$i - $offset] = $postValue;
                     } else {
                         $value[] = $postValue;
                     }
                     $fv->value = json_encode($value);
                     $fv->timestamp = time();
                     $fv->save();
                 } else {
                     $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]);
                     $fv->save();
                 }
             } else {
                 if (isset($fv->field_id)) {
                     $value = json_decode($fv->value, true);
                     if (isset($value[$i])) {
                         unset($value[$i]);
                         $value = array_values($value);
                         $offset++;
                     }
                     $fv->value = json_encode($value);
                     $fv->timestamp = time();
                     $fv->save();
                 }
             }
             $i++;
         }
     }
     foreach ($dynamicFieldIds as $fieldId) {
         $i = 1;
         $offset = 0;
         $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]);
         while (isset($_FILES['field_' . $fieldId . '_' . $i])) {
             if (!empty($_FILES['field_' . $fieldId . '_' . $i]['tmp_name'])) {
                 if (null !== $mediaLibrary && $application->isRegistered('phire-media')) {
                     $library = new \Phire\Media\Model\MediaLibrary();
                     $library->getByFolder($mediaLibrary);
                     if (isset($library->id)) {
                         $settings = $library->getSettings();
                         $mediaUpload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder, $settings['max_filesize'], $settings['disallowed_types'], $settings['allowed_types']);
                         if ($mediaUpload->test($_FILES['field_' . $fieldId . '_' . $i])) {
                             $media = new \Phire\Media\Model\Media();
                             $media->save($_FILES['field_' . $fieldId . '_' . $i], ['library_id' => $library->id]);
                             $postValue = $media->file;
                             copy($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $media->file, $_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $media->file);
                         }
                     }
                 } else {
                     $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/', $application->module('phire-fields')->config()['max_size'], $application->module('phire-fields')->config()['allowed_types']);
                     $postValue = $upload->upload($_FILES['field_' . $fieldId . '_' . $i]);
                 }
                 if (isset($fv->field_id)) {
                     $value = json_decode($fv->value, true);
                     if (isset($value[$i - $offset])) {
                         $value[$i - $offset] = $postValue;
                     } else {
                         $value[] = $postValue;
                     }
                     $fv->value = json_encode($value);
                     $fv->timestamp = time();
                     $fv->save();
                 } else {
                     $fv = new Table\FieldValues(['field_id' => $fieldId, 'model_id' => $modelId, 'model' => $model, 'value' => json_encode([$postValue]), 'timestamp' => time()]);
                     $fv->save();
                 }
             }
             $i++;
         }
     }
     foreach ($dynamicFieldIds as $fieldId) {
         $fv = Table\FieldValues::findById([$fieldId, $modelId, $model]);
         if (isset($fv->field_id)) {
             $value = json_decode($fv->value, true);
             if (is_array($value) && isset($value[0]) && is_array($value[0])) {
                 foreach ($value as $key => $val) {
                     if (is_array($val) && isset($val[0]) && (empty($val[0]) || $val[0] == ' ')) {
                         unset($val[0]);
                         $value[$key] = array_values($val);
                         if (count($value[$key]) == 0) {
                             unset($value[$key]);
                         }
                     }
                 }
                 $value = array_values($value);
             } else {
                 if (is_array($value) && isset($value[0]) && (empty($value[0]) || $value[0] == ' ')) {
                     unset($value[0]);
                     $value = array_values($value);
                 }
             }
             if (count($value) == 0) {
                 $fv->delete();
             } else {
                 $fv->value = json_encode($value);
                 $fv->save();
             }
         }
     }
 }
Example #9
0
 /**
  * Init category nav and categories
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function setTemplate(AbstractController $controller, Application $application)
 {
     if ($controller->hasView() && $controller instanceof \Phire\Members\Controller\IndexController) {
         $template = basename($controller->view()->getTemplate()->getTemplate());
         $memberName = $controller->view()->memberName;
         $memberUri = $controller->view()->memberUri;
         $templateName = null;
         if ($application->isRegistered('phire-templates')) {
             switch ($template) {
                 case 'login.phtml':
                     $templateName = $memberName . ' Login';
                     break;
                 case 'forgot.phtml':
                     $templateName = $memberName . ' Forgot';
                     break;
                 case 'index.phtml':
                     $templateName = $memberName . ' Index';
                     break;
                 case 'profile.phtml':
                     $templateName = $memberName . ' Profile';
                     break;
                 case 'register.phtml':
                     $templateName = $memberName . ' Register';
                     break;
                 case 'unsubscribe.phtml':
                     $templateName = $memberName . ' Unsubscribe';
                     break;
                 case 'verify.phtml':
                     $templateName = $memberName . ' Verify';
                     break;
             }
             if (null !== $templateName) {
                 $tmpl = \Phire\Templates\Table\Templates::findBy(['name' => $templateName]);
                 if (isset($tmpl->id)) {
                     $controller->view()->setTemplate($tmpl->template);
                 }
             }
         } else {
             if ($application->isRegistered('phire-themes')) {
                 switch ($template) {
                     case 'login.phtml':
                         $templateName = $memberUri . '/login';
                         break;
                     case 'forgot.phtml':
                         $templateName = $memberUri . '/forgot';
                         break;
                     case 'index.phtml':
                         $templateName = $memberUri . '/index';
                         break;
                     case 'profile.phtml':
                         $templateName = $memberUri . '/profile';
                         break;
                     case 'register.phtml':
                         $templateName = $memberUri . '/register';
                         break;
                     case 'unsubscribe.phtml':
                         $templateName = $memberUri . '/unsubscribe';
                         break;
                     case 'verify.phtml':
                         $templateName = $memberUri . '/verify';
                         break;
                 }
                 if (null !== $templateName) {
                     $theme = \Phire\Themes\Table\Themes::findBy(['active' => 1]);
                     if (isset($theme->id)) {
                         $templateName = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder . $templateName;
                         $tmpl = null;
                         if (file_exists($templateName . '.phtml')) {
                             $tmpl = $templateName . '.phtml';
                         } else {
                             if (file_exists($templateName . '.php')) {
                                 $tmpl = $templateName . '.php';
                             }
                         }
                         if (null !== $tmpl) {
                             $controller->view()->setTemplate($tmpl);
                         }
                     }
                 }
             }
         }
     }
 }
Example #10
0
 /**
  * Parse the body
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function parseBody(AbstractController $controller, Application $application)
 {
     if ($controller->hasView() && ($application->isRegistered('phire-categories') && $controller instanceof \Phire\Categories\Controller\IndexController || $application->isRegistered('phire-content') && $controller instanceof \Phire\Content\Controller\IndexController)) {
         $controller->response()->setBody(self::parse($controller->response()->getBody()));
     }
 }
Example #11
0
 /**
  * Set the template for the content
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  * @return void
  */
 public static function setTemplate(AbstractController $controller, Application $application)
 {
     $template = null;
     $themePath = null;
     $parentThemePath = null;
     $realThemePath = null;
     $theme = Table\Themes::findBy(['active' => 1]);
     if (isset($theme->id)) {
         $themePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $theme->folder . '/';
         if (null !== $theme->parent_id) {
             $parentTheme = Table\Themes::findById($theme->parent_id);
             if (isset($parentTheme->id)) {
                 $parentThemePath = $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/themes/' . $parentTheme->folder . '/';
             }
         }
     }
     if ($application->isRegistered('phire-content') && $controller instanceof \Phire\Content\Controller\IndexController && $controller->hasView()) {
         if (null !== $controller->getTemplate()) {
             if (isset($theme->id)) {
                 $controller->view()->themePath = $themePath;
                 $controller->view()->parentThemePath = $parentThemePath;
                 if ($controller->getTemplate() == -1 && (file_exists($themePath . 'error.phtml') || file_exists($themePath . 'error.php'))) {
                     $template = file_exists($themePath . 'error.phtml') ? 'error.phtml' : 'error.php';
                 } else {
                     if ($controller->getTemplate() == -2 && (file_exists($themePath . 'date.phtml') || file_exists($themePath . 'date.php'))) {
                         $template = file_exists($themePath . 'date.phtml') ? 'date.phtml' : 'date.php';
                     } else {
                         if (file_exists($themePath . $controller->getTemplate())) {
                             $template = $controller->getTemplate();
                         }
                     }
                 }
                 $realThemePath = $themePath;
                 if (null === $template && null !== $parentThemePath) {
                     if ($controller->getTemplate() == -1 && (file_exists($parentThemePath . 'error.phtml') || file_exists($parentThemePath . 'error.php'))) {
                         $template = file_exists($parentThemePath . 'error.phtml') ? 'error.phtml' : 'error.php';
                     } else {
                         if ($controller->getTemplate() == -2 && (file_exists($parentThemePath . 'date.phtml') || file_exists($parentThemePath . 'date.php'))) {
                             $template = file_exists($parentThemePath . 'date.phtml') ? 'date.phtml' : 'date.php';
                         } else {
                             if (file_exists($parentThemePath . $controller->getTemplate())) {
                                 $template = $controller->getTemplate();
                             }
                         }
                     }
                     $realThemePath = $parentThemePath;
                 }
                 if (null !== $template && null !== $realThemePath) {
                     $device = self::getDevice($controller->request()->getQuery('mobile'));
                     if (null !== $device && file_exists($realThemePath . $device . '/' . $template)) {
                         $template = $device . '/' . $template;
                     }
                     $controller->view()->setTemplate($realThemePath . $template);
                 }
             }
         }
     }
 }
Example #12
0
 /**
  * Delete dynamic field values
  *
  * @param  AbstractController $controller
  * @param  Application        $application
  *
  * @return void
  */
 public static function delete(AbstractController $controller, Application $application)
 {
     if ($_POST) {
         $uploadFolder = BASE_PATH . CONTENT_PATH . '/files';
         $mediaLibrary = $application->module('phire-fields')->config()['media_library'];
         foreach ($_POST as $key => $value) {
             if (substr($key, 0, 3) == 'rm_' && is_array($value)) {
                 $fields = Table\Fields::findBy();
                 $fieldIds = [];
                 $fieldTypes = [];
                 $fieldStorage = [];
                 foreach ($fields->rows() as $field) {
                     $fieldIds[$field->id] = $field->name;
                     $fieldTypes[$field->id] = $field->type;
                     $fieldStorage[$field->id] = $field->storage;
                 }
                 foreach ($value as $id) {
                     foreach ($fieldIds as $fieldId => $fieldName) {
                         if ($fieldStorage[$fieldId] == 'eav') {
                             $fv = Table\FieldValues::findBy(['model_id' => (int) $id]);
                             if ($fv->hasRows()) {
                                 foreach ($fv->rows() as $f) {
                                     $fValue = json_decode($f->value, true);
                                     if (!is_array($fValue)) {
                                         $fValue = [$fValue];
                                     }
                                     foreach ($fValue as $f) {
                                         if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f)) {
                                             unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f);
                                         }
                                         if (null !== $mediaLibrary && $application->isRegistered('phire-media')) {
                                             $library = new \Phire\Media\Model\MediaLibrary();
                                             $library->getByFolder($mediaLibrary);
                                             if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f)) {
                                                 $media = new \Phire\Media\Model\Media();
                                                 $media->getByFile($f);
                                                 if (isset($media->id)) {
                                                     $media->remove(['rm_media' => [$media->id]]);
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             $fv = new Table\FieldValues();
                             $fv->delete(['model_id' => (int) $id]);
                         } else {
                             $fv = new Record();
                             $fv->setPrefix(DB_PREFIX)->setPrimaryKeys(['id'])->setTable('field_' . $fieldName);
                             $fv->findRecordsBy(['model_id' => (int) $id]);
                             if ($fieldTypes[$fieldId] == 'file' && $fv->hasRows()) {
                                 foreach ($fv->rows() as $f) {
                                     if (file_exists($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value)) {
                                         unlink($_SERVER['DOCUMENT_ROOT'] . $uploadFolder . '/' . $f->value);
                                     }
                                     if (null !== $mediaLibrary && $application->isRegistered('phire-media')) {
                                         $library = new \Phire\Media\Model\MediaLibrary();
                                         $library->getByFolder($mediaLibrary);
                                         if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/' . $library->folder . '/' . $f->value)) {
                                             $media = new \Phire\Media\Model\Media();
                                             $media->getByFile($f->value);
                                             if (isset($media->id)) {
                                                 $media->remove(['rm_media' => [$media->id]]);
                                             }
                                         }
                                     }
                                 }
                             }
                             $fv->delete(['model_id' => (int) $id]);
                         }
                     }
                 }
             }
         }
     }
 }
Example #13
0
 /**
  * Record logout/session end
  *
  * @param  \Pop\Application $application
  * @return void
  */
 public static function logout(Application $application)
 {
     $sess = $application->getService('session');
     $userUri = APP_URI;
     $key = 'user';
     if (isset($sess->member) && $application->isRegistered('phire-members')) {
         $key = 'member';
         $memberAdmin = new \Phire\Members\Model\MembersAdmin();
         $memberAdmin->getByRoleId($sess->member->role_id);
         if (isset($memberAdmin->uri)) {
             $userUri = $memberAdmin->uri;
         }
     }
     if ($application->router()->getRouteMatch()->getRoute() == $userUri . '/logout') {
         $path = BASE_PATH . APP_URI;
         if ($path == '') {
             $path = '/';
         }
         $cookie = Cookie::getInstance(['path' => $path]);
         $cookie->delete('phire_session_timeout');
         $cookie->delete('phire_session_path');
         $cookie->delete('phire_session_warning_dismiss');
         $sess = $application->getService('session');
         if (isset($sess[$key]) && isset($sess[$key]->session)) {
             $session = Table\UserSessions::findById((int) $sess[$key]->session->id);
             if (isset($session->id)) {
                 $session->delete();
             }
         }
     }
 }