PageRenderTime 42ms CodeModel.GetById 20ms app.highlight 8ms RepoModel.GetById 12ms app.codeStats 0ms

/modules/core/Forms/bootstrap.php

https://github.com/scogle/cockpit
PHP | 130 lines | 83 code | 42 blank | 5 comment | 17 complexity | b3b16ac2541d34d460512d3ff449959f MD5 | raw file
  1<?php
  2
  3// API
  4
  5$app->bind("/api/forms/submit/:form", function($params) use($app){
  6
  7    $form = $params["form"];
  8
  9    // Security check
 10
 11    if($formhash = $app->param("__csrf", false)) {
 12
 13        if($formhash != $app->hash($form)) {
 14            return false;
 15        }
 16
 17    } else {
 18        return false;
 19    }
 20
 21    $frm = $app->db->findOne("common/forms", ["name"=>$form]);
 22
 23    if(!$frm) {
 24        return false;
 25    }
 26
 27    if($formdata = $app->param("form", false)) {
 28
 29        if(isset($frm["email"]) && filter_var($frm["email"], FILTER_VALIDATE_EMAIL)) {
 30
 31            $body = array();
 32
 33            foreach ($formdata as $key => $value) {
 34                $body[] = "<b>{$key}:</b>\n<br>";
 35                $body[] = (is_string($value) ? $value:json_encode($value))."\n<br>";
 36            }
 37
 38            $app->mailer->mail($frm["email"], $app->param("__mailsubject", "New form data for: ".$form), implode("\n<br>", $body));
 39        }
 40
 41        if(isset($frm["entry"]) && $frm["entry"]) {
 42
 43            $collection = "form".$frm["_id"];
 44            $entry      = ["data" => $formdata, "created"=>time()];
 45            $app->db->insert("forms/{$collection}", $entry);
 46        }
 47
 48        return json_encode($formdata);
 49
 50    } else {
 51        return "false";
 52    }
 53
 54});
 55
 56$this->module("forms")->extend([
 57
 58    "form" => function($name, $options = []) use($app) {
 59
 60        $options = array_merge(array(
 61            "id"    => uniqid("form"),
 62            "class" => "",
 63            "csrf"  => $app->hash($name)
 64        ), $options);
 65
 66        $app->renderView("forms:views/api/form.php", compact('name', 'options'));
 67    },
 68
 69    "collectionById" => function($formId) use($app) {
 70
 71        $entrydb = "form{$formId}";
 72
 73        return $app->db->getCollection("forms/{$entrydb}");
 74    }
 75]);
 76
 77
 78if (!function_exists('form')) {
 79
 80    function form($name, $options = []) {
 81        cockpit("forms")->form($name, $options);
 82    }
 83}
 84
 85// ADMIN
 86
 87if(COCKPIT_ADMIN && !COCKPIT_REST) {
 88
 89    $app->on("admin.init", function() use($app){
 90
 91        if(!$app->module("auth")->hasaccess("Forms", ['manage.forms', 'manage.entries'])) return;
 92
 93        $app->bindClass("Forms\\Controller\\Forms", "forms");
 94        $app->bindClass("Forms\\Controller\\Api", "api/forms");
 95
 96        $app("admin")->menu("top", [
 97            "url"    => $app->routeUrl("/forms"),
 98            "label"  => '<i class="uk-icon-inbox"></i>',
 99            "title"  => $app("i18n")->get("Forms"),
100            "active" => (strpos($app["route"], '/forms') === 0)
101        ], 5);
102
103        // handle global search request
104        $app->on("cockpit.globalsearch", function($search, $list) use($app){
105
106            foreach ($app->db->find("common/forms") as $f) {
107                if(stripos($f["name"], $search)!==false){
108                    $list[] = [
109                        "title" => '<i class="uk-icon-inbox"></i> '.$f["name"],
110                        "url"   => $app->routeUrl('/forms/form/'.$f["_id"])
111                    ];
112                }
113            }
114        });
115    });
116
117    $app->on("admin.dashboard.aside", function() use($app){
118
119        if(!$app->module("auth")->hasaccess("Forms", ['manage.forms', 'manage.entries'])) return;
120
121        $title = $app("i18n")->get("Forms");
122        $badge = $app->db->getCollection("common/forms")->count();
123        $forms = $app->db->find("common/forms", ["limit"=> 3, "sort"=>["created"=>-1] ])->toArray();
124
125        $app->renderView("forms:views/dashboard.php with cockpit:views/layouts/dashboard.widget.php", compact('title', 'badge', 'forms'));
126    });
127
128    // acl
129    $app("acl")->addResource("Forms", ['manage.forms', 'manage.entries']);
130}